@peopl-health/nexus 6.6.0 → 6.6.2
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.
|
@@ -4,9 +4,9 @@ const { FOLLOWUP_STATUSES } = require('../models/escalationReviewModel');
|
|
|
4
4
|
const { CASE_STATUS_BY_REVIEW_STATUS } = require('../fhir/constants/escalationCaseStatus');
|
|
5
5
|
const { escalationUntakenAlertsLive } = require('../clinical/flags/escalationFlags');
|
|
6
6
|
|
|
7
|
-
const { DRAFT_OPERATOR, PRE_DECISION_STATUSES, caseTakenBy, caseTakenReason } = require('./escalationReviewService');
|
|
7
|
+
const { DRAFT_OPERATOR, PRE_DECISION_STATUSES, caseSettledOrHospitalizedReason, caseTakenBy, caseTakenReason } = require('./escalationReviewService');
|
|
8
8
|
const { graveMissingOf, isDraftCurrent } = require('./gravePendingService');
|
|
9
|
-
const { pendingWaitMinutes } = require('./reviewDeadlineService');
|
|
9
|
+
const { HANDOFF_TAKES_LEVELS, pendingWaitMinutes } = require('./reviewDeadlineService');
|
|
10
10
|
const { dueStageOf, untakenClockOf } = require('./untakenAlertService');
|
|
11
11
|
|
|
12
12
|
const WITH_DOCTOR_STATUSES = ['handing_off', 'awaiting_doctor'];
|
|
@@ -35,9 +35,15 @@ function waitingOn(review, clock, now) {
|
|
|
35
35
|
return dueAt > now ? { since: new Date(ask.sentAt), dueAt, question: ask.question ?? null } : null;
|
|
36
36
|
}
|
|
37
37
|
|
|
38
|
-
|
|
38
|
+
// Past an urgent deadline with the doctor, team work no longer counts as taken, as in the untaken sweep.
|
|
39
|
+
const undecidedPastDeadline = (review, clock, now) => HANDOFF_TAKES_LEVELS.includes(clock.level) && review.status === 'awaiting_doctor'
|
|
40
|
+
&& now >= clock.stages.find((stage) => stage.stage === 'deadline').dueAt;
|
|
41
|
+
|
|
42
|
+
async function takenOf(review, clock, now) {
|
|
39
43
|
try {
|
|
40
|
-
const reason =
|
|
44
|
+
const reason = undecidedPastDeadline(review, clock, now)
|
|
45
|
+
? await caseSettledOrHospitalizedReason(review, clock.since, clock.level)
|
|
46
|
+
: await caseTakenReason(review, clock.since, clock.level);
|
|
41
47
|
if (!reason) return null;
|
|
42
48
|
const by = await caseTakenBy(review, clock.since, reason).catch((error) => {
|
|
43
49
|
logger.warn('[EscalationCaseState] Could not tell who took the case', { escalationReviewId: String(review._id), error: redactDirectIdentifiers(String(error?.message || error)) });
|
|
@@ -61,7 +67,7 @@ async function caseStateOf(review, { now = new Date(), policy } = {}) {
|
|
|
61
67
|
if (!clock) return state('in_progress', label);
|
|
62
68
|
|
|
63
69
|
const nextAlert = nextAlertOf(review, clock, now);
|
|
64
|
-
const taken = await takenOf(review, clock);
|
|
70
|
+
const taken = await takenOf(review, clock, now);
|
|
65
71
|
if (taken) return state('taken', 'Tomado', { since: clock.since, takenReason: taken.reason, takenBy: taken.by });
|
|
66
72
|
const grave = graveMissingOf(review, clock, now).map(({ flag }) => flag);
|
|
67
73
|
if (grave.length) return state('grave_missing', 'Falta dato grave', { since: clock.since, nextAlert, graveFlags: grave });
|
|
@@ -1131,11 +1131,26 @@ const takingLoweringOf = (review, from) => {
|
|
|
1131
1131
|
};
|
|
1132
1132
|
const takingCallOf = (review, from) => (review.callLogs ?? []).find((call) => call.outcome === 'contacted' && atOrAfterOf(from)(call.calledAt));
|
|
1133
1133
|
|
|
1134
|
-
// What
|
|
1135
|
-
|
|
1134
|
+
// What settles the case itself from `from` on: a decision, a STAT alarm that took it over, or a lowered urgency.
|
|
1135
|
+
function caseSettledReason(review, from, level) {
|
|
1136
1136
|
if (takingDecisionOf(review, from)) return 'decision';
|
|
1137
1137
|
if (level !== 'stat' && levelsAtOf(review)(new Date(8.64e15)).some((entry) => entry.level === 'stat')) return 'stat_alarm';
|
|
1138
1138
|
if (takingLoweringOf(review, from)) return 'reclassified';
|
|
1139
|
+
return null;
|
|
1140
|
+
}
|
|
1141
|
+
|
|
1142
|
+
// What settles a case the doctor has past its deadline: the reasons above, or the patient in hospital, seen in person.
|
|
1143
|
+
async function caseSettledOrHospitalizedReason(review, from, level) {
|
|
1144
|
+
const settled = caseSettledReason(review, from, level);
|
|
1145
|
+
if (settled) return settled;
|
|
1146
|
+
const thread = await getThreadInfo(review.patientCode, { rethrow: true });
|
|
1147
|
+
return thread?.hospitalizedAt ? 'patient_hospitalized' : null;
|
|
1148
|
+
}
|
|
1149
|
+
|
|
1150
|
+
// What says the team took the case from `from` on, for a case timed at `level`.
|
|
1151
|
+
async function caseTakenReason(review, from, level) {
|
|
1152
|
+
const settled = caseSettledReason(review, from, level);
|
|
1153
|
+
if (settled) return settled;
|
|
1139
1154
|
if (takingCallOf(review, from)) return 'call_contacted';
|
|
1140
1155
|
// A read error goes back to the sweep to retry; only a thread that does not exist (no turn to run
|
|
1141
1156
|
// anyway) skips the prompt.
|
|
@@ -1567,6 +1582,11 @@ function updatesDoctor(review) {
|
|
|
1567
1582
|
return botHandoffs.length <= MAX_DOCTOR_UPDATES;
|
|
1568
1583
|
}
|
|
1569
1584
|
|
|
1585
|
+
async function markDraftFailed(review, lease, reason) {
|
|
1586
|
+
const marked = await getEscalationReview().updateOne({ _id: review._id, closedAt: null, draftDueAt: lease }, { $set: { draftFailedAt: new Date() } }, BOOKKEEPING);
|
|
1587
|
+
if (marked.modifiedCount) await audit(review, DRAFT_OPERATOR, null, { action: 'draft_generation_failed', reason });
|
|
1588
|
+
}
|
|
1589
|
+
|
|
1570
1590
|
async function generateBotDraft({ id, lease = null } = {}) {
|
|
1571
1591
|
const startedAt = new Date();
|
|
1572
1592
|
const review = await loadReview(reviewIdOf(id));
|
|
@@ -1579,6 +1599,7 @@ async function generateBotDraft({ id, lease = null } = {}) {
|
|
|
1579
1599
|
const identity = await readDoctorIdentity(review);
|
|
1580
1600
|
if (identity.missing.length) {
|
|
1581
1601
|
if (!review.identityBlock && !refreshing) await blockForMissingIdentity({ review, missing: identity.missing, operator: DRAFT_OPERATOR });
|
|
1602
|
+
if (lease && refreshing) await markDraftFailed(review, lease, 'missing_doctor_identity');
|
|
1582
1603
|
return { review: null, retry: false };
|
|
1583
1604
|
}
|
|
1584
1605
|
|
|
@@ -1598,6 +1619,7 @@ async function generateBotDraft({ id, lease = null } = {}) {
|
|
|
1598
1619
|
}
|
|
1599
1620
|
if (!draft.completed) {
|
|
1600
1621
|
logger.warn('[EscalationReview] The automatic draft was not written', { escalationReviewId: String(review._id), reason: draft.reason });
|
|
1622
|
+
if (draft.configured === false && lease) await markDraftFailed(review, lease, 'not_configured');
|
|
1601
1623
|
return { review: null, retry: draft.configured !== false, rejected: draft.rejected === true };
|
|
1602
1624
|
}
|
|
1603
1625
|
|
|
@@ -2609,6 +2631,7 @@ module.exports = {
|
|
|
2609
2631
|
addCallLog,
|
|
2610
2632
|
remindDoctorOfCase,
|
|
2611
2633
|
caseTakenBy,
|
|
2634
|
+
caseSettledOrHospitalizedReason,
|
|
2612
2635
|
caseTakenReason,
|
|
2613
2636
|
promptedSince,
|
|
2614
2637
|
currentAlarms,
|
|
@@ -9,7 +9,7 @@ const { escalationAutoHandoffLive, escalationPatientTimerLive, escalationUntaken
|
|
|
9
9
|
|
|
10
10
|
const { logPatientChange } = require('./patientService');
|
|
11
11
|
const { BUSINESS_HOURS_LEVELS, HANDOFF_TAKES_LEVELS, doctorReminderAt, levelsOf, responseDeadlinePolicy, untakenStagesOf } = require('./reviewDeadlineService');
|
|
12
|
-
const { BOOKKEEPING, DOCTOR_BRIEF_TEMPLATE_KEY, FEVER_CATEGORY, PRE_DECISION_STATUSES, appliedLevelOf, caseTakenReason, currentAlarms, doctorLink, promptedSince, remindDoctorOfCase, teamAlertIdentity } = require('./escalationReviewService');
|
|
12
|
+
const { BOOKKEEPING, DOCTOR_BRIEF_TEMPLATE_KEY, FEVER_CATEGORY, PRE_DECISION_STATUSES, appliedLevelOf, caseSettledOrHospitalizedReason, caseTakenReason, currentAlarms, doctorLink, promptedSince, remindDoctorOfCase, teamAlertIdentity } = require('./escalationReviewService');
|
|
13
13
|
const { settleCharacterization } = require('./characterizationOutcomeService');
|
|
14
14
|
const { sendTeamAlert } = require('./teamAlertService');
|
|
15
15
|
const { digestDueAt, digestStillDue, sendDueDoctorDigests } = require('./doctorDigestService');
|
|
@@ -28,6 +28,7 @@ const ACTIONS = {
|
|
|
28
28
|
asap: 'Tomar el caso: escribir o llamar y registrar la llamada',
|
|
29
29
|
urgent: 'Tomar el caso: escribir o llamar y registrar la llamada',
|
|
30
30
|
};
|
|
31
|
+
const UNDECIDED_ACTION = 'Llamar al médico para que decida, o decidir con el equipo';
|
|
31
32
|
const MAX_QUESTION_LENGTH = 200;
|
|
32
33
|
const RECENT_DOCTOR_RING_MS = 5 * 60 * 1000;
|
|
33
34
|
const RECENT_DIGEST_MS = 60 * 60 * 1000;
|
|
@@ -123,13 +124,13 @@ function doctorUntakenMessage(review, clock, { head, note, question, patient, do
|
|
|
123
124
|
return { body: lines.join('\n'), template: Object.fromEntries(lines.map((line, index) => [index + 1, line])), templateKey: DOCTOR_BRIEF_TEMPLATE_KEY };
|
|
124
125
|
}
|
|
125
126
|
|
|
126
|
-
function untakenMessage(review, clock, stage, now, { patient, doctor, link }) {
|
|
127
|
+
function untakenMessage(review, clock, stage, now, { patient, doctor, link }, undecided) {
|
|
127
128
|
const deadline = deadlineText(clock, stage);
|
|
128
129
|
const level = LEVEL_LABELS[clock.level];
|
|
129
|
-
const action = ACTIONS[clock.level];
|
|
130
|
+
const action = undecided ? UNDECIDED_ACTION : ACTIONS[clock.level];
|
|
130
131
|
const question = pendingQuestionOf(review, clock);
|
|
131
132
|
const note = patientPromptNote(review, stage, now);
|
|
132
|
-
const head = `⏰ ${level} sin tomar · ${deadline}`;
|
|
133
|
+
const head = `⏰ ${level} ${undecided ? 'con el médico, sin decisión' : 'sin tomar'} · ${deadline}`;
|
|
133
134
|
return {
|
|
134
135
|
// Without the interface URL the doctor's copy has no link, so the doctors keep the team text.
|
|
135
136
|
...(runtimeConfig.get(DOCTOR_BRIEF_TEMPLATE_KEY) && runtimeConfig.get('PEOPL_INTERFACE_URL') && { doctorMessage: doctorUntakenMessage(review, clock, { head, note, question, patient, doctor }) }),
|
|
@@ -181,13 +182,15 @@ async function alertIfUntaken(review, clock, stage, now, policy) {
|
|
|
181
182
|
if (digestAt && digestAt <= stage.dueAt.getTime() + DIGEST_GRACE_MS && now.getTime() < digestAt + DIGEST_GRACE_MS) return false;
|
|
182
183
|
const withDoctor = HANDOFF_TAKES_LEVELS.includes(clock.level) && review.status === 'awaiting_doctor'
|
|
183
184
|
&& (doctorRangSince(review, clock.stagesFrom) || new Date(review.digestSentAt ?? 0) >= clock.stagesFrom);
|
|
184
|
-
const
|
|
185
|
+
const undecided = withDoctor && stage.stage === 'deadline';
|
|
186
|
+
const taken = undecided ? await caseSettledOrHospitalizedReason(review, clock.since, clock.level)
|
|
187
|
+
: withDoctor ? 'with_doctor' : await caseTakenReason(review, clock.since, clock.level);
|
|
185
188
|
if (taken) {
|
|
186
189
|
await claimStage(review, clock, stage, now, taken);
|
|
187
190
|
return false;
|
|
188
191
|
}
|
|
189
192
|
const who = await teamAlertIdentity(review);
|
|
190
|
-
const message = untakenMessage(review, clock, stage, now, who);
|
|
193
|
+
const message = untakenMessage(review, clock, stage, now, who, undecided);
|
|
191
194
|
// Replies to the group message Airtable posted for this alarm, so the case reads as one thread.
|
|
192
195
|
const quoted = await readGroupMessageId(clock.emergencyRecordId, review.patientCode);
|
|
193
196
|
if (!await claimStage(review, clock, stage, now)) return false;
|