@peopl-health/nexus 5.76.1 → 5.77.0-dev.15992
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -55,6 +55,16 @@ const tableCountsSchema = new mongoose.Schema({
|
|
|
55
55
|
failed: { type: Number, default: 0 },
|
|
56
56
|
}, { _id: false });
|
|
57
57
|
|
|
58
|
+
// One eligible group as the plan saw it, so a later delete can name exactly the groups that were reviewed.
|
|
59
|
+
const plannedGroupSchema = new mongoose.Schema({
|
|
60
|
+
groupId: { type: String, required: true },
|
|
61
|
+
// Every patient of the group's records: a shared questionnaire can join two patients.
|
|
62
|
+
patientRecordIds: { type: [String], default: [] },
|
|
63
|
+
// None of them has a patient-linked questionnaire or triage since the cutoff.
|
|
64
|
+
inactive: { type: Boolean, required: true },
|
|
65
|
+
records: { type: Map, of: Number, default: {} },
|
|
66
|
+
}, { _id: false });
|
|
67
|
+
|
|
58
68
|
const airtableArchiveRunSchema = new mongoose.Schema({
|
|
59
69
|
runId: { type: String, required: true },
|
|
60
70
|
baseId: { type: String, required: true },
|
|
@@ -69,6 +79,7 @@ const airtableArchiveRunSchema = new mongoose.Schema({
|
|
|
69
79
|
kept: { type: Map, of: Number, default: {} },
|
|
70
80
|
},
|
|
71
81
|
tables: { type: Map, of: tableCountsSchema, default: {} },
|
|
82
|
+
eligibleGroups: { type: [plannedGroupSchema], default: [] },
|
|
72
83
|
errorCode: { type: String, default: null },
|
|
73
84
|
}, { collection: 'airtable_archive_runs', minimize: false, timestamps: true });
|
|
74
85
|
|
|
@@ -297,6 +297,14 @@ async function copyBatch({ baseId, runId, batch, schema, schemaId, counts, now,
|
|
|
297
297
|
}
|
|
298
298
|
}
|
|
299
299
|
|
|
300
|
+
function plannedGroup(group, patientsWithRecent) {
|
|
301
|
+
const patientRecordIds = [...new Set(group.records.map(({ patient }) => patient?.recordId).filter(Boolean))];
|
|
302
|
+
const records = {};
|
|
303
|
+
group.records.forEach(({ table }) => { records[table] = (records[table] || 0) + 1; });
|
|
304
|
+
const isInactive = (patientId) => Object.values(patientsWithRecent).every((patients) => !patients.has(patientId));
|
|
305
|
+
return { groupId: group.groupId, patientRecordIds, inactive: patientRecordIds.length > 0 && patientRecordIds.every(isInactive), records };
|
|
306
|
+
}
|
|
307
|
+
|
|
300
308
|
// Batches never split a group, so a changed record holds back its whole group.
|
|
301
309
|
function groupBatches(groups) {
|
|
302
310
|
const batches = [];
|
|
@@ -348,13 +356,17 @@ async function runArchive({ baseId, cutoff, mode = 'plan', now = new Date() }) {
|
|
|
348
356
|
}
|
|
349
357
|
}
|
|
350
358
|
|
|
359
|
+
// Only a plan stores its groups: a copy run's list would include groups it held back, so slices are picked from plans.
|
|
360
|
+
const eligibleGroups = mode === 'plan' ? plan.eligible.map((group) => plannedGroup(group, patientsWithRecent)) : [];
|
|
351
361
|
const failed = Object.values(counts).reduce((sum, c) => sum + c.failed, 0);
|
|
352
362
|
const summary = {
|
|
353
363
|
runId, mode, cutoff, status: failed ? 'failed' : 'succeeded',
|
|
354
364
|
components: { eligible: plan.eligible.length, kept: plan.keptByReason }, tables: counts,
|
|
355
365
|
};
|
|
356
366
|
const finished = await getAirtableArchiveRun().updateOne({ runId, status: 'running' }, {
|
|
357
|
-
$set: {
|
|
367
|
+
$set: {
|
|
368
|
+
status: summary.status, finishedAt: new Date(), components: summary.components, tables: counts, eligibleGroups, errorCode: failed ? 'records_failed' : null,
|
|
369
|
+
},
|
|
358
370
|
});
|
|
359
371
|
if (!finished?.matchedCount) throw archiveError('archive_lease_lost');
|
|
360
372
|
logger.info('[airtableArchive] Run finished', summary);
|