This repository was archived by the owner on Feb 3, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.js
More file actions
232 lines (177 loc) · 6.4 KB
/
db.js
File metadata and controls
232 lines (177 loc) · 6.4 KB
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
const cache = require('memory-cache');
const postgres = require("postgres");
let { PGHOST, PGDATABASE, PGUSER, PGPASSWORD, ENDPOINT_ID } = process.env;
const sql = postgres({
host: PGHOST,
database: PGDATABASE,
username: PGUSER,
password: PGPASSWORD,
port: 5432,
ssl: "require",
connection: {
options: `project=${ENDPOINT_ID}`,
},
onnotice: false
});
function updateCache(cacheKey, id, items) {
const itemMap = new Map();
const setItem = (item) => itemMap.set(item[id], item);
// Get existing items from cache and add to map
cache.get(cacheKey)?.forEach(setItem);
// Add new items to map, overwriting duplicates
if (Array.isArray(items)) {
items.forEach(setItem);
} else {
setItem(items);
}
// Update cache with new array of items
cache.put(cacheKey, Array.from(itemMap.values()));
}
let getPrescriptionsByAppointmentId = async (id) => {
let prescriptions = await sql`SELECT * FROM prescription WHERE appointmentid = ${id};`;
if (prescriptions[0]) updateCache('prescriptions', 'prescriptionid', prescriptions);
return prescriptions;
};
let getVitals = async (id) => {
let vitals = cache.get('vitals')?.find((vital) => vital.patientid == id);
if (!vitals) {
vitals = (await sql`SELECT * FROM vitals WHERE patientid = ${id};`)[0];
if (vitals) updateCache('vitals', 'patientid', vitals);
}
return vitals;
};
let getMedicalHistory = async (id) => {
let medicalHistories = cache.get('medicalHistories')?.find((medicalHistory) => medicalHistory.patientid == id);
if (!medicalHistories) {
medicalHistories = (await sql`SELECT * FROM medicalhistory WHERE patientid = ${id};`)[0];
if (medicalHistories) updateCache('vitals', 'patientid', medicalHistories);
}
return medicalHistories;
};
module.exports = {
loginUser: async (email, password) => {
email = email.toLowerCase();
password = password;
const type = email.endsWith("@aphella.com") ? 'staff' : 'patient';
let user = cache.get('users')?.find((user) => user.emailaddress == email);
if (!user) {
user = (await sql`SELECT * FROM ${sql(type)} WHERE emailaddress = ${email}`)[0];
if (user) updateCache('users', 'emailaddress', user);
}
return { user, userInDatabase: !!user };
},
getStaff: async (id) => {
let staff;
if (!id) {
staff = await sql`SELECT * FROM staff`;
if (staff[0]) {
staff = await Promise.all(staff.map(async (staff) => {
staff.phonenumber = staff.phonenumber.trim();
if (!staff.type) {
if (staff.jobid == 8) staff.type = 'admin';
else staff.type = 'staff';
}
return staff;
}));
updateCache('staff', 'staffid', staff);
}
} else {
staff = cache.get('staff')?.find((staff) => staff.staffid == id);
if (!staff) staff = (await sql`SELECT * FROM staff WHERE staffid = ${id}`)[0];
if (staff) {
staff.phonenumber = staff.phonenumber.trim();
if (!staff.type) {
if (staff.jobid == 8) staff.type = 'admin';
else staff.type = 'staff';
}
updateCache('staff', 'staffid', staff);
}
}
return staff;
},
getPatient: async (id) => {
let patient = cache.get('patients')?.find((patient) => patient.patientid == id);
if (!patient) patient = (await sql`SELECT * FROM patient WHERE patientid = ${id}`)[0];
if (patient) {
patient.phonenumber = patient.phonenumber.trim();
if (!patient.type) patient.type = 'patient';
if (!patient.medicalHistory) patient.medicalHistory = await getMedicalHistory(patient.patientid);
if (!patient.vitals) patient.vitals = await getVitals(patient.patientid);
updateCache('patients', 'patientid', patient);
}
return patient;
},
getJob: async (id) => {
let job = cache.get('jobs')?.find((job) => job.jobid == id);
if (!job) {
job = (await sql`SELECT * FROM job WHERE jobid = ${id};`)[0];
if (job) updateCache('jobs', 'jobid', job);
}
return job;
},
getAppointmentsByUserId: async (type, id) => {
let appointments;
if (type == 'patient') {
appointments = await sql`SELECT * FROM appointment WHERE patientid = ${id}`;
} else {
appointments = await sql`SELECT * FROM appointment AS appt JOIN appointmentstafflink AS link ON appt.appointmentid = link.appointmentid WHERE link.staffid = ${id};`;
}
if (appointments[0]) {
appointments = await Promise.all(appointments?.map(async (appointment) => {
appointment.prescriptions = await getPrescriptionsByAppointmentId(appointment.appointmentid);
return appointment;
}));
updateCache('appointments', 'appointmentid', appointments);
}
return appointments;
},
getPatientsByDoctor: async function (id) {
let patients = await sql`SELECT * FROM patient WHERE maindoctor = ${id};`;
if (patients[0]) {
patients = await Promise.all(patients.map(async (patient) => {
patient.phonenumber = patient.phonenumber.trim();
patient.type = 'patient';
patient.medicalHistory = await getMedicalHistory(patient.patientid);
patient.vitals = await getVitals(patient.patientid);
return patient;
}));
updateCache('patients', 'patientid', patients);
}
return patients;
},
getRequestsReceivedByStaffId: async (id) => {
let requests = await sql`SELECT * FROM request WHERE request.tostaff = ${id};`;
if (requests[0]) {
updateCache('requests', 'requestid', requests);
}
return requests;
},
getLastXRequestsReceivedByStaffId: async (id, amount) => {
let requests = await sql`SELECT * FROM request WHERE date <= CURRENT_DATE AND request.tostaff = ${id} ORDER BY date DESC LIMIT ${amount};`;
if (requests[0]) updateCache('requests', 'requestid', requests);
return requests;
},
getPatients: async () => {
let patients = await sql`SELECT * FROM patient`;
if (patients[0]) {
patients = await Promise.all(patients.map(async (patient) => {
patient.phonenumber = patient.phonenumber.trim();
patient.type = 'patient';
patient.medicalHistory = await getMedicalHistory(patient.patientid);
patient.vitals = await getVitals(patient.patientid);
return patient;
}));
updateCache('patients', 'patientid', patients);
}
return patients;
},
getAppointmentStaff: async (id) => {
let staff = cache.get('appointmentstafflink')?.find((link) => link.appointmentid == id);
if (!staff) staff = await sql`SELECT staffid FROM appointmentstafflink WHERE appointmentid = ${id}`;
if (staff) updateCache('appointmentstafflink', 'appointmentid', staff);
return staff;
},
getPrescriptionsByAppointmentId,
getMedicalHistory,
getVitals
}