-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmedication_script
More file actions
58 lines (52 loc) · 2.23 KB
/
Copy pathmedication_script
File metadata and controls
58 lines (52 loc) · 2.23 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
let INTAKE_TIME = 'Time';
let INTAKE_MEDICATION = 'Medication';
let INTAKE_COUNT = 'Count';
let intakeTable = base.getTable("Intake");
let recentIntakeView = intakeTable.getView("Last 24h");
let intakeQuery = await recentIntakeView.selectRecordsAsync({
fields: [INTAKE_TIME, INTAKE_MEDICATION, INTAKE_COUNT]
});
let MEDICATION_DOSAGE = 'Dosage';
let medicationTable = base.getTable("Medication");
let medicationQuery = await medicationTable.selectRecordsAsync({
fields: [MEDICATION_DOSAGE]
});
let DOSAGE_INGREDIENT = 'Ingredient';
let DOSAGE_MG = 'mg';
let dosageTable = base.getTable("Dosage");
let dosageQuery = await dosageTable.selectRecordsAsync({
fields: [DOSAGE_INGREDIENT, DOSAGE_MG]
});
let INGREDIENT_NAME = 'Name';
let INGREDIENT_TOTAL_INTAKE = 'mg over last 24h';
let INGREDIENT_LAST_INTAKE = 'last time taken';
let ingredientTable = base.getTable("Ingredient");
let ingredientView = ingredientTable.getView("Grid view");
let ingredientsQuery = await ingredientView.selectRecordsAsync({
fields: [INGREDIENT_NAME, INGREDIENT_TOTAL_INTAKE, INGREDIENT_LAST_INTAKE]
});
for (let ingredientRecord of ingredientsQuery.records) {
let totalIntake = 0;
let lastIntake = new Date(0);
for (let intakeRecord of intakeQuery.records) {
// There is only one medication per intake record.
let medicationId = intakeRecord.getCellValue(INTAKE_MEDICATION)[0].id;
let medicationRecord = medicationQuery.getRecord(medicationId);
let dosageRecords = medicationRecord.getCellValue(MEDICATION_DOSAGE).map((dosage) =>
dosageQuery.getRecord(dosage.id)
);
for (let dosageRecord of dosageRecords) {
if (dosageRecord.getCellValue(DOSAGE_INGREDIENT)[0].id == ingredientRecord.id) {
totalIntake += intakeRecord.getCellValue(INTAKE_COUNT) * dosageRecord.getCellValue(DOSAGE_MG);
let intakeTime = new Date(intakeRecord.getCellValue(INTAKE_TIME));
if (intakeTime > lastIntake) {
lastIntake = intakeTime;
}
}
}
}
await ingredientTable.updateRecordAsync(ingredientRecord, {
[INGREDIENT_TOTAL_INTAKE]: totalIntake,
[INGREDIENT_LAST_INTAKE]: lastIntake.toISOString(),
});
}