@peopl-health/nexus 6.4.20 → 6.4.21

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.
@@ -53,6 +53,18 @@ const graveDueAt = (review, clock, flag) => {
53
53
  return BUSINESS_HOURS_LEVELS.includes(entry.level ?? clock.level) ? teamOpenAt(new Date(entry.at), responseDeadlinePolicy()).getTime() : timeOf(entry.at);
54
54
  };
55
55
 
56
+ function teamGraveOf(review, clock, grave, now) {
57
+ const waitMs = pendingWaitMinutes(clock.level) * 60000;
58
+ const askedAt = ({ flag, firstSeenAt }) => Math.min(...(review.blockingAsks ?? [])
59
+ .filter((ask) => ASKED_OUTCOMES.includes(ask.outcome) && timeOf(ask.draftAt) >= timeOf(firstSeenAt) && (ask.flags ?? []).map(normalizedKeyOf).includes(flag))
60
+ .map((ask) => timeOf(ask.sentAt)));
61
+ const dueAt = (entry) => (entry.reason === 'unobtainable' ? -Infinity : askedAt(entry) + waitMs);
62
+ // A datum the bot never asked about calls no one: it reaches the doctor in the note.
63
+ const asked = grave.filter((entry) => dueAt(entry) < Infinity);
64
+ if (asked.some((entry) => dueAt(entry) <= now.getTime())) return { due: asked, waiting: [] };
65
+ return { due: [], waiting: asked };
66
+ }
67
+
56
68
  // Whether the bot's latest draft goes to the doctor now, and with which note; null while the patient still has time to answer.
57
69
  function outcomeOf(review, clock, now) {
58
70
  const wait = pendingWaitMinutes(clock?.level);
@@ -69,7 +81,8 @@ function outcomeOf(review, clock, now) {
69
81
  if (review.status === 'awaiting_doctor' && !update) return null;
70
82
  const withUpdate = (note) => (update ? noteOf([UPDATE_NOTE, note].filter(Boolean).join(' · ')) : note);
71
83
 
72
- const grave = graveMissingOf(review, clock, now);
84
+ const graveFlags = graveMissingOf(review, clock, now);
85
+ const { due: grave, waiting } = teamGraveOf(review, clock, graveFlags, now);
73
86
  const present = latest.urgentPresentRedFlags ?? [];
74
87
  // An update reaches the doctor's phone only with a grave flag the draft they have did not show; the panel always shows the latest draft.
75
88
  const handedPresent = new Set((update ? review.drafts[last.draftNumber - 1]?.urgentPresentRedFlags ?? [] : []).map(flagKey));
@@ -79,13 +92,13 @@ function outcomeOf(review, clock, now) {
79
92
  && sent.transport && sent.transport !== 'airtable' && timeOf(sent.sentAt) > timeOf(last.createdAt) && raisedSince(sent.urgencyLevel));
80
93
  const notify = !update || (!rangSince && present.some((flag) => !handedPresent.has(flagKey(flag))));
81
94
  // A missing grave datum is the team's to get: a first handoff waits the level's wait after the team's alert falls due, unless a grave flag is present.
82
- const teamFirst = !update && !present.length && grave.length > 0;
83
- const held = teamFirst && grave.some(({ flag }) => now.getTime() < graveDueAt(review, clock, flag) + wait * 60000);
84
- if (grave.length || present.length) {
85
- const missing = grave.length && `Falta dato grave: ${grave.map(({ flag }) => flag).join(', ')}${teamFirst && !held ? ' (no se consiguió)' : ''}`;
95
+ const teamFirst = !update && !present.length && grave.length + waiting.length > 0;
96
+ const held = teamFirst && (waiting.length > 0 || grave.some(({ flag }) => now.getTime() < graveDueAt(review, clock, flag) + wait * 60000));
97
+ if (graveFlags.length || present.length) {
98
+ const lacking = graveFlags.length && `Falta dato grave: ${graveFlags.map(({ flag }) => flag).join(', ')}${teamFirst && !held ? ' (no se consiguió)' : ''}`;
86
99
  // A grave datum still inside its wait goes in the note too, so the doctor knows it is still open.
87
- const pending = pendingFlagsOf(review, clock.since).flags.map(({ flag }) => flag).filter((flag) => !grave.some((entry) => entry.flag === flag));
88
- const note = [present.length && `Bandera grave presente: ${present.join(', ')}`, missing, pending.length && `Pendiente: ${pending.join(', ')}`];
100
+ const pending = pendingFlagsOf(review, clock.since).flags.map(({ flag }) => flag).filter((flag) => !graveFlags.some((entry) => entry.flag === flag));
101
+ const note = [present.length && `Bandera grave presente: ${present.join(', ')}`, lacking, pending.length && `Pendiente: ${pending.join(', ')}`];
89
102
  return { grave, update, notify, held, note: withUpdate(noteOf(note.filter(Boolean).join(' · '))) };
90
103
  }
91
104
  // A grave question still inside its wait holds the case even when the rest is enough.
@@ -429,7 +442,7 @@ async function settleCharacterization(review, clock, now) {
429
442
  const outcome = outcomeOf(review, clock, now);
430
443
  // With the doctor after the bot's handoff, a grave datum falls due even when the patient writes nothing more.
431
444
  const grave = outcome?.grave
432
- ?? (OUTCOME_LEVELS.includes(clock.level) && review.status === 'awaiting_doctor' && stillDocumented(review) ? graveMissingOf(review, clock, now) : []);
445
+ ?? (OUTCOME_LEVELS.includes(clock.level) && review.status === 'awaiting_doctor' && stillDocumented(review) ? teamGraveOf(review, clock, graveMissingOf(review, clock, now), now).due : []);
433
446
  const graveWork = grave.length || unsentGraveAlerts(review, clock).length;
434
447
  const hospitalized = graveWork ? Boolean((await getThreadInfo(review.patientCode))?.hospitalizedAt) : false;
435
448
  const claimed = grave.length && !hospitalized ? await claimGraveAlerts(review, clock, grave, now) : [];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@peopl-health/nexus",
3
- "version": "6.4.20",
3
+ "version": "6.4.21",
4
4
  "description": "Core messaging and assistant library for WhatsApp communication platforms",
5
5
  "keywords": [
6
6
  "whatsapp",