@peopl-health/nexus 5.61.0-dev.11232 → 5.61.0-dev.11234

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.
@@ -6,6 +6,8 @@ const { logger } = require('../utils/logger');
6
6
 
7
7
  const CALL_OUTCOMES = ['contacted', 'no_answer', 'voicemail'];
8
8
  const URGENCY_LEVELS = ['stat', 'asap', 'urgent', 'routine'];
9
+ // One ranking for the board's order and the doctor alerts' urgency gate.
10
+ const URGENCY_RANK = { routine: 1, urgent: 2, asap: 3, stat: 4 };
9
11
  const HANDOFF_MODES = ['sent_whatsapp', 'copied'];
10
12
  const INTERRUPTED_STATUSES = ['handing_off', 'sending'];
11
13
  const CANCELLABLE_AFTER_HANDOFF_STATUSES = ['awaiting_doctor', 'approved', 'returned', 'indications_queued'];
@@ -221,6 +223,7 @@ module.exports = {
221
223
  HANDOFF_MODES,
222
224
  INTERRUPTED_STATUSES,
223
225
  RETURN_REASONS,
226
+ URGENCY_RANK,
224
227
  getEscalationReview,
225
228
  escalationReviewSchema,
226
229
  };
@@ -15,6 +15,7 @@ const {
15
15
  HANDOFF_MODES,
16
16
  INTERRUPTED_STATUSES,
17
17
  RETURN_REASONS,
18
+ URGENCY_RANK,
18
19
  getEscalationReview,
19
20
  } = require('../models/escalationReviewModel');
20
21
  const { getPatientAuditLog } = require('../models/patientAuditLogModel');
@@ -58,7 +59,6 @@ const DECIDABLE_STATUSES = ['draft_ready', 'awaiting_doctor'];
58
59
  const FOLLOWUP_STATUSES = ['indications_sent', 'indications_queued'];
59
60
  const SETTLED_TRAJECTORIES = ['improving', 'stable'];
60
61
  const ALERTED_URGENCIES = ['stat', 'asap'];
61
- const URGENCY_RANK = { routine: 1, urgent: 2, asap: 3, stat: 4 };
62
62
  const DEFAULT_STAT_REALERT_MINUTES = 15;
63
63
  const PRE_DECISION_STATUSES = ['open', 'documenting', 'draft_ready', 'handing_off', 'awaiting_doctor'];
64
64
  const ALERT_OPERATOR = 'bot:escalationAlert';
@@ -796,11 +796,16 @@ async function suggestDraft({ id, requestedBy, requestId = null } = {}) {
796
796
  return { ...draft, doctorText: withIdentity({ identity, review, doctorText: draft.doctorText }) };
797
797
  }
798
798
 
799
- // The most urgent level the doctor was actually alerted about; a failed attempt told them nothing.
799
+ // The most urgent level the doctor was actually alerted about since the last decision: a failed attempt
800
+ // told them nothing, and a review reopened after a decision is a new question for them.
800
801
  // Scheduling and sending gate on the same one.
801
- const alertedRankOf = (review) => Math.max(0, ...(review.doctorAlerts ?? [])
802
- .filter((sent) => sent.kind === 'initial' && !sent.error)
803
- .map((sent) => URGENCY_RANK[sent.urgencyLevel] || 0));
802
+ const alertedRankOf = (review) => {
803
+ const decidedAt = (review.decisions ?? []).at(-1)?.createdAt;
804
+ const sinceMs = decidedAt ? new Date(decidedAt).getTime() : -Infinity;
805
+ return Math.max(0, ...(review.doctorAlerts ?? [])
806
+ .filter((sent) => sent.kind === 'initial' && !sent.error && new Date(sent.sentAt).getTime() > sinceMs)
807
+ .map((sent) => URGENCY_RANK[sent.urgencyLevel] || 0));
808
+ };
804
809
 
805
810
  // A stat or asap alarm more urgent than anything the doctor was already told about is due now.
