-
Notifications
You must be signed in to change notification settings - Fork 1
/
methods.js
879 lines (792 loc) · 29.6 KB
/
methods.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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
const { Patient, Caretaker, Session, NDC, SessionIntake, Medication, sequelize, Cabinet, CabinetBox, Schedule } = require('./database');
const constants = require("./constants");
const axios = require("axios");
const utils = require("./utils");
const _ = require('lodash'); // Lodash makes grouping easier
const nodemailer = require('nodemailer');
const Handlebars = require('handlebars');
const { INTEGER } = require('sequelize');
Handlebars.registerHelper('ifEquals', function (arg1, arg2, options) {
return (arg1 == arg2) ? options.fn(this) : options.inverse(this);
});
// Portal Endpoints
/**
* Renders the profile page with patient and caretaker data.
*
* @param {Object} req - The request object.
* @param {Object} res - The response object.
*/
const profile = async (req, res) => {
if (req.session.user) {
try {
return res.render('profile', {
patients: await getAllPatients(),
careTaker: await getCaretaker(req.session.user),
});
} catch (error) {
console.error(error);
}
} else {
res.redirect('/login');
}
}
const getMedicationsWithDetails = async (patientId) => {
try {
if (typeof patientId !== 'string') {
console.error('Error fetching medications: Invalid patient ID type');
return [];
}
// Assuming Medication has a foreign key relationship with NDC and CabinetBox
const medications = await Medication.findAll({
include: [
{
model: NDC,
attributes: ['code']
},
{
model: CabinetBox,
attributes: ['box', 'quantity']
},
]
});
return medications.map(medication => {
const medicationJSON = medication.toJSON();
return {
...medicationJSON,
ndc: medicationJSON.NDCs?.[0]?.code,
box: medicationJSON.CabinetBoxes?.[0]?.box,
quantity: medicationJSON.CabinetBoxes?.[0]?.quantity,
};
});
} catch (err) {
console.error('Error fetching medications with details', err);
throw err;
}
};
const getMedicationSchedules = async (patientId) => {
try {
if (typeof patientId !== 'string') {
console.error('Error fetching medication schedules: Invalid patient ID type');
return [];
}
const schedules = await Schedule.findAll({
where: { patient_id: patientId },
include: [
{
model: Medication
},
{
model: SessionIntake // Include session intakes
},
]
});
return schedules.map(schedule => schedule.toJSON());
} catch (err) {
console.error('Error fetching medication schedules', err);
throw err;
}
};
const getSessionIntakesForMedicationIds = async (medicationIds) => {
try {
const sessionIntakes = await SessionIntake.findAll({
where: { medication_id: medicationIds }
});
return sessionIntakes.map(intake => intake.toJSON());
} catch (error) {
console.error('Error fetching session intakes for medication IDs:', error);
throw error;
}
};
const getIngestionChartData = async (req, res) => {
try {
const session_intakes = await SessionIntake.findAll();
var dict_session_intakes = {};
session_intakes.map(intake => {
if (dict_session_intakes[intake.medication_id] === undefined) {
dict_session_intakes[intake.medication_id] = [(intake.end_time - intake.start_time) / 1000, 1];
} else {
dict_session_intakes[intake.medication_id][0] += (intake.end_time - intake.start_time) / 1000;
dict_session_intakes[intake.medication_id][1] += 1;
}
})
var final_array = [];
for (let key in dict_session_intakes) {
const medication = await getMedicationById(key);
if (medication) { // Check if medication exists
final_array.push({
name: medication.brand_name,
y: dict_session_intakes[key][0] / dict_session_intakes[key][1]
});
}
}
return final_array;
} catch (error) {
console.error('Error in getIngestionChartData:', error);
throw error;
}
}
const getFilterMedicationData = (medicationDetails) => {
try {
return medicationDetails.filter(medication => medication.box !== undefined && medication.quantity !== undefined)
.sort((a, b) => parseInt(a.box) - parseInt(b.box));
} catch (error) {
console.error('Error in getFilterMedicationData:', error);
throw error;
}
};
const convertDayToNumber = (dayName) => {
const dayMap = {
'Sunday': 0,
'Monday': 1,
'Tuesday': 2,
'Wednesday': 3,
'Thursday': 4,
'Friday': 5,
'Saturday': 6
};
return dayMap[dayName];
}
/**
* Renders the patient page with patient information and caretaker options.
*
* @param {Object} req - The request object.
* @param {Object} res - The response object.
*
* @returns {undefined}
*/
const patient = async (req, res) => {
if (req.session.user) {
try {
const patientDetails = await getPatient(req.query.id);
const caretakerDetails = await getCaretaker(patientDetails.caretaker_id);
const medicationDetails = await getMedicationsWithDetails(patientDetails.id);
const scheduleDetails = await getMedicationSchedules(patientDetails.id);
const medicationIds = scheduleDetails.map(schedule => schedule.medication_id);
// Fetch session intakes for medication IDs
const sessionIntakes = await getSessionIntakesForMedicationIds(medicationIds);
// Combine session intakes with schedule details
const schedulesWithIngestion = scheduleDetails.map(schedule => {
const sessionIntake = sessionIntakes.find(intake => intake.medication_id === schedule.medication_id && isIngestedOnDay(intake, schedule));
const ingested = sessionIntake ? sessionIntake.ingested : null; // Get ingested value or null if not found for the day
return {
...schedule,
ingested: ingested // Assign ingested value
};
});
// Rendering logic for session intake value
const sessionIntakeDisplay = schedulesWithIngestion.map(schedule => {
const ingested = schedule.ingested;
let displayValue = '';
if (ingested === 1) {
displayValue = 'true';
} else if (ingested === 0) {
displayValue = 'false';
}
// If ingested is null or undefined, displayValue remains blank
return displayValue;
});
// console.log("Schedules with ingestion:", schedulesWithIngestion);
const ingestionChartData = await getIngestionChartData(patientDetails.id);
const sessionDetails = await getAlarmResponseTime(patientDetails.id);
const ingestionFailureData = await getMedicationFailureRate(patientDetails.id);
const filterMedicationData = getFilterMedicationData(medicationDetails);
// console.log("scheduleDetails =>", scheduleDetails);
return res.render('patient', {
patient: patientDetails,
careTaker: caretakerDetails,
medicationData: filterMedicationData,
schedules: schedulesWithIngestion, // Use schedulesWithIngestion instead of scheduleDetails
sessionIntake: sessionIntakeDisplay, // Rendered session intake display values
sessionDetails: JSON.stringify(sessionDetails),
ingestionChartData: JSON.stringify(ingestionChartData),
ingestionFailureData: JSON.stringify(ingestionFailureData)
});
} catch (error) {
console.error(error);
}
} else {
res.redirect('/login');
}
}
// Helper function to check if session intake occurred on the same day as the schedule
const isIngestedOnDay = (sessionIntake, schedule) => {
// Extracting the day from the session intake start time
const intakeDay = new Date(sessionIntake.start_time).getDay();
return intakeDay === schedule.day;
};
// API Endpoints
/**
* Searches for medications based on the provided search query.
*
* @async
* @param {Object} req - The request object.
* @param {Object} res - The response object.
* @returns {Promise} A promise representing the search results.
* @throws {Error} If an error occurs during the search process.
* @description This function takes a search query as input, constructs a query string,
* sends a request to the FDA API, and returns the filtered medication data.
*/
const search_medications = async (req, res) => {
const searchQuery = req.params.searchQuery;
const query = `search=(openfda.brand_name:"${searchQuery}" OR openfda.generic_name:"${searchQuery}"OR openfda.product_ndc:"${searchQuery}")`;
const url = `${constants.FDABaseUrl}?${query}&limit=10`;
try {
const response = await axios.get(url);
if (response.data.results) {
const filteredMedications = utils.mapMedicationData(response.data);
res.json(filteredMedications);
} else {
res.json([]);
}
} catch (error) {
res.status(404).json([]);
}
}
/**
* Retrieves medication data from FDA API based on the provided ID.
*
* @param {object} req - The request object.
* @param {object} res - The response object.
* @return {Promise<void>} - The async function returns nothing directly, but
* modifies the response object.
*/
const get_medication = async (req, res) => {
const id = req.params.id;
const query = `search=(id:"${id}")`;
const url = `${constants.FDABaseUrl}?${query}&limit=1`;
try {
const response = await axios.get(url);
if (response.data.results) {
const filteredMedications = utils.mapMedicationData(response.data);
res.json(filteredMedications[0]);
} else {
res.json({});
}
} catch (error) {
res.status(404).send({ message: 'No medication found', code: error.code });
}
}
/**
* Function to handle the process of posting medication data.
*
* @async
* @param {Object} req - The request object.
* @param {Object} res - The response object.
* @returns {Promise} A promise that resolves when the medication data is successfully posted.
*/
const post_medication = async (req, res) => {
const cabinet_id = req.body.cabinet_id;
const t = await sequelize.transaction();
try {
if (cabinet_id === undefined) {
throw Error;
}
const medications = req.body.medications;
for (const med of medications) {
await createMedication(med.medication_id);
await CabinetBox.upsert({
cabinet_id: cabinet_id,
medication_id: med.medication_id,
box: med.box,
quantity: med.quantity
});
}
await t.commit();
res.status(200).send({ message: 'Medication received successfully.' });
} catch (err) {
await t.rollback();
console.error(err);
res.status(400).send({ message: 'Bad Request' });
}
}
/**
* Create medication schedules for a patient.
* @async
* @param {Object} req - The request object.
* @param {Object} res - The response object.
* @returns {void}
*/
const post_schedule = async (req, res) => {
const cabinet_id = req.body.cabinet_id;
const patient_id = req.body.patient_id;
const t = await sequelize.transaction();
try {
if (patient_id === undefined || cabinet_id === undefined) {
throw Error;
}
const medication_schedules = req.body.medications;
for (const schedule of medication_schedules) {
await Schedule.create({
patient_id: patient_id,
medication_id: schedule.medication_id,
day: schedule.day,
time: schedule.time,
});
}
await t.commit();
res.status(200).send({ message: 'Schedule received successfully.' });
} catch (err) {
await t.rollback();
console.error(err);
res.status(400).send({ message: 'Bad Request' });
}
}
/**
* Creates a session and session intakes for the given request and response objects.
*
* @async
* @param {Object} req - The request object contains the cabinet ID, patient ID, and session intakes.
* @param {Object} res - The response object used to send the session status.
* @returns {Promise<void>} - A Promise that resolves when the session is created and committed successfully, otherwise rejects with an error.
*/
const getCaretakerEmail = async (patientId) => {
try {
// Find the patient by ID and include the associated caretaker
const patient = await Patient.findOne({
where: { id: patientId },
include: [{
model: Caretaker,
attributes: ['email']
}]
});
// Extract the caretaker's email from the patient object
if (patient && patient.Caretaker) {
return {
patient: patient.toJSON(),
caretakerEmail: patient.Caretaker.email
};
} else {
throw new Error('Patient or caretaker not found.');
}
} catch (error) {
console.error('Error fetching patient and caretaker email:', error);
throw error;
}
};
// Define email sending function using Outlook SMTP settings
const sendEmailToCaretaker = async (caretakerEmail) => {
const transporter = nodemailer.createTransport({
host: 'smtp-mail.outlook.com',
port: 587,
secure: false,
auth: {
user: 'caretaker.portal@outlook.com',
pass: 'Capstone2023/2024'
}
});
const mailOptions = {
from: 'caretaker.portal@outlook.com',
to: caretakerEmail,
subject: 'Medication Intake Alert',
text: `Dear Caretaker,\n\nThis is to inform you that patient has missed their medication intake.\n\nPlease take necessary action.`
};
try {
console.log('Caretaker email:', caretakerEmail); // Log the caretaker's email address
await transporter.sendMail(mailOptions);
console.log('Email sent to caretaker:', caretakerEmail);
} catch (error) {
console.error('Error sending email:', error);
throw error;
}
};
const post_session = async (req, res) => {
const cabinet_id = req.body.cabinet_id;
const patient_id = req.body.patient_id;
const t = await sequelize.transaction();
try {
if (patient_id === undefined || cabinet_id === undefined) {
throw Error;
}
const session_intakes = req.body.session_intakes;
const session = await Session.create(req.body, { transaction: t });
for (const intake of session_intakes) {
await SessionIntake.create({
medication_id: intake.medication_id,
start_time: intake.start_time,
ingest_time: intake.ingest_time,
end_time: intake.end_time,
ingested: intake.ingested,
session_id: session.id
}, { transaction: t });
// Check if the medication was not ingested and send email to caretaker
if (!intake.ingested) {
//call function to get caretaker email by patient id
const caretakerEmail = await getCaretakerEmail(patient_id);
// Send email to caretaker
await sendEmailToCaretaker(caretakerEmail.caretakerEmail);
}
}
await t.commit();
res.status(200).send({ message: 'Session received successfully.' });
} catch (err) {
await t.rollback();
console.error(err);
res.status(400).send({ message: 'Bad Request' });
}
}
// Functions
/**
* Retrieves medication by ID from FDA API.
*
* @param {string} id - The ID of the medication to fetch.
* @returns {Object|null} - The medication object if found, or null if not found.
* @throws {Error} - If an error occurs while fetching the medication.
*/
const getMedicationById = async (id) => {
const query = `search=(id:"${id}")`;
const url = `${constants.FDABaseUrl}?${query}&limit=1`;
try {
const response = await axios.get(url);
if (response.data.results) {
const filteredMedications = utils.mapMedicationData(response.data);
return filteredMedications[0];
} else {
return null;
}
} catch (error) {
console.error("Error fetching medication:", error);
}
}
/**
* Creates or updates a medication by its ID.
*
* @param {number} id - The ID of the medication.
* @returns {Promise} - A promise that resolves to the created or updated medication.
*/
const createMedication = async (id) => {
const medication = await getMedicationById(id);
await Medication.upsert(medication);
for (const ndc of medication.product_ndc) {
await NDC.upsert({
code: ndc,
medication_id: id
})
}
return medication;
}
/**
* Retrieves all patients along with their associated caretaker information.
*
* @async
* @returns {Promise<Array<object>>} - Array of patient objects
*
* @throws {Error} if there is any error encountered while fetching patients for caretaker
*/
const getAllPatients = async () => {
try {
const patients = await Patient.findAll({
include: [{
model: Caretaker, attributes: ['first_name', 'last_name']
}]
});
return patients.map(patient => {
const patientJson = patient.toJSON();
return {
...patientJson,
caretaker_first_name: patientJson.Caretaker.first_name,
caretaker_last_name: patientJson.Caretaker.last_name,
Caretaker: undefined
};
});
} catch (err) {
console.error('Error fetching patients for caretaker:', err);
throw err;
}
}
/**
* Retrieves a patient with the specified ID from the database.
*
* @async
* @param {string} id - The ID of the patient to retrieve.
* @returns {Promise<Object|null>} - A promise that resolves with the patient object if found, or null if not found.
* @throws {Error} - If there was an error fetching the patient.
*/
const getPatient = async (id) => {
try {
const patient = await Patient.findByPk(id);
return patient ? patient.toJSON() : null;
} catch (err) {
console.error('Error fetching patient', err);
throw err;
}
};
/**
* Fetches all caretakers from the database.
*
* @returns {Promise} A promise that resolves to an array of caretaker objects.
* @throws {Error} If there is an error fetching caretakers from the database.
*/
const getAllCaretakers = async () => {
try {
const caretakers = await Caretaker.findAll();
return caretakers.map(caretaker => caretaker.toJSON());
} catch (err) {
console.error('Error fetching caretakers', err);
throw err;
}
};
/**
* Fetches a caretaker by id.
*
* @param {number} id - The id of the caretaker.
* @returns {Promise<Object|null>} - A promise that resolves to the caretaker object
* if found, otherwise null.
* @throws {Error} - If there was an error fetching the caretaker.
*/
const getCaretaker = async (id) => {
try {
const caretaker = await Caretaker.findByPk(id);
return caretaker ? caretaker.toJSON() : null;
} catch (err) {
console.error('Error fetching caretaker', err);
throw err;
}
};
/**
* Retrieves the medication schedules for a given patient.
*
* @param {number} patientId - The ID of the patient.
* @returns {Promise<Array>} A promise that resolves to an array of medication schedules for the patient.
* @throws {Error} If there is an error fetching the medication schedules.
*/
const getPatientMedicationSchedule = async (patientId) => {
try {
const schedules = await Schedule.findAll({
where: { patient_id: patientId },
include: [
{
model: Medication,
required: true
},
{
model: Patient,
required: true,
where: { id: patientId }
}
]
});
const groupedByMedicationName = _.groupBy(schedules, (schedule) => schedule.Medication.brand_name);
return Object.keys(groupedByMedicationName).map(medicationName => ({
brand_name: medicationName,
schedules: groupedByMedicationName[medicationName]
}));
} catch (error) {
console.error('Error fetching medication schedules for patient:', error);
throw error;
}
};
/**
* Retrieves the medications for a given patient.
*
* @async
* @param {number} patientId - The ID of the patient.
* @returns {Promise<Object[]>} Array of medication objects.
* @throws {Error} If there is an error fetching medications.
*/
const getPatientMedications = async (patientId) => {
try {
const patient = await Patient.findByPk(patientId, {
include: [{
model: Cabinet,
include: [{
model: CabinetBox,
include: [{
model: Medication,
include: [{
model: NDC,
}],
}]
}]
}]
});
if (!patient || !patient.Cabinet) {
console.log('Patient or patient cabinet not found');
return [];
}
return patient.Cabinet.CabinetBoxes.reduce((acc, cabinetBox) => {
if (cabinetBox.Medication) {
acc.push({
// id: cabinetBox.Medication.id,
NDC: cabinetBox.Medication.NDCs,
brand_name: cabinetBox.Medication.brand_name,
generic_name: cabinetBox.Medication.generic_name,
box: cabinetBox.box,
quantity: cabinetBox.quantity,
// Include other attributes as needed
});
}
return acc;
}, []);
} catch (error) {
console.error('Error fetching medications for patient:', error);
throw error;
}
};
/**
* Calculates the average of ingestion times for each medication in SessionIntake.
*
* @param {string} id - The ID of the patient.
* @returns {Promise<Array>} - A list of medications and their historical average ingestion times.
* @throws {Error} - If there was an error during the calculation of ingestion times.
*/
const getIngestionTime = async (id) => {
try {
const sessionIntakes = await SessionIntake.findAll({
include: [
{
model: Medication,
required: true
},
{
model: Patient,
required: true,
where: { id: id }
}
]
});
const ingestionTimes = sessionIntakes.map(intake => {
const startTime = new Date(intake.start_time);
const endTime = new Date(intake.end_time);
const ingestionTime = (endTime - startTime) / 60; // in minutes
return {
medication_id: intake.medication_id,
brand_name: intake.Medication.brand_name,
generic_name: intake.Medication.generic_name,
ingestionTime: ingestionTime
};
});
const groupedByMedicationId = _.groupBy(ingestionTimes, (item) => item.medication_id);
return Object.keys(groupedByMedicationId).map(medicationId => {
const avgIngestionTime = _.meanBy(groupedByMedicationId[medicationId], 'ingestionTime');
return {
brand_name: groupedByMedicationId[medicationId][0].brand_name,
generic_name: groupedByMedicationId[medicationId][0].generic_name,
average_ingestion_time: avgIngestionTime
};
});
} catch (error) {
console.error('Error calculating average ingestion times:', error);
throw error;
}
};
// const getIngestionTime = async (id) => {
//
// };
const getAlarmResponseTime = async (id) => {
try {
const sessions = await Session.findAll({
where: { patient_id: id },
});
var calAlarmTime_dict = {};
sessions.map(sessions => {
const estDate = sessions.start_time;
const date = new Date(estDate).getUTCDay();
const diffTime = (sessions.start_time - sessions.alarm_time) / 1000;
if (calAlarmTime_dict[date] === undefined) {
calAlarmTime_dict[date] = [[estDate], diffTime, 1];
} else {
calAlarmTime_dict[date][0].push(estDate);
calAlarmTime_dict[date][1] += diffTime;
calAlarmTime_dict[date][2] += 1;
}
})
// console.log(calAlarmTime_dict)
var final_array = [];
const dayNames = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
for (var key in calAlarmTime_dict) {
final_array.push(
{
name: dayNames[new Date(calAlarmTime_dict[key][0][0]).getUTCDay()],
y: Math.ceil(parseFloat(String(Math.floor((calAlarmTime_dict[key][1] / calAlarmTime_dict[key][2]) / 60)) + "." + String(Math.floor((calAlarmTime_dict[key][1] / calAlarmTime_dict[key][2]) % 60))))
}
)
}
console.log(final_array)
// const dict_session_details = {};
// sessions.map(session => {
// if (dict_session_details[session.id] === undefined) {
// dict_session_details[session.id] = (session.end_time - session.start_time) / 1000;
// }
// });
// const final_dict = {};
// const session_intakes = await SessionIntake.findAll();
// for (let key in dict_session_details) {
// const res = session_intakes.filter(intake => intake.session_id === key);
// if (res.length > 0) {
// const medicationId = res[0].medication_id;
// if (medicationId) { // Check if medication_id exists
// if (final_dict[medicationId] === undefined) {
// final_dict[medicationId] = [dict_session_details[key], 1];
// } else {
// final_dict[medicationId][0] += dict_session_details[key];
// final_dict[medicationId][1] += 1;
// }
// }
// }
// }
// const final_array = [];
// for (let key in final_dict) {
// const medicationDetails = await getMedicationById(key);
// if (medicationDetails) {
// final_array.push({
// name: medicationDetails.brand_name,
// y: final_dict[key][0] / final_dict[key][1]
// });
// }
// }
return final_array;
} catch (error) {
console.error('Error calculating alarm response time:', error);
throw error;
}
};
const getMedicationFailureRate = async (id) => {
try {
const session_intakes = await SessionIntake.findAll();
var dict_failure_calculation = {};
session_intakes.map(intake => {
if (dict_failure_calculation[intake.medication_id] === undefined) {
if (intake.ingested)
dict_failure_calculation[intake.medication_id] = { ingested: 1, notIngested: 0, total: 1 };
else
dict_failure_calculation[intake.medication_id] = { ingested: 0, notIngested: 1, total: 1 }
} else {
if (intake.ingested)
dict_failure_calculation[intake.medication_id].ingested += 1;
else
dict_failure_calculation[intake.medication_id].notIngested += 1;
dict_failure_calculation[intake.medication_id].total += 1;
}
})
var final_array = [];
for (let key in dict_failure_calculation) {
const medicationDetails = await getMedicationById(key);
if (medicationDetails) { // Check if medication exists
final_array.push(
{
name: medicationDetails.brand_name,
y: dict_failure_calculation[key].notIngested === 0 ? 0 : (dict_failure_calculation[key].notIngested / dict_failure_calculation[key].total) * 100
}
);
}
}
return final_array;
} catch (error) {
console.error('Error in getMedicationFailureRate:', error);
throw error;
}
};
module.exports = {
profile,
patient,
search_medications,
get_medication,
post_medication,
post_schedule,
post_session,
getPatientMedicationSchedule,
getPatientMedications,
getIngestionTime,
getCaretakerEmail,
sendEmailToCaretaker
}