@peopl-health/nexus 5.49.0-dev.7474 → 5.49.0-dev.7484
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.
|
@@ -67,6 +67,7 @@ const escalationReviewSchema = new mongoose.Schema({
|
|
|
67
67
|
patientCode: { type: String, required: true },
|
|
68
68
|
patientCaseRecordId: { type: String, default: null },
|
|
69
69
|
activeCaseKey: { type: String },
|
|
70
|
+
operationClaim: { type: String, default: null },
|
|
70
71
|
status: {
|
|
71
72
|
type: String,
|
|
72
73
|
enum: [
|
|
@@ -31,6 +31,11 @@ const OUTSIDE_WINDOW_ROUTES = ['template_recovery', 'predefined_template'];
|
|
|
31
31
|
const MAX_TEMPLATE_TEXT_LENGTH = 800;
|
|
32
32
|
const STATUS_CONFLICT = 'The escalation review does not allow this action in its current status';
|
|
33
33
|
const INTERRUPTION_CONFLICT = 'The escalation review was interrupted; closing it requires interruptionNotes stating what was verified';
|
|
34
|
+
// A send still in flight also sits in handing_off or sending, and the patient-send path reaches
|
|
35
|
+
// Airtable, whose shared client waits up to 300s. Adjudicating before then can close the review
|
|
36
|
+
// under a worker that is about to record a message it really sent.
|
|
37
|
+
const INTERRUPTION_GRACE_MS = 10 * 60 * 1000;
|
|
38
|
+
const INTERRUPTION_TOO_SOON = 'The escalation review may still be sending; wait for it to settle before adjudicating it';
|
|
34
39
|
|
|
35
40
|
const patientCodeOf = (code) => {
|
|
36
41
|
const patientCode = ensureWhatsAppFormat(String(code ?? '').trim());
|
|
@@ -94,7 +99,43 @@ async function loadReview(id) {
|
|
|
94
99
|
return review;
|
|
95
100
|
}
|
|
96
101
|
|
|
97
|
-
const
|
|
102
|
+
const newOperationClaim = () => String(new mongoose.Types.ObjectId());
|
|
103
|
+
|
|
104
|
+
const releaseReview = (id, from, to, claim = null) => getEscalationReview().updateOne(
|
|
105
|
+
{ _id: id, status: from, ...(claim ? { operationClaim: claim } : {}) },
|
|
106
|
+
{ $set: { status: to, operationClaim: null } },
|
|
107
|
+
);
|
|
108
|
+
|
|
109
|
+
// The record of a message that really went out is never gated on status: the operator may have
|
|
110
|
+
// adjudicated the review while the send was still running. Only the status transition is gated,
|
|
111
|
+
// so a closed review keeps its handoff or delivery without being resurrected.
|
|
112
|
+
async function completeOperation(id, claim, fromStatus, recordUpdate, nextStatus) {
|
|
113
|
+
const EscalationReview = getEscalationReview();
|
|
114
|
+
const record = { ...recordUpdate, $set: { ...recordUpdate.$set, operationClaim: null } };
|
|
115
|
+
const settled = await EscalationReview.findOneAndUpdate(
|
|
116
|
+
{ _id: id, status: fromStatus, operationClaim: claim },
|
|
117
|
+
{ ...record, $set: { ...record.$set, status: nextStatus } },
|
|
118
|
+
{ new: true },
|
|
119
|
+
).lean();
|
|
120
|
+
if (settled) return settled;
|
|
121
|
+
|
|
122
|
+
const recorded = await EscalationReview.findOneAndUpdate(
|
|
123
|
+
{ _id: id, operationClaim: claim },
|
|
124
|
+
record,
|
|
125
|
+
{ new: true },
|
|
126
|
+
).lean();
|
|
127
|
+
if (recorded) {
|
|
128
|
+
logger.error('[EscalationReview] The review moved on while sending; the record was kept and the status left alone', {
|
|
129
|
+
escalationReviewId: String(id),
|
|
130
|
+
status: recorded.status,
|
|
131
|
+
expected: fromStatus,
|
|
132
|
+
});
|
|
133
|
+
return recorded;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
if (!await EscalationReview.exists({ _id: id })) throw httpError(404, 'Escalation review not found');
|
|
137
|
+
throw httpError(409, STATUS_CONFLICT);
|
|
138
|
+
}
|
|
98
139
|
|
|
99
140
|
async function releaseClosedCase(EscalationReview, review) {
|
|
100
141
|
if (review.status !== 'closed' || !review.activeCaseKey) return review;
|
|
@@ -326,17 +367,18 @@ async function handOffToDoctor({ id, mode, physicianEmail = null, sentBy, reques
|
|
|
326
367
|
if (!approver) throw httpError(403, 'The physician is not an escalation review approver');
|
|
327
368
|
}
|
|
328
369
|
|
|
329
|
-
|
|
370
|
+
const claim = newOperationClaim();
|
|
371
|
+
await updateReview(reviewId, [review.status], { $set: { status: 'handing_off', operationClaim: claim } }, { drafts: { $size: draftNumber } });
|
|
330
372
|
let delivery = {};
|
|
331
373
|
let updated;
|
|
332
374
|
try {
|
|
333
375
|
if (approver) delivery = { physicianEmail: approver.email, ...(await sendDoctorAlert({ review, draft, approver, operator })) };
|
|
334
|
-
|
|
376
|
+
const recordUpdate = withCaseMirror(review, {
|
|
335
377
|
$push: { doctorHandoffs: { mode, draftNumber, ...delivery, sentBy: operator } },
|
|
336
|
-
|
|
337
|
-
|
|
378
|
+
}, { status: 'en revision_medica' });
|
|
379
|
+
updated = await completeOperation(reviewId, claim, 'handing_off', recordUpdate, 'awaiting_doctor');
|
|
338
380
|
} catch (error) {
|
|
339
|
-
await releaseReview(reviewId, 'handing_off', review.status);
|
|
381
|
+
await releaseReview(reviewId, 'handing_off', review.status, claim);
|
|
340
382
|
throw error;
|
|
341
383
|
}
|
|
342
384
|
await audit(updated, operator, requestId, {
|
|
@@ -407,7 +449,8 @@ async function sendApprovedIndications({ id, outsideWindowRoute = null, sentBy,
|
|
|
407
449
|
throw httpError(400, `outsideWindowRoute must be one of: ${OUTSIDE_WINDOW_ROUTES.join(', ')}`);
|
|
408
450
|
}
|
|
409
451
|
|
|
410
|
-
const
|
|
452
|
+
const claim = newOperationClaim();
|
|
453
|
+
const claimed = await updateReview(reviewId, ['approved'], { $set: { status: 'sending', operationClaim: claim } });
|
|
411
454
|
const decisionNumber = claimed.decisions.length;
|
|
412
455
|
const { finalText } = claimed.decisions[decisionNumber - 1];
|
|
413
456
|
|
|
@@ -425,7 +468,7 @@ async function sendApprovedIndications({ id, outsideWindowRoute = null, sentBy,
|
|
|
425
468
|
});
|
|
426
469
|
throw httpError(502, 'The patient send could not be confirmed; check the chat before sending again');
|
|
427
470
|
}
|
|
428
|
-
await releaseReview(reviewId, 'sending', 'approved');
|
|
471
|
+
await releaseReview(reviewId, 'sending', 'approved', claim);
|
|
429
472
|
throw error;
|
|
430
473
|
}
|
|
431
474
|
|
|
@@ -439,9 +482,10 @@ async function sendApprovedIndications({ id, outsideWindowRoute = null, sentBy,
|
|
|
439
482
|
sentBy: operator,
|
|
440
483
|
};
|
|
441
484
|
const status = deferred ? 'indications_queued' : 'indications_sent';
|
|
442
|
-
const
|
|
443
|
-
$set: {
|
|
444
|
-
}, { recommendation: finalText })
|
|
485
|
+
const recordUpdate = withCaseMirror(claimed, {
|
|
486
|
+
$set: { patientDelivery: delivery },
|
|
487
|
+
}, { recommendation: finalText });
|
|
488
|
+
const review = await completeOperation(reviewId, claim, 'sending', recordUpdate, status);
|
|
445
489
|
await audit(review, operator, requestId, { action: status, decisionNumber, transport: delivery.transport });
|
|
446
490
|
return syncCase(review, operator, requestId);
|
|
447
491
|
}
|
|
@@ -457,15 +501,22 @@ async function closeEscalationReview({ id, closedBy, deliveryConfirmed = false,
|
|
|
457
501
|
}
|
|
458
502
|
const interrupted = INTERRUPTED_STATUSES.includes(current.status);
|
|
459
503
|
const notes = String(interruptionNotes ?? '').trim();
|
|
460
|
-
if (interrupted
|
|
504
|
+
if (interrupted) {
|
|
505
|
+
if (!notes) throw httpError(409, INTERRUPTION_CONFLICT);
|
|
506
|
+
const sinceStatusChanged = Date.now() - new Date(current.updatedAt ?? current.createdAt ?? 0).getTime();
|
|
507
|
+
if (sinceStatusChanged < INTERRUPTION_GRACE_MS) throw httpError(409, INTERRUPTION_TOO_SOON);
|
|
508
|
+
}
|
|
461
509
|
|
|
462
510
|
const closure = { status: 'closed', closedBy: operator, closedAt: new Date() };
|
|
463
511
|
if (queued) Object.assign(closure, { 'patientDelivery.confirmedBy': operator, 'patientDelivery.confirmedAt': new Date() });
|
|
464
512
|
if (interrupted) closure.interruptionResolution = { fromStatus: current.status, notes };
|
|
465
513
|
|
|
466
|
-
const review = await updateReview(
|
|
467
|
-
|
|
468
|
-
|
|
514
|
+
const review = await updateReview(
|
|
515
|
+
reviewId,
|
|
516
|
+
interrupted ? [current.status] : CLOSABLE_STATUSES,
|
|
517
|
+
withCaseMirror(current, { $set: closure }, { operationsStatus: 'cerrado' }),
|
|
518
|
+
interrupted ? { operationClaim: current.operationClaim ?? null } : {},
|
|
519
|
+
);
|
|
469
520
|
const closeChanges = { action: interrupted ? 'closed_from_interruption' : 'closed' };
|
|
470
521
|
if (interrupted) closeChanges.fromStatus = current.status;
|
|
471
522
|
if (queued) closeChanges.deliveryConfirmed = true;
|