-
Notifications
You must be signed in to change notification settings - Fork 0
/
2-finalizer.js
87 lines (74 loc) · 3.22 KB
/
2-finalizer.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
import fs from 'fs';
import readline from 'readline';
import './env/index.js';
import { deleteResource, doesPatientHaveResources } from './fhir/index.js';
import { deleteElasticPatient, deleteElasticRawResources, removeLingeringRawResources } from './elastic/index.js';
import { deleteClickhouseRawResources, deleteClickhouseAllPatients } from './clickhouse/index.js';
import { flushCursor, getCursor, writePatientId } from './filesystem/index.js';
async function main() {
const healthFacilityId = process.env.FACILITY_ID;
if (!healthFacilityId || healthFacilityId === 'placeholder') {
throw new Error('Failed to set the FACILITY_ID environment variable, got: ', healthFacilityId);
}
const start = new Date().getTime();
console.log(`${new Date().toISOString()} - starting processing`);
const cursor = await getCursor();
let previousCursorFound = cursor === '';
const patientIdReader = readline.createInterface({
input: fs.createReadStream(`${process.env.OUTPUT_PATH}/${process.env.PATIENT_ID_FILENAME}`)
});
let hasFailedPatients = false;
for await (const patientId of patientIdReader) {
if (!previousCursorFound) {
if (patientId === cursor) previousCursorFound = true;
else continue;
}
await flushCursor(patientId);
const unsafeToDeletePatient = await doesPatientHaveResources(patientId);
if (unsafeToDeletePatient) {
console.log(`${new Date().toISOString()} - patient: ${patientId} still has resources attached. Saving to ${process.env.FAILED_PATIENT_FILENAME}`);
await writePatientId(patientId, process.env.FAILED_PATIENT_FILENAME);
hasFailedPatients = true;
continue;
}
console.log(`${new Date().toISOString()} - checking for lingering fhir-raw resources: ${patientId}`);
await removeLingeringRawResources(patientId);
try {
console.log(`${new Date().toISOString()} - deleting patient: ${patientId}`);
await Promise.all([
deleteElasticPatient(patientId),
deleteResource(`Patient/${patientId}`)
]);
} catch (err) {
if (err.response && err.response.data) {
console.error(JSON.stringify(err.response.data));
}
throw err;
}
}
// Handle deletion from ClickHouse
try {
console.log(`${new Date().toISOString()} - Deleting all patients ClickHouse raw resources`);
await deleteClickhouseAllPatients(patientIdReader);
} catch (err) {
if (err.response && err.response.data) {
console.error(JSON.stringify(err.response.data));
}
throw err;
}
await flushCursor('');
console.log(`${new Date().toISOString()} - deleting organization: `, healthFacilityId);
if (hasFailedPatients) {
console.warn('Skipping orgainzation deleting as there is at least 1 failed patient');
} else {
await deleteResource(`Organization/${healthFacilityId}`);
await deleteElasticRawResources([`Organization/${healthFacilityId}`]);
await deleteClickhouseRawResources([`Organization/${healthFacilityId}`]);
}
console.log(`${new Date().toISOString()} - finished processing`);
const end = new Date().getTime();
const duration = (end - start) / 1000;
console.log(`${new Date().toISOString()} - finished processing in ${duration} seconds`);
}
main()
.catch(err => console.error(err))