@peopl-health/nexus 5.75.7 → 5.76.0-dev.15838
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.
|
@@ -28,7 +28,13 @@ const hashFields = (fields, skip = new Set()) => crypto.createHash('sha256')
|
|
|
28
28
|
const schemaSnapshotId = (baseId, snapshot) => `${baseId}:${crypto.createHash('sha256').update(stableStringify(snapshot)).digest('hex').slice(0, 24)}`;
|
|
29
29
|
|
|
30
30
|
// Same shape as airtableService.getRecordByFilter(..., withRecordId = true), flagged as archived.
|
|
31
|
-
const toAirtableRow = (doc) => ({
|
|
31
|
+
const toAirtableRow = (doc) => ({
|
|
32
|
+
...doc.fields,
|
|
33
|
+
_recordId: doc.recordId,
|
|
34
|
+
_createdTime: new Date(doc.createdTime).toISOString(),
|
|
35
|
+
_archived: true,
|
|
36
|
+
...(doc.attachments ? { _attachments: doc.attachments } : {}),
|
|
37
|
+
});
|
|
32
38
|
|
|
33
39
|
// Union by record id, live row winning; archived rows are appended, so callers sort after merging.
|
|
34
40
|
function mergeArchivedRows(liveRows = [], archivedRows = []) {
|
|
@@ -42,6 +42,7 @@ const airtableArchiveRecordSchema = new mongoose.Schema({
|
|
|
42
42
|
|
|
43
43
|
airtableArchiveRecordSchema.index({ baseId: 1, tableName: 1, 'patient.whatsappId': 1, createdTime: -1 });
|
|
44
44
|
airtableArchiveRecordSchema.index({ baseId: 1, tableName: 1, 'patient.recordId': 1, createdTime: -1 });
|
|
45
|
+
airtableArchiveRecordSchema.index({ baseId: 1, tableName: 1, createdTime: -1 });
|
|
45
46
|
airtableArchiveRecordSchema.index({ baseId: 1, groupId: 1 });
|
|
46
47
|
airtableArchiveRecordSchema.index({ runId: 1 });
|
|
47
48
|
|
|
@@ -1,25 +1,50 @@
|
|
|
1
|
+
const { getModelDatabase } = require('../config/mongoConfig');
|
|
1
2
|
const { getAirtableArchiveRecord, archiveRecordId } = require('../models/airtableArchiveModel');
|
|
2
3
|
const { toAirtableRow } = require('../helpers/airtableArchiveHelper');
|
|
3
4
|
|
|
4
|
-
|
|
5
|
+
// Only rows already deleted from Airtable: a copy whose source is still live must never reach a reader.
|
|
6
|
+
const DELETED_FROM_SOURCE = { sourceDeletedAt: { $ne: null } };
|
|
5
7
|
|
|
6
|
-
|
|
8
|
+
// Unmapped, the model would fall back to the default database and every read would come back empty.
|
|
9
|
+
function archiveRecords() {
|
|
10
|
+
if (!getModelDatabase('AirtableArchiveRecord')) throw new Error('AirtableArchiveRecord db not mapped (setModelDatabases)');
|
|
11
|
+
return getAirtableArchiveRecord();
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function readProjection({ fields, withAttachments = false } = {}) {
|
|
15
|
+
const projection = { recordId: 1, createdTime: 1 };
|
|
16
|
+
if (fields?.length) fields.forEach((name) => { projection[`fields.${name}`] = 1; });
|
|
17
|
+
else projection.fields = 1;
|
|
18
|
+
if (withAttachments) projection.attachments = 1;
|
|
19
|
+
return projection;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
async function findArchivedByPatient({ baseId, tableName, whatsappId, patientRecordId, since, fields, withAttachments }) {
|
|
7
23
|
if (!whatsappId && !patientRecordId) return [];
|
|
8
|
-
const query = { baseId, tableName };
|
|
24
|
+
const query = { baseId, tableName, ...DELETED_FROM_SOURCE };
|
|
9
25
|
if (patientRecordId) query['patient.recordId'] = patientRecordId;
|
|
10
26
|
else query['patient.whatsappId'] = whatsappId;
|
|
11
27
|
if (since) query.createdTime = { $gte: since };
|
|
12
|
-
const docs = await
|
|
28
|
+
const docs = await archiveRecords().find(query, readProjection({ fields, withAttachments })).sort({ createdTime: -1 }).lean();
|
|
13
29
|
return docs.map(toAirtableRow);
|
|
14
30
|
}
|
|
15
31
|
|
|
16
|
-
async function findArchivedByRecordIds({ baseId, tableName, recordIds }) {
|
|
32
|
+
async function findArchivedByRecordIds({ baseId, tableName, recordIds, fields, withAttachments }) {
|
|
17
33
|
if (!recordIds?.length) return [];
|
|
18
|
-
const
|
|
34
|
+
const query = { _id: { $in: recordIds.map((id) => archiveRecordId(baseId, id)) }, tableName, ...DELETED_FROM_SOURCE };
|
|
35
|
+
const docs = await archiveRecords().find(query, readProjection({ fields, withAttachments })).lean();
|
|
36
|
+
return docs.map(toAirtableRow);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
async function findArchivedByTable({ baseId, tableName, since, fields }) {
|
|
40
|
+
const query = { baseId, tableName, ...DELETED_FROM_SOURCE };
|
|
41
|
+
if (since) query.createdTime = { $gte: since };
|
|
42
|
+
const docs = await archiveRecords().find(query, readProjection({ fields })).sort({ createdTime: -1 }).lean();
|
|
19
43
|
return docs.map(toAirtableRow);
|
|
20
44
|
}
|
|
21
45
|
|
|
22
46
|
module.exports = {
|
|
23
47
|
findArchivedByPatient,
|
|
24
48
|
findArchivedByRecordIds,
|
|
49
|
+
findArchivedByTable,
|
|
25
50
|
};
|