diff --git a/src/app.ts b/src/app.ts index bae4aa5..f0d4dc0 100644 --- a/src/app.ts +++ b/src/app.ts @@ -9,6 +9,7 @@ import { TelegramWebhookRoute } from "./routes/TelegramWebhookRoute"; import { LoginRoute } from "./routes/LoginRoute"; import { CodeRoute } from "./routes/CodeRoute"; import { VerifyRoute } from "./routes/VerifyRoute"; +import { CovidRoute } from "./routes/CovidRoute"; console.log(`Node Environment: ${process.env.NODE_ENV}`); diff --git a/src/db/models/User.helper.ts b/src/db/models/User.helper.ts index 7fbeabc..342522d 100644 --- a/src/db/models/User.helper.ts +++ b/src/db/models/User.helper.ts @@ -51,3 +51,30 @@ export function getUserByVerification( callback(undefined); }); } + +export function getUserCovidPositivity(telegramID: TelegramID, callback: (isInfected?: boolean) => void): void { + getUserByTelegramID(telegramID, user => { + if (!!user) { + const infectionDuration = +user.infectionDate - Date.now(); + if (infectionDuration > 60 * 60 * 24 * 14) { + setUserCovidPositivity(telegramID, false, success => { + callback(success ? false : undefined); + }); + } else { + callback(user.isInfected); + } + } else { + callback(); + } + }); +} + +export function setUserCovidPositivity(telegramID: TelegramID, infectionState: boolean, callback: (success: boolean) => void): void { + getUserByTelegramID(telegramID, user => { + if (!!user) { + user.isInfected = infectionState; + user.infectionDate = new Date(); + user.save().then(() => callback(true)).catch(() => callback(false)); + } else { callback(false) } + }); +} diff --git a/src/routes/CovidRoute.js b/src/routes/CovidRoute.js deleted file mode 100644 index b36cf5d..0000000 --- a/src/routes/CovidRoute.js +++ /dev/null @@ -1,61 +0,0 @@ -const { User } = require("../db/db"); - -function CovidRoute(req, res){ - if(!req.session.user){ - res.status(401).send("Not logged in"); - return; - } - - console.log(`SetPositive: ${req.body.setPositive}`); - - if(req.body.setPositive){ - setUserCovidPositive(req.session.user, true, response=>{ - res.send({covidPositive: response}); - }); - }else{ - getUserCovidPositivity(req.session.user, (success, positivity)=>{ - res.status(success ? 200 : 400).send({covidPositive: positivity}); - }); - } -} - -function getUserCovidPositivity(telegramID, callback){ - User.findOne({ - where: {telegram: telegramID}, - }) - .then(user=>{ - if(user){ - const infectionDuration = user.infectionDate - Date.now(); - if(infectionDuration > 60 * 60 * 24 * 14){ - setUserCovidPositive(telegramID, false, res=>{ - callback(res, res ? false : null); - }); - }else{ - callback(true, user.isInfected); - } - }else{ - callback(false, null); - } - }) - .catch(()=>{ - callback(false, null); - }) -} - -function setUserCovidPositive(telegramID, infectionState, callback){ - User.findOne({ - where: {telegram: telegramID}, - }) - .then(user=>{ - if(user){ - user.isInfected = infectionState; - user.infectionDate = Date.now(); - user.save().then(()=>callback(true)).catch(()=>callback(false)); - }else{ - callback(false); - } - }) - .catch(()=>callback(false)); -} - -exports.CovidRoute = CovidRoute; \ No newline at end of file diff --git a/src/routes/CovidRoute.ts b/src/routes/CovidRoute.ts new file mode 100644 index 0000000..82c7026 --- /dev/null +++ b/src/routes/CovidRoute.ts @@ -0,0 +1,26 @@ +import { Request, Response } from "express"; +import { getUserCovidPositivity, setUserCovidPositivity } from "../db/models/User.helper"; + +interface CovidRouteRequest extends Request { + body:{ + setPositive: boolean; + } +} + +export function CovidRoute(req: CovidRouteRequest, res:Response){ + if(!req.session.userTelegramID){ + res.status(401).send("Not logged in"); + return; + } + + if(req.body.setPositive){ + setUserCovidPositivity(req.session.userTelegramID, true, success=>{ + res.send({covidPositive: true}); + }); + }else{ + getUserCovidPositivity(req.session.userTelegramID, isInfected=>{ + res.status(isInfected ? 200 : 400).send({covidPositive: isInfected}); + }); + } +} +