@peopl-health/nexus 5.75.7 → 5.75.8

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.
@@ -171,13 +171,20 @@ function planArchive({ recordsByTable, patientsWithRecent = {}, schema }) {
171
171
  continue;
172
172
  }
173
173
  const patientEntry = records.map(({ id }) => entries.get(id)).find(patientOf);
174
+ const patient = {
175
+ recordId: patientEntry ? patientOf(patientEntry) : null,
176
+ whatsappId: firstString(patientEntry?.record.fields?.[schema.whatsappFields[patientEntry.table]]),
177
+ };
178
+ // A group can join two patients' records through a shared questionnaire; each record keeps its own patient.
179
+ const patientFor = (entry) => {
180
+ const own = patientOf(entry);
181
+ if (!own || own === patient.recordId) return patient;
182
+ return { recordId: own, whatsappId: firstString(entry.record.fields?.[schema.whatsappFields[entry.table]]) };
183
+ };
174
184
  eligible.push({
175
185
  groupId: groupIdOf(records.map(({ id }) => id)),
176
- patient: {
177
- recordId: patientEntry ? patientOf(patientEntry) : null,
178
- whatsappId: firstString(patientEntry?.record.fields?.[schema.whatsappFields[patientEntry.table]]),
179
- },
180
- records,
186
+ patient,
187
+ records: records.map((record) => ({ ...record, patient: patientFor(entries.get(record.id)) })),
181
188
  });
182
189
  }
183
190
 
@@ -237,7 +237,7 @@ async function copyBatch({ baseId, runId, batch, schema, schemaId, counts, now,
237
237
 
238
238
  const expected = new Map();
239
239
  const ops = [];
240
- for (const { table, record: scanned, group } of batch) {
240
+ for (const { table, record: scanned, group, patient } of batch) {
241
241
  if (changedGroups.has(group.groupId)) {
242
242
  counts[table].failed += 1;
243
243
  logger.error('[airtableArchive] Group changed or gone since the scan', { runId, table, recordId: scanned.id, groupId: group.groupId });
@@ -249,7 +249,7 @@ async function copyBatch({ baseId, runId, batch, schema, schemaId, counts, now,
249
249
  const fieldsHash = hashFields(normalized);
250
250
  const sourceHash = hashFields(normalized, schema.computed.get(table));
251
251
  const tableId = schema.tableIds[table];
252
- const placement = placementOf(table, group.groupId, group.patient, record.createdTime);
252
+ const placement = placementOf(table, group.groupId, patient, record.createdTime);
253
253
  const previous = existing.get(_id);
254
254
  const previousIntact = previous?.tableId && previous.schemaId && hashFields(previous.fields) === previous.fieldsHash;
255
255
  if (previousIntact && previous.sourceHash === sourceHash && placementOf(previous.tableName, previous.groupId, previous.patient, previous.createdTime) === placement) {
@@ -265,7 +265,7 @@ async function copyBatch({ baseId, runId, batch, schema, schemaId, counts, now,
265
265
  update: {
266
266
  $set: {
267
267
  baseId, tableId, tableName: table, recordId: record.id, createdTime: new Date(record.createdTime),
268
- fields: normalized, fieldsHash, sourceHash, patient: group.patient, groupId: group.groupId, schemaId, attachments: hosted,
268
+ fields: normalized, fieldsHash, sourceHash, patient, groupId: group.groupId, schemaId, attachments: hosted,
269
269
  schemaVersion: ARCHIVE_SCHEMA_VERSION,
270
270
  },
271
271
  $setOnInsert: { runId, archivedAt: now, sourceDeletedAt: null },
@@ -337,7 +337,7 @@ async function runArchive({ baseId, cutoff, mode = 'plan', now = new Date() }) {
337
337
 
338
338
  Object.entries(recordsByTable).forEach(([table, records]) => { counts[table].scanned = records.length; });
339
339
  const recordsById = new Map(Object.values(recordsByTable).flat().map((record) => [record.id, record]));
340
- const work = plan.eligible.map((group) => group.records.map(({ table, id }) => ({ table, record: recordsById.get(id), group })));
340
+ const work = plan.eligible.map((group) => group.records.map(({ table, id, patient }) => ({ table, record: recordsById.get(id), group, patient })));
341
341
  work.flat().forEach(({ table }) => { counts[table].eligible += 1; });
342
342
 
343
343
  if (mode === 'copy' && work.length) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@peopl-health/nexus",
3
- "version": "5.75.7",
3
+ "version": "5.75.8",
4
4
  "description": "Core messaging and assistant library for WhatsApp communication platforms",
5
5
  "keywords": [
6
6
  "whatsapp",