@peopl-health/nexus 6.4.12 → 6.4.14

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.
@@ -1410,13 +1410,21 @@ async function alertWithoutEmergencyRow({ reviewId, alarmId = null, urgencyLevel
1410
1410
  }
1411
1411
  }
1412
1412
 
1413
- async function reopenFromFollowUp(review, reason) {
1413
+ async function reopenFromFollowUp(review, reason, { background = false } = {}) {
1414
1414
  const reopened = await getEscalationReview()
1415
1415
  .findOneAndUpdate({ _id: review._id, status: review.status }, { $set: { status: 'documenting', updatedSinceSeen: true } }, { new: true })
1416
1416
  .lean();
1417
1417
  if (!reopened) return review;
1418
1418
  await audit(reopened, AGENT_OPERATOR, null, { action: 'followup_result', result: reason, statusFrom: review.status });
1419
1419
  await cancelCaseFollowUp(reopened, 'case_reopened');
1420
+ // A new alarm already reaches the group through its emergency row.
1421
+ if (reason === 'worsening') {
1422
+ const notice = noticeCaseThread(reopened, {
1423
+ header: '📈 La paciente va peor después de la decisión del médico', kind: 'worsening',
1424
+ action: 'Qué hacer: llamar a la paciente y, si hace falta, pedirle al médico una nueva indicación',
1425
+ });
1426
+ if (!background) await notice;
1427
+ }
1420
1428
  return reopened;
1421
1429
  }
1422
1430
 
@@ -1474,7 +1482,7 @@ async function settleFollowUp(snapshot, { background = true } = {}) {
1474
1482
  .filter((impression) => known.has(impression.caseId) && readingOf(impression))
1475
1483
  .forEach((impression) => latest.set(impression.caseId, readingOf(impression))));
1476
1484
 
1477
- if ([...latest.values()].includes('worsening')) return reopenFromFollowUp(review, 'worsening');
1485
+ if ([...latest.values()].includes('worsening')) return reopenFromFollowUp(review, 'worsening', { background });
1478
1486
  const settled = [...known].every((caseId) => SETTLED_TRAJECTORIES.includes(latest.get(caseId)));
1479
1487
  if (!settled || !SETTLEABLE_STATUSES.includes(review.status)) {
1480
1488
  await getEscalationReview().updateOne({ _id: review._id }, { $set: { updatedSinceSeen: true } });
@@ -2186,20 +2194,17 @@ function replyOutcome(delivery, sendThrew) {
2186
2194
  return 'unknown';
2187
2195
  }
2188
2196
 
2189
- // A notice that fails is logged and never undoes the send.
2190
- async function notifyPlanIssues(review, issues) {
2197
+ // A notice that fails is logged and never undoes what it reports.
2198
+ async function noticeCaseThread(review, { header, action, kind }) {
2191
2199
  try {
2192
2200
  const { patient, doctor, link } = await teamAlertIdentity(review);
2193
- const header = '⚠️ Revisar el envío del plan';
2194
- const lines = issues.map((issue) => `- ${issue}`);
2195
- const action = 'Qué hacer: revisar el chat; corregir con la paciente o volver a mandar la receta si hace falta';
2196
- // Every alarm, not only those since the decision: the plan is always sent after it, so the case's alarm came before.
2201
+ // Every alarm, not only those since the decision: the case's alarm always came before it.
2197
2202
  const recordId = [...(review.alarms ?? [])].reverse().find((alarm) => alarm.emergencyRecordId)?.emergencyRecordId;
2198
2203
  const quoted = await readGroupMessageId(recordId, review.patientCode);
2199
- const sent = await sendGroupNotice([header, `${patient} (${doctor})`, ...lines, action, link].join('\n'), quoted, [header, ...lines, action].join('\n'));
2200
- if (!sent.delivered) logger.error('[EscalationReview] The plan check notice did not reach the group', { escalationReviewId: String(review._id), error: sent.error });
2204
+ const sent = await sendGroupNotice([header, `${patient} (${doctor})`, action, link].join('\n'), quoted, [header, action].join('\n'));
2205
+ if (!sent.delivered) logger.error(`[EscalationReview] The ${kind} notice did not reach the group`, { escalationReviewId: String(review._id), error: sent.error });
2201
2206
  } catch (error) {
2202
- logger.error('[EscalationReview] The plan check notice failed', { escalationReviewId: String(review._id), error: redactedError(error) });
2207
+ logger.error(`[EscalationReview] The ${kind} notice failed`, { escalationReviewId: String(review._id), error: redactedError(error) });
2203
2208
  }
2204
2209
  }
2205
2210
 
@@ -2240,7 +2245,6 @@ async function sendPlanThroughAssistant({ claimed, claim, decisionNumber, plan,
2240
2245
  }
2241
2246
  if (outcome === 'unknown') {
2242
2247
  logger.error('[EscalationReview] The assistant reply could not be confirmed; leaving the review in sending', { escalationReviewId: String(reviewId) });
2243
- await notifyPlanIssues(claimed, ['no se pudo confirmar que el mensaje salió; el caso sigue en «enviando»']);
2244
2248
  throw httpError(502, 'The assistant reply could not be confirmed; check the chat before sending again', SEND_UNCONFIRMED);
2245
2249
  }
2246
2250
 
@@ -2260,10 +2264,14 @@ async function sendPlanThroughAssistant({ claimed, claim, decisionNumber, plan,
2260
2264
  await audit(review, operator, requestId, {
2261
2265
  action: status, decisionNumber, transport: 'assistant', checks: issues.length, ...(prescriberEmail && { prescription: prescriptionFailed ? 'failed' : 'created' }),
2262
2266
  });
2263
- if (issues.length) await notifyPlanIssues(review, issues);
2267
+ if (prescriptionFailed) {
2268
+ await noticeCaseThread(review, {
2269
+ header: '⚠️ No se pudo crear la receta', kind: 'prescription',
2270
+ action: 'Qué hacer: revisar el chat y volver a mandar la receta',
2271
+ });
2272
+ }
2264
2273
  if (!queued) await armCaseFollowUp(review);
2265
- // Closing waits for a sent reply that kept to the plan and the receta it promised; otherwise the team still has something to do.
2266
- if (plan.followUp.kind === 'none' && !queued && !issues.length) {
2274
+ if (plan.followUp.kind === 'none' && !queued && !prescriptionFailed) {
2267
2275
  try {
2268
2276
  return await closeEscalationReview({ id: reviewId, closedBy: operator, requestId });
2269
2277
  } catch (error) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@peopl-health/nexus",
3
- "version": "6.4.12",
3
+ "version": "6.4.14",
4
4
  "description": "Core messaging and assistant library for WhatsApp communication platforms",
5
5
  "keywords": [
6
6
  "whatsapp",