@peopl-health/nexus 5.62.2 → 5.63.0-dev.11429

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.
@@ -12,7 +12,6 @@ const {
12
12
  listEscalationReviews,
13
13
  openEscalationReview,
14
14
  recordDoctorDecision,
15
- requestBotDraft,
16
15
  sendApprovedIndications,
17
16
  setReviewCases,
18
17
  suggestDraft,
@@ -125,16 +124,6 @@ const suggestEscalationReviewDraftController = async (req, res) => {
125
124
  }
126
125
  };
127
126
 
128
- const requestEscalationReviewDraftController = async (req, res) => {
129
- try {
130
- const { triggeredBy } = req.body || {};
131
- const review = await requestBotDraft({ id: req.params.id, requestedBy: triggeredBy, requestId: req.requestId });
132
- res.status(202).json({ success: true, review });
133
- } catch (error) {
134
- respondWithError(res, error, 'requesting an escalation review draft', { reviewId: req.params?.id });
135
- }
136
- };
137
-
138
127
  const suggestEscalationReviewQuestionsController = async (req, res) => {
139
128
  try {
140
129
  const { triggeredBy } = req.body || {};
@@ -260,7 +249,6 @@ module.exports = {
260
249
  closeEscalationReviewController,
261
250
  getEscalationReviewController,
262
251
  getReviewTimelineController,
263
- requestEscalationReviewDraftController,
264
252
  handOffEscalationReviewController,
265
253
  listEscalationReviewsController,
266
254
  listReviewBoardController,
@@ -11,7 +11,7 @@ const HANDOFF_MODES = ['sent_whatsapp', 'copied'];
11
11
  const INTERRUPTED_STATUSES = ['handing_off', 'sending'];
12
12
  const CANCELLABLE_AFTER_HANDOFF_STATUSES = ['awaiting_doctor', 'approved', 'returned', 'indications_queued'];
13
13
  const CANCELLABLE_STATUSES = ['open', 'documenting', 'draft_ready', ...CANCELLABLE_AFTER_HANDOFF_STATUSES];
14
- const DOCTOR_DECISIONS = ['approved', 'edited_approved', 'needs_intervention', 'returned', 'direct_chat'];
14
+ const DOCTOR_DECISIONS = ['approved', 'edited_approved', 'needs_intervention', 'returned', 'monitor', 'direct_chat'];
15
15
  const DECISION_SOURCES = ['platform', 'registered_by_navigator', 'chat'];
16
16
  const RETURN_REASONS = ['llamar', 'falta_dato', 'no_requiere'];
17
17
 
@@ -168,6 +168,7 @@ const escalationReviewSchema = new mongoose.Schema({
168
168
  'sending',
169
169
  'indications_queued',
170
170
  'indications_sent',
171
+ 'monitoring',
171
172
  'closed',
172
173
  'cancelled',
173
174
  ],
@@ -127,7 +127,6 @@ const escalationReviewRouteDefinitions = {
127
127
  'POST /:id/cases': 'setEscalationReviewCasesController',
128
128
  'POST /:id/call-logs': 'addEscalationReviewCallLogController',
129
129
  'POST /:id/draft-suggestions': 'suggestEscalationReviewDraftController',
130
- 'POST /:id/draft-requests': 'requestEscalationReviewDraftController',
131
130
  'POST /:id/key-questions': 'suggestEscalationReviewQuestionsController',
132
131
  'POST /:id/note-facts': 'confirmEscalationReviewNoteFactsController',
133
132
  'POST /:id/drafts': 'addEscalationReviewDraftController',
@@ -300,7 +299,6 @@ const builtInControllers = {
300
299
  listReviewBoardController: escalationReviewController.listReviewBoardController,
301
300
  getEscalationReviewController: escalationReviewController.getEscalationReviewController,
302
301
  getReviewTimelineController: escalationReviewController.getReviewTimelineController,
303
- requestEscalationReviewDraftController: escalationReviewController.requestEscalationReviewDraftController,
304
302
  setEscalationReviewCasesController: escalationReviewController.setEscalationReviewCasesController,
305
303
  addEscalationReviewCallLogController: escalationReviewController.addEscalationReviewCallLogController,
306
304
  suggestEscalationReviewDraftController: escalationReviewController.suggestEscalationReviewDraftController,
@@ -51,10 +51,12 @@ const DUPLICATE_KEY_ERROR = 11000;
51
51
  const DOCUMENTING_STATUSES = ['open', 'documenting', 'draft_ready', 'returned'];
52
52
  const UNDRAFTED_STATUSES = DOCUMENTING_STATUSES.filter((status) => status !== 'draft_ready');
53
53
  const HANDOFF_STATUSES = ['draft_ready', 'awaiting_doctor'];
54
- const CLOSABLE_STATUSES = ['indications_queued', 'indications_sent', 'needs_intervention'];
54
+ const CLOSABLE_STATUSES = ['indications_queued', 'indications_sent', 'monitoring', 'needs_intervention'];
55
55
  const SENDABLE_STATUSES = ['approved', 'needs_intervention'];
56
56
  const DECIDABLE_STATUSES = ['draft_ready', 'awaiting_doctor'];
57
- const FOLLOWUP_STATUSES = ['indications_sent', 'indications_queued'];
57
+ const FOLLOWUP_STATUSES = ['indications_sent', 'indications_queued', 'monitoring'];
58
+ const SETTLEABLE_STATUSES = ['indications_sent', 'monitoring'];
59
+ const DRAFTLESS_DECISIONS = ['monitor', 'returned'];
58
60
  const SETTLED_TRAJECTORIES = ['improving', 'stable'];
59
61
  const ALERTED_URGENCIES = ['stat', 'asap'];
60
62
  const ALERT_MARKS = { stat: '🔴', asap: '🟠' };
@@ -93,7 +95,8 @@ const configured = (key) => {
93
95
  return value;
94
96
  };
95
97
 
96
- const statusAfter = (decision) => (decision === 'edited_approved' ? 'approved' : decision);
98
+ const STATUS_AFTER = { edited_approved: 'approved', monitor: 'monitoring' };
99
+ const statusAfter = (decision) => STATUS_AFTER[decision] ?? decision;
97
100
 
98
101
  const caseIdsOf = (review) => (Array.isArray(review.patientCaseRecordIds)
99
102
  ? review.patientCaseRecordIds
@@ -580,13 +583,7 @@ async function openEscalationReview({
580
583
  if (!patient) throw httpError(404, 'Patient not found');
581
584
 
582
585
  const recordIds = await requestedCaseIds({ patientCode, patientCaseRecordId, patientCaseRecordIds });
583
- const fields = {
584
- patientCode,
585
- openedBy: operator,
586
- patientCaseRecordIds: recordIds,
587
- assistantPausedByReview: false,
588
- draftDueAt: new Date(Date.now() + DRAFT_DEBOUNCE_MS),
589
- };
586
+ const fields = { patientCode, openedBy: operator, patientCaseRecordIds: recordIds, assistantPausedByReview: false };
590
587
 
591
588
  const EscalationReview = getEscalationReview();
592
589
  let review;
@@ -622,35 +619,6 @@ async function openEscalationReview({
622
619
  return { review, created: true };
623
620
  }
624
621
 
625
- async function requestBotDraft({ id, requestedBy, requestId = null } = {}) {
626
- const reviewId = reviewIdOf(id);
627
- const operator = requiredText(requestedBy, 'requestedBy');
628
- const review = await loadReview(reviewId);
629
- if (!DOCUMENTING_STATUSES.includes(review.status)) throw httpError(409, STATUS_CONFLICT);
630
- if ((review.drafts ?? []).at(-1)?.writtenBy && review.drafts.at(-1).writtenBy !== DRAFT_OPERATOR) {
631
- throw httpError(409, 'The latest draft was written by the team; edit it instead of asking for a new one');
632
- }
633
- const now = new Date();
634
- if (review.draftDueAt && new Date(review.draftDueAt) > now) return review;
635
- const scheduled = await getEscalationReview().findOneAndUpdate(
636
- {
637
- _id: reviewId,
638
- status: { $in: DOCUMENTING_STATUSES },
639
- drafts: { $size: (review.drafts ?? []).length },
640
- $or: [{ draftDueAt: null }, { draftDueAt: { $lte: now } }],
641
- },
642
- { $set: { draftDueAt: now, draftAttempts: 0 } },
643
- { new: true },
644
- ).lean();
645
- if (!scheduled) {
646
- const latest = await loadReview(reviewId);
647
- if (latest.draftDueAt && new Date(latest.draftDueAt) > now) return latest;
648
- throw httpError(409, STATUS_CONFLICT);
649
- }
650
- await audit(scheduled, operator, requestId, { action: 'draft_requested' });
651
- return scheduled;
652
- }
653
-
654
622
  async function isEscalationApprover(operator) {
655
623
  try {
656
624
  return Boolean(await getEscalationApprover(operator));
@@ -744,29 +712,34 @@ async function setReviewCases({ id, patientCaseRecordIds, updatedBy, requestId =
744
712
  if (!DOCUMENTING_STATUSES.includes(current.status)) throw httpError(409, STATUS_CONFLICT);
745
713
 
746
714
  const recordIds = await requestedCaseIds({ patientCode: current.patientCode, patientCaseRecordIds });
747
- const casesChanged = {
748
- $not: [{
749
- $setEquals: [
750
- {
751
- $ifNull: [
752
- '$patientCaseRecordIds',
753
- { $cond: [{ $ifNull: ['$patientCaseRecordId', false] }, ['$patientCaseRecordId'], []] },
754
- ],
755
- },
756
- recordIds,
757
- ],
758
- }],
759
- };
760
- const isBotDraft = (current.drafts ?? []).at(-1)?.writtenBy === DRAFT_OPERATOR;
761
715
 
762
716
  const review = await updateReview(reviewId, DOCUMENTING_STATUSES, [{
763
717
  $set: {
764
718
  patientCaseRecordIds: recordIds,
765
- status: { $cond: [{ $and: [{ $eq: ['$status', 'draft_ready'] }, casesChanged] }, 'documenting', '$status'] },
766
- ...(isBotDraft && {
767
- draftDueAt: { $cond: [casesChanged, new Date(Date.now() + DRAFT_DEBOUNCE_MS), '$draftDueAt'] },
768
- draftAttempts: { $cond: [casesChanged, 0, '$draftAttempts'] },
769
- }),
719
+ status: {
720
+ $cond: [
721
+ {
722
+ $and: [
723
+ { $eq: ['$status', 'draft_ready'] },
724
+ {
725
+ $not: [{
726
+ $setEquals: [
727
+ {
728
+ $ifNull: [
729
+ '$patientCaseRecordIds',
730
+ { $cond: [{ $ifNull: ['$patientCaseRecordId', false] }, ['$patientCaseRecordId'], []] },
731
+ ],
732
+ },
733
+ recordIds,
734
+ ],
735
+ }],
736
+ },
737
+ ],
738
+ },
739
+ 'documenting',
740
+ '$status',
741
+ ],
742
+ },
770
743
  pendingCaseMirrors: {
771
744
  $filter: {
772
745
  input: { $ifNull: ['$pendingCaseMirrors', []] },
@@ -1058,7 +1031,7 @@ async function settleFollowUp(snapshot, { background = true } = {}) {
1058
1031
 
1059
1032
  if ([...latest.values()].includes('worsening')) return reopenFromFollowUp(review, 'worsening');
1060
1033
  const settled = [...known].every((caseId) => SETTLED_TRAJECTORIES.includes(latest.get(caseId)));
1061
- if (!settled || review.status !== 'indications_sent') {
1034
+ if (!settled || !SETTLEABLE_STATUSES.includes(review.status)) {
1062
1035
  await getEscalationReview().updateOne({ _id: review._id }, { $set: { updatedSinceSeen: true } });
1063
1036
  return review;
1064
1037
  }
@@ -1412,11 +1385,13 @@ async function recordDoctorDecision({
1412
1385
  }
1413
1386
 
1414
1387
  const review = await loadReview(reviewId);
1415
- if (!DECIDABLE_STATUSES.includes(review.status)) throw httpError(409, STATUS_CONFLICT);
1388
+ const isDraftless = DRAFTLESS_DECISIONS.includes(decision);
1389
+ const allowedStatuses = isDraftless ? [...new Set([...DOCUMENTING_STATUSES, ...DECIDABLE_STATUSES])] : DECIDABLE_STATUSES;
1390
+ if (!allowedStatuses.includes(review.status)) throw httpError(409, STATUS_CONFLICT);
1416
1391
  const handoff = review.status === 'awaiting_doctor' ? review.doctorHandoffs.at(-1) : null;
1417
1392
  const draftNumber = handoff?.draftNumber ?? review.drafts.length;
1418
1393
  const draft = review.drafts[draftNumber - 1];
1419
- if (!draft) throw httpError(409, STATUS_CONFLICT);
1394
+ if (!draft && !isDraftless) throw httpError(409, STATUS_CONFLICT);
1420
1395
  if (seenDraftNumber !== null && Number(seenDraftNumber) !== draftNumber) throw httpError(409, DRAFT_CHANGED);
1421
1396
 
1422
1397
  let approvedText = null;
@@ -1441,7 +1416,7 @@ async function recordDoctorDecision({
1441
1416
  }
1442
1417
  const warnings = decision === 'edited_approved' ? draftlessDrugWarnings(approvedText, draft) : [];
1443
1418
 
1444
- const updated = await updateReview(reviewId, DECIDABLE_STATUSES, {
1419
+ const updated = await updateReview(reviewId, allowedStatuses, {
1445
1420
  $push: {
1446
1421
  decisions: {
1447
1422
  decision,
@@ -1793,7 +1768,6 @@ module.exports = {
1793
1768
  recordDoctorDecision,
1794
1769
  sendScheduledDoctorAlert,
1795
1770
  recordReviewUpdate,
1796
- requestBotDraft,
1797
1771
  sendApprovedIndications,
1798
1772
  setReviewCases,
1799
1773
  suggestDraft,
@@ -3,7 +3,7 @@ const { URGENCY_RANK, getEscalationReview } = require('../models/escalationRevie
3
3
 
4
4
  const DONE_WINDOW_MS = 7 * 24 * 60 * 60 * 1000;
5
5
  const RETURNED_STATUSES = ['returned'];
6
- const FOLLOWUP_STATUSES = ['indications_sent', 'indications_queued'];
6
+ const FOLLOWUP_STATUSES = ['indications_sent', 'indications_queued', 'monitoring'];
7
7
  const DONE_STATUSES = ['closed', 'cancelled'];
8
8
  const BOARD_BUCKETS = ['pending', 'returned', 'followup', 'done'];
9
9
  const BOARD_FILTER_PREFIX = 'clinical-review-';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@peopl-health/nexus",
3
- "version": "5.62.2",
3
+ "version": "5.63.0-dev.11429",
4
4
  "description": "Core messaging and assistant library for WhatsApp communication platforms",
5
5
  "keywords": [
6
6
  "whatsapp",