@peopl-health/nexus 5.72.18 → 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.
|
@@ -52,11 +52,13 @@ function drugOf(symptom, item) {
|
|
|
52
52
|
|
|
53
53
|
const proposedDrugs = ({ proposal }) => (proposal?.items ?? []).map((item) => drugOf(proposal.symptom, item)).filter(Boolean);
|
|
54
54
|
|
|
55
|
-
function proposalLine({ proposal,
|
|
55
|
+
function proposalLine({ proposal, conduct, sections }) {
|
|
56
56
|
const drugs = proposedDrugs({ proposal });
|
|
57
57
|
if (!drugs.length) {
|
|
58
|
-
const
|
|
59
|
-
return
|
|
58
|
+
const modifiers = (Array.isArray(sections?.treatmentModifiers) ? sections.treatmentModifiers : []).map((key) => inlineOf(key)?.replace(/_/g, ' ')).filter(Boolean);
|
|
59
|
+
return modifiers.length
|
|
60
|
+
? `→ Sin propuesta de tratamiento: condición que cambia el algoritmo (${modifiers.join(', ')}).`
|
|
61
|
+
: '→ Sin propuesta de tratamiento.';
|
|
60
62
|
}
|
|
61
63
|
const declared = [...(proposal.conditions ?? []), ...(proposal.ruledOut ?? []).map((key) => `descartado ${key}`)]
|
|
62
64
|
.map((key) => inlineOf(key)?.replace(/_/g, ' ')).filter(Boolean);
|
|
@@ -70,7 +72,7 @@ function proposalLine({ proposal, withheldReason, conduct, sections }) {
|
|
|
70
72
|
const armedConduct = urgent.length ? inlineOf(conduct) : null;
|
|
71
73
|
return [
|
|
72
74
|
`${lead}: ${sentence(drugs.join('; '))}`,
|
|
73
|
-
armedConduct ? sentence(armedConduct) :
|
|
75
|
+
armedConduct ? sentence(armedConduct) : gate.length && 'Si algo de lo pendiente es positivo: valoración presencial.',
|
|
74
76
|
declared.length && `Verificar: ${sentence(declared.join(', '))}`,
|
|
75
77
|
].filter(Boolean).join(' ');
|
|
76
78
|
}
|
|
@@ -79,14 +81,11 @@ function symptomParagraph(entry, fallbackSymptom) {
|
|
|
79
81
|
const sections = entry.sections ?? {};
|
|
80
82
|
const grade = [1, 2, 3].includes(sections.grade) ? `G${sections.grade}` : 'grado no establecido';
|
|
81
83
|
const { present, absent, unassessed } = flagsByStatus(sections);
|
|
82
|
-
const isUndocumented = !present.length && !absent.length && !unassessed.length;
|
|
83
84
|
const summary = inlineOf(sections.characterizationSummary);
|
|
84
85
|
return [
|
|
85
86
|
`${inlineOf(sections.symptom) || fallbackSymptom || 'Síntoma no especificado'} (${grade}):`,
|
|
86
87
|
summary && sentence(summary),
|
|
87
88
|
present.length && `${present.length > 1 ? 'Banderas rojas presentes' : 'Bandera roja presente'}: ${sentence(present.join(', '))}`,
|
|
88
|
-
unassessed.length && `Falta confirmar: ${sentence(unassessed.join(', '))}`,
|
|
89
|
-
isUndocumented && 'Banderas rojas no documentadas.',
|
|
90
89
|
!present.length && !unassessed.length && absent.length && 'Sin banderas rojas.',
|
|
91
90
|
proposalLine(entry),
|
|
92
91
|
].filter(Boolean).join(' ');
|
|
@@ -23,31 +23,24 @@ const TECHNICAL_TOKEN = /\b(urgentIfPositive|blocksDrug|null)\b|\b[a-z]+_\w+|\\n
|
|
|
23
23
|
|
|
24
24
|
const trimmed = (value) => String(value ?? '').trim();
|
|
25
25
|
|
|
26
|
-
function
|
|
27
|
-
const
|
|
28
|
-
|
|
29
|
-
const reasons = [];
|
|
30
|
-
if (present.length) reasons.push(present.length === 1 ? 'hay una bandera roja presente' : 'hay banderas rojas presentes');
|
|
31
|
-
if (modifiers.length) reasons.push(`condición que cambia el algoritmo (${modifiers.join(', ')})`);
|
|
32
|
-
return reasons.join('; ') || null;
|
|
26
|
+
function withholds(sections) {
|
|
27
|
+
const modifiers = (Array.isArray(sections?.treatmentModifiers) ? sections.treatmentModifiers : []).map(trimmed).filter(Boolean);
|
|
28
|
+
return flagsByStatus(sections).present.length > 0 || modifiers.length > 0;
|
|
33
29
|
}
|
|
34
30
|
|
|
35
31
|
function settleProposal(parsed) {
|
|
36
|
-
const withheld = withholdReason(parsed);
|
|
37
32
|
const raw = parsed?.treatmentProposal;
|
|
38
33
|
const items = Array.isArray(raw?.items) ? raw.items : [];
|
|
39
34
|
const stated = trimmed(parsed?.proposalWithheldReason);
|
|
40
35
|
const conduct = stated && !drugsNamedIn(stated).length ? stated : null;
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
if (withheld) return { proposal: null, withheldReason: [withheld, callFirst].filter(Boolean).join('; '), conduct, strippedItems: items.length };
|
|
44
|
-
if (!items.length) return { proposal: null, withheldReason: callFirst, conduct, strippedItems: 0 };
|
|
36
|
+
if (withholds(parsed)) return { proposal: null, conduct, strippedItems: items.length };
|
|
37
|
+
if (!items.length) return { proposal: null, conduct, strippedItems: 0 };
|
|
45
38
|
|
|
46
39
|
const conditions = Array.isArray(raw.conditions) ? raw.conditions : [];
|
|
47
40
|
const ruledOut = Array.isArray(raw.ruledOut) ? raw.ruledOut : [];
|
|
48
41
|
const proposal = { symptom: raw.symptom, items, conditions, ruledOut };
|
|
49
42
|
const { valid, issues } = validateProposal(proposal, { conditions, ruledOut });
|
|
50
|
-
return valid ? { proposal,
|
|
43
|
+
return valid ? { proposal, conduct, strippedItems: 0 } : { invalid: issues };
|
|
51
44
|
}
|
|
52
45
|
|
|
53
46
|
const textList = (value) => (Array.isArray(value) ? value.map(trimmed).filter(Boolean) : []);
|
|
@@ -178,7 +171,7 @@ async function generateEscalationDraft({
|
|
|
178
171
|
|
|
179
172
|
const draft = {
|
|
180
173
|
shared: parsed,
|
|
181
|
-
symptoms: settled.map(({ block, proposal,
|
|
174
|
+
symptoms: settled.map(({ block, proposal, conduct }) => ({ sections: block, proposal, conduct })),
|
|
182
175
|
};
|
|
183
176
|
const doctorText = composeDoctorBody({ ...draft, alarm: alarms[alarms.length - 1] ?? null });
|
|
184
177
|
|
|
@@ -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
|
+
};
|