@peopl-health/nexus 6.0.19 → 6.0.20

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.
@@ -34,6 +34,12 @@ const noteOf = (text) => oneLineOf(text).slice(0, MAX_NOTE_LENGTH);
34
34
  const flagKey = (flag) => String(flag ?? '').trim().toLowerCase();
35
35
  const timeOf = (value) => new Date(value).getTime();
36
36
  const minutesSince = (from, now) => (now.getTime() - timeOf(from)) / 60000;
37
+ // When the team's alert for this grave datum first fell due, which a failed send's retry does not move; one not recorded yet has not.
38
+ const graveDueAt = (review, clock, flag) => {
39
+ const entry = (review.graveAlerts ?? []).find((alert) => alert.flag === flag && timeOf(alert.since) === clock.since.getTime());
40
+ if (!entry) return Infinity;
41
+ return BUSINESS_HOURS_LEVELS.includes(entry.level ?? clock.level) ? teamOpenAt(new Date(entry.at), responseDeadlinePolicy()).getTime() : timeOf(entry.at);
42
+ };
37
43
 
38
44
  // Whether the bot's latest draft goes to the doctor now, and with which note; null while the patient still has time to answer.
39
45
  function outcomeOf(review, clock, now) {
@@ -56,9 +62,13 @@ function outcomeOf(review, clock, now) {
56
62
  // 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.
57
63
  const handedPresent = new Set((update ? review.drafts[last.draftNumber - 1]?.urgentPresentRedFlags ?? [] : []).map(flagKey));
58
64
  const notify = !update || present.some((flag) => !handedPresent.has(flagKey(flag)));
65
+ // 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.
66
+ const teamFirst = !update && !present.length && grave.length > 0;
67
+ const held = teamFirst && grave.some(({ flag }) => now.getTime() < graveDueAt(review, clock, flag) + wait * 60000);
59
68
  if (grave.length || present.length) {
60
- const note = [present.length && `Bandera grave presente: ${present.join(', ')}`, grave.length && `Falta dato grave: ${grave.map(({ flag }) => flag).join(', ')}`];
61
- return { grave, update, notify, note: withUpdate(noteOf(note.filter(Boolean).join(' · '))) };
69
+ const missing = grave.length && `Falta dato grave: ${grave.map(({ flag }) => flag).join(', ')}${teamFirst && !held ? ' (no se consiguió)' : ''}`;
70
+ const note = [present.length && `Bandera grave presente: ${present.join(', ')}`, missing];
71
+ return { grave, update, notify, held, note: withUpdate(noteOf(note.filter(Boolean).join(' · '))) };
62
72
  }
63
73
  // A grave question still inside its wait holds the case even when the rest is enough.
64
74
  if (pendingFlagsOf(review, clock.since).flags.length) return null;
@@ -174,7 +184,7 @@ async function postHandoffNotice(review, clock, { note }) {
174
184
  async function graveMessage(review, clock, entries) {
175
185
  const { patient, doctor, link } = await teamAlertIdentity(review);
176
186
  const missing = entries.map(({ flag, reason }) => `${flag} (${GRAVE_REASONS[reason]})`).join(', ');
177
- const action = 'Llamar a la paciente para conseguirlo y avisar al médico';
187
+ const action = 'Llamar a la paciente para conseguirlo y registrar la llamada';
178
188
  return {
179
189
  body: [
180
190
  `❗ Falta dato grave · ${LEVEL_LABELS[clock.level]}`,
@@ -195,7 +205,7 @@ const setOn = (review, entry, fields, filter = {}) => getEscalationReview().upda
195
205
  BOOKKEEPING,
196
206
  );
197
207
 
198
- // Sends the grave alerts that are due: to the team while nobody took the case, to the group once someone did; the doctor already has it in the handoff.
208
+ // Sends the grave alerts that are due: to the team while nobody took the case, to the group once someone did; the doctor gets it with the handoff.
199
209
  async function sendDueGraveAlerts(review, clock, entries, now) {
200
210
  const due = entries.filter((entry) => !entry.sentAt && timeOf(entry.dueAt) <= now.getTime() && timeOf(entry.since) === clock.since.getTime());
201
211
  // A datum held for the night is dropped once a current draft written after it no longer lists it as pending.
@@ -353,7 +363,8 @@ async function settleCharacterization(review, clock, now) {
353
363
  // Each failure doubles the wait; after the last attempt the team hands the case over by hand.
354
364
  const backingOff = attempts >= MAX_HANDOFF_ATTEMPTS
355
365
  || (attempts > 0 && now.getTime() - timeOf(review.autoHandoffFailedAt) < HANDOFF_RETRY_MS * 2 ** (attempts - 1));
356
- if (outcome && !backingOff && await handOff(review, clock, outcome, now) && (hospitalized || !outcome.grave.length)) await sendHandoffNotice(review, clock, outcome);
366
+ // A patient in the hospital gets no team call, so nothing holds the doctor's handoff.
367
+ if (outcome && (!outcome.held || hospitalized) && !backingOff && await handOff(review, clock, outcome, now) && (hospitalized || !outcome.grave.length)) await sendHandoffNotice(review, clock, outcome);
357
368
  if (hospitalized) await skipGraveAlerts(review, clock, now);
358
369
  else await sendDueGraveAlerts(review, clock, [...(review.graveAlerts ?? []), ...claimed], now);
359
370
  await remindQuietPatient(review, clock, now);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@peopl-health/nexus",
3
- "version": "6.0.19",
3
+ "version": "6.0.20",
4
4
  "description": "Core messaging and assistant library for WhatsApp communication platforms",
5
5
  "keywords": [
6
6
  "whatsapp",