@peopl-health/nexus 5.72.19 → 5.73.0-dev.15248
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.
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
const mongoose = require('mongoose');
|
|
2
|
+
|
|
3
|
+
const { getDb, getModelDatabase } = require('../config/mongoConfig');
|
|
4
|
+
|
|
5
|
+
const ARCHIVE_SCHEMA_VERSION = 1;
|
|
6
|
+
const ARCHIVE_RUN_MODES = ['plan', 'copy'];
|
|
7
|
+
const ARCHIVE_RUN_STATUSES = ['running', 'succeeded', 'failed'];
|
|
8
|
+
|
|
9
|
+
const archivedAttachmentSchema = new mongoose.Schema({
|
|
10
|
+
field: { type: String, required: true },
|
|
11
|
+
id: { type: String, required: true },
|
|
12
|
+
filename: { type: String, default: null },
|
|
13
|
+
type: { type: String, default: null },
|
|
14
|
+
size: { type: Number, default: null },
|
|
15
|
+
s3Key: { type: String, required: true },
|
|
16
|
+
}, { _id: false });
|
|
17
|
+
|
|
18
|
+
const airtableArchiveRecordSchema = new mongoose.Schema({
|
|
19
|
+
_id: { type: String, required: true },
|
|
20
|
+
baseId: { type: String, required: true },
|
|
21
|
+
tableName: { type: String, required: true },
|
|
22
|
+
recordId: { type: String, required: true },
|
|
23
|
+
createdTime: { type: Date, required: true },
|
|
24
|
+
fields: { type: mongoose.Schema.Types.Mixed, required: true },
|
|
25
|
+
fieldsHash: { type: String, required: true },
|
|
26
|
+
// Hash of the editable fields only: formulas and lookups change between runs without any source edit.
|
|
27
|
+
sourceHash: { type: String, required: true },
|
|
28
|
+
patient: {
|
|
29
|
+
recordId: { type: String, default: null },
|
|
30
|
+
whatsappId: { type: String, default: null },
|
|
31
|
+
},
|
|
32
|
+
groupId: { type: String, required: true },
|
|
33
|
+
attachments: { type: [archivedAttachmentSchema], default: [] },
|
|
34
|
+
runId: { type: String, required: true },
|
|
35
|
+
archivedAt: { type: Date, required: true },
|
|
36
|
+
sourceDeletedAt: { type: Date, default: null },
|
|
37
|
+
schemaVersion: { type: Number, required: true, default: ARCHIVE_SCHEMA_VERSION },
|
|
38
|
+
}, { collection: 'airtable_archive_records', minimize: false, versionKey: false });
|
|
39
|
+
|
|
40
|
+
airtableArchiveRecordSchema.index({ baseId: 1, tableName: 1, 'patient.whatsappId': 1, createdTime: -1 });
|
|
41
|
+
airtableArchiveRecordSchema.index({ baseId: 1, tableName: 1, 'patient.recordId': 1, createdTime: -1 });
|
|
42
|
+
airtableArchiveRecordSchema.index({ baseId: 1, groupId: 1 });
|
|
43
|
+
airtableArchiveRecordSchema.index({ runId: 1 });
|
|
44
|
+
|
|
45
|
+
const tableCountsSchema = new mongoose.Schema({
|
|
46
|
+
scanned: { type: Number, default: 0 },
|
|
47
|
+
eligible: { type: Number, default: 0 },
|
|
48
|
+
copied: { type: Number, default: 0 },
|
|
49
|
+
refreshed: { type: Number, default: 0 },
|
|
50
|
+
verified: { type: Number, default: 0 },
|
|
51
|
+
failed: { type: Number, default: 0 },
|
|
52
|
+
}, { _id: false });
|
|
53
|
+
|
|
54
|
+
const airtableArchiveRunSchema = new mongoose.Schema({
|
|
55
|
+
runId: { type: String, required: true },
|
|
56
|
+
baseId: { type: String, required: true },
|
|
57
|
+
mode: { type: String, enum: ARCHIVE_RUN_MODES, required: true },
|
|
58
|
+
cutoff: { type: Date, required: true },
|
|
59
|
+
status: { type: String, enum: ARCHIVE_RUN_STATUSES, required: true, default: 'running' },
|
|
60
|
+
startedAt: { type: Date, required: true },
|
|
61
|
+
finishedAt: { type: Date, default: null },
|
|
62
|
+
leaseExpiresAt: { type: Date, required: true },
|
|
63
|
+
components: {
|
|
64
|
+
eligible: { type: Number, default: 0 },
|
|
65
|
+
kept: { type: Map, of: Number, default: {} },
|
|
66
|
+
},
|
|
67
|
+
tables: { type: Map, of: tableCountsSchema, default: {} },
|
|
68
|
+
errorCode: { type: String, default: null },
|
|
69
|
+
}, { collection: 'airtable_archive_runs', minimize: false, timestamps: true });
|
|
70
|
+
|
|
71
|
+
airtableArchiveRunSchema.index({ runId: 1 }, { unique: true });
|
|
72
|
+
// At most one running run per base: a second run fails to insert instead of racing the first.
|
|
73
|
+
airtableArchiveRunSchema.index({ baseId: 1 }, { unique: true, partialFilterExpression: { status: 'running' } });
|
|
74
|
+
|
|
75
|
+
const getDatabase = (modelName) => {
|
|
76
|
+
const dbName = getModelDatabase(modelName);
|
|
77
|
+
return dbName ? getDb(dbName) : mongoose;
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
const getAirtableArchiveRecord = () => {
|
|
81
|
+
const db = getDatabase('AirtableArchiveRecord');
|
|
82
|
+
return db.models.AirtableArchiveRecord || db.model('AirtableArchiveRecord', airtableArchiveRecordSchema);
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
const getAirtableArchiveRun = () => {
|
|
86
|
+
const db = getDatabase('AirtableArchiveRun');
|
|
87
|
+
return db.models.AirtableArchiveRun || db.model('AirtableArchiveRun', airtableArchiveRunSchema);
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
const archiveRecordId = (baseId, recordId) => `${baseId}:${recordId}`;
|
|
91
|
+
|
|
92
|
+
module.exports = {
|
|
93
|
+
ARCHIVE_SCHEMA_VERSION,
|
|
94
|
+
ARCHIVE_RUN_MODES,
|
|
95
|
+
airtableArchiveRecordSchema,
|
|
96
|
+
getAirtableArchiveRecord,
|
|
97
|
+
getAirtableArchiveRun,
|
|
98
|
+
archiveRecordId,
|
|
99
|
+
};
|