@peopl-health/nexus 5.49.0-dev.7421 → 5.49.0-dev.7424
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,8 @@ const sendEscalationReviewIndicationsController = async (req, res) => {
|
|
|
121
121
|
|
|
122
122
|
const closeEscalationReviewController = async (req, res) => {
|
|
123
123
|
try {
|
|
124
|
-
const { triggeredBy } = req.body || {};
|
|
125
|
-
const { review, caseMirror } = await closeEscalationReview({ id: req.params.id, closedBy: triggeredBy, requestId: req.requestId });
|
|
124
|
+
const { triggeredBy, deliveryConfirmed = false } = req.body || {};
|
|
125
|
+
const { review, caseMirror } = await closeEscalationReview({ id: req.params.id, closedBy: triggeredBy, deliveryConfirmed, requestId: req.requestId });
|
|
126
126
|
res.status(200).json({ success: true, review, caseMirror });
|
|
127
127
|
} catch (error) {
|
|
128
128
|
respondWithError(res, error, 'closing escalation review', { reviewId: req.params?.id });
|
|
@@ -321,11 +321,18 @@ class NexusMessaging {
|
|
|
321
321
|
async sendMessage(messageData) {
|
|
322
322
|
const claim = await acquireSendClaim(messageData);
|
|
323
323
|
if (!claim.acquired) {
|
|
324
|
-
return { success: false, messageId: null, status: 'duplicate_suppressed', duplicate: true, delivered: false, deferred: false };
|
|
324
|
+
return { success: false, messageId: null, status: 'duplicate_suppressed', duplicate: true, completed: claim.completed === true, delivered: false, deferred: false };
|
|
325
325
|
}
|
|
326
326
|
|
|
327
327
|
try {
|
|
328
|
-
|
|
328
|
+
const result = await this._sendMessage(messageData);
|
|
329
|
+
if (result?.success === true && result?.deferred !== true && !await completeSendClaim(claim.claimId)) {
|
|
330
|
+
logger.error('[NexusMessaging] send claim completion failed; a retry after the claim expires may send again', {
|
|
331
|
+
code: messageData.code,
|
|
332
|
+
claimId: String(claim.claimId),
|
|
333
|
+
});
|
|
334
|
+
}
|
|
335
|
+
return result;
|
|
329
336
|
} catch (error) {
|
|
330
337
|
await releaseSendClaim(claim.claimId);
|
|
331
338
|
throw error;
|
|
@@ -47,6 +47,9 @@ const patientDeliverySchema = new mongoose.Schema({
|
|
|
47
47
|
messageId: { type: String, default: null },
|
|
48
48
|
status: { type: String, default: null },
|
|
49
49
|
transport: { type: String, enum: ['text', 'template_recovery', 'predefined_template'], required: true },
|
|
50
|
+
deferred: { type: Boolean, default: false },
|
|
51
|
+
confirmedBy: { type: String, default: null },
|
|
52
|
+
confirmedAt: { type: Date, default: null },
|
|
50
53
|
sentBy: { type: String, required: true },
|
|
51
54
|
}, { timestamps: { createdAt: true, updatedAt: false } });
|
|
52
55
|
|
|
@@ -70,6 +73,7 @@ const escalationReviewSchema = new mongoose.Schema({
|
|
|
70
73
|
'needs_intervention',
|
|
71
74
|
'returned',
|
|
72
75
|
'sending',
|
|
76
|
+
'indications_queued',
|
|
73
77
|
'indications_sent',
|
|
74
78
|
'closed',
|
|
75
79
|
],
|
|
@@ -23,8 +23,9 @@ const AUDIT_SOURCE = 'escalation_review';
|
|
|
23
23
|
const RECENT_REVIEW_LIMIT = 20;
|
|
24
24
|
const DUPLICATE_KEY_ERROR = 11000;
|
|
25
25
|
const DOCUMENTING_STATUSES = ['open', 'documenting', 'draft_ready', 'returned'];
|
|
26
|
+
const UNDRAFTED_STATUSES = DOCUMENTING_STATUSES.filter((status) => status !== 'draft_ready');
|
|
26
27
|
const HANDOFF_STATUSES = ['draft_ready', 'awaiting_doctor'];
|
|
27
|
-
const CLOSABLE_STATUSES = ['indications_sent', 'needs_intervention'];
|
|
28
|
+
const CLOSABLE_STATUSES = ['indications_queued', 'indications_sent', 'needs_intervention'];
|
|
28
29
|
const OUTSIDE_WINDOW_ROUTES = ['template_recovery', 'predefined_template'];
|
|
29
30
|
const MAX_TEMPLATE_TEXT_LENGTH = 800;
|
|
30
31
|
const STATUS_CONFLICT = 'The escalation review does not allow this action in its current status';
|
|
@@ -154,7 +155,7 @@ async function sendDoctorAlert({ review, draft, approver, operator }) {
|
|
|
154
155
|
? { code: doctorCode, body: `${draft.doctorText}\n\n${link}`, triggeredBy: operator }
|
|
155
156
|
: { code: doctorCode, contentSid: configured('ESCALATION_REVIEW_DOCTOR_ALERT_CONTENT_SID'), variables: { 1: link }, triggeredBy: operator };
|
|
156
157
|
const result = await sendMessage(message);
|
|
157
|
-
if (!result?.success && result?.
|
|
158
|
+
if (!result?.success && !result?.completed) {
|
|
158
159
|
throw httpError(502, 'The WhatsApp message to the physician could not be sent');
|
|
159
160
|
}
|
|
160
161
|
return { transport: inWindow ? 'text' : 'template', messageId: result?.messageId || null };
|
|
@@ -241,6 +242,27 @@ async function listEscalationReviews({ code } = {}) {
|
|
|
241
242
|
return getEscalationReview().find({ patientCode }).sort({ createdAt: -1 }).limit(RECENT_REVIEW_LIMIT).lean();
|
|
242
243
|
}
|
|
243
244
|
|
|
245
|
+
async function appendKeepingDraft(id, entry) {
|
|
246
|
+
const EscalationReview = getEscalationReview();
|
|
247
|
+
for (let attempt = 0; attempt < 2; attempt += 1) {
|
|
248
|
+
const kept = await EscalationReview
|
|
249
|
+
.findOneAndUpdate({ _id: id, status: 'draft_ready' }, { $push: { callLogs: entry } }, { new: true })
|
|
250
|
+
.lean();
|
|
251
|
+
if (kept) return kept;
|
|
252
|
+
|
|
253
|
+
const moved = await EscalationReview
|
|
254
|
+
.findOneAndUpdate(
|
|
255
|
+
{ _id: id, status: { $in: UNDRAFTED_STATUSES } },
|
|
256
|
+
{ $push: { callLogs: entry }, $set: { status: 'documenting' } },
|
|
257
|
+
{ new: true },
|
|
258
|
+
)
|
|
259
|
+
.lean();
|
|
260
|
+
if (moved) return moved;
|
|
261
|
+
}
|
|
262
|
+
if (!await EscalationReview.exists({ _id: id })) throw httpError(404, 'Escalation review not found');
|
|
263
|
+
throw httpError(409, STATUS_CONFLICT);
|
|
264
|
+
}
|
|
265
|
+
|
|
244
266
|
async function addCallLog({ id, outcome, notes = null, calledAt = null, loggedBy, requestId = null } = {}) {
|
|
245
267
|
const reviewId = reviewIdOf(id);
|
|
246
268
|
const operator = requiredText(loggedBy, 'loggedBy');
|
|
@@ -248,10 +270,10 @@ async function addCallLog({ id, outcome, notes = null, calledAt = null, loggedBy
|
|
|
248
270
|
const callTime = calledAt == null ? new Date() : new Date(calledAt);
|
|
249
271
|
if (Number.isNaN(callTime.getTime())) throw httpError(400, 'calledAt must be a valid date');
|
|
250
272
|
|
|
251
|
-
const
|
|
252
|
-
|
|
253
|
-
$set: { status: 'documenting' }
|
|
254
|
-
|
|
273
|
+
const entry = { outcome, notes: optionalText(notes), calledAt: callTime, loggedBy: operator };
|
|
274
|
+
const review = outcome === 'contacted'
|
|
275
|
+
? await updateReview(reviewId, DOCUMENTING_STATUSES, { $push: { callLogs: entry }, $set: { status: 'documenting' } })
|
|
276
|
+
: await appendKeepingDraft(reviewId, entry);
|
|
255
277
|
await audit(review, operator, requestId, { action: 'call_logged', outcome });
|
|
256
278
|
return review;
|
|
257
279
|
}
|
|
@@ -389,29 +411,42 @@ async function sendApprovedIndications({ id, outsideWindowRoute = null, sentBy,
|
|
|
389
411
|
throw error;
|
|
390
412
|
}
|
|
391
413
|
|
|
414
|
+
const deferred = result.deferred === true;
|
|
392
415
|
const delivery = {
|
|
393
416
|
decisionNumber,
|
|
394
417
|
messageId: result.messageId || null,
|
|
395
418
|
status: result.status || null,
|
|
396
419
|
transport: deliveryTransport(message, result),
|
|
420
|
+
deferred,
|
|
397
421
|
sentBy: operator,
|
|
398
422
|
};
|
|
423
|
+
const status = deferred ? 'indications_queued' : 'indications_sent';
|
|
399
424
|
const review = await updateReview(reviewId, ['sending'], withCaseMirror(claimed, {
|
|
400
|
-
$set: { status
|
|
425
|
+
$set: { status, patientDelivery: delivery },
|
|
401
426
|
}, { recommendation: finalText }));
|
|
402
|
-
await audit(review, operator, requestId, { action:
|
|
427
|
+
await audit(review, operator, requestId, { action: status, decisionNumber, transport: delivery.transport });
|
|
403
428
|
return syncCase(review, operator, requestId);
|
|
404
429
|
}
|
|
405
430
|
|
|
406
|
-
async function closeEscalationReview({ id, closedBy, requestId = null } = {}) {
|
|
431
|
+
async function closeEscalationReview({ id, closedBy, deliveryConfirmed = false, requestId = null } = {}) {
|
|
407
432
|
const reviewId = reviewIdOf(id);
|
|
408
433
|
const operator = requiredText(closedBy, 'closedBy');
|
|
409
434
|
|
|
410
435
|
const current = await loadReview(reviewId);
|
|
436
|
+
const queued = current.status === 'indications_queued';
|
|
437
|
+
if (queued && deliveryConfirmed !== true) {
|
|
438
|
+
throw httpError(409, 'The indications are still queued; confirm the patient received them before closing');
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
const closure = { status: 'closed', closedBy: operator, closedAt: new Date() };
|
|
442
|
+
if (queued) Object.assign(closure, { 'patientDelivery.confirmedBy': operator, 'patientDelivery.confirmedAt': new Date() });
|
|
443
|
+
|
|
411
444
|
const review = await updateReview(reviewId, CLOSABLE_STATUSES, withCaseMirror(current, {
|
|
412
|
-
$set:
|
|
445
|
+
$set: closure,
|
|
413
446
|
}, { operationsStatus: 'cerrado' }));
|
|
414
|
-
await audit(review, operator, requestId,
|
|
447
|
+
await audit(review, operator, requestId, queued
|
|
448
|
+
? { action: 'closed', deliveryConfirmed: true }
|
|
449
|
+
: { action: 'closed' });
|
|
415
450
|
return syncCase(review, operator, requestId);
|
|
416
451
|
}
|
|
417
452
|
|