@peopl-health/nexus 6.7.0-dev.21504 → 6.7.0-dev.21506

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.
@@ -52,6 +52,10 @@ function escalationCaseFollowUpLive() {
52
52
  return flagNotOff('ESCALATION_CASE_FOLLOW_UP_LIVE');
53
53
  }
54
54
 
55
+ function escalationDoctorCaseStatusLive() {
56
+ return flagOn('ESCALATION_DOCTOR_CASE_STATUS_LIVE');
57
+ }
58
+
55
59
  function escalationCaseStatusReminderLive() {
56
60
  return flagOn('ESCALATION_CASE_STATUS_REMINDER_LIVE');
57
61
  }
@@ -67,6 +71,7 @@ function escalationUnreviewedCaseNoticeLive() {
67
71
  module.exports = {
68
72
  escalationCaseFollowUpLive,
69
73
  escalationCaseStatusReminderLive,
74
+ escalationDoctorCaseStatusLive,
70
75
  escalationUnreviewedCaseNoticeLive,
71
76
  escalationApproverAlertLive,
72
77
  escalationAssessmentLive,
@@ -4,6 +4,7 @@ const { DONE_STATUSES, getEscalationReview, supersededCaseIds } = require('../mo
4
4
  const { escalationCaseFollowUpLive, escalationCaseStatusReminderLive, escalationStaleQueueNoticeLive } = require('../clinical/flags/escalationFlags');
5
5
 
6
6
  const { logPatientChange } = require('./patientService');
7
+ const { noticeLiveSince } = require('./noticeRolloutService');
7
8
  const { getThreadInfo } = require('./threadService');
8
9
  const { UNTRIAGED_CASE_STATUSES, readPatientCases } = require('./patientTaskService');
9
10
  const { BOOKKEEPING, closeEscalationReview, followUpReadingOf, noticeCaseThread, resolveSettledCases } = require('./escalationReviewService');
@@ -162,10 +163,11 @@ async function sweepCaseStatuses({ now = new Date() } = {}) {
162
163
  if (!escalationCaseStatusReminderLive()) return 0;
163
164
  const policy = responseDeadlinePolicy();
164
165
  if (teamOpenAt(now, policy).getTime() !== now.getTime()) return 0;
166
+ const since = Math.max(now.getTime() - CASE_STATUS_LOOKBACK_MS, (await noticeLiveSince('caseStatus', now)).getTime());
165
167
  const reviews = await getEscalationReview()
166
168
  .find({
167
169
  status: { $in: DONE_STATUSES },
168
- closedAt: { $lte: new Date(now.getTime() - CASE_STATUS_NOTICE_MS), $gt: new Date(now.getTime() - CASE_STATUS_LOOKBACK_MS) },
170
+ closedAt: { $lte: new Date(now.getTime() - CASE_STATUS_NOTICE_MS), $gt: new Date(since) },
169
171
  caseStatusNoticeAt: null,
170
172
  'patientCaseRecordIds.0': { $exists: true },
171
173
  })
@@ -46,6 +46,7 @@ const {
46
46
  escalationPatientTimerLive: escalationTimerLive,
47
47
  escalationApproverAlertLive,
48
48
  escalationAutoHandoffLive,
49
+ escalationDoctorCaseStatusLive,
49
50
  } = require('../clinical/flags/escalationFlags');
50
51
  const { chemoContextLive } = require('../clinical/flags/clinicalContextFlags');
51
52
  const { cytotoxicWindowDays, daysSinceLastCytotoxic } = require('../clinical/helpers/chemoContextHelper');
@@ -168,6 +169,7 @@ const IMPLIED_CASE_STATUS = { monitor: 'En vigilancia', approved: 'En tratamient
168
169
 
169
170
  async function impliedCaseStatusMirrors(review, decision, returnReason, chosen) {
170
171
  const status = returnReason === 'no_requiere' ? 'Resuelto' : IMPLIED_CASE_STATUS[decision];
172
+ if (!escalationDoctorCaseStatusLive()) return [];
171
173
  const chosenIds = new Set(chosen.map(({ recordId }) => recordId));
172
174
  const covered = new Set(caseIdsOf(review).filter((recordId) => !chosenIds.has(recordId)));
173
175
  if (!status || !covered.size) return [];
@@ -986,6 +988,7 @@ async function setReviewCases({ id, patientCaseRecordIds, updatedBy, requestId =
986
988
  }
987
989
 
988
990
  async function setCaseStatuses({ id, caseStatuses, updatedBy, requestId = null } = {}) {
991
+ if (!escalationDoctorCaseStatusLive()) throw httpError(409, 'Setting case statuses is not enabled');
989
992
  const reviewId = reviewIdOf(id);
990
993
  const operator = requiredText(updatedBy, 'updatedBy');
991
994
  const current = await loadReview(reviewId);
@@ -1581,6 +1584,7 @@ async function followUpCasesOf(review, sinceMs) {
1581
1584
  }
1582
1585
 
1583
1586
  async function resolveSettledCases(review, operator) {
1587
+ if (!escalationDoctorCaseStatusLive()) return review;
1584
1588
  const setByDoctor = new Map((review.doctorCaseStatuses ?? []).map(({ recordId, status }) => [recordId, status]));
1585
1589
  if (![...setByDoctor.values()].some((status) => RESOLVABLE_CASE_STATUSES.includes(status))) return review;
1586
1590
  try {
@@ -2108,7 +2112,7 @@ async function recordDoctorDecision({
2108
2112
  }
2109
2113
 
2110
2114
  const review = await loadReview(reviewId);
2111
- const chosenCaseMirrors = caseStatusMirrorsOf(caseStatuses, review);
2115
+ const chosenCaseMirrors = escalationDoctorCaseStatusLive() ? caseStatusMirrorsOf(caseStatuses, review) : [];
2112
2116
  const isDraftless = DRAFTLESS_DECISIONS.includes(decision);
2113
2117
  const allowedStatuses = isDraftless ? [...new Set([...DOCUMENTING_STATUSES, ...DECIDABLE_STATUSES])] : DECIDABLE_STATUSES;
2114
2118
  if (!allowedStatuses.includes(review.status)) throw httpError(409, STATUS_CONFLICT);
@@ -0,0 +1,13 @@
1
+ const { WriteClaimStore } = require('../clinical/services/writeClaimStore');
2
+
3
+ const DAY_MS = 24 * 60 * 60 * 1000;
4
+ const rollout = new WriteClaimStore({ namespace: 'teamNoticeRollout', ttlMs: 10 * 365 * DAY_MS });
5
+
6
+ // A team notice only covers what happened after it was first switched on, so turning it on does not flush the backlog.
7
+ async function noticeLiveSince(name, now) {
8
+ await rollout.acquire(name, now.toISOString());
9
+ const since = new Date((await rollout.get(name)) ?? now);
10
+ return Number.isNaN(since.getTime()) ? now : since;
11
+ }
12
+
13
+ module.exports = { noticeLiveSince };
@@ -84,10 +84,14 @@ async function doctorPingedRecently(review, physicianEmail, now) {
84
84
  return others > 0;
85
85
  }
86
86
 
87
+ // One reminder per ring: a stat/asap case whose doctor already got its early reminder goes straight to the team stage.
88
+ const remindedSince = (review, ringAt) => (review.doctorAlerts ?? []).some((sent) => sent.kind === 'reminder'
89
+ && (sent.transport || sent.possiblyDelivered) && timeOf(sent.sentAt) >= ringAt.getTime());
90
+
87
91
  async function remindDoctor(review, { ringAt, dueAt, physicianEmail, level }, now) {
88
92
  const taken = await caseTakenReason(review, ringAt, level);
89
93
  const testPatient = !taken && (await teamAlertIdentity(review)).identity.testPatient;
90
- const skippedReason = taken || (testPatient ? 'test_patient' : null);
94
+ const skippedReason = taken || (testPatient ? 'test_patient' : null) || (remindedSince(review, ringAt) ? 'already_reminded' : null);
91
95
  if (!skippedReason && await doctorPingedRecently(review, physicianEmail, now)) return false;
92
96
  if (!await claim(review, { stage: 'doctor', ringAt, dueAt, physicianEmail, skippedReason, sentAt: now })) return false;
93
97
  if (skippedReason) return false;
@@ -6,6 +6,7 @@ const { escalationUnreviewedCaseNoticeLive } = require('../clinical/flags/escala
6
6
  const { WriteClaimStore } = require('../clinical/services/writeClaimStore');
7
7
 
8
8
  const { readUnreviewedCaseCandidates } = require('./patientTaskService');
9
+ const { noticeLiveSince } = require('./noticeRolloutService');
9
10
  const { sendGroupNotice } = require('./teamAlertService');
10
11
  const { teamAlertIdentity } = require('./escalationReviewService');
11
12
  const { duration } = require('./untakenAlertService');
@@ -52,8 +53,9 @@ async function sweepUnreviewedCases({ now = new Date() } = {}) {
52
53
  if (!escalationUnreviewedCaseNoticeLive()) return 0;
53
54
  const policy = responseDeadlinePolicy();
54
55
  if (teamOpenAt(now, policy).getTime() !== now.getTime()) return 0;
55
- const due = (await readUnreviewedCaseCandidates(new Date(now.getTime() - LOOKBACK_MS)))
56
- .filter((patientCase) => patientCase.isOpen && patientCase.patientCode && isDue(patientCase, now, policy));
56
+ const since = Math.max(now.getTime() - LOOKBACK_MS, (await noticeLiveSince('unreviewedCase', now)).getTime());
57
+ const due = (await readUnreviewedCaseCandidates(new Date(since)))
58
+ .filter((patientCase) => patientCase.isOpen && patientCase.patientCode && Date.parse(patientCase.createdAt) > since && isDue(patientCase, now, policy));
57
59
  if (!due.length) return 0;
58
60
  const reviewed = await reviewedCaseIds(due.map((patientCase) => patientCase.recordId));
59
61
  const byPatient = new Map();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@peopl-health/nexus",
3
- "version": "6.7.0-dev.21504",
3
+ "version": "6.7.0-dev.21506",
4
4
  "description": "Core messaging and assistant library for WhatsApp communication platforms",
5
5
  "keywords": [
6
6
  "whatsapp",