|
@ -3,57 +3,65 @@ const { User, Contact } = require("../db/db"); |
|
|
const { sendTelegramMessage } = require("../telegram"); |
|
|
const { sendTelegramMessage } = require("../telegram"); |
|
|
|
|
|
|
|
|
function TelegramWebhookRoute(req, res) { |
|
|
function TelegramWebhookRoute(req, res) { |
|
|
|
|
|
|
|
|
try{ |
|
|
|
|
|
|
|
|
try { |
|
|
const messageText = req.body.message.text; |
|
|
const messageText = req.body.message.text; |
|
|
const telegramID = req.body.message.from.id; |
|
|
const telegramID = req.body.message.from.id; |
|
|
if (messageText.toLowerCase() == "/covidpositive") { |
|
|
if (messageText.toLowerCase() == "/covidpositive") { |
|
|
userInfected(telegramID, (result) => { |
|
|
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.", ()=>{}); |
|
|
|
|
|
|
|
|
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){ |
|
|
|
|
|
|
|
|
} catch (e) { |
|
|
console.log("Could not get Telegram Message"); |
|
|
console.log("Could not get Telegram Message"); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
res.send(); |
|
|
res.send(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
function informContacts(telegramID, doneCallback){ |
|
|
|
|
|
|
|
|
function informContacts(telegramID, doneCallback) { |
|
|
User.findOne({ |
|
|
User.findOne({ |
|
|
where: { |
|
|
where: { |
|
|
telegram: telegramID, |
|
|
telegram: telegramID, |
|
|
} |
|
|
|
|
|
}).then(user => { |
|
|
|
|
|
if(user){ |
|
|
|
|
|
|
|
|
}, |
|
|
|
|
|
}).then((user) => { |
|
|
|
|
|
if (user) { |
|
|
const userRowID = user.id; |
|
|
const userRowID = user.id; |
|
|
Contact.findAll({ |
|
|
Contact.findAll({ |
|
|
where: { |
|
|
where: { |
|
|
[Op.or]: [{user: userRowID}, {with: userRowID}], |
|
|
|
|
|
} |
|
|
|
|
|
}) |
|
|
|
|
|
.then(result => { |
|
|
|
|
|
result.forEach(contact => { |
|
|
|
|
|
const otherPersonID = contact.user == userRowID ? contact.with : contact.user; |
|
|
|
|
|
|
|
|
[Op.or]: [{ user: userRowID }, { with: userRowID }], |
|
|
|
|
|
}, |
|
|
|
|
|
}).then((result) => { |
|
|
|
|
|
result.forEach((contact) => { |
|
|
|
|
|
const otherPersonID = |
|
|
|
|
|
contact.user == userRowID ? contact.with : contact.user; |
|
|
User.findOne({ |
|
|
User.findOne({ |
|
|
where: { |
|
|
where: { |
|
|
id: otherPersonID, |
|
|
id: otherPersonID, |
|
|
} |
|
|
|
|
|
}).then(otherPerson => { |
|
|
|
|
|
sendTelegramMessage(otherPerson.telegram, "You're infected.", ()=>{}); |
|
|
|
|
|
|
|
|
}, |
|
|
|
|
|
}).then((otherPerson) => { |
|
|
|
|
|
sendTelegramMessage( |
|
|
|
|
|
otherPerson.telegram, |
|
|
|
|
|
"You're infected.", |
|
|
|
|
|
() => {} |
|
|
|
|
|
); |
|
|
}); |
|
|
}); |
|
|
}); |
|
|
}); |
|
|
}); |
|
|
}); |
|
|
} |
|
|
} |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
function userInfected(telegramID, doneCallback) { |
|
|
function userInfected(telegramID, doneCallback) { |
|
@ -61,21 +69,26 @@ function userInfected(telegramID, doneCallback) { |
|
|
where: { |
|
|
where: { |
|
|
telegram: telegramID, |
|
|
telegram: telegramID, |
|
|
}, |
|
|
}, |
|
|
}).then((user) => { |
|
|
|
|
|
|
|
|
}) |
|
|
|
|
|
.then((user) => { |
|
|
if (!user) { |
|
|
if (!user) { |
|
|
done({saved: false}); |
|
|
|
|
|
|
|
|
done({ saved: false }); |
|
|
} else { |
|
|
} else { |
|
|
user.isInfected = true; |
|
|
user.isInfected = true; |
|
|
user.save().then(result => { |
|
|
|
|
|
if(result){ |
|
|
|
|
|
|
|
|
|
|
|
doneCallback({saved: 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}); |
|
|
|
|
|
|
|
|
.catch((err) => { |
|
|
|
|
|
doneCallback({ saved: false }); |
|
|
}); |
|
|
}); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|