@peopl-health/nexus 5.49.0-dev.7424 → 5.49.0-dev.7425
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.
|
@@ -121,8 +121,14 @@ const sendEscalationReviewIndicationsController = async (req, res) => {
|
|
|
121
121
|
|
|
122
122
|
const closeEscalationReviewController = async (req, res) => {
|
|
123
123
|
try {
|
|
124
|
-
const { triggeredBy, deliveryConfirmed = false } = req.body || {};
|
|
125
|
-
const { review, caseMirror } = await closeEscalationReview({
|
|
124
|
+
const { triggeredBy, deliveryConfirmed = false, interruptionNotes = null } = req.body || {};
|
|
125
|
+
const { review, caseMirror } = await closeEscalationReview({
|
|
126
|
+
id: req.params.id,
|
|
127
|
+
closedBy: triggeredBy,
|
|
128
|
+
deliveryConfirmed,
|
|
129
|
+
interruptionNotes,
|
|
130
|
+
requestId: req.requestId,
|
|
131
|
+
});
|
|
126
132
|
res.status(200).json({ success: true, review, caseMirror });
|
|
127
133
|
} catch (error) {
|
|
128
134
|
respondWithError(res, error, 'closing escalation review', { reviewId: req.params?.id });
|
|
@@ -133,6 +139,10 @@ const syncEscalationReviewCaseController = async (req, res) => {
|
|
|
133
139
|
try {
|
|
134
140
|
const { triggeredBy } = req.body || {};
|
|
135
141
|
const { review, caseMirror } = await syncEscalationReviewCase({ id: req.params.id, syncedBy: triggeredBy, requestId: req.requestId });
|
|
142
|
+
if (caseMirror === 'failed') {
|
|
143
|
+
res.status(502).json({ success: false, error: 'The patient case could not be updated', review, caseMirror });
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
136
146
|
res.status(200).json({ success: true, review, caseMirror });
|
|
137
147
|
} catch (error) {
|
|
138
148
|
respondWithError(res, error, 'syncing escalation review case', { reviewId: req.params?.id });
|
|
@@ -6,6 +6,7 @@ const { logger } = require('../utils/logger');
|
|
|
6
6
|
|
|
7
7
|
const CALL_OUTCOMES = ['contacted', 'no_answer', 'voicemail'];
|
|
8
8
|
const HANDOFF_MODES = ['sent_whatsapp', 'copied'];
|
|
9
|
+
const INTERRUPTED_STATUSES = ['handing_off', 'sending'];
|
|
9
10
|
const DOCTOR_DECISIONS = ['approved', 'edited_approved', 'needs_intervention', 'returned'];
|
|
10
11
|
const DECISION_SOURCES = ['platform', 'registered_by_navigator'];
|
|
11
12
|
|
|
@@ -57,6 +58,11 @@ const pendingCaseMirrorSchema = new mongoose.Schema({
|
|
|
57
58
|
changes: { type: mongoose.Schema.Types.Mixed, required: true },
|
|
58
59
|
}, { timestamps: { createdAt: true, updatedAt: false } });
|
|
59
60
|
|
|
61
|
+
const interruptionResolutionSchema = new mongoose.Schema({
|
|
62
|
+
fromStatus: { type: String, enum: INTERRUPTED_STATUSES, required: true },
|
|
63
|
+
notes: { type: String, required: true },
|
|
64
|
+
}, { timestamps: { createdAt: true, updatedAt: false } });
|
|
65
|
+
|
|
60
66
|
const escalationReviewSchema = new mongoose.Schema({
|
|
61
67
|
patientCode: { type: String, required: true },
|
|
62
68
|
patientCaseRecordId: { type: String, default: null },
|
|
@@ -86,6 +92,7 @@ const escalationReviewSchema = new mongoose.Schema({
|
|
|
86
92
|
decisions: { type: [decisionSchema], default: [] },
|
|
87
93
|
patientDelivery: { type: patientDeliverySchema, default: null },
|
|
88
94
|
pendingCaseMirrors: { type: [pendingCaseMirrorSchema], default: [] },
|
|
95
|
+
interruptionResolution: { type: interruptionResolutionSchema, default: null },
|
|
89
96
|
closedBy: { type: String, default: null },
|
|
90
97
|
closedAt: { type: Date, default: null },
|
|
91
98
|
}, { timestamps: true });
|
|
@@ -105,6 +112,7 @@ module.exports = {
|
|
|
105
112
|
DECISION_SOURCES,
|
|
106
113
|
DOCTOR_DECISIONS,
|
|
107
114
|
HANDOFF_MODES,
|
|
115
|
+
INTERRUPTED_STATUSES,
|
|
108
116
|
getEscalationReview,
|
|
109
117
|
escalationReviewSchema,
|
|
110
118
|
};
|
|
@@ -9,6 +9,7 @@ const {
|
|
|
9
9
|
DECISION_SOURCES,
|
|
10
10
|
DOCTOR_DECISIONS,
|
|
11
11
|
HANDOFF_MODES,
|
|
12
|
+
INTERRUPTED_STATUSES,
|
|
12
13
|
getEscalationReview,
|
|
13
14
|
} = require('../models/escalationReviewModel');
|
|
14
15
|
const { isWithin24HourWindow } = require('../helpers/conversationWindowHelper');
|
|
@@ -29,6 +30,7 @@ const CLOSABLE_STATUSES = ['indications_queued', 'indications_sent', 'needs_inte
|
|
|
29
30
|
const OUTSIDE_WINDOW_ROUTES = ['template_recovery', 'predefined_template'];
|
|
30
31
|
const MAX_TEMPLATE_TEXT_LENGTH = 800;
|
|
31
32
|
const STATUS_CONFLICT = 'The escalation review does not allow this action in its current status';
|
|
33
|
+
const INTERRUPTION_CONFLICT = 'The escalation review was interrupted; closing it requires interruptionNotes stating what was verified';
|
|
32
34
|
|
|
33
35
|
const patientCodeOf = (code) => {
|
|
34
36
|
const patientCode = ensureWhatsAppFormat(String(code ?? '').trim());
|
|
@@ -95,9 +97,9 @@ async function loadReview(id) {
|
|
|
95
97
|
const releaseReview = (id, from, to) => getEscalationReview().updateOne({ _id: id, status: from }, { $set: { status: to } });
|
|
96
98
|
|
|
97
99
|
async function releaseClosedCase(EscalationReview, review) {
|
|
98
|
-
if (review.status !== 'closed' ||
|
|
100
|
+
if (review.status !== 'closed' || !review.activeCaseKey) return review;
|
|
99
101
|
const released = await EscalationReview.findOneAndUpdate(
|
|
100
|
-
{ _id: review._id, status: 'closed'
|
|
102
|
+
{ _id: review._id, status: 'closed' },
|
|
101
103
|
{ $unset: { activeCaseKey: '' } },
|
|
102
104
|
{ new: true },
|
|
103
105
|
).lean();
|
|
@@ -110,6 +112,22 @@ async function syncCase(review, operator, requestId) {
|
|
|
110
112
|
const pending = review.pendingCaseMirrors || [];
|
|
111
113
|
if (!pending.length) return { review: await releaseClosedCase(EscalationReview, review), caseMirror: 'updated' };
|
|
112
114
|
|
|
115
|
+
const owner = await EscalationReview
|
|
116
|
+
.findOne({ activeCaseKey: activeCaseKeyOf(review.patientCode, review.patientCaseRecordId) }, { _id: 1 })
|
|
117
|
+
.lean();
|
|
118
|
+
if (owner && String(owner._id) !== String(review._id)) {
|
|
119
|
+
logger.warn('[EscalationReview] Dropping case changes a newer review has superseded', {
|
|
120
|
+
escalationReviewId: String(review._id),
|
|
121
|
+
supersededBy: String(owner._id),
|
|
122
|
+
});
|
|
123
|
+
const cleared = await EscalationReview.findOneAndUpdate(
|
|
124
|
+
{ _id: review._id },
|
|
125
|
+
{ $pull: { pendingCaseMirrors: { _id: { $in: pending.map((mirror) => mirror._id) } } } },
|
|
126
|
+
{ new: true },
|
|
127
|
+
).lean();
|
|
128
|
+
return { review: await releaseClosedCase(EscalationReview, cleared || review), caseMirror: 'superseded' };
|
|
129
|
+
}
|
|
130
|
+
|
|
113
131
|
try {
|
|
114
132
|
await updatePatientTask({
|
|
115
133
|
code: review.patientCode,
|
|
@@ -126,7 +144,7 @@ async function syncCase(review, operator, requestId) {
|
|
|
126
144
|
error: redactedError(error),
|
|
127
145
|
stack: redactedStack(error),
|
|
128
146
|
});
|
|
129
|
-
return { review, caseMirror: 'failed' };
|
|
147
|
+
return { review: await releaseClosedCase(EscalationReview, review), caseMirror: 'failed' };
|
|
130
148
|
}
|
|
131
149
|
|
|
132
150
|
try {
|
|
@@ -428,7 +446,7 @@ async function sendApprovedIndications({ id, outsideWindowRoute = null, sentBy,
|
|
|
428
446
|
return syncCase(review, operator, requestId);
|
|
429
447
|
}
|
|
430
448
|
|
|
431
|
-
async function closeEscalationReview({ id, closedBy, deliveryConfirmed = false, requestId = null } = {}) {
|
|
449
|
+
async function closeEscalationReview({ id, closedBy, deliveryConfirmed = false, interruptionNotes = null, requestId = null } = {}) {
|
|
432
450
|
const reviewId = reviewIdOf(id);
|
|
433
451
|
const operator = requiredText(closedBy, 'closedBy');
|
|
434
452
|
|
|
@@ -437,16 +455,21 @@ async function closeEscalationReview({ id, closedBy, deliveryConfirmed = false,
|
|
|
437
455
|
if (queued && deliveryConfirmed !== true) {
|
|
438
456
|
throw httpError(409, 'The indications are still queued; confirm the patient received them before closing');
|
|
439
457
|
}
|
|
458
|
+
const interrupted = INTERRUPTED_STATUSES.includes(current.status);
|
|
459
|
+
const notes = String(interruptionNotes ?? '').trim();
|
|
460
|
+
if (interrupted && !notes) throw httpError(409, INTERRUPTION_CONFLICT);
|
|
440
461
|
|
|
441
462
|
const closure = { status: 'closed', closedBy: operator, closedAt: new Date() };
|
|
442
463
|
if (queued) Object.assign(closure, { 'patientDelivery.confirmedBy': operator, 'patientDelivery.confirmedAt': new Date() });
|
|
464
|
+
if (interrupted) closure.interruptionResolution = { fromStatus: current.status, notes };
|
|
443
465
|
|
|
444
|
-
const review = await updateReview(reviewId, CLOSABLE_STATUSES, withCaseMirror(current, {
|
|
466
|
+
const review = await updateReview(reviewId, interrupted ? [current.status] : CLOSABLE_STATUSES, withCaseMirror(current, {
|
|
445
467
|
$set: closure,
|
|
446
468
|
}, { operationsStatus: 'cerrado' }));
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
469
|
+
const closeChanges = { action: interrupted ? 'closed_from_interruption' : 'closed' };
|
|
470
|
+
if (interrupted) closeChanges.fromStatus = current.status;
|
|
471
|
+
if (queued) closeChanges.deliveryConfirmed = true;
|
|
472
|
+
await audit(review, operator, requestId, closeChanges);
|
|
450
473
|
return syncCase(review, operator, requestId);
|
|
451
474
|
}
|
|
452
475
|
|