-
Notifications
You must be signed in to change notification settings - Fork 6
/
database.js
402 lines (370 loc) · 14.1 KB
/
database.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
const { Pool } = require("pg");
const bcrypt = require("bcrypt");
const Filter = require("bad-words");
const customFilter = new Filter({ placeHolder: "x" });
let pool, client;
pool = new Pool({
connectionString: process.env.DATABASE_URL,
ssl: { rejectUnauthorized: false }
});
pool.on("error", async (err) => {
console.log("Database Pool ERROR: ", err);
console.log("Trying to reconnect to database...");
client = await pool.connect();
});
// ------------------------------------------------------------------------------------
// ------------------------------------- INITIALIZE -----------------------------------
// ------------------------------------------------------------------------------------
exports.init = async () => {
try {
client = await pool.connect();
console.log("Connected to database.");
} catch (e) {
console.log("Error connecting to database.");
}
// initialize tables if not exists
await client.query(`
CREATE TABLE IF NOT EXISTS "patient" (
"id" varchar PRIMARY KEY,
"password" varchar NOT NULL,
"full_name" varchar,
"address" varchar,
"dob" date,
"gender" varchar,
"phone" varchar
);
CREATE TABLE IF NOT EXISTS "doctor" (
"id" varchar PRIMARY KEY,
"password" varchar NOT NULL,
"full_name" varchar,
"gender" varchar,
"phone" varchar,
"dob" date,
"specialization" varchar,
"year_of_passing" int,
"mbbs_reg" varchar UNIQUE
);
CREATE TABLE IF NOT EXISTS "appointment" (
"id" varchar PRIMARY KEY,
"patient_id" varchar NOT NULL,
"doctor_id" varchar NOT NULL,
"status" varchar NOT NULL,
"reason" varchar NOT NULL,
"date" timestamptz NOT NULL
);
CREATE TABLE IF NOT EXISTS "admin" (
"id" varchar PRIMARY KEY,
"phone" varchar NOT NULL,
"password" varchar NOT NULL
);
CREATE TABLE IF NOT EXISTS "hospital" (
"name" varchar PRIMARY KEY,
"address" varchar NOT NULL,
"phone" varchar NOT NULL
);
ALTER TABLE "appointment" ADD FOREIGN KEY ("patient_id") REFERENCES "patient" ("id");
ALTER TABLE "appointment" ADD FOREIGN KEY ("doctor_id") REFERENCES "doctor" ("id");
`);
};
// ------------------------------------------------------------------------------------
// ------------------------------------- PATIENT -------------------------------------
// ------------------------------------------------------------------------------------
function verifyPatientData(patient) {
let { id, full_name, address, dob, gender, phone, password } = patient;
let errors = [];
if (!id) errors.push("No email provided.");
if (!password) errors.push("No password provided.");
if (!full_name) errors.push("No full name provided.");
if (!dob) errors.push("No date of birth provided.");
if (!gender) errors.push("No gender provided.");
if (!address) errors.push("No address provided.");
if (!phone) errors.push("No phone provided.");
return errors;
}
exports.addPatientData = async (patient) => {
let { id, full_name, address, dob, gender, phone, password } = patient;
id = customFilter.clean(id);
full_name = customFilter.clean(full_name);
address = customFilter.clean(address);
gender = customFilter.clean(gender);
phone = customFilter.clean(phone);
let errors = verifyPatientData(patient);
if (errors.length != 0) return errors.join(" ");
try {
let res = await client.query("INSERT INTO patient (id, full_name, address, dob, gender, phone, password) VALUES ($1, $2, $3, $4, $5, $6, $7)",
[id, full_name, address, dob, gender, phone, password]);
if (res.command == "INSERT") return "SUCCESS";
} catch (err) {
console.log(err, `\n\naddPatientData ERROR: ${err.detail}\n MESSAGE ${err.message}`);
}
};
const getPatientByID = async (id, alsoPassword) => {
if (!id) return null;
try {
let res;
res = await client.query("SELECT * FROM patient WHERE id = $1",
[id]);
if (res && res.rows && res.rows[0]) {
if (!alsoPassword) {
delete res.rows[0].password;
}
res.rows[0].role = "patient";
return res.rows[0];
}
else return null;
} catch (err) {
console.log(err, `\n\ngetPatientByID ERROR: ${err.detail}\n MESSAGE ${err.message}`);
return null;
}
};
exports.getPatientByID = getPatientByID;
const signupPatient = async (id, password) => {
let hashed_password = await bcrypt.hash(password, 10);
try {
let res = await client.query("INSERT INTO patient (id, password) VALUES ($1, $2) ON CONFLICT DO NOTHING;",
[id, hashed_password]);
if (res.command == "INSERT") return "SUCCESS";
else return null;
} catch (err) {
console.log(err, `\n\nsignupPatient ERROR: ${err.detail}\n MESSAGE ${err.message}`);
}
};
exports.signupPatient = signupPatient;
const getAllPatients = async () => {
let res = await client.query("SELECT * from patient;");
if (res && res.rows) {
res.rows.map(row => {
if (row.password) delete row.password;
});
}
return res.rows || [];
};
exports.getAllPatients = getAllPatients;
const getAllDoctors = async () => {
let res = await client.query("SELECT * FROM doctor;");
res.rows.map(row => {
if (row.password) delete row.password;
});
return res.rows || [];
};
exports.getAllDoctors = getAllDoctors;
const setPatientData = async (id, { full_name, address, dob, gender, phone }) => {
try {
full_name = customFilter.clean(full_name);
address = customFilter.clean(address);
gender = customFilter.clean(gender);
phone = customFilter.clean(phone);
let res = await client.query("UPDATE patient SET full_name = $2, address = $3, dob = $4, gender = $5, phone = $6 WHERE id = $1", [
id, full_name, address, dob, gender, phone
]);
if (res.command == "UPDATE") return "SUCCESS";
else return null;
} catch (err) {
console.log("setPatientData Error: ", err);
return null;
}
};
exports.setPatientData = setPatientData;
const makeAppointment = async ({ id, patient_id, doctor_id, date, status, reason }) => {
try {
date = date.replace(" +05.30", "+05:30");
reason = customFilter.clean(reason);
status = customFilter.clean(status);
let res = await client.query("INSERT INTO appointment (id, patient_id, doctor_id, status, reason, date) VALUES ($1, $2, $3, $4, $5, $6)", [
id, patient_id, doctor_id, status, reason, date
]);
if (res.command == "INSERT") return "SUCCESS";
else return null;
} catch (err) {
console.log("makeAppointment Error: ", err);
return null;
}
};
exports.makeAppointment = makeAppointment;
const cancelAppointmentByPatient = async (id, patientId) => {
try {
let res = await client.query("UPDATE appointment SET status = 'CANCELLED' WHERE id = $1 AND patient_id = $2", [
id, patientId
]);
if (res.command && res.command == "UPDATE") return true;
return false;
} catch (err) {
console.log("cancelAppointmentByPatient Error: ", err);
return null;
}
};
exports.cancelAppointmentByPatient = cancelAppointmentByPatient;
const getAppointmentsByPatientID = async (patient_id) => {
try {
let res = await client.query("SELECT * FROM appointment WHERE patient_id = $1", [patient_id]);
if (res && res.rows) return res.rows;
return [];
} catch (err) {
console.log("getAppointmentsByPatientID Error: ", err);
return [];
}
};
exports.getAppointmentsByPatientID = getAppointmentsByPatientID;
// ------------------------------------------------------------------------------------
// ------------------------------------- HOSPITAL -------------------------------------
// ------------------------------------------------------------------------------------
const getHospitalByName = async (name) => {
try {
let res = await client.query("SELECT * FROM hospital WHERE name = $1", [name]);
if (res.rows && res.rows[0]) return res.rows[0];
return [];
} catch (err) {
console.log("getHospitalByName Error: ", err);
return null;
}
};
exports.getHospitalByName = getHospitalByName;
// ----------------------------------------------------------------------------------
// ------------------------------------- DOCTOR -------------------------------------
// ----------------------------------------------------------------------------------
const getDoctorByID = async (id, alsoPassword) => {
if (!id) return null;
try {
let res;
res = await client.query("SELECT * FROM doctor WHERE id = $1",
[id]);
if (res && res.rows && res.rows[0]) {
if (!alsoPassword) {
delete res.rows[0].password;
}
res.rows[0].role = "doctor";
return res.rows[0];
}
else return null;
} catch (err) {
console.log(err, `\n\ngetDoctorByID ERROR: ${err.detail}\n MESSAGE ${err.message}`);
return null;
}
};
exports.getDoctorByID = getDoctorByID;
const getAppointmentsByDoctorID = async (doctor_id) => {
try {
let res = await client.query("SELECT * FROM appointment WHERE doctor_id = $1 AND status = 'APPROVED'", [doctor_id]);
if (res && res.rows) return res.rows;
return [];
} catch (err) {
console.log("getAppointmentsByDoctorID Error: ", err);
return [];
}
};
exports.getAppointmentsByDoctorID = getAppointmentsByDoctorID;
const completeAppointmentByDoctor = async (id, doctor_id) => {
try {
let res = await client.query("UPDATE appointment SET status = 'COMPLETED' WHERE id = $1 AND doctor_id = $2", [
id, doctor_id
]);
if (res.command && res.command == "UPDATE") return true;
return false;
} catch (err) {
console.log("completeAppointmentByDoctor Error: ", err);
return null;
}
};
exports.completeAppointmentByDoctor = completeAppointmentByDoctor;
// ----------------------------------------------------------------------------------
// ------------------------------------- ADMIN --------------------------------------
// ----------------------------------------------------------------------------------
const getAllAppointments = async () => {
try {
let res = await client.query("SELECT * FROM appointment WHERE status = 'WAITING_APPROVAL'");
if (res && res.rows) return res.rows;
return [];
} catch (err) {
console.log("getAllAppointments Error: ", err);
return [];
}
};
exports.getAllAppointments = getAllAppointments;
const getAdminByID = async (id, alsoPassword) => {
if (!id) return null;
try {
let res;
res = await client.query("SELECT * FROM admin WHERE id = $1",
[id]);
if (res && res.rows && res.rows[0]) {
if (!alsoPassword) {
delete res.rows[0].password;
}
res.rows[0].role = "admin";
return res.rows[0];
}
else return null;
} catch (err) {
console.log(err, `\n\ngetAdminByID ERROR: ${err.detail}\n MESSAGE ${err.message}`);
return null;
}
};
exports.getAdminByID = getAdminByID;
const approveAppointmentByAdmin = async (id) => {
try {
let res = await client.query("UPDATE appointment SET status = 'APPROVED' WHERE id = $1", [id]);
if (res.command && res.command == "UPDATE") return true;
return false;
} catch (err) {
console.log("approveAppointmentByAdmin Error: ", err);
return null;
}
};
exports.approveAppointmentByAdmin = approveAppointmentByAdmin;
const cancelAppointmentByAdmin = async (id) => {
try {
let res = await client.query("UPDATE appointment SET status = 'CANCELLED' WHERE id = $1", [id]);
if (res.command && res.command == "UPDATE") return true;
return false;
} catch (err) {
console.log("cancelAppointmentByAdmin Error: ", err);
return null;
}
};
exports.cancelAppointmentByAdmin = cancelAppointmentByAdmin;
const getDoctorByMBBS = async (mbbs_reg, alsoPassword) => {
if (!mbbs_reg) return null;
try {
let res;
res = await client.query("SELECT * FROM doctor WHERE mbbs_reg = $1",
[mbbs_reg]);
if (res && res.rows && res.rows[0]) {
if (!alsoPassword) {
delete res.rows[0].password;
}
res.rows[0].role = "doctor";
return res.rows[0];
}
else return null;
} catch (err) {
console.log(err, `\n\ngetDoctorByMBBS ERROR: ${err.detail}\n MESSAGE ${err.message}`);
return null;
}
};
exports.getDoctorByMBBS = getDoctorByMBBS;
exports.addDoctor = async (id, { full_name, password, gender, phone, specialization, year_of_passing, mbbs_reg, dob }) => {
if (!id) return;
full_name = customFilter.clean(full_name);
gender = customFilter.clean(gender);
phone = customFilter.clean(phone);
specialization = customFilter.clean(specialization);
mbbs_reg = customFilter.clean(mbbs_reg);
let hashed_password = await bcrypt.hash(password, 10);
try {
let res = await client.query("INSERT INTO doctor (id, password, full_name, gender, phone, specialization, year_of_passing, mbbs_reg, dob) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)",
[id, hashed_password, full_name, gender, phone, specialization, year_of_passing, mbbs_reg, dob]);
if (res.command == "INSERT") return "SUCCESS";
} catch (err) {
console.log(err, `\n\naddDoctor ERROR: ${err.detail}\n MESSAGE ${err.message}`);
}
};
exports.deleteDoctor = async (id) => {
if (!id) return;
try {
let res = await client.query("DELETE FROM doctor WHERE id = $1",
[id]);
if (res.command == "DELETE") return "SUCCESS";
} catch (err) {
console.log(err, `\n\ndeleteDoctor ERROR: ${err.detail}\n MESSAGE ${err.message}`);
}
};