@peopl-health/nexus 5.53.2 → 5.53.3

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.
@@ -19,7 +19,7 @@ const { sendMessage } = require('../core/NexusMessaging');
19
19
 
20
20
  const { getPatientInformation, logPatientChange } = require('./patientService');
21
21
  const { getPatientTasks, updatePatientTask } = require('./patientTaskService');
22
- const { getEscalationApprover } = require('./physicianService');
22
+ const { getEscalationApprover, listEscalationApprovers } = require('./physicianService');
23
23
 
24
24
  const AUDIT_SOURCE = 'escalation_review';
25
25
  const RECENT_REVIEW_LIMIT = 20;
@@ -371,6 +371,17 @@ async function addDraft({ id, doctorText, patientMessage, writtenBy, requestId =
371
371
  return review;
372
372
  }
373
373
 
374
+ async function soleApprover() {
375
+ const approvers = await listEscalationApprovers();
376
+ if (!approvers.length) {
377
+ throw httpError(409, 'No escalation review approver is configured: tick aprueba_vaiven on a physician with a WhatsApp number');
378
+ }
379
+ if (approvers.length > 1) {
380
+ throw httpError(409, `More than one physician approves escalation reviews; name one of: ${approvers.map((approver) => approver.email).join(', ')}`);
381
+ }
382
+ return approvers[0];
383
+ }
384
+
374
385
  async function handOffToDoctor({ id, mode, physicianEmail = null, sentBy, requestId = null } = {}) {
375
386
  const reviewId = reviewIdOf(id);
376
387
  const operator = requiredText(sentBy, 'sentBy');
@@ -383,7 +394,7 @@ async function handOffToDoctor({ id, mode, physicianEmail = null, sentBy, reques
383
394
  const draft = review.drafts[draftNumber - 1];
384
395
  let approver = null;
385
396
  if (mode === 'sent_whatsapp') {
386
- approver = await getEscalationApprover(physicianEmail);
397
+ approver = physicianEmail ? await getEscalationApprover(physicianEmail) : await soleApprover();
387
398
  if (!approver) throw httpError(403, 'The physician is not an escalation review approver');
388
399
  }
389
400
 
@@ -6,6 +6,12 @@ const { getRecordByFilter } = require('./airtableService');
6
6
 
7
7
  const APPROVER_FIELDS = ['physician_name', 'email', 'phone_number'];
8
8
 
9
+ const approverOf = (physician, email = null) => ({
10
+ name: physician.physician_name || null,
11
+ email: email ?? (physician.email ? String(physician.email).trim().toLowerCase() : null),
12
+ phone: physician.phone_number || null,
13
+ });
14
+
9
15
  async function getEscalationApprover(email) {
10
16
  const normalized = String(email ?? '').trim().toLowerCase();
11
17
  if (!normalized) return null;
@@ -14,7 +20,12 @@ async function getEscalationApprover(email) {
14
20
  const [physician] = await getRecordByFilter(Monitoreo_ID, 'physicians', formula, null, APPROVER_FIELDS, true) || [];
15
21
  if (!physician) return null;
16
22
 
17
- return { name: physician.physician_name || null, email: normalized, phone: physician.phone_number || null };
23
+ return approverOf(physician, normalized);
24
+ }
25
+
26
+ async function listEscalationApprovers() {
27
+ const physicians = await getRecordByFilter(Monitoreo_ID, 'physicians', '{aprueba_vaiven}', null, APPROVER_FIELDS, true) || [];
28
+ return physicians.filter((physician) => physician.email).map((physician) => approverOf(physician));
18
29
  }
19
30
 
20
- module.exports = { getEscalationApprover };
31
+ module.exports = { getEscalationApprover, listEscalationApprovers };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@peopl-health/nexus",
3
- "version": "5.53.2",
3
+ "version": "5.53.3",
4
4
  "description": "Core messaging and assistant library for WhatsApp communication platforms",
5
5
  "keywords": [
6
6
  "whatsapp",