diff --git a/src/routes/CovidRoute.ts b/src/routes/CovidRoute.ts index 9299fca..01b725f 100644 --- a/src/routes/CovidRoute.ts +++ b/src/routes/CovidRoute.ts @@ -3,6 +3,7 @@ import { getUserCovidPositivity, setUserCovidPositivity, } from "../db/models/User.helper"; +import { informContacts } from "../telegram"; interface CovidRouteRequest extends Request { body: { @@ -18,6 +19,7 @@ export async function CovidRoute(req: CovidRouteRequest, res: Response) { try { if (req.body.setPositive) { await setUserCovidPositivity(req.session.userTelegramID, true); + await informContacts(req.session.userTelegramID); res.send({ covidPositive: true }); } else { const isInfected = await getUserCovidPositivity( diff --git a/src/routes/TelegramWebhookRoute.ts b/src/routes/TelegramWebhookRoute.ts index 3c1c3be..70863e1 100644 --- a/src/routes/TelegramWebhookRoute.ts +++ b/src/routes/TelegramWebhookRoute.ts @@ -1,10 +1,7 @@ import { Request, Response } from "express"; -import { Op } from "sequelize"; -import { Contact } from "../db/models/Contact"; -import { User } from "../db/models/User"; -import { getUserByRowID, getUserByTelegramID } from "../db/models/User.helper"; +import { getUserByTelegramID } from "../db/models/User.helper"; import { strings_en } from "../strings"; -import { sendTelegramMessage } from "../telegram"; +import { informContacts, sendTelegramMessage } from "../telegram"; import { TelegramID } from "../types"; interface TelegramWebhookRequest extends Request { @@ -50,26 +47,6 @@ export async function TelegramWebhookRoute( res.send(); } -async function informContacts(telegramID: TelegramID): Promise { - const user = await getUserByTelegramID(telegramID); - if (!user) throw new Error("User not found"); - const contacts = await Contact.findAll({ - where: { - [Op.or]: [{ user: user.id }, { with: user.id }], - }, - }); - - contacts.forEach(async (contact) => { - const otherPersonID = contact.user == user.id ? contact.with : contact.user; - const otherUser = await getUserByRowID(otherPersonID); - if (!otherUser) throw new Error("Other user does not exist"); - await sendTelegramMessage( - otherUser.telegram, - strings_en.telegram_inform_infect - ); - }); -} - async function userInfected(telegramID: TelegramID): Promise { const user = await getUserByTelegramID(telegramID); if (!user) throw new Error("User not found"); diff --git a/src/telegram.ts b/src/telegram.ts index 9cca20a..518cbb4 100644 --- a/src/telegram.ts +++ b/src/telegram.ts @@ -1,4 +1,8 @@ import axios from "axios"; +import { Op } from "sequelize"; +import { Contact } from "./db/models/Contact"; +import { getUserByRowID, getUserByTelegramID } from "./db/models/User.helper"; +import { strings_en } from "./strings"; import { TelegramID } from "./types"; export async function setTelegramWebHook(): Promise { @@ -21,6 +25,26 @@ export async function sendTelegramMessage( }); } +export async function informContacts(telegramID: TelegramID): Promise { + const user = await getUserByTelegramID(telegramID); + if (!user) throw new Error("User not found"); + const contacts = await Contact.findAll({ + where: { + [Op.or]: [{ user: user.id }, { with: user.id }], + }, + }); + + contacts.forEach(async (contact) => { + const otherPersonID = contact.user == user.id ? contact.with : contact.user; + const otherUser = await getUserByRowID(otherPersonID); + if (!otherUser) throw new Error("Other user does not exist"); + await sendTelegramMessage( + otherUser.telegram, + strings_en.telegram_inform_infect + ); + }); +} + setTelegramWebHook() .catch(error=>{