@peopl-health/nexus 5.57.0-dev.9177 → 5.57.0-dev.9184
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.
|
@@ -21,6 +21,7 @@ const noteFactSchema = new mongoose.Schema({
|
|
|
21
21
|
quote: { type: String, default: null },
|
|
22
22
|
callLogNumber: { type: Number, required: true },
|
|
23
23
|
state: { type: String, enum: ['proposed', 'settling', 'confirmed', 'declined'], default: 'proposed' },
|
|
24
|
+
settlingAt: { type: Date, default: null },
|
|
24
25
|
settledBy: { type: String, default: null },
|
|
25
26
|
}, { timestamps: { createdAt: true, updatedAt: false } });
|
|
26
27
|
|
|
@@ -40,9 +40,6 @@ const MAX_TEMPLATE_TEXT_LENGTH = 800;
|
|
|
40
40
|
const STATUS_CONFLICT = 'The escalation review does not allow this action in its current status';
|
|
41
41
|
const CANCEL_NEEDS_REASON = 'The doctor is already waiting on this review; cancelling it requires a reason';
|
|
42
42
|
const INTERRUPTION_CONFLICT = 'The escalation review was interrupted; closing it requires interruptionNotes stating what was verified';
|
|
43
|
-
// A send still in flight also sits in handing_off or sending, and the patient-send path reaches
|
|
44
|
-
// Airtable, whose shared client waits up to 300s. Adjudicating before then can close the review
|
|
45
|
-
// under a worker that is about to record a message it really sent.
|
|
46
43
|
const INTERRUPTION_GRACE_MS = 10 * 60 * 1000;
|
|
47
44
|
const INTERRUPTION_TOO_SOON = 'The escalation review may still be sending; wait for it to settle before adjudicating it';
|
|
48
45
|
|
|
@@ -162,9 +159,6 @@ const releaseReview = (id, from, to, claim = null) => getEscalationReview().upda
|
|
|
162
159
|
{ $set: { status: to, operationClaim: null } },
|
|
163
160
|
);
|
|
164
161
|
|
|
165
|
-
// The record of a message that really went out is never gated on status: the operator may have
|
|
166
|
-
// adjudicated the review while the send was still running. Only the status transition is gated,
|
|
167
|
-
// so a closed review keeps its handoff or delivery without being resurrected.
|
|
168
162
|
async function completeOperation(id, claim, fromStatus, recordUpdate, nextStatus, lateUpdate = recordUpdate) {
|
|
169
163
|
const EscalationReview = getEscalationReview();
|
|
170
164
|
const claimCleared = (update) => ({ ...update, $set: { ...update.$set, operationClaim: null } });
|
|
@@ -630,6 +624,18 @@ async function proposeNoteFacts(review, operator, requestId) {
|
|
|
630
624
|
}
|
|
631
625
|
}
|
|
632
626
|
|
|
627
|
+
// A process that dies between the claim and the settlement leaves its facts in 'settling' with
|
|
628
|
+
// nothing left to move them. After this long they are offered again: re-storing an assertion the
|
|
629
|
+
// record already holds merges onto the same claim rather than adding a second one.
|
|
630
|
+
const SETTLING_TTL_MS = 10 * 60 * 1000;
|
|
631
|
+
|
|
632
|
+
function settleable(fact, now) {
|
|
633
|
+
if (fact.state === 'proposed') return true;
|
|
634
|
+
if (fact.state !== 'settling') return false;
|
|
635
|
+
const since = Date.parse(fact.settlingAt ?? '');
|
|
636
|
+
return !Number.isFinite(since) || now.getTime() - since >= SETTLING_TTL_MS;
|
|
637
|
+
}
|
|
638
|
+
|
|
633
639
|
function factIndexes(values, field) {
|
|
634
640
|
if (!Array.isArray(values)) throw httpError(400, `${field} must be an array`);
|
|
635
641
|
const indexes = new Set();
|
|
@@ -652,18 +658,20 @@ async function confirmNoteFacts({ id, accepted = [], declined = [], confirmedBy,
|
|
|
652
658
|
const review = await loadReview(reviewId);
|
|
653
659
|
if (!DOCUMENTING_STATUSES.includes(review.status)) throw httpError(409, STATUS_CONFLICT);
|
|
654
660
|
const proposed = (review.noteFacts ?? []).map((fact, index) => ({ fact, index }))
|
|
655
|
-
.filter(({ fact }) => fact
|
|
661
|
+
.filter(({ fact }) => settleable(fact, now));
|
|
656
662
|
const unknown = [...wanted, ...refused].find((index) => !proposed.some((entry) => entry.index === index));
|
|
657
663
|
if (unknown !== undefined) throw httpError(409, 'That fact is not waiting to be confirmed');
|
|
658
664
|
if (!wanted.size && !refused.size) return review;
|
|
659
665
|
|
|
660
666
|
// Nothing is final until the record has taken it, declines included: a request that fails
|
|
661
|
-
// halfway has to leave the whole selection offerable again.
|
|
667
|
+
// halfway has to leave the whole selection offerable again. The filter names the state each
|
|
668
|
+
// fact was in when it was read, so a claim that raced another request finds nothing and loses.
|
|
662
669
|
const claim = { _id: reviewId, status: { $in: DOCUMENTING_STATUSES } };
|
|
663
670
|
const claimed = {};
|
|
664
|
-
for (const index of
|
|
665
|
-
claim[`noteFacts.${index}.state`] =
|
|
671
|
+
for (const { fact, index } of proposed.filter((entry) => wanted.has(entry.index) || refused.has(entry.index))) {
|
|
672
|
+
claim[`noteFacts.${index}.state`] = fact.state;
|
|
666
673
|
claimed[`noteFacts.${index}.state`] = 'settling';
|
|
674
|
+
claimed[`noteFacts.${index}.settlingAt`] = now;
|
|
667
675
|
claimed[`noteFacts.${index}.settledBy`] = operator;
|
|
668
676
|
}
|
|
669
677
|
const held = await getEscalationReview().findOneAndUpdate(claim, { $set: claimed }, { new: true }).lean();
|
|
@@ -686,9 +694,18 @@ async function confirmNoteFacts({ id, accepted = [], declined = [], confirmedBy,
|
|
|
686
694
|
}
|
|
687
695
|
}
|
|
688
696
|
|
|
697
|
+
const settledAs = (index) => {
|
|
698
|
+
if (wanted.has(index)) return stored.has(index) ? 'confirmed' : 'proposed';
|
|
699
|
+
return failure ? 'proposed' : 'declined';
|
|
700
|
+
};
|
|
689
701
|
const settle = {};
|
|
690
|
-
for (const index of wanted
|
|
691
|
-
|
|
702
|
+
for (const index of [...wanted, ...refused]) {
|
|
703
|
+
const state = settledAs(index);
|
|
704
|
+
settle[`noteFacts.${index}.state`] = state;
|
|
705
|
+
settle[`noteFacts.${index}.settlingAt`] = null;
|
|
706
|
+
// A fact handed back never got settled, so it must stop naming the operator who tried.
|
|
707
|
+
if (state === 'proposed') settle[`noteFacts.${index}.settledBy`] = null;
|
|
708
|
+
}
|
|
692
709
|
const updated = await getEscalationReview()
|
|
693
710
|
.findOneAndUpdate({ _id: reviewId }, { $set: settle }, { new: true }).lean()
|
|
694
711
|
.catch((error) => {
|