@peopl-health/nexus 6.4.0-dev.18830 → 6.4.0-dev.18831

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.
@@ -31,7 +31,7 @@ const respondWithError = (res, error, action, context) => {
31
31
  stack: redactDirectIdentifiers(String(error.stack || '')),
32
32
  ...context,
33
33
  });
34
- res.status(error.statusCode || 500).json({ success: false, error: error.message });
34
+ res.status(error.statusCode || 500).json({ success: false, error: error.message, ...(error.errorCode && { errorCode: error.errorCode }) });
35
35
  };
36
36
 
37
37
  const withCaseStatus = (review) => (review ? { ...review, caseStatus: CASE_STATUS_BY_REVIEW_STATUS[review.status] ?? null } : review);
@@ -108,6 +108,10 @@ const SAME_URGENCY = 'The review is already at that urgency';
108
108
  const RECLASSIFY_CHANGED = 'The review changed while you were reclassifying it; read it again before changing the urgency';
109
109
  const DIRECT_CHAT_STATUSES = ['open', 'documenting', 'draft_ready', 'returned', 'awaiting_doctor', 'approved', 'needs_intervention'];
110
110
  const APPROVALS = ['approved', 'edited_approved', 'plan_approved'];
111
+ // The interface offers a resend only for SEND_NOT_SENT; SEND_UNCONFIRMED may have reached the patient.
112
+ const SEND_NOT_SENT = 'send_not_sent';
113
+ const SEND_UNCONFIRMED = 'send_unconfirmed';
114
+ const withSendCode = (error, errorCode) => (error instanceof Error && !Object.isFrozen(error) && !error.errorCode ? Object.assign(error, { errorCode }) : error);
111
115
  const STALE_DRAFT = 'New information reached the review after this draft; wait for the updated draft before approving';
112
116
  const STALE_HANDOFF = 'New information reached the review after the draft the doctor received; it went back to documenting for a new draft';
113
117
  const DRAFT_CHANGED = 'A newer draft was written after the one you saw; read it before deciding';
@@ -2086,7 +2090,7 @@ async function sendApprovedIndications({ id, outsideWindowRoute = null, sentBy,
2086
2090
  escalationReviewId: String(claimed._id), error: releaseError.message,
2087
2091
  });
2088
2092
  });
2089
- throw error;
2093
+ throw withSendCode(error, SEND_NOT_SENT);
2090
2094
  }
2091
2095
 
2092
2096
  let result;
