Browse Source

implemented

feature/setCovideStateRoute
Nareshkumar Rao 3 years ago
parent
commit
e4dbf4ef7b
  1. 2
      src/app.js
  2. 3
      src/db/db.js
  3. 61
      src/routes/CovidRoute.js

2
src/app.js

@ -8,6 +8,7 @@ const { CodeRoute } = require("./routes/CodeRoute");
const { VerifyRoute } = require("./routes/VerifyRoute"); const { VerifyRoute } = require("./routes/VerifyRoute");
const { corsOpts, sessionOpts } = require("./session"); const { corsOpts, sessionOpts } = require("./session");
const { TelegramWebhookRoute } = require("./routes/TelegramWebhookRoute"); const { TelegramWebhookRoute } = require("./routes/TelegramWebhookRoute");
const { CovidRoute } = require("./routes/CovidRoute");
console.log(`Node Environment: ${process.env.NODE_ENV}`); console.log(`Node Environment: ${process.env.NODE_ENV}`);
@ -21,6 +22,7 @@ app.post(`/${process.env.TELEGRAM_SECRET}`, TelegramWebhookRoute);
app.post("/login", LoginRoute); app.post("/login", LoginRoute);
app.get("/code", CodeRoute); app.get("/code", CodeRoute);
app.post("/verify", VerifyRoute); app.post("/verify", VerifyRoute);
app.post("/covid", CovidRoute);
const port = process.env.PORT || 8080; const port = process.env.PORT || 8080;
app.listen(port, () => { app.listen(port, () => {

3
src/db/db.js

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

61
src/routes/CovidRoute.js

@ -0,0 +1,61 @@
const { User } = require("../db/db");
function CovidRoute(req, res){
if(!req.session.user){
res.status(401).send("Not logged in");
return;
}
console.log(`SetPositive: ${req.body.setPositive}`);
if(req.body.setPositive){
setUserCovidPositive(req.session.user, true, response=>{
res.send({covidPositive: response});
});
}else{
getUserCovidPositivity(req.session.user, (success, positivity)=>{
res.status(success ? 200 : 400).send({covidPositive: positivity});
});
}
}
function getUserCovidPositivity(telegramID, callback){
User.findOne({
where: {telegram: telegramID},
})
.then(user=>{
if(user){
const infectionDuration = user.infectionDate - Date.now();
if(infectionDuration > 60 * 60 * 24 * 14){
setUserCovidPositive(telegramID, false, res=>{
callback(res, res ? false : null);
});
}else{
callback(true, user.isInfected);
}
}else{
callback(false, null);
}
})
.catch(()=>{
callback(false, null);
})
}
function setUserCovidPositive(telegramID, infectionState, callback){
User.findOne({
where: {telegram: telegramID},
})
.then(user=>{
if(user){
user.isInfected = infectionState;
user.infectionDate = Date.now();
user.save().then(()=>callback(true)).catch(()=>callback(false));
}else{
callback(false);
}
})
.catch(()=>callback(false));
}
exports.CovidRoute = CovidRoute;
Loading…
Cancel
Save