Browse Source

Merge pull request #14 from naresh97/bug/11-inform-contacts

fixes #11 inform contacts when report covid positive
development
Nareshkumar Rao 3 years ago
committed by GitHub
parent
commit
35c04ee3ca
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  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, getUserCovidPositivity,
setUserCovidPositivity, setUserCovidPositivity,
} from "../db/models/User.helper"; } from "../db/models/User.helper";
import { informContacts } from "../telegram";
interface CovidRouteRequest extends Request { interface CovidRouteRequest extends Request {
body: { body: {
@ -18,6 +19,7 @@ export async function CovidRoute(req: CovidRouteRequest, res: Response) {
try { try {
if (req.body.setPositive) { if (req.body.setPositive) {
await setUserCovidPositivity(req.session.userTelegramID, true); await setUserCovidPositivity(req.session.userTelegramID, true);
await informContacts(req.session.userTelegramID);
res.send({ covidPositive: true }); res.send({ covidPositive: true });
} else { } else {
const isInfected = await getUserCovidPositivity( const isInfected = await getUserCovidPositivity(

27
src/routes/TelegramWebhookRoute.ts

@ -1,10 +1,7 @@
import { Request, Response } from "express"; 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 { strings_en } from "../strings";
import { sendTelegramMessage } from "../telegram";
import { informContacts, sendTelegramMessage } from "../telegram";
import { TelegramID } from "../types"; import { TelegramID } from "../types";
interface TelegramWebhookRequest extends Request { interface TelegramWebhookRequest extends Request {
@ -50,26 +47,6 @@ export async function TelegramWebhookRoute(
res.send(); 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> { async function userInfected(telegramID: TelegramID): Promise<void> {
const user = await getUserByTelegramID(telegramID); const user = await getUserByTelegramID(telegramID);
if (!user) throw new Error("User not found"); if (!user) throw new Error("User not found");

24
src/telegram.ts

@ -1,4 +1,8 @@
import axios from "axios"; 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"; import { TelegramID } from "./types";
export async function setTelegramWebHook(): Promise<void> { 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() setTelegramWebHook()
.catch(error=>{ .catch(error=>{

Loading…
Cancel
Save