@peopl-health/nexus 5.59.0-dev.10730 → 5.59.0-dev.10732

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.
@@ -57,6 +57,7 @@ const doctorHandoffSchema = new mongoose.Schema({
57
57
  transport: { type: String, enum: [null, 'text', 'template'], default: null },
58
58
  messageId: { type: String, default: null },
59
59
  message: { type: String, default: null },
60
+ alarmCount: { type: Number, default: null },
60
61
  sentBy: { type: String, required: true },
61
62
  }, { timestamps: { createdAt: true, updatedAt: false } });
62
63
 
@@ -929,8 +929,8 @@ async function handOffToDoctor({ id, mode, physicianEmail = null, sentBy, reques
929
929
  if (approver) delivery = { physicianEmail: approver.email, ...(await sendDoctorAlert({ message, approver, operator })) };
930
930
  const sent = delivery.transport === 'template' ? Object.values(message.template).join('\n') : message.body;
931
931
  const handoff = {
932
- $push: { doctorHandoffs: { mode, draftNumber, ...delivery, message: sent, sentBy: operator } },
933
- $set: { patientCaseRecordIds },
932
+ $push: { doctorHandoffs: { mode, draftNumber, ...delivery, message: sent, alarmCount: (review.alarms ?? []).length, sentBy: operator } },
933
+ $addToSet: { patientCaseRecordIds: { $each: patientCaseRecordIds } },
934
934
  $unset: { identityBlock: '' },
935
935
  };
936
936
  const recordUpdate = withCaseMirror({ ...review, patientCaseRecordIds }, handoff, { status: 'en revision_medica' });
@@ -1076,22 +1076,25 @@ async function sendApprovedIndications({ id, outsideWindowRoute = null, sentBy,
1076
1076
  const review = await completeOperation(reviewId, claim, 'sending', recordUpdate, status);
1077
1077
  await audit(review, operator, requestId, { action: status, decisionNumber, transport: delivery.transport });
1078
1078
  const sent = await syncCase(review, operator, requestId);
1079
- if (review.status !== 'indications_sent' || decision === 'needs_intervention' || result.success !== true) return sent;
1080
- const decidedAt = new Date(review.decisions[decisionNumber - 1]?.createdAt ?? 0).getTime();
1081
- if ((review.alarms ?? []).some((alarm) => new Date(alarm.raisedAt).getTime() > decidedAt)) {
1082
- logger.warn('[EscalationReview] An alarm arrived after the doctor decided; the review stays open for it', {
1079
+ if (review.status !== 'indications_sent' || decision === 'needs_intervention') return sent;
1080
+ const decidedDraft = review.decisions[decisionNumber - 1]?.draftNumber;
1081
+ const handoff = (review.doctorHandoffs ?? []).findLast((entry) => entry.draftNumber === decidedDraft);
1082
+ if (handoff?.alarmCount == null || (review.alarms ?? []).length > handoff.alarmCount) {
1083
+ logger.warn('[EscalationReview] The doctor did not see every alarm on this review; it stays open for them', {
1083
1084
  escalationReviewId: String(review._id),
1084
1085
  });
1085
1086
  return sent;
1086
1087
  }
1087
1088
  try {
1088
1089
  const closed = await closeEscalationReview({ id: reviewId, closedBy: operator, requestId, expectedAlarmCount: (review.alarms ?? []).length });
1089
- return { ...closed, caseMirror: sent.caseMirror, caseMirrors: sent.caseMirrors };
1090
+ return closed;
1090
1091
  } catch (error) {
1091
- logger.error('[EscalationReview] The indications were delivered but the review did not close; close it by hand', {
1092
- escalationReviewId: String(review._id), error: redactedError(error),
1093
- });
1094
- return { ...sent, review: await loadReview(reviewId).catch(() => sent.review) };
1092
+ const stored = await loadReview(reviewId).catch(() => sent.review);
1093
+ let note = 'The indications were delivered but the review did not close; close it by hand';
1094
+ if (stored.status === 'closed') note = 'The review closed, but a step after closing failed';
1095
+ else if (error.statusCode === 409) note = 'An alarm joined while the review was closing; it stays open for it';
1096
+ logger.error(`[EscalationReview] ${note}`, { escalationReviewId: String(review._id), error: redactedError(error) });
1097
+ return { ...sent, review: stored };
1095
1098
  }
1096
1099
  }
1097
1100
 
@@ -1184,7 +1187,13 @@ async function closeEscalationReview({ id, closedBy, deliveryConfirmed = false,
1184
1187
  const closeChanges = { action: interrupted ? 'closed_from_interruption' : 'closed' };
1185
1188
  if (interrupted) closeChanges.fromStatus = current.status;
1186
1189
  if (queued) closeChanges.deliveryConfirmed = true;
1187
- await audit(review, operator, requestId, closeChanges);
1190
+ try {
1191
+ await audit(review, operator, requestId, closeChanges);
1192
+ } catch (error) {
1193
+ logger.error('[EscalationReview] The review closed but its audit entry was not written', {
1194
+ escalationReviewId: String(review._id), error: redactedError(error),
1195
+ });
1196
+ }
1188
1197
  await recordEpisode(review);
1189
1198
  return syncCase(await resumeAssistant(review, operator, requestId), operator, requestId);
1190
1199
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@peopl-health/nexus",
3
- "version": "5.59.0-dev.10730",
3
+ "version": "5.59.0-dev.10732",
4
4
  "description": "Core messaging and assistant library for WhatsApp communication platforms",
5
5
  "keywords": [
6
6
  "whatsapp",