@peopl-health/nexus 6.6.1 → 6.7.0-dev.21498
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.
- package/lib/controllers/escalationReviewController.js +18 -0
- package/lib/models/escalationReviewModel.js +8 -0
- package/lib/routes/index.js +2 -0
- package/lib/services/escalationCaseStateService.js +11 -5
- package/lib/services/escalationReviewService.js +82 -4
- package/lib/services/patientTaskService.js +2 -0
- package/lib/services/untakenAlertService.js +9 -6
- package/package.json +1 -1
|
@@ -16,6 +16,7 @@ const {
|
|
|
16
16
|
recordDoctorDecision,
|
|
17
17
|
requestBotDraft,
|
|
18
18
|
sendApprovedIndications,
|
|
19
|
+
setCaseStatuses,
|
|
19
20
|
setReviewCases,
|
|
20
21
|
suggestDraft,
|
|
21
22
|
suggestKeyQuestions,
|
|
@@ -120,6 +121,20 @@ const setEscalationReviewCasesController = async (req, res) => {
|
|
|
120
121
|
}
|
|
121
122
|
};
|
|
122
123
|
|
|
124
|
+
const setEscalationReviewCaseStatusesController = async (req, res) => {
|
|
125
|
+
try {
|
|
126
|
+
const { caseStatuses, triggeredBy } = req.body || {};
|
|
127
|
+
const { review, caseMirror, caseMirrors } = await setCaseStatuses({ id: req.params.id, caseStatuses, updatedBy: triggeredBy, requestId: req.requestId });
|
|
128
|
+
if (caseMirror === 'failed' || caseMirror === 'partial') {
|
|
129
|
+
res.status(502).json({ success: false, error: 'The patient case could not be updated', review: withCaseStatus(review), caseMirror, caseMirrors });
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
res.status(200).json({ success: true, review: withCaseStatus(review), caseMirror, caseMirrors });
|
|
133
|
+
} catch (error) {
|
|
134
|
+
respondWithError(res, error, 'setting escalation review case statuses', { reviewId: req.params?.id });
|
|
135
|
+
}
|
|
136
|
+
};
|
|
137
|
+
|
|
123
138
|
const addEscalationReviewCallLogController = async (req, res) => {
|
|
124
139
|
try {
|
|
125
140
|
const { outcome, notes, calledAt, triggeredBy } = req.body || {};
|
|
@@ -207,6 +222,7 @@ const recordEscalationReviewDecisionController = async (req, res) => {
|
|
|
207
222
|
reason = null,
|
|
208
223
|
draftNumber = null,
|
|
209
224
|
closingCaseIds = null,
|
|
225
|
+
caseStatuses = null,
|
|
210
226
|
triggeredBy,
|
|
211
227
|
} = req.body || {};
|
|
212
228
|
const review = await recordDoctorDecision({
|
|
@@ -223,6 +239,7 @@ const recordEscalationReviewDecisionController = async (req, res) => {
|
|
|
223
239
|
reason,
|
|
224
240
|
draftNumber,
|
|
225
241
|
closingCaseIds,
|
|
242
|
+
caseStatuses,
|
|
226
243
|
recordedBy: triggeredBy,
|
|
227
244
|
requestId: req.requestId,
|
|
228
245
|
});
|
|
@@ -322,6 +339,7 @@ module.exports = {
|
|
|
322
339
|
reclassifyEscalationReviewUrgencyController,
|
|
323
340
|
recordEscalationReviewDecisionController,
|
|
324
341
|
sendEscalationReviewIndicationsController,
|
|
342
|
+
setEscalationReviewCaseStatusesController,
|
|
325
343
|
setEscalationReviewCasesController,
|
|
326
344
|
suggestEscalationReviewDraftController,
|
|
327
345
|
suggestEscalationReviewQuestionsController,
|
|
@@ -291,6 +291,13 @@ const pendingCaseMirrorSchema = new mongoose.Schema({
|
|
|
291
291
|
changes: { type: mongoose.Schema.Types.Mixed, required: true },
|
|
292
292
|
}, { timestamps: { createdAt: true, updatedAt: false } });
|
|
293
293
|
|
|
294
|
+
const doctorCaseStatusSchema = new mongoose.Schema({
|
|
295
|
+
recordId: { type: String, required: true },
|
|
296
|
+
status: { type: String, required: true },
|
|
297
|
+
setBy: { type: String, required: true },
|
|
298
|
+
setAt: { type: Date, required: true },
|
|
299
|
+
}, { _id: false });
|
|
300
|
+
|
|
294
301
|
const interruptionResolutionSchema = new mongoose.Schema({
|
|
295
302
|
fromStatus: { type: String, enum: INTERRUPTED_STATUSES, required: true },
|
|
296
303
|
notes: { type: String, required: true },
|
|
@@ -388,6 +395,7 @@ const escalationReviewSchema = new mongoose.Schema({
|
|
|
388
395
|
patientDelivery: { type: patientDeliverySchema, default: null },
|
|
389
396
|
patientSends: { type: [patientSendSchema], default: undefined },
|
|
390
397
|
pendingCaseMirrors: { type: [pendingCaseMirrorSchema], default: [] },
|
|
398
|
+
doctorCaseStatuses: { type: [doctorCaseStatusSchema], default: [] },
|
|
391
399
|
interruptionResolution: { type: interruptionResolutionSchema, default: null },
|
|
392
400
|
identityBlock: { type: identityBlockSchema, default: null },
|
|
393
401
|
closedBy: { type: String, default: null },
|
package/lib/routes/index.js
CHANGED
|
@@ -131,6 +131,7 @@ const escalationReviewRouteDefinitions = {
|
|
|
131
131
|
'GET /:id': 'getEscalationReviewController',
|
|
132
132
|
'GET /:id/timeline': 'getReviewTimelineController',
|
|
133
133
|
'POST /:id/cases': 'setEscalationReviewCasesController',
|
|
134
|
+
'POST /:id/case-statuses': 'setEscalationReviewCaseStatusesController',
|
|
134
135
|
'POST /:id/call-logs': 'addEscalationReviewCallLogController',
|
|
135
136
|
'POST /:id/draft-suggestions': 'suggestEscalationReviewDraftController',
|
|
136
137
|
'POST /:id/draft-requests': 'requestEscalationReviewDraftController',
|
|
@@ -317,6 +318,7 @@ const builtInControllers = {
|
|
|
317
318
|
getReviewTimelineController: escalationReviewController.getReviewTimelineController,
|
|
318
319
|
previewEscalationReviewPlanController: escalationReviewController.previewEscalationReviewPlanController,
|
|
319
320
|
requestEscalationReviewDraftController: escalationReviewController.requestEscalationReviewDraftController,
|
|
321
|
+
setEscalationReviewCaseStatusesController: escalationReviewController.setEscalationReviewCaseStatusesController,
|
|
320
322
|
setEscalationReviewCasesController: escalationReviewController.setEscalationReviewCasesController,
|
|
321
323
|
addEscalationReviewCallLogController: escalationReviewController.addEscalationReviewCallLogController,
|
|
322
324
|
suggestEscalationReviewDraftController: escalationReviewController.suggestEscalationReviewDraftController,
|
|
@@ -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 });
|
|
@@ -56,7 +56,7 @@ const { ageFrom, birthDateFrom } = require('../fhir/resources/Patient');
|
|
|
56
56
|
const { armCaseFollowUp, cancelCaseFollowUp } = require('./caseFollowUpService');
|
|
57
57
|
|
|
58
58
|
const { getPatientInformation, logPatientChange } = require('./patientService');
|
|
59
|
-
const { MAX_NOTE_LENGTH, openCaseIds, readPatientCases, updatePatientTask } = require('./patientTaskService');
|
|
59
|
+
const { MAX_NOTE_LENGTH, UNTRIAGED_CASE_STATUSES, openCaseIds, readPatientCases, updatePatientTask } = require('./patientTaskService');
|
|
60
60
|
const { countMessages, getMessages } = require('./messageService');
|
|
61
61
|
const { getEscalationApprover, listEscalationApprovers } = require('./physicianService');
|
|
62
62
|
const { createPrescription } = require('./prescriptionService');
|
|
@@ -146,6 +146,48 @@ function closingCaseIdsOf(value) {
|
|
|
146
146
|
return [...new Set(value.map((recordId) => recordId.trim()).filter(Boolean))];
|
|
147
147
|
}
|
|
148
148
|
|
|
149
|
+
// The clinical statuses a doctor closes a case with from the review; ops keep the rest in Pendientes.
|
|
150
|
+
const DOCTOR_CASE_STATUSES = ['En tratamiento', 'En vigilancia', 'Resuelto'];
|
|
151
|
+
|
|
152
|
+
function caseStatusMirrorsOf(value, review) {
|
|
153
|
+
if (value == null) return [];
|
|
154
|
+
if (!Array.isArray(value)) throw httpError(400, 'caseStatuses must be a list of { recordId, status }');
|
|
155
|
+
const covered = new Set(caseIdsOf(review));
|
|
156
|
+
const byCase = new Map();
|
|
157
|
+
for (const entry of value) {
|
|
158
|
+
const recordId = typeof entry?.recordId === 'string' ? entry.recordId.trim() : '';
|
|
159
|
+
if (!covered.has(recordId)) throw httpError(400, `The review does not cover case ${recordId || '(none)'}`);
|
|
160
|
+
if (!DOCTOR_CASE_STATUSES.includes(entry.status)) throw httpError(400, `status must be one of: ${DOCTOR_CASE_STATUSES.join(', ')}`);
|
|
161
|
+
byCase.set(recordId, entry.status);
|
|
162
|
+
}
|
|
163
|
+
return [...byCase].map(([recordId, status]) => ({ recordId, changes: { status } }));
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
const IMPLIED_CASE_STATUS = { monitor: 'En vigilancia', approved: 'En tratamiento', edited_approved: 'En tratamiento', plan_approved: 'En tratamiento' };
|
|
167
|
+
|
|
168
|
+
async function impliedCaseStatusMirrors(review, decision, returnReason, chosen) {
|
|
169
|
+
const status = returnReason === 'no_requiere' ? 'Resuelto' : IMPLIED_CASE_STATUS[decision];
|
|
170
|
+
const chosenIds = new Set(chosen.map(({ recordId }) => recordId));
|
|
171
|
+
const covered = new Set(caseIdsOf(review).filter((recordId) => !chosenIds.has(recordId)));
|
|
172
|
+
if (!status || !covered.size) return [];
|
|
173
|
+
try {
|
|
174
|
+
return (await readPatientCases(review.patientCode))
|
|
175
|
+
.filter((patientCase) => covered.has(patientCase.recordId) && UNTRIAGED_CASE_STATUSES.includes(patientCase.status))
|
|
176
|
+
.map((patientCase) => ({ recordId: patientCase.recordId, changes: { status } }));
|
|
177
|
+
} catch (error) {
|
|
178
|
+
logger.error('[EscalationReview] The cases were read for no implied clinical status', { escalationReviewId: String(review._id), error: redactedError(error) });
|
|
179
|
+
return [];
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
const caseStatusPushOf = (mirrors, setBy) => {
|
|
184
|
+
const setAt = new Date();
|
|
185
|
+
return {
|
|
186
|
+
pendingCaseMirrors: { $each: mirrors },
|
|
187
|
+
doctorCaseStatuses: { $each: mirrors.map(({ recordId, changes }) => ({ recordId, status: changes.status, setBy, setAt })) },
|
|
188
|
+
};
|
|
189
|
+
};
|
|
190
|
+
|
|
149
191
|
const reviewIdOf = (id) => {
|
|
150
192
|
if (!mongoose.Types.ObjectId.isValid(id)) throw httpError(400, 'A valid escalation review id is required');
|
|
151
193
|
return id;
|
|
@@ -942,6 +984,20 @@ async function setReviewCases({ id, patientCaseRecordIds, updatedBy, requestId =
|
|
|
942
984
|
return review;
|
|
943
985
|
}
|
|
944
986
|
|
|
987
|
+
async function setCaseStatuses({ id, caseStatuses, updatedBy, requestId = null } = {}) {
|
|
988
|
+
const reviewId = reviewIdOf(id);
|
|
989
|
+
const operator = requiredText(updatedBy, 'updatedBy');
|
|
990
|
+
const current = await loadReview(reviewId);
|
|
991
|
+
const mirrors = caseStatusMirrorsOf(caseStatuses, current);
|
|
992
|
+
if (!mirrors.length) throw httpError(400, 'caseStatuses must name at least one case');
|
|
993
|
+
const superseded = await supersededCaseIds(getEscalationReview(), current);
|
|
994
|
+
const held = mirrors.find(({ recordId }) => superseded.has(recordId));
|
|
995
|
+
if (held) throw httpError(409, `A newer review holds case ${held.recordId}; set its status there`);
|
|
996
|
+
const review = await updateReview(reviewId, [current.status], { $push: caseStatusPushOf(mirrors, operator) });
|
|
997
|
+
await audit(review, operator, requestId, { action: 'case_statuses_set', caseStatuses: mirrors.map(({ recordId, changes }) => ({ recordId, status: changes.status })) });
|
|
998
|
+
return syncCase(review, operator, requestId);
|
|
999
|
+
}
|
|
1000
|
+
|
|
945
1001
|
async function addCallLog({ id, outcome, notes = null, calledAt = null, loggedBy, requestId = null } = {}) {
|
|
946
1002
|
const reviewId = reviewIdOf(id);
|
|
947
1003
|
const operator = requiredText(loggedBy, 'loggedBy');
|
|
@@ -1131,11 +1187,26 @@ const takingLoweringOf = (review, from) => {
|
|
|
1131
1187
|
};
|
|
1132
1188
|
const takingCallOf = (review, from) => (review.callLogs ?? []).find((call) => call.outcome === 'contacted' && atOrAfterOf(from)(call.calledAt));
|
|
1133
1189
|
|
|
1134
|
-
// What
|
|
1135
|
-
|
|
1190
|
+
// What settles the case itself from `from` on: a decision, a STAT alarm that took it over, or a lowered urgency.
|
|
1191
|
+
function caseSettledReason(review, from, level) {
|
|
1136
1192
|
if (takingDecisionOf(review, from)) return 'decision';
|
|
1137
1193
|
if (level !== 'stat' && levelsAtOf(review)(new Date(8.64e15)).some((entry) => entry.level === 'stat')) return 'stat_alarm';
|
|
1138
1194
|
if (takingLoweringOf(review, from)) return 'reclassified';
|
|
1195
|
+
return null;
|
|
1196
|
+
}
|
|
1197
|
+
|
|
1198
|
+
// What settles a case the doctor has past its deadline: the reasons above, or the patient in hospital, seen in person.
|
|
1199
|
+
async function caseSettledOrHospitalizedReason(review, from, level) {
|
|
1200
|
+
const settled = caseSettledReason(review, from, level);
|
|
1201
|
+
if (settled) return settled;
|
|
1202
|
+
const thread = await getThreadInfo(review.patientCode, { rethrow: true });
|
|
1203
|
+
return thread?.hospitalizedAt ? 'patient_hospitalized' : null;
|
|
1204
|
+
}
|
|
1205
|
+
|
|
1206
|
+
// What says the team took the case from `from` on, for a case timed at `level`.
|
|
1207
|
+
async function caseTakenReason(review, from, level) {
|
|
1208
|
+
const settled = caseSettledReason(review, from, level);
|
|
1209
|
+
if (settled) return settled;
|
|
1139
1210
|
if (takingCallOf(review, from)) return 'call_contacted';
|
|
1140
1211
|
// A read error goes back to the sweep to retry; only a thread that does not exist (no turn to run
|
|
1141
1212
|
// anyway) skips the prompt.
|
|
@@ -1980,6 +2051,7 @@ async function recordDoctorDecision({
|
|
|
1980
2051
|
reason = null,
|
|
1981
2052
|
draftNumber: seenDraftNumber = null,
|
|
1982
2053
|
closingCaseIds = null,
|
|
2054
|
+
caseStatuses = null,
|
|
1983
2055
|
recordedBy,
|
|
1984
2056
|
requestId = null,
|
|
1985
2057
|
} = {}) {
|
|
@@ -2018,6 +2090,7 @@ async function recordDoctorDecision({
|
|
|
2018
2090
|
}
|
|
2019
2091
|
|
|
2020
2092
|
const review = await loadReview(reviewId);
|
|
2093
|
+
const chosenCaseMirrors = caseStatusMirrorsOf(caseStatuses, review);
|
|
2021
2094
|
const isDraftless = DRAFTLESS_DECISIONS.includes(decision);
|
|
2022
2095
|
const allowedStatuses = isDraftless ? [...new Set([...DOCUMENTING_STATUSES, ...DECIDABLE_STATUSES])] : DECIDABLE_STATUSES;
|
|
2023
2096
|
if (!allowedStatuses.includes(review.status)) throw httpError(409, STATUS_CONFLICT);
|
|
@@ -2061,8 +2134,10 @@ async function recordDoctorDecision({
|
|
|
2061
2134
|
}
|
|
2062
2135
|
const warnings = decision === 'edited_approved' ? draftlessDrugWarnings(approvedText, draft) : [];
|
|
2063
2136
|
|
|
2064
|
-
const
|
|
2137
|
+
const caseStatusMirrors = [...chosenCaseMirrors, ...await impliedCaseStatusMirrors(review, decision, returnReason, chosenCaseMirrors)];
|
|
2138
|
+
let updated = await updateReview(reviewId, allowedStatuses, {
|
|
2065
2139
|
$push: {
|
|
2140
|
+
...(caseStatusMirrors.length && caseStatusPushOf(caseStatusMirrors, operator)),
|
|
2066
2141
|
decisions: {
|
|
2067
2142
|
decision,
|
|
2068
2143
|
source,
|
|
@@ -2091,6 +2166,7 @@ async function recordDoctorDecision({
|
|
|
2091
2166
|
const { review: cancelled } = await syncCase(await resumeAssistant(updated, operator, requestId), operator, requestId);
|
|
2092
2167
|
return cancelled;
|
|
2093
2168
|
}
|
|
2169
|
+
if (caseStatusMirrors.length) ({ review: updated } = await syncCase(updated, operator, requestId));
|
|
2094
2170
|
return updated;
|
|
2095
2171
|
}
|
|
2096
2172
|
|
|
@@ -2616,6 +2692,7 @@ module.exports = {
|
|
|
2616
2692
|
addCallLog,
|
|
2617
2693
|
remindDoctorOfCase,
|
|
2618
2694
|
caseTakenBy,
|
|
2695
|
+
caseSettledOrHospitalizedReason,
|
|
2619
2696
|
caseTakenReason,
|
|
2620
2697
|
promptedSince,
|
|
2621
2698
|
currentAlarms,
|
|
@@ -2649,6 +2726,7 @@ module.exports = {
|
|
|
2649
2726
|
recordReviewUpdate,
|
|
2650
2727
|
requestBotDraft,
|
|
2651
2728
|
sendApprovedIndications,
|
|
2729
|
+
setCaseStatuses,
|
|
2652
2730
|
setReviewCases,
|
|
2653
2731
|
suggestDraft,
|
|
2654
2732
|
suggestKeyQuestions,
|
|
@@ -59,6 +59,7 @@ const CASE_STATUSES = [
|
|
|
59
59
|
'radioterapia_revisión',
|
|
60
60
|
'internado',
|
|
61
61
|
];
|
|
62
|
+
const UNTRIAGED_CASE_STATUSES = [null, 'Pendiente', 'Activo', 'En evaluación', 'en revision_medica'];
|
|
62
63
|
// The operations status of a case is its FHIR case Task status, the same vocabulary nexus mirrors.
|
|
63
64
|
const OPERATIONS_STATUSES = [...new Set(Object.values(CASE_STATUS_BY_REVIEW_STATUS).map(({ label }) => label))];
|
|
64
65
|
|
|
@@ -214,6 +215,7 @@ async function updatePatientTask({ code, kind, recordId, changes, updatedBy, sou
|
|
|
214
215
|
|
|
215
216
|
module.exports = {
|
|
216
217
|
MAX_NOTE_LENGTH,
|
|
218
|
+
UNTRIAGED_CASE_STATUSES,
|
|
217
219
|
openCaseIds,
|
|
218
220
|
readPatientCases,
|
|
219
221
|
getPatientTasks,
|
|
@@ -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;
|