@peopl-health/nexus 6.6.0-dev.20702 → 6.6.0-dev.20728

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.
@@ -8,9 +8,8 @@ const { whatsAppCodeFromPhone } = require('../helpers/twilioHelper');
8
8
  const { doctorDigestLine, doctorDigestTitle } = require('../clinical/helpers/doctorReviewMessageHelper');
9
9
 
10
10
  const { logPatientChange } = require('./patientService');
11
- const { listEscalationApprovers } = require('./physicianService');
12
11
  const { effectiveUrgencyOf, responseDeadlineOf, responseDeadlinePolicy, teamOpenAt } = require('./reviewDeadlineService');
13
- const { BOOKKEEPING, DOCTOR_BRIEF_TEMPLATE_KEY, doctorAlertMessage, doctorLink, teamAlertIdentity } = require('./escalationReviewService');
12
+ const { BOOKKEEPING, DOCTOR_BRIEF_TEMPLATE_KEY, doctorAlertMessage, doctorLink, notifiedApprover, teamAlertIdentity } = require('./escalationReviewService');
14
13
 
15
14
  const DIGEST_TEMPLATE_KEY = 'ESCALATION_REVIEW_DOCTOR_DIGEST_CONTENT_SID';
16
15
  // Meta has not approved this template.
@@ -35,9 +34,9 @@ const redacted = (error) => redactDirectIdentifiers(String(error?.message || err
35
34
  // Urgent cases ready for the doctor go in a digest only once its template exists.
36
35
  const urgentDigestLive = () => Boolean(runtimeConfig.get(DIGEST_BRIEF_TEMPLATE_KEY) || runtimeConfig.get(DIGEST_TEMPLATE_KEY));
37
36
 
38
- // The approving doctor's WhatsApp, or null when there is none to send the digest to.
37
+ // The notified approver's WhatsApp, or null when their number is unusable; throws when no single approver can be chosen.
39
38
  async function digestRecipient() {
40
- const [approver] = await listEscalationApprovers();
39
+ const approver = await notifiedApprover();
41
40
  const code = whatsAppCodeFromPhone(approver?.phone);
42
41
  return PATIENT_CODE.test(code) ? code : null;
43
42
  }
@@ -1158,7 +1158,7 @@ const alarmFor = (review, urgencyLevel, alarmId) => (alarmId && (review.alarms ?
1158
1158
  async function prepareDoctorAlert(review, urgencyLevel, { recipient = null, alarmId = null } = {}) {
1159
1159
  const identity = await readDoctorIdentity(review);
1160
1160
  if (identity.missing.length) throw new Error(`missing ${identity.missing.join(' and ')}`);
1161
- const approver = recipient ?? await soleApprover();
1161
+ const approver = recipient ?? await notifiedApprover();
1162
1162
  const link = conversationLink(review.patientCode);
1163
1163
  if (!link) throw new Error('missing the interface URL for the chat link');
1164
1164
  const alarm = alarmFor(review, urgencyLevel, alarmId);
@@ -1367,7 +1367,7 @@ async function sendScheduledDoctorAlert({ id, kind, lease = null, now = new Date
1367
1367
 
1368
1368
  async function fallbackRecipient() {
1369
1369
  const phone = String(runtimeConfig.get('ESCALATION_FALLBACK_ALERT_PHONE', '') ?? '').trim();
1370
- return phone ? { name: null, email: null, phone } : soleApprover();
1370
+ return phone ? { name: null, email: null, phone } : notifiedApprover();
1371
1371
  }
1372
1372
 
1373
1373
  // Who and where for a team alert; a patient record that cannot be read never holds the alert back.
@@ -1787,15 +1787,15 @@ async function addDraft({ id, doctorText, patientMessage, writtenBy, requestId =
1787
1787
  return review;
1788
1788
  }
1789
1789
 
1790
- async function soleApprover() {
1790
+ async function notifiedApprover() {
1791
1791
  const approvers = await listEscalationApprovers();
1792
1792
  if (!approvers.length) {
1793
1793
  throw httpError(409, 'No escalation review approver is configured: tick aprueba_vaiven on a physician with a WhatsApp number');
1794
1794
  }
1795
- if (approvers.length > 1) {
1796
- throw httpError(409, `More than one physician approves escalation reviews; name one of: ${approvers.map((approver) => approver.email).join(', ')}`);
1797
- }
1798
- return approvers[0];
1795
+ if (approvers.length === 1) return approvers[0];
1796
+ const primaries = approvers.filter((approver) => approver.primary);
1797
+ if (primaries.length === 1) return primaries[0];
1798
+ throw httpError(409, `More than one physician approves escalation reviews; name one of: ${approvers.map((approver) => approver.email).join(', ')}, or tick aprobador_principal on exactly one`);
1799
1799
  }
1800
1800
 
1801
1801
  function doctorAlertMessage({ review, draft, identity, link, kind, note, level, symptom, change, reminder = null, now = new Date() }) {
@@ -1834,7 +1834,7 @@ function doctorAlertMessage({ review, draft, identity, link, kind, note, level,
1834
1834
  async function remindDoctorOfCase({ id, promptAt = null, now = new Date(), operator }) {
1835
1835
  const review = await loadReview(reviewIdOf(id));
1836
1836
  const handoff = (review.doctorHandoffs ?? []).at(-1);
1837
- const approver = handoff?.physicianEmail ? await getEscalationApprover(handoff.physicianEmail) : await soleApprover();
1837
+ const approver = handoff?.physicianEmail ? await getEscalationApprover(handoff.physicianEmail) : await notifiedApprover();
1838
1838
  if (!approver) throw httpError(409, 'The case has no approving doctor to remind');
1839
1839
  const draft = review.drafts[(handoff?.draftNumber ?? review.drafts.length) - 1];
1840
1840
  const lead = promptAt ? Math.round((new Date(promptAt).getTime() - now.getTime()) / 60000) : 0;
@@ -1856,7 +1856,7 @@ async function handOffToDoctor({ id, mode, physicianEmail = null, sentBy, handof
1856
1856
  const draft = review.drafts[draftNumber - 1];
1857
1857
  let approver = null;
1858
1858
  if (mode === 'sent_whatsapp') {
1859
- approver = physicianEmail ? await getEscalationApprover(physicianEmail) : await soleApprover();
1859
+ approver = physicianEmail ? await getEscalationApprover(physicianEmail) : await notifiedApprover();
1860
1860
  if (!approver) throw httpError(403, 'The physician is not an escalation review approver');
1861
1861
  }
1862
1862
 
@@ -2522,6 +2522,7 @@ async function syncEscalationReviewCase({ id, syncedBy, requestId = null } = {})
2522
2522
 
2523
2523
  module.exports = {
2524
2524
  BOOKKEEPING,
2525
+ notifiedApprover,
2525
2526
  AUTO_HANDOFF_OPERATOR,
2526
2527
  DOCUMENTING_STATUSES,
2527
2528
  DRAFT_OPERATOR,
@@ -6,11 +6,13 @@ const { escapeFormulaValue } = require('../helpers/airtableFormulaHelper');
6
6
  const { getRecordByFilter } = require('./airtableService');
7
7
 
8
8
  const APPROVER_FIELDS = ['physician_name', 'email', 'phone_number'];
9
+ const PRIMARY_FIELD = 'aprobador_principal';
9
10
 
10
11
  const approverOf = (physician, email = null) => ({
11
12
  name: physician.physician_name || null,
12
13
  email: email ?? (physician.email ? String(physician.email).trim().toLowerCase() : null),
13
14
  phone: physician.phone_number || null,
15
+ ...(physician[PRIMARY_FIELD] && { primary: true }),
14
16
  });
15
17
 
16
18
  async function getEscalationApprover(email) {
@@ -26,7 +28,7 @@ async function getEscalationApprover(email) {
26
28
 
27
29
  // A physician approves escalations only for the programs ticked on their row.
28
30
  async function listEscalationApprovers() {
29
- const physicians = await getRecordByFilter(Monitoreo_ID, 'physicians', `AND({aprueba_vaiven}, ${programSourceFormula()})`, null, APPROVER_FIELDS, true) || [];
31
+ const physicians = await getRecordByFilter(Monitoreo_ID, 'physicians', `AND({aprueba_vaiven}, ${programSourceFormula()})`, null, [...APPROVER_FIELDS, PRIMARY_FIELD], true) || [];
30
32
  return physicians.filter((physician) => physician.email).map((physician) => approverOf(physician));
31
33
  }
32
34
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@peopl-health/nexus",
3
- "version": "6.6.0-dev.20702",
3
+ "version": "6.6.0-dev.20728",
4
4
  "description": "Core messaging and assistant library for WhatsApp communication platforms",
5
5
  "keywords": [
6
6
  "whatsapp",