Browse Source

Merge branch 'development' into tsMigration

tsMigration
Nareshkumar Rao 3 years ago
parent
commit
2920dcb876
  1. 4
      README.md
  2. 2
      package.json
  3. 1
      src/app.ts
  4. 4
      src/db/models/User.ts
  5. 61
      src/routes/CovidRoute.js

4
README.md

@ -1,6 +1,6 @@
# SSR Tracing Backend
# OurSejahtera Backend
This is the backend API provider for the [SSR Tracing Web-App](https://github.com/naresh97/ssr-tracing).
This is the backend API provider for the [SSR Tracing Web-App](https://github.com/naresh97/our-sejahtera).
## Development

2
package.json

@ -1,5 +1,5 @@
{
"name": "ssr-tracing-backend",
"name": "our-sejahtera-backend",
"version": "1.0.0",
"description": "",
"main": "src/app.js",

1
src/app.ts

@ -22,6 +22,7 @@ app.post(`/${process.env.TELEGRAM_SECRET}`, TelegramWebhookRoute);
app.post("/login", LoginRoute);
app.get("/code", CodeRoute);
app.post("/verify", VerifyRoute);
app.post("/covid", CovidRoute);
const port = process.env.PORT || 8080;
app.listen(port, () => {

4
src/db/models/User.ts

@ -7,6 +7,7 @@ interface UserAttributes {
telegram: TelegramID;
verification: VerificationString;
isInfected: boolean;
infectionDate: Date;
}
interface UserCreationAttributes {
telegram: TelegramID;
@ -32,6 +33,9 @@ export const User = sequelize.define<UserInstance>("User", {
isInfected: {
type: DataTypes.BOOLEAN,
},
infectionDate: {
type: DataTypes.DATE,
},
});
User.sync().then(() => {

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