@peopl-health/nexus 5.56.0-dev.8579 → 5.56.0-dev.8580
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/clinical/services/clinicalAirtableService.js +25 -6
- package/lib/clinical/services/safetyNetPledgeService.js +42 -8
- package/lib/clinical/tools/reportMedicalEscalationTool.js +3 -2
- package/lib/clinical/tools/reportResultsReceivedTool.js +3 -2
- package/lib/clinical/tools/reportUnresolvedRequestv2Tool.js +3 -2
- package/lib/controllers/escalationReviewController.js +2 -7
- package/lib/models/escalationReviewModel.js +1 -4
- package/lib/models/safetyNetPledgeModel.js +4 -1
- package/lib/services/escalationReviewService.js +6 -36
- package/package.json +1 -1
|
@@ -7,7 +7,7 @@ const runtimeConfig = require('../../config/runtimeConfig');
|
|
|
7
7
|
const airtableService = require('../../services/airtableService');
|
|
8
8
|
const { logger } = require('../../utils/logger');
|
|
9
9
|
const { fieldEquals, recordIdIn } = require('../../helpers/airtableFormulaHelper');
|
|
10
|
-
const { CLOSED_REQUEST_STATUSES } = require('../../helpers/patientTaskRulesHelper');
|
|
10
|
+
const { CLOSED_REQUEST_STATUSES, needsRequestAttention } = require('../../helpers/patientTaskRulesHelper');
|
|
11
11
|
const { phoneDigits } = require('../../helpers/whatsappHelper');
|
|
12
12
|
const { WriteClaimStore } = require('./writeClaimStore');
|
|
13
13
|
|
|
@@ -96,13 +96,16 @@ async function findIntakePatientRecordId(code) {
|
|
|
96
96
|
}
|
|
97
97
|
|
|
98
98
|
async function writeUnresolvedRequest({ code, trace, question, details, resolutionAttempted = false, forDoctorOffice = false, category = null, urgency = null, replacesClosedTask = false }) {
|
|
99
|
-
const fingerprint = createHash('sha1')
|
|
99
|
+
const fingerprint = createHash('sha1')
|
|
100
|
+
.update(JSON.stringify([code, category || null, question || null, urgency || null, Boolean(forDoctorOffice)]))
|
|
101
|
+
.digest('hex');
|
|
100
102
|
let recordId = null;
|
|
103
|
+
const duplicate = !replacesClosedTask && await recentUnresolvedCache.get(code) === fingerprint;
|
|
101
104
|
const written = await auditedWrite({
|
|
102
105
|
trace,
|
|
103
106
|
code,
|
|
104
107
|
table: 'unresolved_questions',
|
|
105
|
-
skip:
|
|
108
|
+
skip: duplicate ? 'duplicate' : null,
|
|
106
109
|
write: async () => {
|
|
107
110
|
const patientId = await findMonitoreoPatientRecordId(code);
|
|
108
111
|
const fields = {
|
|
@@ -121,7 +124,7 @@ async function writeUnresolvedRequest({ code, trace, question, details, resoluti
|
|
|
121
124
|
},
|
|
122
125
|
});
|
|
123
126
|
if (written) await recentUnresolvedCache.set(code, fingerprint);
|
|
124
|
-
return { written, recordId };
|
|
127
|
+
return { written, recordId, duplicate };
|
|
125
128
|
}
|
|
126
129
|
|
|
127
130
|
async function recordUnresolvedRequest(request) {
|
|
@@ -134,7 +137,7 @@ function conversationLink(code) {
|
|
|
134
137
|
return base && digits ? `${base}/c/${digits}` : null;
|
|
135
138
|
}
|
|
136
139
|
|
|
137
|
-
function
|
|
140
|
+
function formatOpsAlert(code, { label, summary }) {
|
|
138
141
|
const digits = phoneDigits(code);
|
|
139
142
|
const link = conversationLink(code);
|
|
140
143
|
return `${label}${digits ? ` (+${digits})` : ''}: ${summary}.${link ? ` Revisar: ${link}` : ''}`;
|
|
@@ -160,6 +163,19 @@ async function addToOpenAlert(code, open, question) {
|
|
|
160
163
|
return { handled: !written || matched, written: written && matched };
|
|
161
164
|
}
|
|
162
165
|
|
|
166
|
+
async function isTaskAttended(recordId) {
|
|
167
|
+
if (!RECORD_ID_RX.test(recordId || '')) return null;
|
|
168
|
+
try {
|
|
169
|
+
const [task] = await airtableService.getRecordByFilter(Monitoreo_ID, 'unresolved_questions', recordIdIn([recordId]), null, ['status']);
|
|
170
|
+
if (!task) return true;
|
|
171
|
+
const status = String(task.status || '').trim();
|
|
172
|
+
return status !== '' && !needsRequestAttention(status);
|
|
173
|
+
} catch (error) {
|
|
174
|
+
logger.warn('[clinicalAirtableService] Could not read whether the ops task was attended', { recordId, error: error.message });
|
|
175
|
+
return null;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
163
179
|
async function recordPausedThreadMessage({ code, text, alert = OPS_ALERTS.paused }) {
|
|
164
180
|
const question = text || 'Envió un archivo sin texto.';
|
|
165
181
|
const open = await recentPausedAlertCache.get(code);
|
|
@@ -174,7 +190,7 @@ async function recordPausedThreadMessage({ code, text, alert = OPS_ALERTS.paused
|
|
|
174
190
|
code,
|
|
175
191
|
trace: null,
|
|
176
192
|
question,
|
|
177
|
-
details:
|
|
193
|
+
details: formatOpsAlert(code, alert),
|
|
178
194
|
category: 'contact_team_member',
|
|
179
195
|
urgency: 'soon',
|
|
180
196
|
replacesClosedTask,
|
|
@@ -558,7 +574,10 @@ async function recordClusterRecommendationsMirror({ code, triageRecordId, step1
|
|
|
558
574
|
|
|
559
575
|
module.exports = {
|
|
560
576
|
OPS_ALERTS,
|
|
577
|
+
formatOpsAlert,
|
|
578
|
+
isTaskAttended,
|
|
561
579
|
recordUnresolvedRequest,
|
|
580
|
+
writeUnresolvedRequest,
|
|
562
581
|
recordPausedThreadMessage,
|
|
563
582
|
recordEmergency,
|
|
564
583
|
recordClinicalIntake,
|
|
@@ -10,11 +10,25 @@ const { SafetyNetPledge } = require('../../models/safetyNetPledgeModel');
|
|
|
10
10
|
|
|
11
11
|
const { getMessages } = require('../../services/messageService');
|
|
12
12
|
|
|
13
|
-
const { recordUnresolvedRequest } = require('./clinicalAirtableService');
|
|
13
|
+
const { formatOpsAlert, isTaskAttended, recordUnresolvedRequest } = require('./clinicalAirtableService');
|
|
14
14
|
|
|
15
15
|
const SWEEP_LIMIT = 200;
|
|
16
16
|
const BREACH_CATEGORY = 'contact_team_member';
|
|
17
17
|
const BREACH_QUESTION = 'El paciente sigue esperando el contacto que se le prometió';
|
|
18
|
+
const ONBOARDING_BREACH_QUESTION = 'El contacto sigue esperando la llamada de bienvenida';
|
|
19
|
+
|
|
20
|
+
const BREACHES = {
|
|
21
|
+
safety_net: {
|
|
22
|
+
question: BREACH_QUESTION,
|
|
23
|
+
label: 'Contacto prometido sin cumplir',
|
|
24
|
+
summary: (waitedMinutes) => `pidió que lo contactaran y lleva ${waitedMinutes} minutos sin respuesta`,
|
|
25
|
+
},
|
|
26
|
+
onboarding: {
|
|
27
|
+
question: ONBOARDING_BREACH_QUESTION,
|
|
28
|
+
label: 'Llamada de bienvenida sin hacer',
|
|
29
|
+
summary: (waitedMinutes) => `pidió la llamada de bienvenida y lleva ${waitedMinutes} minutos sin que nadie lo contacte`,
|
|
30
|
+
},
|
|
31
|
+
};
|
|
18
32
|
|
|
19
33
|
const DELIVERY_GRACE_MS = 10 * 60 * 1000;
|
|
20
34
|
const HOUR_MS = 60 * 60 * 1000;
|
|
@@ -40,21 +54,28 @@ function pledgeDueAt(pledgedAt) {
|
|
|
40
54
|
return { dueAt: addContactHours(pledgedAt, DEFAULT_PLEDGE_MS / HOUR_MS, CONTACT_WINDOW) };
|
|
41
55
|
}
|
|
42
56
|
|
|
43
|
-
async function openPledge({ code }) {
|
|
57
|
+
async function openPledge({ code, kind = 'safety_net', taskRecordId = null }) {
|
|
44
58
|
const pledgedAt = new Date();
|
|
45
59
|
const { dueAt } = pledgeDueAt(pledgedAt);
|
|
46
|
-
const pledge = await SafetyNetPledge.create({
|
|
47
|
-
|
|
60
|
+
const pledge = await SafetyNetPledge.create({
|
|
61
|
+
code,
|
|
62
|
+
pledgedAt,
|
|
63
|
+
dueAt,
|
|
64
|
+
...(kind === 'safety_net' ? {} : { kind }),
|
|
65
|
+
...(taskRecordId ? { taskRecordId } : {}),
|
|
66
|
+
});
|
|
67
|
+
logger.info('[SafetyNetPledge] Contact pledged', { code, kind, dueAt });
|
|
48
68
|
return { pledgeId: pledge._id, dueAt };
|
|
49
69
|
}
|
|
50
70
|
|
|
51
71
|
async function reportBreach(pledge) {
|
|
52
72
|
const waitedMinutes = Math.round((Date.now() - new Date(pledge.pledgedAt).getTime()) / 60000);
|
|
73
|
+
const breach = BREACHES[pledge.kind] || BREACHES.safety_net;
|
|
53
74
|
return recordUnresolvedRequest({
|
|
54
75
|
code: pledge.code,
|
|
55
76
|
trace: null,
|
|
56
|
-
question:
|
|
57
|
-
details:
|
|
77
|
+
question: breach.question,
|
|
78
|
+
details: formatOpsAlert(pledge.code, { label: breach.label, summary: breach.summary(waitedMinutes) }),
|
|
58
79
|
category: BREACH_CATEGORY,
|
|
59
80
|
urgency: 'urgent'
|
|
60
81
|
});
|
|
@@ -83,7 +104,7 @@ async function sweepSafetyNetPledges() {
|
|
|
83
104
|
state: { $in: ['pending', 'reporting'] },
|
|
84
105
|
dueAt: { $lte: new Date() },
|
|
85
106
|
})
|
|
86
|
-
.select({ _id: 1, code: 1, pledgedAt: 1 })
|
|
107
|
+
.select({ _id: 1, code: 1, kind: 1, taskRecordId: 1, pledgedAt: 1 })
|
|
87
108
|
.limit(SWEEP_LIMIT)
|
|
88
109
|
.lean();
|
|
89
110
|
|
|
@@ -103,6 +124,18 @@ async function sweepSafetyNetPledges() {
|
|
|
103
124
|
continue;
|
|
104
125
|
}
|
|
105
126
|
|
|
127
|
+
if (pledge.kind === 'onboarding' && pledge.taskRecordId) {
|
|
128
|
+
const attended = await isTaskAttended(pledge.taskRecordId);
|
|
129
|
+
if (attended) {
|
|
130
|
+
await settle(pledge._id, 'reporting', 'met');
|
|
131
|
+
continue;
|
|
132
|
+
}
|
|
133
|
+
if (attended === null) {
|
|
134
|
+
await returnToPending(pledge._id);
|
|
135
|
+
continue;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
106
139
|
if (!await reportBreach(pledge)) {
|
|
107
140
|
await returnToPending(pledge._id);
|
|
108
141
|
logger.error('[SafetyNetPledge] Breach was not recorded, pledge returns to pending for the next sweep', { code: pledge.code });
|
|
@@ -135,5 +168,6 @@ module.exports = {
|
|
|
135
168
|
openPledge,
|
|
136
169
|
pledgeDueAt,
|
|
137
170
|
sweepSafetyNetPledges,
|
|
138
|
-
BREACH_QUESTION
|
|
171
|
+
BREACH_QUESTION,
|
|
172
|
+
ONBOARDING_BREACH_QUESTION
|
|
139
173
|
};
|
|
@@ -3,7 +3,7 @@ const { auditReasoningSchema } = require('../helpers/auditReasoningHelper');
|
|
|
3
3
|
const { dispatcherService } = require('../services/dispatcherService');
|
|
4
4
|
const { armEscalationNet } = require('../services/contingencyDispatchService');
|
|
5
5
|
const { evidenceQuoteAnchoring } = require('../helpers/evidenceAnchorHelper');
|
|
6
|
-
const { recordEmergency,
|
|
6
|
+
const { recordEmergency, writeUnresolvedRequest } = require('../services/clinicalAirtableService');
|
|
7
7
|
const { objectArg, safeParse, isPlainObject } = require('../../utils/jsonUtils');
|
|
8
8
|
const { logger } = require('../../utils/logger');
|
|
9
9
|
|
|
@@ -219,7 +219,7 @@ async function handler(args, context = {}) {
|
|
|
219
219
|
|
|
220
220
|
if (runtime.patientCode) {
|
|
221
221
|
if (urgencyLevel === 'routine') {
|
|
222
|
-
|
|
222
|
+
const { written, duplicate } = await writeUnresolvedRequest({
|
|
223
223
|
code: runtime.patientCode,
|
|
224
224
|
trace,
|
|
225
225
|
question: details.symptom || '',
|
|
@@ -227,6 +227,7 @@ async function handler(args, context = {}) {
|
|
|
227
227
|
category: 'medical_routine',
|
|
228
228
|
urgency: 'routine',
|
|
229
229
|
});
|
|
230
|
+
queued = written || duplicate;
|
|
230
231
|
} else {
|
|
231
232
|
queued = await recordEmergency({
|
|
232
233
|
code: runtime.patientCode,
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
const { DEGRADED_NUMERIC_REASON, DEGRADED_SUPPRESSED_REASON, VERBATIM_SOURCES, patientVerbatimHasNumericValues, resolveVerbatimSource, slugify, stampProvenance } = require('../helpers/resultsHelper');
|
|
2
|
-
const {
|
|
2
|
+
const { writeUnresolvedRequest } = require('../services/clinicalAirtableService');
|
|
3
3
|
const { storeResultObservations } = require('../../fhir');
|
|
4
4
|
const { logger } = require('../../utils/logger');
|
|
5
5
|
|
|
@@ -120,7 +120,7 @@ async function handler(args = {}, context = {}) {
|
|
|
120
120
|
logger.warn('[reportResultsReceived] no trace on runtime context; results-received signal not recorded', { turnId: runtime.turnId });
|
|
121
121
|
}
|
|
122
122
|
|
|
123
|
-
const
|
|
123
|
+
const { written, duplicate } = await writeUnresolvedRequest({
|
|
124
124
|
code: runtime.patientCode,
|
|
125
125
|
trace,
|
|
126
126
|
question: ['Resultados recibidos:', resultType, studyName].filter(Boolean).join(' '),
|
|
@@ -128,6 +128,7 @@ async function handler(args = {}, context = {}) {
|
|
|
128
128
|
category: 'results_review',
|
|
129
129
|
urgency: 'soon',
|
|
130
130
|
});
|
|
131
|
+
const queued = written || duplicate;
|
|
131
132
|
|
|
132
133
|
const data = { result_id: resultId, result_slug: resultSlug };
|
|
133
134
|
if (resultType !== rawType) data.result_type = resultType;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
const { auditReasoningSchema } = require('../helpers/auditReasoningHelper');
|
|
2
|
-
const {
|
|
2
|
+
const { writeUnresolvedRequest } = require('../services/clinicalAirtableService');
|
|
3
3
|
const { logger } = require('../../utils/logger');
|
|
4
4
|
|
|
5
5
|
const definition = {
|
|
@@ -111,7 +111,7 @@ async function handler(args = {}, context = {}) {
|
|
|
111
111
|
const patientCode = context?.toolRuntimeContext?.patientCode;
|
|
112
112
|
let isQueued = false;
|
|
113
113
|
if (patientCode) {
|
|
114
|
-
|
|
114
|
+
const { written, duplicate } = await writeUnresolvedRequest({
|
|
115
115
|
code: patientCode,
|
|
116
116
|
trace,
|
|
117
117
|
question,
|
|
@@ -121,6 +121,7 @@ async function handler(args = {}, context = {}) {
|
|
|
121
121
|
category,
|
|
122
122
|
urgency,
|
|
123
123
|
});
|
|
124
|
+
isQueued = written || duplicate;
|
|
124
125
|
}
|
|
125
126
|
|
|
126
127
|
if (isQueued) {
|
|
@@ -167,13 +167,8 @@ const closeEscalationReviewController = async (req, res) => {
|
|
|
167
167
|
const cancelEscalationReviewController = async (req, res) => {
|
|
168
168
|
try {
|
|
169
169
|
const { triggeredBy, reason = null } = req.body || {};
|
|
170
|
-
const {
|
|
171
|
-
|
|
172
|
-
cancelledBy: triggeredBy,
|
|
173
|
-
reason,
|
|
174
|
-
requestId: req.requestId,
|
|
175
|
-
});
|
|
176
|
-
res.status(200).json({ success: true, review, caseMirror, caseMirrors });
|
|
170
|
+
const review = await cancelEscalationReview({ id: req.params.id, cancelledBy: triggeredBy, reason, requestId: req.requestId });
|
|
171
|
+
res.status(200).json({ success: true, review });
|
|
177
172
|
} catch (error) {
|
|
178
173
|
respondWithError(res, error, 'cancelling escalation review', { reviewId: req.params?.id });
|
|
179
174
|
}
|
|
@@ -7,8 +7,7 @@ const { logger } = require('../utils/logger');
|
|
|
7
7
|
const CALL_OUTCOMES = ['contacted', 'no_answer', 'voicemail'];
|
|
8
8
|
const HANDOFF_MODES = ['sent_whatsapp', 'copied'];
|
|
9
9
|
const INTERRUPTED_STATUSES = ['handing_off', 'sending'];
|
|
10
|
-
const
|
|
11
|
-
const CANCELLABLE_STATUSES = ['open', 'documenting', 'draft_ready', ...CANCELLABLE_AFTER_HANDOFF_STATUSES];
|
|
10
|
+
const CANCELLABLE_STATUSES = ['open', 'documenting', 'draft_ready'];
|
|
12
11
|
const DOCTOR_DECISIONS = ['approved', 'edited_approved', 'needs_intervention', 'returned'];
|
|
13
12
|
const DECISION_SOURCES = ['platform', 'registered_by_navigator'];
|
|
14
13
|
|
|
@@ -100,7 +99,6 @@ const escalationReviewSchema = new mongoose.Schema({
|
|
|
100
99
|
interruptionResolution: { type: interruptionResolutionSchema, default: null },
|
|
101
100
|
closedBy: { type: String, default: null },
|
|
102
101
|
closedAt: { type: Date, default: null },
|
|
103
|
-
cancellationReason: { type: String, default: null },
|
|
104
102
|
}, { timestamps: true });
|
|
105
103
|
|
|
106
104
|
escalationReviewSchema.index({ patientCode: 1, createdAt: -1 });
|
|
@@ -115,7 +113,6 @@ const getEscalationReview = () => {
|
|
|
115
113
|
|
|
116
114
|
module.exports = {
|
|
117
115
|
CALL_OUTCOMES,
|
|
118
|
-
CANCELLABLE_AFTER_HANDOFF_STATUSES,
|
|
119
116
|
CANCELLABLE_STATUSES,
|
|
120
117
|
DECISION_SOURCES,
|
|
121
118
|
DOCTOR_DECISIONS,
|
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
const mongoose = require('mongoose');
|
|
2
2
|
|
|
3
3
|
const PLEDGE_STATES = ['pending', 'reporting', 'met', 'breached'];
|
|
4
|
+
const PLEDGE_KINDS = ['safety_net', 'onboarding'];
|
|
4
5
|
|
|
5
6
|
const safetyNetPledgeSchema = new mongoose.Schema({
|
|
6
7
|
code: { type: String, required: true },
|
|
8
|
+
kind: { type: String, enum: PLEDGE_KINDS, default: 'safety_net' },
|
|
9
|
+
taskRecordId: { type: String, default: null },
|
|
7
10
|
pledgedAt: { type: Date, required: true },
|
|
8
11
|
dueAt: { type: Date, required: true },
|
|
9
12
|
state: { type: String, enum: PLEDGE_STATES, default: 'pending' },
|
|
@@ -16,4 +19,4 @@ safetyNetPledgeSchema.index({ state: 1, dueAt: 1 }, { name: 'safety_net_pledge_d
|
|
|
16
19
|
|
|
17
20
|
const SafetyNetPledge = mongoose.model('SafetyNetPledge', safetyNetPledgeSchema);
|
|
18
21
|
|
|
19
|
-
module.exports = { SafetyNetPledge, PLEDGE_STATES };
|
|
22
|
+
module.exports = { SafetyNetPledge, PLEDGE_STATES, PLEDGE_KINDS };
|
|
@@ -6,7 +6,6 @@ const { logger } = require('../utils/logger');
|
|
|
6
6
|
const { PATIENT_CODE, redactDirectIdentifiers } = require('../utils/sanitizerUtils');
|
|
7
7
|
const {
|
|
8
8
|
CALL_OUTCOMES,
|
|
9
|
-
CANCELLABLE_AFTER_HANDOFF_STATUSES,
|
|
10
9
|
CANCELLABLE_STATUSES,
|
|
11
10
|
DECISION_SOURCES,
|
|
12
11
|
DOCTOR_DECISIONS,
|
|
@@ -33,7 +32,6 @@ const CLOSABLE_STATUSES = ['indications_queued', 'indications_sent', 'needs_inte
|
|
|
33
32
|
const OUTSIDE_WINDOW_ROUTES = ['template_recovery', 'predefined_template'];
|
|
34
33
|
const MAX_TEMPLATE_TEXT_LENGTH = 800;
|
|
35
34
|
const STATUS_CONFLICT = 'The escalation review does not allow this action in its current status';
|
|
36
|
-
const CANCEL_NEEDS_REASON = 'The doctor is already waiting on this review; cancelling it requires a reason';
|
|
37
35
|
const INTERRUPTION_CONFLICT = 'The escalation review was interrupted; closing it requires interruptionNotes stating what was verified';
|
|
38
36
|
// A send still in flight also sits in handing_off or sending, and the patient-send path reaches
|
|
39
37
|
// Airtable, whose shared client waits up to 300s. Adjudicating before then can close the review
|
|
@@ -176,6 +174,7 @@ async function syncCase(review, operator, requestId) {
|
|
|
176
174
|
const pending = review.pendingCaseMirrors || [];
|
|
177
175
|
if (!pending.length) return { review, caseMirror: 'updated', caseMirrors: [] };
|
|
178
176
|
|
|
177
|
+
const superseded = await supersededCaseIds(EscalationReview, review);
|
|
179
178
|
const covered = new Set(recordIds);
|
|
180
179
|
const byRecord = new Map();
|
|
181
180
|
const abandoned = [];
|
|
@@ -196,19 +195,6 @@ async function syncCase(review, operator, requestId) {
|
|
|
196
195
|
byRecord.set(recordId, entries);
|
|
197
196
|
}
|
|
198
197
|
|
|
199
|
-
let superseded;
|
|
200
|
-
try {
|
|
201
|
-
superseded = await supersededCaseIds(EscalationReview, review);
|
|
202
|
-
} catch (error) {
|
|
203
|
-
logger.error('[EscalationReview] Could not check whether a newer review supersedes these cases', {
|
|
204
|
-
escalationReviewId: String(review._id),
|
|
205
|
-
error: redactedError(error),
|
|
206
|
-
stack: redactedStack(error),
|
|
207
|
-
});
|
|
208
|
-
const outcomes = [...byRecord.keys()].map((recordId) => ({ recordId, status: 'failed' }));
|
|
209
|
-
return { review, caseMirror: summarise(outcomes), caseMirrors: outcomes };
|
|
210
|
-
}
|
|
211
|
-
|
|
212
198
|
const applied = await Promise.all([...byRecord].map(async ([recordId, entries]) => {
|
|
213
199
|
const ids = entries.map((entry) => entry._id);
|
|
214
200
|
if (superseded.has(recordId)) {
|
|
@@ -717,27 +703,11 @@ async function closeEscalationReview({ id, closedBy, deliveryConfirmed = false,
|
|
|
717
703
|
async function cancelEscalationReview({ id, cancelledBy, reason = null, requestId = null } = {}) {
|
|
718
704
|
const reviewId = reviewIdOf(id);
|
|
719
705
|
const operator = requiredText(cancelledBy, 'cancelledBy');
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
: CANCELLABLE_STATUSES.filter((status) => !CANCELLABLE_AFTER_HANDOFF_STATUSES.includes(status));
|
|
726
|
-
|
|
727
|
-
let review;
|
|
728
|
-
try {
|
|
729
|
-
review = await updateReview(reviewId, allowed, {
|
|
730
|
-
$set: { status: 'cancelled', closedBy: operator, closedAt: new Date(), cancellationReason: notes },
|
|
731
|
-
});
|
|
732
|
-
} catch (error) {
|
|
733
|
-
if (error?.statusCode !== 409 || notes) throw error;
|
|
734
|
-
const current = await loadReview(reviewId);
|
|
735
|
-
if (CANCELLABLE_AFTER_HANDOFF_STATUSES.includes(current.status)) throw httpError(409, CANCEL_NEEDS_REASON);
|
|
736
|
-
throw error;
|
|
737
|
-
}
|
|
738
|
-
|
|
739
|
-
await audit(review, operator, requestId, { action: 'cancelled', reason: notes });
|
|
740
|
-
return syncCase(review, operator, requestId);
|
|
706
|
+
const review = await updateReview(reviewId, CANCELLABLE_STATUSES, {
|
|
707
|
+
$set: { status: 'cancelled', closedBy: operator, closedAt: new Date() },
|
|
708
|
+
});
|
|
709
|
+
await audit(review, operator, requestId, { action: 'cancelled', reason: optionalText(reason) });
|
|
710
|
+
return review;
|
|
741
711
|
}
|
|
742
712
|
|
|
743
713
|
async function syncEscalationReviewCase({ id, syncedBy, requestId = null } = {}) {
|