Browse Source

fixed issue

pull/14/head
Nareshkumar Rao 3 years ago
parent
commit
dacda6314e
  1. 2
      src/routes/CovidRoute.ts
  2. 27
      src/routes/TelegramWebhookRoute.ts
  3. 24
      src/telegram.ts

2
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(

27
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<void> {
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<void> {
const user = await getUserByTelegramID(telegramID);
if (!user) throw new Error("User not found");

24
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<void> {
@ -21,6 +25,26 @@ export async function sendTelegramMessage(
});
}
export async function informContacts(telegramID: TelegramID): Promise<void> {
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=>{

Loading…
Cancel
Save