From 31ee1c2d3cc13e560d94df52b73d43f19310ab0f Mon Sep 17 00:00:00 2001 From: Nareshkumar Rao <_accounts@nareshkumarrao.com> Date: Thu, 29 Jul 2021 21:52:18 +0200 Subject: [PATCH 1/2] fixed bug re-sending contact verification upon login --- src/routes/LoginRoute.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/routes/LoginRoute.js b/src/routes/LoginRoute.js index 7163367..b1b7a9d 100644 --- a/src/routes/LoginRoute.js +++ b/src/routes/LoginRoute.js @@ -4,8 +4,16 @@ const { addContact, createUser } = require("../db/utils"); function LoginRoute(req, res) { const telegramResponse = req.body.telegramResponse; + authUser(telegramResponse, (success, msg) => { if (success) { + + // User is already logged in + if(req.session.user == telegramResponse.id){ + res.send({authorized: success}); + return; + } + const verified = req.session.verified; const verifiedBy = req.session.verifiedBy; req.session.regenerate(() => { @@ -26,6 +34,7 @@ function LoginRoute(req, res) { res.status(401).send({ authorized: success, message: msg }); } }); + } function authUser(telegramResponse, done) { From a4888597ba5b7f05275e0c13fd5423bc0f9edaad Mon Sep 17 00:00:00 2001 From: Nareshkumar Rao Date: Thu, 29 Jul 2021 21:53:41 +0200 Subject: [PATCH 2/2] ran linter --- src/routes/LoginRoute.js | 6 +- src/routes/TelegramWebhookRoute.js | 141 ++++++++++++++++------------- 2 files changed, 79 insertions(+), 68 deletions(-) diff --git a/src/routes/LoginRoute.js b/src/routes/LoginRoute.js index b1b7a9d..89d0292 100644 --- a/src/routes/LoginRoute.js +++ b/src/routes/LoginRoute.js @@ -7,10 +7,9 @@ function LoginRoute(req, res) { authUser(telegramResponse, (success, msg) => { if (success) { - // User is already logged in - if(req.session.user == telegramResponse.id){ - res.send({authorized: success}); + if (req.session.user == telegramResponse.id) { + res.send({ authorized: success }); return; } @@ -34,7 +33,6 @@ function LoginRoute(req, res) { res.status(401).send({ authorized: success, message: msg }); } }); - } function authUser(telegramResponse, done) { diff --git a/src/routes/TelegramWebhookRoute.js b/src/routes/TelegramWebhookRoute.js index 9c1e795..556d4e5 100644 --- a/src/routes/TelegramWebhookRoute.js +++ b/src/routes/TelegramWebhookRoute.js @@ -3,80 +3,93 @@ const { User, Contact } = require("../db/db"); const { sendTelegramMessage } = require("../telegram"); function TelegramWebhookRoute(req, res) { - - try{ - const messageText = req.body.message.text; - const telegramID = req.body.message.from.id; - if (messageText.toLowerCase() == "/covidpositive") { - userInfected(telegramID, (result) => { - if(result.saved){ - sendTelegramMessage(telegramID, "Thanks for informing us. We will notify the people you were in contact with!", ()=>{}); - informContacts(telegramID, ()=>{}); - }else{ - sendTelegramMessage(telegramID, "Sorry, something went wrong.", ()=>{}); - } - }); + try { + const messageText = req.body.message.text; + const telegramID = req.body.message.from.id; + if (messageText.toLowerCase() == "/covidpositive") { + userInfected(telegramID, (result) => { + if (result.saved) { + sendTelegramMessage( + telegramID, + "Thanks for informing us. We will notify the people you were in contact with!", + () => {} + ); + informContacts(telegramID, () => {}); + } else { + sendTelegramMessage( + telegramID, + "Sorry, something went wrong.", + () => {} + ); } + }); } - catch(e){ - console.log("Could not get Telegram Message"); - } - + } catch (e) { + console.log("Could not get Telegram Message"); + } - res.send(); + res.send(); } -function informContacts(telegramID, doneCallback){ - User.findOne({ +function informContacts(telegramID, doneCallback) { + User.findOne({ + where: { + telegram: telegramID, + }, + }).then((user) => { + if (user) { + const userRowID = user.id; + Contact.findAll({ where: { - telegram: telegramID, - } - }).then(user => { - if(user){ - const userRowID = user.id; - Contact.findAll({ - where: { - [Op.or]: [{user: userRowID}, {with: userRowID}], - } - }) - .then(result => { - result.forEach(contact => { - const otherPersonID = contact.user == userRowID ? contact.with : contact.user; - User.findOne({ - where: { - id: otherPersonID, - } - }).then(otherPerson => { - sendTelegramMessage(otherPerson.telegram, "You're infected.", ()=>{}); - }); - }); - }); - } - }); - + [Op.or]: [{ user: userRowID }, { with: userRowID }], + }, + }).then((result) => { + result.forEach((contact) => { + const otherPersonID = + contact.user == userRowID ? contact.with : contact.user; + User.findOne({ + where: { + id: otherPersonID, + }, + }).then((otherPerson) => { + sendTelegramMessage( + otherPerson.telegram, + "You're infected.", + () => {} + ); + }); + }); + }); + } + }); } function userInfected(telegramID, doneCallback) { - User.findOne({ - where: { - telegram: telegramID, - }, - }).then((user) => { - if (!user) { - done({saved: false}); - } else { - user.isInfected = true; - user.save().then(result => { - if(result){ - - doneCallback({saved: true}); - } - }).catch(err=>{doneCallback({saved: false})}); - } + User.findOne({ + where: { + telegram: telegramID, + }, + }) + .then((user) => { + if (!user) { + done({ saved: false }); + } else { + user.isInfected = true; + user + .save() + .then((result) => { + if (result) { + doneCallback({ saved: true }); + } + }) + .catch((err) => { + doneCallback({ saved: false }); + }); + } }) - .catch(err=>{ - doneCallback({saved: false}); + .catch((err) => { + doneCallback({ saved: false }); }); } -exports.TelegramWebhookRoute = TelegramWebhookRoute; \ No newline at end of file +exports.TelegramWebhookRoute = TelegramWebhookRoute;