@peopl-health/nexus 5.78.0-dev.16570 → 5.78.0-dev.16617

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,6 +28,9 @@ const withinRange = (row, fromDate, toDate) => {
28
28
 
29
29
  const linkedIdsOf = (value) => (Array.isArray(value) ? value.filter(Boolean) : []);
30
30
 
31
+ // The symptom's own link to its triage: Airtable unlinks a deleted symptom from the triage but its copy keeps this side.
32
+ const SYMPTOM_TRIAGE_FIELD = 'triaje_id';
33
+
31
34
  // Rows deleted from Airtable exist only in the archive; if it cannot be read, the page shows live rows only.
32
35
  const readArchived = async (read, params) => {
33
36
  try {
@@ -39,12 +42,24 @@ const readArchived = async (read, params) => {
39
42
  };
40
43
 
41
44
  const readPatientHistory = async (code) => {
42
- const [live, archived] = await Promise.all([
45
+ const [live, archived, archivedSymptoms] = await Promise.all([
43
46
  getRecordByFilter(Symptoms_ID, 'triaje_symptoms', fieldEquals('whatsapp_id', code), 'Grid view', TRIAGE_FIELDS, true),
44
47
  readArchived(findArchivedByPatient, { tableName: 'triaje_symptoms', whatsappId: code, fields: TRIAGE_FIELDS }),
48
+ readArchived(findArchivedByPatient, { tableName: 'symptoms', whatsappId: code, fields: [...SYMPTOM_FIELDS, SYMPTOM_TRIAGE_FIELD] }),
45
49
  ]);
46
50
  const ownRows = archived.filter((row) => firstOf(row.whatsapp_id) === code);
47
- return { rows: mergeArchivedRows(live || [], ownRows), symptomsById: new Map() };
51
+ const archivedSymptomIdsByTriage = new Map();
52
+ [...archivedSymptoms].sort((a, b) => new Date(a._createdTime) - new Date(b._createdTime)).forEach((row) => {
53
+ linkedIdsOf(row[SYMPTOM_TRIAGE_FIELD]).forEach((triageId) => {
54
+ archivedSymptomIdsByTriage.set(triageId, [...(archivedSymptomIdsByTriage.get(triageId) || []), row._recordId]);
55
+ });
56
+ });
57
+ return {
58
+ rows: mergeArchivedRows(live || [], ownRows),
59
+ symptomsById: new Map(),
60
+ archivedSymptomsById: new Map(archivedSymptoms.map((row) => [row._recordId, row])),
61
+ archivedSymptomIdsByTriage,
62
+ };
48
63
  };
49
64
 
50
65
  const getPatientHistory = (code) => {
@@ -67,7 +82,8 @@ const getPatientHistory = (code) => {
67
82
 
68
83
  // The `… (from symptoms)` lookups are not used: Airtable omits empty values, shifting every later grade onto the wrong symptom.
69
84
  const getTriageHistory = async ({ code, startDate, endDate, limit, page } = {}) => {
70
- const { rows, symptomsById } = await getPatientHistory(code);
85
+ const { rows, symptomsById, archivedSymptomsById, archivedSymptomIdsByTriage } = await getPatientHistory(code);
86
+ const symptomIdsOf = (row) => [...new Set([...linkedIdsOf(row.symptoms), ...(archivedSymptomIdsByTriage.get(row._recordId || row.recordID) || [])])];
71
87
 
72
88
  const fromDate = startDate ? new Date(`${startDate}T00:00:00.000Z`) : null;
73
89
  const toDate = endDate ? new Date(`${endDate}T23:59:59.999Z`) : null;
@@ -76,23 +92,25 @@ const getTriageHistory = async ({ code, startDate, endDate, limit, page } = {})
76
92
  const sorted = [...filtered].sort((a, b) => new Date(b?.Created || 0) - new Date(a?.Created || 0));
77
93
  const { records: pageRows, pagination } = paginate(sorted, { limit, page, defaultLimit: DEFAULT_LIMIT });
78
94
 
79
- const symptomIds = [...new Set(pageRows.flatMap((row) => linkedIdsOf(row.symptoms)))];
80
- const missingIds = symptomIds.filter((id) => !symptomsById.has(id));
81
- if (missingIds.length) {
95
+ // Linked symptoms are read live first, so a live symptom always wins over an archived copy of it.
96
+ const linkedMissing = [...new Set(pageRows.flatMap((row) => linkedIdsOf(row.symptoms)))].filter((id) => !symptomsById.has(id));
97
+ if (linkedMissing.length) {
82
98
  const symptomRows = await getRecordByFilter(
83
- Symptoms_ID, 'symptoms', recordIdIn(missingIds), null, SYMPTOM_FIELDS, true,
99
+ Symptoms_ID, 'symptoms', recordIdIn(linkedMissing), null, SYMPTOM_FIELDS, true,
84
100
  ) || [];
85
101
  for (const symptomRow of symptomRows) symptomsById.set(symptomRow._recordId, symptomRow);
86
- const archivedIds = missingIds.filter((id) => !symptomsById.has(id));
87
- if (archivedIds.length) {
88
- const archivedRows = await readArchived(findArchivedByRecordIds, { tableName: 'symptoms', recordIds: archivedIds, fields: SYMPTOM_FIELDS });
89
- for (const symptomRow of archivedRows) symptomsById.set(symptomRow._recordId, symptomRow);
90
- }
102
+ }
103
+ const symptomIds = [...new Set(pageRows.flatMap(symptomIdsOf))];
104
+ for (const id of symptomIds) if (!symptomsById.has(id) && archivedSymptomsById.has(id)) symptomsById.set(id, archivedSymptomsById.get(id));
105
+ const archivedIds = symptomIds.filter((id) => !symptomsById.has(id));
106
+ if (archivedIds.length) {
107
+ const archivedRows = await readArchived(findArchivedByRecordIds, { tableName: 'symptoms', recordIds: archivedIds, fields: SYMPTOM_FIELDS });
108
+ for (const symptomRow of archivedRows) symptomsById.set(symptomRow._recordId, symptomRow);
91
109
  }
92
110
 
93
111
  const records = pageRows.map((row) => buildTriageRecord(
94
112
  row,
95
- linkedIdsOf(row.symptoms).map((id) => symptomsById.get(id)).filter(Boolean),
113
+ symptomIdsOf(row).map((id) => symptomsById.get(id)).filter(Boolean),
96
114
  ));
97
115
 
98
116
  return { records, pagination };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@peopl-health/nexus",
3
- "version": "5.78.0-dev.16570",
3
+ "version": "5.78.0-dev.16617",
4
4
  "description": "Core messaging and assistant library for WhatsApp communication platforms",
5
5
  "keywords": [
6
6
  "whatsapp",