Browse Source

ran linter

pull/4/head
Nareshkumar Rao 3 years ago
parent
commit
a4888597ba
  1. 2
      src/routes/LoginRoute.js
  2. 57
      src/routes/TelegramWebhookRoute.js

2
src/routes/LoginRoute.js

@ -7,7 +7,6 @@ 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 });
@ -34,7 +33,6 @@ function LoginRoute(req, res) {
res.status(401).send({ authorized: success, message: msg });
}
});
}
function authUser(telegramResponse, done) {

57
src/routes/TelegramWebhookRoute.js

@ -3,26 +3,31 @@ 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!", ()=>{});
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.", ()=>{});
sendTelegramMessage(
telegramID,
"Sorry, something went wrong.",
() => {}
);
}
});
}
}
catch(e){
} catch (e) {
console.log("Could not get Telegram Message");
}
res.send();
}
@ -30,30 +35,33 @@ function informContacts(telegramID, doneCallback){
User.findOne({
where: {
telegram: telegramID,
}
}).then(user => {
},
}).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;
},
}).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.", ()=>{});
},
}).then((otherPerson) => {
sendTelegramMessage(
otherPerson.telegram,
"You're infected.",
() => {}
);
});
});
});
}
});
}
function userInfected(telegramID, doneCallback) {
@ -61,20 +69,25 @@ function userInfected(telegramID, doneCallback) {
where: {
telegram: telegramID,
},
}).then((user) => {
})
.then((user) => {
if (!user) {
done({ saved: false });
} else {
user.isInfected = true;
user.save().then(result => {
user
.save()
.then((result) => {
if (result) {
doneCallback({ saved: true });
}
}).catch(err=>{doneCallback({saved: false})});
})
.catch((err) => {
doneCallback({ saved: false });
});
}
})
.catch(err=>{
.catch((err) => {
doneCallback({ saved: false });
});
}

Loading…
Cancel
Save