Skip to content

Commit

Permalink
webservice: automatically handle async errors
Browse files Browse the repository at this point in the history
  • Loading branch information
siddharthvp committed May 17, 2024
1 parent f979eca commit 7cfb591
Show file tree
Hide file tree
Showing 11 changed files with 598 additions and 155 deletions.
73 changes: 35 additions & 38 deletions bot-monitor/web-endpoint.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as express from "express";
import 'express-async-errors';
import {alertsDb} from "./AlertsDb";
import {bot} from "../botbase";
import {CustomError} from "../utils";
Expand All @@ -8,47 +9,43 @@ const router = express.Router();
alertsDb.connect();

router.all('/pause', async (req, res, next) => {
try {
const { task, webKey } = req.query as Record<string, string>;
const { date, unpause } = req.body as Record<string, string>;
if (!webKey || !task) {
return next(new CustomError(400, "Missing one of required query params: task, webKey"));
}
const { task, webKey } = req.query as Record<string, string>;
const { date, unpause } = req.body as Record<string, string>;
if (!webKey || !task) {
return next(new CustomError(400, "Missing one of required query params: task, webKey"));
}

let current = '';
let dateVal = '';
if (date) { // POST
const tillDate = new bot.Date(date);
tillDate.setUTCHours(23, 59, 59); // pause till end of selected day
const rowsUpdated = await alertsDb.setPausedTillTime(task, webKey, tillDate);
if (!rowsUpdated) {
return next(new CustomError(403, "Unauthorized"));
}
current = `<span style="color: green; font-weight: bold">Successfully paused notifications till ${tillDate.format('D MMMM YYYY')} (UTC).</span>`;
dateVal = tillDate.format('YYYY-MM-DD');
} else if (unpause) { // POST
const rowsUpdated = await alertsDb.setPausedTillTime(task, webKey);
if (!rowsUpdated) {
return next(new CustomError(403, "Unauthorized"));
}
current = `<span style="color: green; font-weight: bold">Successfully unpaused notifications.</span>`;
} else { // GET
let pausedTill = await alertsDb.getPausedTillTime(task, webKey);
if (pausedTill) {
current = `Notifications are currently paused till ${pausedTill.format('D MMMM YYYY')} (UTC).`;
dateVal = pausedTill.format('YYYY-MM-DD');
}
let current = '';
let dateVal = '';
if (date) { // POST
const tillDate = new bot.Date(date);
tillDate.setUTCHours(23, 59, 59); // pause till end of selected day
const rowsUpdated = await alertsDb.setPausedTillTime(task, webKey, tillDate);
if (!rowsUpdated) {
return next(new CustomError(403, "Unauthorized"));
}
current = `<span style="color: green; font-weight: bold">Successfully paused notifications till ${tillDate.format('D MMMM YYYY')} (UTC).</span>`;
dateVal = tillDate.format('YYYY-MM-DD');
} else if (unpause) { // POST
const rowsUpdated = await alertsDb.setPausedTillTime(task, webKey);
if (!rowsUpdated) {
return next(new CustomError(403, "Unauthorized"));
}
current = `<span style="color: green; font-weight: bold">Successfully unpaused notifications.</span>`;
} else { // GET
let pausedTill = await alertsDb.getPausedTillTime(task, webKey);
if (pausedTill) {
current = `Notifications are currently paused till ${pausedTill.format('D MMMM YYYY')} (UTC).`;
dateVal = pausedTill.format('YYYY-MM-DD');
}

return res.render('bot-monitor/web-endpoint', {
task,
webKey,
current,
dateVal
});
} catch (e) {
next(e);
}

return res.render('bot-monitor/web-endpoint', {
task,
webKey,
current,
dateVal
});
});


Expand Down
1 change: 1 addition & 0 deletions db-tabulator/web-endpoint.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as express from "express";
import 'express-async-errors';
import {
checkShutoff,
fetchQueriesForPage,
Expand Down
1 change: 1 addition & 0 deletions dyk-counts/web-endpoint.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as express from "express";
import 'express-async-errors';
import { enwikidb } from "../db";
import { getRedisInstance } from "../redis";

Expand Down
1 change: 1 addition & 0 deletions most-gans/web-endpoint.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as express from "express";
import 'express-async-errors';
import { toolsdb } from '../db';
import { AuthManager, bot } from "../botbase";
import { TABLE } from '../../SDZeroBot/most-gans/model';
Expand Down
Loading

0 comments on commit 7cfb591

Please sign in to comment.