806
811
  async function scheduleDoctorAlert(review, alarm, now) {
@@ -1442,7 +1447,7 @@ async function recordDoctorDecision({
1442
1447
  ? { status: 'cancelled', closedBy: operator, closedAt: new Date(), cancellationReason: `no_requiere${comment ? `: ${comment}` : ''}` }
1443
1448
  : { status: statusAfter(decision) },
1444
1449
  // Nothing new since the draft was checked against: an update landing meanwhile refuses the decision.
1445
- }, { drafts: { $size: review.drafts.length }, alarms: { $size: (review.alarms ?? []).length }, updates: { $size: (review.updates ?? []).length } });
1450
+ }, { drafts: { $size: review.drafts.length }, alarms: { $size: (review.alarms ?? []).length }, ...latestUpdateGuard(review) });
1446
1451
  await audit(updated, operator, requestId, {
1447
1452
  action: 'doctor_decision', decision, source, draftNumber, ...(returnReason && { returnReason }),
1448
1453
  });
@@ -1524,6 +1529,13 @@ async function recordDirectIndication({ code, triggeredBy, body, messageId = nul
1524
1529
  }
1525
1530
  }
1526
1531
 
1532
+ // The updates history is capped, so once full its length stays put; the guard names the newest update.
1533
+ const latestUpdateGuard = (review) => {
1534
+ const updates = review.updates ?? [];
1535
+ if (!updates.length) return { 'updates.0': { $exists: false } };
1536
+ return { updates: { $size: updates.length }, [`updates.${updates.length - 1}.turnId`]: updates.at(-1).turnId };
1537
+ };
1538
+
1527
1539
  const arrivedAfter = (review, since) => Boolean(since) && [
1528
1540
  ...(review.alarms ?? []).map((alarm) => alarm.raisedAt),
1529
1541
  ...(review.updates ?? []).map((update) => update.at),
@@ -1,8 +1,7 @@
1
1
  const { httpError } = require('../utils/httpErrorUtils');
2
- const { getEscalationReview } = require('../models/escalationReviewModel');
2
+ const { URGENCY_RANK, getEscalationReview } = require('../models/escalationReviewModel');
3
3
 
4
4
  const DONE_WINDOW_MS = 7 * 24 * 60 * 60 * 1000;
5
- const URGENCY_RANK = { stat: 4, asap: 3, urgent: 2, routine: 1 };
6
5
  const RETURNED_STATUSES = ['returned'];
7
6
  const FOLLOWUP_STATUSES = ['indications_sent', 'indications_queued'];
8
7
  const DONE_STATUSES = ['closed', 'cancelled'];
@@ -45,6 +44,7 @@ const boardRowOf = (review, bucket) => {
45
44
  alarmCount: alarms.length,
46
45
  since,
47
46
  closedAt: review.closedAt || null,
47
+ updatedSinceSeen: review.updatedSinceSeen === true,
48
48
  };
49
49
  };
50
50
 
@@ -56,7 +56,7 @@ const byMostRecentlyClosed = (a, b) => new Date(b.closedAt) - new Date(a.closedA
56
56
  async function listReviewBoard({ bucket, now = new Date() } = {}) {
57
57
  const reviews = await getEscalationReview()
58
58
  .find(bucketQuery(bucket, now), {
59
- patientCode: 1, status: 1, 'alarms.urgencyLevel': 1, 'alarms.symptom': 1, 'alarms.raisedAt': 1, createdAt: 1, closedAt: 1,
59
+ patientCode: 1, status: 1, 'alarms.urgencyLevel': 1, 'alarms.symptom': 1, 'alarms.raisedAt': 1, createdAt: 1, closedAt: 1, updatedSinceSeen: 1,
60
60
  })
61
61
  .lean();
62
62
  const rows = reviews.map((review) => boardRowOf(review, bucket))
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@peopl-health/nexus",
3
- "version": "5.61.0-dev.11232",
3
+ "version": "5.61.0-dev.11234",
4
4
  "description": "Core messaging and assistant library for WhatsApp communication platforms",
5
5
  "keywords": [
6
6
  "whatsapp",