@peopl-health/nexus 6.1.0-dev.18137 → 6.1.0-dev.18138

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.
@@ -90,7 +90,7 @@ function symptomParagraph(entry, fallbackSymptom) {
90
90
  const label = inlineOf(sections.symptom) || fallbackSymptom || 'Síntoma no especificado';
91
91
  const graded = [1, 2, 3, 4, 5].includes(sections.grade);
92
92
  const grade = graded ? `G${sections.grade}` : 'grado no establecido';
93
- const { present, absent, unassessed } = flagsByStatus(sections);
93
+ const { present, absent, unassessed, urgent } = flagsByStatus(sections);
94
94
  const summary = inlineOf(sections.characterizationSummary);
95
95
  return [
96
96
  // An ungraded symptom whose label already names a grade is not called ungraded.
@@ -98,6 +98,8 @@ function symptomParagraph(entry, fallbackSymptom) {
98
98
  summary && sentence(summary),
99
99
  present.length && `${present.length > 1 ? 'Banderas rojas presentes' : 'Bandera roja presente'}: ${sentence(present.join(', '))}`,
100
100
  !present.length && !unassessed.length && absent.length && 'Sin banderas rojas.',
101
+ // A proposal names its own pending grave flags; without one they would go unsaid.
102
+ !proposedDrugs(entry).length && urgent.length && `Falta descartar: ${sentence(urgent.join(', '))}`,
101
103
  proposalLine(entry),
102
104
  ].filter(Boolean).join(' ');
103
105
  }
@@ -10,7 +10,7 @@ const { countMessages, getMessages } = require('./messageService');
10
10
  const { logPatientChange } = require('./patientService');
11
11
  const { getThreadInfo } = require('./threadService');
12
12
  const { AUTO_HANDOFF_OPERATOR, BOOKKEEPING, DRAFT_OPERATOR, MAX_DOCTOR_UPDATES, PRE_DECISION_STATUSES, caseTakenReason, handOffToDoctor, teamAlertIdentity } = require('./escalationReviewService');
13
- const { graveMissingOf, isDraftCurrent, pendingFlagsOf } = require('./gravePendingService');
13
+ const { graveMissingOf, isDraftCurrent, pendingFlagsOf, stillDocumented } = require('./gravePendingService');
14
14
  const { BUSINESS_HOURS_LEVELS, pendingWaitMinutes, quietReminderMinutes, responseDeadlinePolicy, teamOpenAt } = require('./reviewDeadlineService');
15
15
  const { sendGroupNotice, sendTeamAlert } = require('./teamAlertService');
16
16
  const { digestRecipient, digestTimeOf, urgentDigestLive } = require('./doctorDigestService');
@@ -75,7 +75,9 @@ function outcomeOf(review, clock, now) {
75
75
  const held = teamFirst && grave.some(({ flag }) => now.getTime() < graveDueAt(review, clock, flag) + wait * 60000);
76
76
  if (grave.length || present.length) {
77
77
  const missing = grave.length && `Falta dato grave: ${grave.map(({ flag }) => flag).join(', ')}${teamFirst && !held ? ' (no se consiguió)' : ''}`;
78
- const note = [present.length && `Bandera grave presente: ${present.join(', ')}`, missing];
78
+ // A grave datum still inside its wait goes in the note too, so the doctor knows it is being asked.
79
+ const asking = pendingFlagsOf(review, clock.since).flags.map(({ flag }) => flag).filter((flag) => !grave.some((entry) => entry.flag === flag));
80
+ const note = [present.length && `Bandera grave presente: ${present.join(', ')}`, missing, asking.length && `Preguntando: ${asking.join(', ')}`];
79
81
  return { grave, update, notify, held, note: withUpdate(noteOf(note.filter(Boolean).join(' · '))) };
80
82
  }
81
83
  // A grave question still inside its wait holds the case even when the rest is enough.
@@ -351,13 +353,16 @@ async function remindQuietPatient(review, clock, now) {
351
353
 
352
354
  // Once per draft the bot asks its blocking question or the pending grave data, unless a question is open; after a patient message, once the bot replied.
353
355
  async function askBlockingQuestion(review, clock, now) {
354
- if (!OUTCOME_LEVELS.includes(clock.level) || review.status !== 'draft_ready' || review.closedAt) return;
356
+ // With the doctor, only a case the bot handed over is still asked, and only its grave data.
357
+ const withDoctor = review.status === 'awaiting_doctor';
358
+ if (!OUTCOME_LEVELS.includes(clock.level) || !(review.status === 'draft_ready' || (withDoctor && stillDocumented(review))) || review.closedAt) return;
355
359
  const latest = (review.drafts ?? []).at(-1);
356
360
  if (latest?.writtenBy !== DRAFT_OPERATOR || timeOf(latest.createdAt) < clock.since.getTime() || !isDraftCurrent(review, latest)) return;
357
- const graveFlags = latest.blockingQuestion || latest.urgentPresentRedFlags?.length ? []
361
+ const blocking = withDoctor ? null : latest.blockingQuestion;
362
+ const graveFlags = blocking || latest.urgentPresentRedFlags?.length ? []
358
363
  : pendingFlagsOf(review, clock.since).flags.filter(({ unobtainable }) => !unobtainable).map(({ flag }) => flag);
359
- const question = latest.blockingQuestion || graveFlags.join('; ') || null;
360
- const flags = latest.blockingQuestion ? [latest.blockingFlag].filter(Boolean).map(normalizedKeyOf) : graveFlags;
364
+ const question = blocking || graveFlags.join('; ') || null;
365
+ const flags = blocking ? [latest.blockingFlag].filter(Boolean).map(normalizedKeyOf) : graveFlags;
361
366
  if (!question) return;
362
367
  const asks = review.blockingAsks ?? [];
363
368
  if (asks.some((entry) => timeOf(entry.draftAt) === timeOf(latest.createdAt))) return;
@@ -377,11 +382,11 @@ async function askBlockingQuestion(review, clock, now) {
377
382
  // The turn runs in the background so the sweep does not wait on it; while it waits for the chat lock the case can be handed over, redrafted or taken.
378
383
  const beforeRun = async () => {
379
384
  const fresh = await getEscalationReview().findById(review._id).lean();
380
- if (!fresh || fresh.closedAt || fresh.status !== 'draft_ready') return 'moved';
385
+ if (!fresh || fresh.closedAt || fresh.status !== review.status || (withDoctor && !stillDocumented(fresh))) return 'moved';
381
386
  if (!isDraftCurrent(fresh, latest) || timeOf(fresh.drafts?.at(-1)?.createdAt) !== timeOf(latest.createdAt)) return 'superseded';
382
387
  return takenOf(fresh, clock);
383
388
  };
384
- const instruction = latest.blockingQuestion ? blockingAskText(latest.blockingQuestion) : graveAskText(graveFlags);
389
+ const instruction = blocking ? blockingAskText(blocking) : graveAskText(graveFlags);
385
390
  getMessaging().processInstruction(review.patientCode, instruction, 'developer', { triggeredBy: OPERATOR, beforeRun })
386
391
  .then((result) => result?.outcome ?? null, (error) => {
387
392
  logger.error('[CharacterizationOutcome] The blocking question was not asked', {
@@ -1,6 +1,6 @@
1
1
  const { normalizedKeyOf } = require('../utils/coercionUtils');
2
2
 
3
- const { DOCUMENTING_STATUSES } = require('./escalationReviewService');
3
+ const { AUTO_HANDOFF_OPERATOR, DOCUMENTING_STATUSES } = require('./escalationReviewService');
4
4
  const { pendingWaitMinutes } = require('./reviewDeadlineService');
5
5
 
6
6
  const keysOf = (flags) => [...new Set((flags ?? []).map(normalizedKeyOf).filter(Boolean))];
@@ -20,6 +20,10 @@ function pendingFlagsOf(review, since) {
20
20
  return { latest: drafts.at(-1) ?? null, flags: [...firstSeen].map(([flag, firstSeenAt]) => ({ flag, firstSeenAt, unobtainable: unobtainable.includes(flag) })) };
21
21
  }
22
22
 
23
+ // The bot keeps documenting a case it handed to the doctor itself, so its grave questions still wait for the patient there.
24
+ const stillDocumented = (review) => DOCUMENTING_STATUSES.includes(review.status)
25
+ || (review.status === 'awaiting_doctor' && review.doctorHandoffs?.at(-1)?.sentBy === AUTO_HANDOFF_OPERATOR);
26
+
23
27
  // The latest draft speaks for the case only while no newer patient turn or redraft is waiting to replace it.
24
28
  const isDraftCurrent = (review, latest) => !review.draftDueAt
25
29
  && !(review.updates ?? []).some((update) => new Date(update.at).getTime() > new Date(latest.createdAt).getTime());
@@ -30,8 +34,8 @@ function graveMissingOf(review, clock, now) {
30
34
  if (!limit) return [];
31
35
  const { latest, flags } = pendingFlagsOf(review, clock.since);
32
36
  if (!latest || !isDraftCurrent(review, latest)) return [];
33
- // Once the case left documenting (with the doctor) no new draft comes, so the wait no longer means unanswered.
34
- const waiting = DOCUMENTING_STATUSES.includes(review.status);
37
+ // Once a person handed the case over no new draft comes, so the wait no longer means unanswered.
38
+ const waiting = stillDocumented(review);
35
39
  const isDue = ({ firstSeenAt, unobtainable }) => unobtainable || (waiting && now.getTime() - firstSeenAt.getTime() >= limit * 60 * 1000);
36
40
  if (!flags.some(isDue)) return [];
37
41
  // Only the first alert gathers the data still waiting; one that appears after it waits its own time.
@@ -40,4 +44,4 @@ function graveMissingOf(review, clock, now) {
40
44
  .map(({ flag, firstSeenAt, unobtainable }) => ({ flag, firstSeenAt, reason: unobtainable ? 'unobtainable' : 'unanswered' }));
41
45
  }
42
46
 
43
- module.exports = { graveMissingOf, isDraftCurrent, pendingFlagsOf };
47
+ module.exports = { graveMissingOf, isDraftCurrent, pendingFlagsOf, stillDocumented };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@peopl-health/nexus",
3
- "version": "6.1.0-dev.18137",
3
+ "version": "6.1.0-dev.18138",
4
4
  "description": "Core messaging and assistant library for WhatsApp communication platforms",
5
5
  "keywords": [
6
6
  "whatsapp",