@@ -2094,7 +2098,7 @@ async function sendApprovedIndications({ id, outsideWindowRoute = null, sentBy,
2094
2098
  result = await getMessaging().sendMessage(message);
2095
2099
  const alreadyDelivered = result?.status === 'duplicate_suppressed' && result?.completed === true;
2096
2100
  if (!result?.success && !alreadyDelivered) {
2097
- throw httpError(502, 'The indications could not be sent to the patient');
2101
+ throw httpError(502, 'The indications could not be sent to the patient', SEND_NOT_SENT);
2098
2102
  }
2099
2103
  } catch (error) {
2100
2104
  if (!sendDidNotHappen(error)) {
@@ -2102,14 +2106,14 @@ async function sendApprovedIndications({ id, outsideWindowRoute = null, sentBy,
2102
2106
  escalationReviewId: String(claimed._id),
2103
2107
  error: redactedError(error),
2104
2108
  });
2105
- throw httpError(502, 'The patient send could not be confirmed; check the chat before sending again');
2109
+ throw httpError(502, 'The patient send could not be confirmed; check the chat before sending again', SEND_UNCONFIRMED);
2106
2110
  }
2107
2111
  await releaseReview(reviewId, 'sending', claimedFrom, claim).catch((releaseError) => {
2108
2112
  logger.error('[EscalationReview] The claim was not released after a confirmed non-send', {
2109
2113
  escalationReviewId: String(claimed._id), error: releaseError.message,
2110
2114
  });
2111
2115
  });
2112
- throw error;
2116
+ throw withSendCode(error, SEND_NOT_SENT);
2113
2117
  }
2114
2118
 
2115
2119
  const deferred = result.deferred === true;
@@ -2125,7 +2129,7 @@ async function sendApprovedIndications({ id, outsideWindowRoute = null, sentBy,
2125
2129
  const recordUpdate = withCaseMirror(claimed, {
2126
2130
  $set: { patientDelivery: delivery },
2127
2131
  }, { recommendation: finalText });
2128
- const review = await completeOperation(reviewId, claim, 'sending', recordUpdate, status);
2132
+ const review = await completeOperation(reviewId, claim, 'sending', recordUpdate, status).catch((error) => { throw withSendCode(error, SEND_UNCONFIRMED); });
2129
2133
  await audit(review, operator, requestId, { action: status, decisionNumber, transport: delivery.transport });
2130
2134
  return syncCase(review, operator, requestId);
2131
2135
  }
@@ -2193,22 +2197,22 @@ async function sendPlanThroughAssistant({ claimed, claim, decisionNumber, plan,
2193
2197
  logger.error('[EscalationReview] The assistant turn failed after the instructions were stored; leaving the review in sending', {
2194
2198
  escalationReviewId: String(reviewId),
2195
2199
  });
2196
- throw httpError(502, 'The assistant turn failed after the instructions were stored; check the chat before sending again');
2200
+ throw httpError(502, 'The assistant turn failed after the instructions were stored; check the chat before sending again', SEND_UNCONFIRMED);
2197
2201
  }
2198
2202
  await release();
2199
- if (error?.cause instanceof DrainingError) throw httpError(503, 'The server is restarting; send the plan again in a minute');
2200
- throw error;
2203
+ if (error?.cause instanceof DrainingError) throw httpError(503, 'The server is restarting; send the plan again in a minute', SEND_NOT_SENT);
2204
+ throw withSendCode(error, SEND_NOT_SENT);
2201
2205
  }
2202
2206
 
2203
2207
  const outcome = replyOutcome(delivery, sendThrew);
2204
2208
  if (outcome === 'not_sent') {
2205
2209
  await release();
2206
- throw httpError(502, 'The assistant did not send a message to the patient; check the chat and send the plan again');
2210
+ throw httpError(502, 'The assistant did not send a message to the patient; check the chat and send the plan again', SEND_NOT_SENT);
2207
2211
  }
2208
2212
  if (outcome === 'unknown') {
2209
2213
  logger.error('[EscalationReview] The assistant reply could not be confirmed; leaving the review in sending', { escalationReviewId: String(reviewId) });
2210
2214
  await notifyPlanIssues(claimed, ['no se pudo confirmar que el mensaje salió; el caso sigue en «enviando»']);
2211
- throw httpError(502, 'The assistant reply could not be confirmed; check the chat before sending again');
2215
+ throw httpError(502, 'The assistant reply could not be confirmed; check the chat before sending again', SEND_UNCONFIRMED);
2212
2216
  }
2213
2217
 
2214
2218
  // A queued reply goes out once the patient answers the recovery template; the receta, a template itself, goes now.
@@ -2223,7 +2227,7 @@ async function sendPlanThroughAssistant({ claimed, claim, decisionNumber, plan,
2223
2227
  };
2224
2228
  const recordUpdate = withCaseMirror(claimed, { $set: { patientDelivery: record } }, { recommendation: planSummary(plan) });
2225
2229
  const status = queued ? 'indications_queued' : 'indications_sent';
2226
- const review = await completeOperation(reviewId, claim, 'sending', recordUpdate, status);
2230
+ const review = await completeOperation(reviewId, claim, 'sending', recordUpdate, status).catch((error) => { throw withSendCode(error, SEND_UNCONFIRMED); });
2227
2231
  await audit(review, operator, requestId, {
2228
2232
  action: status, decisionNumber, transport: 'assistant', checks: issues.length, ...(prescriberEmail && { prescription: prescriptionFailed ? 'failed' : 'created' }),
2229
2233
  });
@@ -1,4 +1,4 @@
1
- const httpError = (statusCode, message) => Object.assign(new Error(message), { statusCode });
1
+ const httpError = (statusCode, message, errorCode) => Object.assign(new Error(message), { statusCode }, errorCode && { errorCode });
2
2
 
3
3
  const requiredText = (value, name) => {
4
4
  const text = String(value ?? '').trim();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@peopl-health/nexus",
3
- "version": "6.4.0-dev.18830",
3
+ "version": "6.4.0-dev.18831",
4
4
  "description": "Core messaging and assistant library for WhatsApp communication platforms",
5
5
  "keywords": [
6
6
  "whatsapp",