-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroutes.js
222 lines (202 loc) · 7.76 KB
/
routes.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
const jwt = require("jsonwebtoken");
const { DateTime } = require("luxon");
const webpush = require("web-push")
webpush.setVapidDetails(
"mailto:test@test.com",
process.env.VAPID_PUB_KEY,
process.env.VAPID_PRIV_KEY
);
const Doctor = require("./model/doctor-model");
const Patient = require("./model/patient-model");
const sendMail = require("./mail");
const port = process.env.PORT || 4444;
const authToken = (req) => {
const authHeader = req.headers["authorization"];
console.log(authHeader);
const token = authHeader && authHeader.split(" ")[1];
if (token == null) return 401;
return jwt.verify(token, process.env.SECRET, (err, user) => {
if (err) return 403;
return user;
});
};
const appLogic = (app, jsonData) => {
var subscription;
app.post("/subscribe", (req, res) => {
subscription = req.body;
res.status(201);
// const payload = JSON.stringify({ title: "Medico" });
// webpush
// .sendNotification(subscription, payload)
// .catch(err => console.log("Notif Error", err))
})
app.get("/", async (req, res) => {
const user = authToken(req);
if (user == 401 || user == 403) {
console.log("Home notlogged in", user);
res.render("home.ejs");
} else {
console.log("Home logged in", user);
const patientData = await Patient.findById(user);
if (patientData == null) {
console.log("doctor login", user);
const doctorData = await Doctor.findById(user);
if (doctorData !== null) {
const allPatientData = await Patient.find({});
res.render("doctor.ejs", { userData: doctorData, allPatientData: allPatientData });
}
else
res.sendStatus(403);
}
else {
res.render("home-logged-in.ejs", { userData: patientData });
patientData.medSchedule.forEach(({ med, quantity, period }) => {
const payload = JSON.stringify({
title: "Time for medicine",
body: `Please take ${med} (${quantity})`,
period
})
webpush
.sendNotification(subscription, payload)
.catch(err => console.log("Notif Error", err))
})
// TODO: Remove
webpush
.sendNotification(subscription, JSON.stringify({ title: "Logged in" }))
.catch(err => console.log("Notif Error", err))
}
}
});
app.get("/how-it-works", (req, res) => {
res.render("how-it-works.ejs");
});
app.get("/about", (req, res) => {
res.render("about.ejs", { data: jsonData });
});
app.get("/#-#", (req, res) => {
res.render("login-#.ejs");
});
app.post("/#-#", async (req, res) => {
if (req.body.reqType === "login") {
const data = {
name: req.body.name,
password: req.body.password, // TODO: password should be salted
};
let accessToken;
console.log(req.body)
if (req.body.usertype === "doctor") {
const returnData = await Doctor.findOne(data);
if (returnData == null) res.sendStatus(403);
accessToken = jwt.sign(returnData._id.toString(), process.env.SECRET);
} else {
const returnData = await Patient.findOne(data);
if (returnData == null) res.sendStatus(403);
console.log(returnData);
accessToken = jwt.sign(returnData._id.toString(), process.env.SECRET);
}
res.json({ accessToken: accessToken });
} else {
const data = {
name: req.body.name,
email: req.body.email,
password: req.body.password, // TODO: password should be salted
imageURL: req.body.imageURL,
};
let accessToken;
if (req.body.usertype === "doctor") {
let newDoctor = new Doctor(data);
const savedData = await newDoctor.save();
accessToken = jwt.sign(savedData._id.toString(), process.env.SECRET);
} else {
let newPatient = new Patient(data);
const savedData = await newPatient.save();
accessToken = jwt.sign(savedData._id.toString(), process.env.SECRET);
}
res.json({ accessToken: accessToken });
}
});
app.post("/increaseCredit", async (req, res) => {
console.log("REQBODY", req.body);
const user = authToken(req);
if (user == 401 || user == 403) {
console.log("Unauthed credit attempt", user);
res.sendStatus(user);
} else {
console.log("Credit attempt", user);
// const userData = await Doctor.findById(user);
// if (userData == null) {
// res.sendStatus(403);
// }
const patient = await Patient.findById(req.body.patientId);
console.log(req.body)
if (patient == null) {
res.sendStatus(404);
}
await patient.updateOne({
credit: patient.credit + req.body.increaseCreditBy,
});
res.redirect("/");
}
});
app.post("/addAppointment", async (req, res) => {
const user = authToken(req);
if (user == 401 || user == 403) {
console.log("unauthed appointment attempt", user);
res.sendStatus(user);
} else {
console.log("appointment attempt", user);
const userData = await Doctor.findById(user);
if (userData == null) {
res.sendStatus(403);
}
let patient = await Patient.findById(req.body.patientId);
if (patient == null) {
res.sendStatus(404);
}
patient.appointments.push({
doctor: user,
date: req.body.appointmentDate,
});
sendMail({
subject: "Appointment Scheduled",
html: `<h3>An appointment has been scheduled with Dr. ${userData.name} at ${DateTime.fromISO(req.body.appointmentDate).toLocaleString(DateTime.DATETIME_MED)}</h3>
<h3>Kindly add that to your calendar</h3>
- Team ${process.env.NAME}
`,
to: patient.email,
when: DateTime.now().toISO(),
});
await patient.save();
res.redirect("/");
}
});
app.post("/addMedicine", async (req, res) => {
const user = authToken(req);
if (user == 401 || user == 403) {
console.log("unauthed presc attempt", user);
res.sendStatus(user);
} else {
console.log("presc attempt", user);
const userData = await Doctor.findById(user);
if (userData == null) {
res.sendStatus(403);
}
console.log(req.body)
let patient = await Patient.findById(req.body.patientId);
if (patient == null) {
res.sendStatus(404);
}
patient.medSchedule.push({
med: req.body.med,
quantity: req.body.quantity,
period: req.body.period,
});
await patient.save();
res.redirect("/");
}
});
app.listen(port, () => {
console.log(`${process.env.NAME} app running on port ${port}`);
});
};
module.exports = { appLogic };