-
Notifications
You must be signed in to change notification settings - Fork 6
/
adminRoutes.js
188 lines (163 loc) · 7.76 KB
/
adminRoutes.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
const { getPatientByID, getAllAppointments, approveAppointmentByAdmin,
getDoctorByID, getDoctorByMBBS, addDoctor, deleteDoctor, getAllDoctors,
cancelAppointmentByAdmin } = require("./database.js");
const isNum = require("is-number");
const Filter = require("bad-words");
const filter = new Filter();
module.exports = function (app, passport, renderTemplate) {
function checkAdminAuthenticated(req, res, next) {
if (req.isAuthenticated() && req.user && req.user.role == "admin") {
return next();
}
req.session.returnTo = req.originalUrl;
return res.redirect("/admin/login");
}
function checkAdminNotAuthenticated(req, res, next) {
if (req.isAuthenticated() && req.user.role == "admin") {
return res.redirect("/admin");
}
return next();
}
app.get("/admin", async (req, res) => {
renderTemplate(req, res, "pages/admin/index");
});
// Get login
app.get("/admin/login", checkAdminNotAuthenticated, (req, res) => {
if (req.user && req.user.role == "admin") res.redirect("/admin");
else renderTemplate(req, res, "pages/admin/login");
});
app.post("/admin/login", checkAdminNotAuthenticated, passport.authenticate("admin", {
successReturnToOrRedirect: "/admin",
failureRedirect: "/admin/login",
failureFlash: true
}));
app.get("/admin/logout", checkAdminAuthenticated, (req, res) => {
req.logOut();
req.flash("message", { type: "success", title: "Logout Successfull", text: "You have been logged out successfully." });
return res.redirect("/admin");
});
app.get("/admin/viewappointments", checkAdminAuthenticated, async (req, res) => {
let appointments = await getAllAppointments();
let patientIDs = [], doctorIDs = [];
appointments.forEach(({ patient_id, doctor_id }) => {
if (!patientIDs.includes(patient_id)) patientIDs.push(patient_id);
if (!doctorIDs.includes(doctor_id)) doctorIDs.push(doctor_id);
});
let patients = [], doctors = [];
patientIDs.forEach(id => patients.push(getPatientByID(id)));
doctorIDs.forEach(id => doctors.push(getDoctorByID(id)));
patients = await Promise.all(patients);
doctors = await Promise.all(doctors);
return renderTemplate(req, res, "pages/admin/viewappointments", { appointments, patients, doctors });
});
app.get("/admin/viewdoctors", checkAdminAuthenticated, async (req, res) => {
let doctors = await getAllDoctors();
return renderTemplate(req, res, "pages/admin/viewdoctors", { doctors });
});
app.post("/admin/approveappointment", checkAdminAuthenticated, async (req, res) => {
let id = req.body.id;
function showError(title, text) {
req.flash("message", { type: "danger", title, text });
return res.redirect("/admin/viewappointments");
}
if (!id) {
showError("Error", "Unable to mark appointment as approved.");
return;
}
try {
let response = await approveAppointmentByAdmin(id, req.user.id);
if (response) {
req.flash("message", { type: "success", title: "Success", text: "The appointment was marked as approved." });
return res.redirect("/admin/viewappointments");
}
req.flash("message", { type: "danger", title: "Error", text: "An error occured when marking appointment as approved." });
return res.redirect("/admin/viewappointments");
} catch (err) {
console.log(err);
}
return res.redirect("/admin/viewappointments");
});
app.post("/admin/cancelappointment", checkAdminAuthenticated, async (req, res) => {
let id = req.body.id;
function showError(title, text) {
req.flash("message", { type: "danger", title, text });
return res.redirect("/admin/viewappointments");
}
if (!id) {
showError("Error", "Unable to mark appointment as cancelled.");
return;
}
try {
let response = await cancelAppointmentByAdmin(id, req.user.id);
if (response) {
req.flash("message", { type: "success", title: "Success", text: "The appointment was marked as cancelled." });
return res.redirect("/admin/viewappointments");
}
req.flash("message", { type: "danger", title: "Error", text: "An error occured when marking appointment as cancelled." });
return res.redirect("/admin/viewappointments");
} catch (err) {
console.log(err);
}
return res.redirect("/admin/viewappointments");
});
app.get("/admin/adddoctor", checkAdminAuthenticated, async (req, res) => {
let doctors = await getAllDoctors();
return renderTemplate(req, res, "pages/admin/adddoctor", { doctors });
});
app.post("/admin/adddoctor", checkAdminAuthenticated, async (req, res) => {
let { id, password, full_name, gender, phone, specialization, year_of_passing, mbbs_reg, dob } = req.body;
let message = {
type: "danger",
title: "Error",
text: "Doctor with email already exists."
};
let returnError = (text) => {
message.text = text;
req.flash("message", message);
return res.redirect("/admin/adddoctor");
};
try {
if (filter.isProfane(id)) return returnError("Email contains explicit content.");
let doesExists = await getDoctorByID(id);
let doesExists2 = await getDoctorByMBBS(mbbs_reg);
if (doesExists) return returnError("Doctor with email already exists.");
if (doesExists2) return returnError("Doctor with MBBS Registration Number already exists.");
if (password.length < 5) return returnError("Password must be more than 5 characters.");
if (!isNum(year_of_passing) || Number(year_of_passing) > new Date().getFullYear()) return returnError("Year of passing must be a valid year.");
year_of_passing = Number(year_of_passing);
await addDoctor(id, {
full_name, gender, phone, specialization, year_of_passing, mbbs_reg, dob, password
});
req.flash("message", { type: "success", title: "Success", text: "The doctor was added successfully." });
return res.redirect("/admin");
} catch (err) {
console.log("adddoctor Error: ", err);
req.flash("message", { type: "danger", title: "Add Doctor Error", text: "Ensure all the fields are filled correctly." });
return res.redirect("/admin");
}
});
app.post("/admin/deletedoctor", checkAdminAuthenticated, async (req, res) => {
let { id } = req.body;
let message = {
type: "danger",
title: "Error",
text: "Doctor doesn't exist."
};
let returnError = (text) => {
message.text = text;
req.flash("message", message);
return res.redirect("/admin/viewdoctors");
};
try {
let doesExists = await getDoctorByID(id);
if (!doesExists) return returnError("Doctor with email doesn't exist.");
await deleteDoctor(id);
req.flash("message", { type: "success", title: "Success", text: "The doctor was deleted successfully." });
return res.redirect("/admin/viewdoctors");
} catch (err) {
console.log("adddoctor Error: ", err);
req.flash("message", { type: "danger", title: "Add Doctor Error", text: "Ensure all the fields are filled correctly." });
return res.redirect("/admin/viewdoctors");
}
});
};