Browse Source

implemented inform contacts

feature/informcontacts
Nareshkumar Rao 3 years ago
parent
commit
81940d8409
  1. 6
      src/app.js
  2. 3
      src/db/db.js
  3. 82
      src/routes/TelegramWebhookRoute.js

6
src/app.js

@ -7,6 +7,7 @@ const { LoginRoute } = require("./routes/LoginRoute");
const { CodeRoute } = require("./routes/CodeRoute");
const { VerifyRoute } = require("./routes/VerifyRoute");
const { corsOpts, sessionOpts } = require("./session");
const { TelegramWebhookRoute } = require("./routes/TelegramWebhookRoute");
console.log(`Node Environment: ${process.env.NODE_ENV}`);
@ -16,10 +17,7 @@ app.use(session(sessionOpts));
app.use(cors(corsOpts));
app.use(express.json());
app.post(`/${process.env.TELEGRAM_SECRET}`, (req, res) => {
res.send();
});
app.post(`/${process.env.TELEGRAM_SECRET}`, TelegramWebhookRoute);
app.post("/login", LoginRoute);
app.get("/code", CodeRoute);
app.post("/verify", VerifyRoute);

3
src/db/db.js

@ -60,6 +60,9 @@ const User = sequelize.define("User", {
verification: {
type: DataTypes.STRING,
},
isInfected: {
type: DataTypes.BOOLEAN,
},
});
Contact.sync();

82
src/routes/TelegramWebhookRoute.js

@ -0,0 +1,82 @@
const { Op } = require("sequelize");
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.", ()=>{});
}
});
}
}
catch(e){
console.log("Could not get Telegram Message");
}
res.send();
}
function informContacts(telegramID, doneCallback){
User.findOne({
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.", ()=>{});
});
});
});
}
});
}
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})});
}
})
.catch(err=>{
doneCallback({saved: false});
});
}
exports.TelegramWebhookRoute = TelegramWebhookRoute;
Loading…
Cancel
Save