@peopl-health/nexus 5.61.0-dev.11187 → 5.61.0-dev.11231

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.
package/lib/index.js CHANGED
@@ -32,8 +32,6 @@ const { registerSafetyNetRoutes } = require('./clinical/services/pausedSafetyNet
32
32
  const { ensureOnePledgePerContact, startSafetyNetSweep } = require('./clinical/services/safetyNetPledgeService');
33
33
  const { startRecommendationRecoverySweep } = require('./clinical/services/recommendationRecoveryService');
34
34
  const { startInboundRecoverySweep } = require('./services/inboundRecoveryService');
35
- const { startReviewDraftSweep } = require('./services/reviewDraftSweepService');
36
- const { startReviewAlertSweep } = require('./services/reviewAlertSweepService');
37
35
  const { requestIdMiddleware, getRequestId } = require('./middleware/requestId');
38
36
  const { QueueAdapter, LocalQueueAdapter, RedisQueueAdapter, createQueueAdapter, registerQueueAdapter } = require('./queue');
39
37
  const { DrainingError } = require('./core/BatchingManager');
@@ -52,6 +50,7 @@ const { registerDashboardProvider } = require('./services/dashboard-provider-reg
52
50
  const supportActionService = require('./clinical/services/supportActionService');
53
51
  const supportLightService = require('./clinical/services/supportLightService');
54
52
  const supportInvitability = require('./clinical/services/supportInvitability');
53
+ const supportLightEvaluationService = require('./clinical/services/supportLightEvaluationService');
55
54
  const {
56
55
  NutritionSafetyDecision,
57
56
  FINDING_CODES,
@@ -205,24 +204,6 @@ class Nexus {
205
204
  logger.warn('Warning: failed to arm the paused-thread safety net:', e?.message || e);
206
205
  }
207
206
 
208
- try {
209
- if (options.reviewDrafts?.enabled === true) {
210
- startReviewDraftSweep({ intervalMs: options.reviewDrafts.intervalMs });
211
- logger.info('[Nexus] clinical review draft sweep armed');
212
- }
213
- } catch (e) {
214
- logger.warn('Warning: failed to arm the clinical review draft sweep:', e?.message || e);
215
- }
216
-
217
- try {
218
- if (options.reviewAlerts?.enabled === true) {
219
- startReviewAlertSweep({ intervalMs: options.reviewAlerts.intervalMs });
220
- logger.info('[Nexus] clinical review doctor alert sweep armed');
221
- }
222
- } catch (e) {
223
- logger.warn('Warning: failed to arm the clinical review doctor alert sweep:', e?.message || e);
224
- }
225
-
226
207
  try {
227
208
  if (options.inboundRecovery?.enabled === true) {
228
209
  startInboundRecoverySweep({
@@ -379,6 +360,7 @@ module.exports = {
379
360
  supportActionService,
380
361
  supportLightService,
381
362
  supportInvitability,
363
+ supportLightEvaluationService,
382
364
  NutritionSafetyDecision,
383
365
  FINDING_CODES,
384
366
  FINDING_VALUES,
@@ -10,9 +10,8 @@ const HANDOFF_MODES = ['sent_whatsapp', 'copied'];
10
10
  const INTERRUPTED_STATUSES = ['handing_off', 'sending'];
11
11
  const CANCELLABLE_AFTER_HANDOFF_STATUSES = ['awaiting_doctor', 'approved', 'returned', 'indications_queued'];
12
12
  const CANCELLABLE_STATUSES = ['open', 'documenting', 'draft_ready', ...CANCELLABLE_AFTER_HANDOFF_STATUSES];
13
- const DOCTOR_DECISIONS = ['approved', 'edited_approved', 'needs_intervention', 'returned', 'direct_chat'];
14
- const DECISION_SOURCES = ['platform', 'registered_by_navigator', 'chat'];
15
- const RETURN_REASONS = ['llamar', 'falta_dato', 'no_requiere'];
13
+ const DOCTOR_DECISIONS = ['approved', 'edited_approved', 'needs_intervention', 'returned'];
14
+ const DECISION_SOURCES = ['platform', 'registered_by_navigator'];
16
15
 
17
16
  const alarmSchema = new mongoose.Schema({
18
17
  urgencyLevel: { type: String, enum: URGENCY_LEVELS, required: true },
@@ -22,41 +21,9 @@ const alarmSchema = new mongoose.Schema({
22
21
  suspectedCondition: { type: String, default: null },
23
22
  recommendedAction: { type: String, default: null },
24
23
  justification: { type: String, default: null },
25
- indicators: { type: [String], default: undefined },
26
- turnId: { type: String, default: null },
27
24
  raisedAt: { type: Date, required: true },
28
25
  }, { _id: false });
29
26
 
30
- const impressionUpdateSchema = new mongoose.Schema({
31
- caseId: { type: String, default: null },
32
- ctcaeTerm: { type: String, default: null },
33
- event: { type: String, default: null },
34
- grade: { type: Number, default: null },
35
- trajectory: { type: String, default: null },
36
- missingForHigherGrade: { type: String, default: null },
37
- checkInQuestion: { type: String, default: null },
38
- }, { _id: false });
39
-
40
- const doctorAlertSchema = new mongoose.Schema({
41
- kind: { type: String, enum: ['initial', 'realert'], required: true },
42
- urgencyLevel: { type: String, enum: URGENCY_LEVELS, default: null },
43
- physicianEmail: { type: String, default: null },
44
- transport: { type: String, default: null },
45
- messageId: { type: String, default: null },
46
- error: { type: String, default: null },
47
- teamAlerted: { type: Boolean, default: false },
48
- sentAt: { type: Date, required: true },
49
- }, { _id: false });
50
-
51
- const reviewUpdateSchema = new mongoose.Schema({
52
- turnId: { type: String, required: true },
53
- tools: { type: [String], default: [] },
54
- patientText: { type: String, default: null },
55
- impressions: { type: [impressionUpdateSchema], default: [] },
56
- safetyFlags: { type: [String], default: [] },
57
- at: { type: Date, required: true },
58
- }, { _id: false });
59
-
60
27
  const noteFactSchema = new mongoose.Schema({
61
28
  variableId: { type: String, required: true },
62
29
  concept: { type: String, default: null },
@@ -77,21 +44,10 @@ const callLogSchema = new mongoose.Schema({
77
44
  loggedBy: { type: String, required: true },
78
45
  }, { timestamps: { createdAt: true, updatedAt: false } });
79
46
 
80
- const suggestCallSchema = new mongoose.Schema({
81
- value: { type: Boolean, required: true },
82
- reason: { type: String, default: null },
83
- }, { _id: false });
84
-
85
47
  const draftSchema = new mongoose.Schema({
86
48
  doctorText: { type: String, required: true },
87
49
  patientMessage: { type: String, required: true },
88
50
  writtenBy: { type: String, required: true },
89
- protocolVersion: { type: String, default: null },
90
- keyQuestions: { type: [String], default: undefined },
91
- unassessedRedFlags: { type: [String], default: undefined },
92
- missingCharacterization: { type: [String], default: undefined },
93
- suggestCall: { type: suggestCallSchema, default: undefined },
94
- sectionsUnavailable: { type: [String], default: undefined },
95
51
  }, { timestamps: { createdAt: true, updatedAt: false } });
96
52
 
97
53
  const doctorHandoffSchema = new mongoose.Schema({
@@ -112,8 +68,6 @@ const decisionSchema = new mongoose.Schema({
112
68
  finalText: { type: String, default: null },
113
69
  comment: { type: String, default: null },
114
70
  doctorReply: { type: String, default: null },
115
- returnReason: { type: String, enum: [...RETURN_REASONS, null], default: null },
116
- warnings: { type: [String], default: undefined },
117
71
  decidedBy: { type: String, required: true },
118
72
  recordedBy: { type: String, required: true },
119
73
  }, { timestamps: { createdAt: true, updatedAt: false } });
@@ -172,19 +126,6 @@ const escalationReviewSchema = new mongoose.Schema({
172
126
  openedBy: { type: String, required: true },
173
127
  assistantPausedByReview: { type: Boolean, default: false },
174
128
  alarms: { type: [alarmSchema], default: [] },
175
- updates: { type: [reviewUpdateSchema], default: [] },
176
- updatedSinceSeen: { type: Boolean, default: false },
177
- followUpCases: { type: [String], default: undefined },
178
- // The decision the follow-up cases were fixed for; a later indication extends them.
179
- followUpDecision: { type: Number, default: null },
180
- draftDueAt: { type: Date, default: null },
181
- draftAttempts: { type: Number, default: 0 },
182
- doctorAlerts: { type: [doctorAlertSchema], default: [] },
183
- doctorAlertDueAt: { type: Date, default: null },
184
- doctorAlertKind: { type: String, enum: ['initial', 'realert', null], default: null },
185
- doctorAlertAttempts: { type: Number, default: 0 },
186
- approverViewedAt: { type: Date, default: null },
187
- previousReviewId: { type: String, default: null },
188
129
  callLogs: { type: [callLogSchema], default: [] },
189
130
  noteFacts: { type: [noteFactSchema], default: [] },
190
131
  drafts: { type: [draftSchema], default: [] },
@@ -200,9 +141,6 @@ const escalationReviewSchema = new mongoose.Schema({
200
141
  }, { timestamps: true });
201
142
 
202
143
  escalationReviewSchema.index({ patientCode: 1, createdAt: -1 });
203
- escalationReviewSchema.index({ status: 1, closedAt: -1 });
204
- escalationReviewSchema.index({ draftDueAt: 1 }, { partialFilterExpression: { draftDueAt: { $type: 'date' } } });
205
- escalationReviewSchema.index({ doctorAlertDueAt: 1 }, { partialFilterExpression: { doctorAlertDueAt: { $type: 'date' } } });
206
144
  escalationReviewSchema.index({ patientCode: 1 }, { unique: true, partialFilterExpression: { closedAt: null } });
207
145
 
208
146
  const getEscalationReview = () => {
@@ -220,7 +158,6 @@ module.exports = {
220
158
  DOCTOR_DECISIONS,
221
159
  HANDOFF_MODES,
222
160
  INTERRUPTED_STATUSES,
223
- RETURN_REASONS,
224
161
  getEscalationReview,
225
162
  escalationReviewSchema,
226
163
  };
@@ -120,10 +120,8 @@ const traceRouteDefinitions = {
120
120
 
121
121
  const escalationReviewRouteDefinitions = {
122
122
  'POST /': 'openEscalationReviewController',
123
- 'GET /': 'listReviewBoardController',
124
123
  'GET /by-patient/:code': 'listEscalationReviewsController',
125
124
  'GET /:id': 'getEscalationReviewController',
126
- 'GET /:id/timeline': 'getReviewTimelineController',
127
125
  'POST /:id/cases': 'setEscalationReviewCasesController',
128
126
  'POST /:id/call-logs': 'addEscalationReviewCallLogController',
129
127
  'POST /:id/draft-suggestions': 'suggestEscalationReviewDraftController',
@@ -296,9 +294,7 @@ const builtInControllers = {
296
294
  // Escalation review controllers
297
295
  openEscalationReviewController: escalationReviewController.openEscalationReviewController,
298
296
  listEscalationReviewsController: escalationReviewController.listEscalationReviewsController,
299
- listReviewBoardController: escalationReviewController.listReviewBoardController,
300
297
  getEscalationReviewController: escalationReviewController.getEscalationReviewController,
301
- getReviewTimelineController: escalationReviewController.getReviewTimelineController,
302
298
  setEscalationReviewCasesController: escalationReviewController.setEscalationReviewCasesController,
303
299
  addEscalationReviewCallLogController: escalationReviewController.addEscalationReviewCallLogController,
304
300
  suggestEscalationReviewDraftController: escalationReviewController.suggestEscalationReviewDraftController,
@@ -6,7 +6,6 @@ const { Thread } = require('../models/threadModel');
6
6
 
7
7
  const { getRecordByFilter } = require('../services/airtableService');
8
8
  const { getOpenTaskSummary } = require('../services/patientTaskSummaryService');
9
- const { boardBucketOfFilter, listReviewBoard } = require('../services/reviewBoardService');
10
9
 
11
10
  const { sendMessage } = require('../core/NexusMessaging');
12
11
  const { ensurePatientResource } = require('./patientResourceService');
@@ -41,37 +40,6 @@ const buildThreadFilter = (filter, { taskCodes }) => {
41
40
  }
42
41
  };
43
42
 
44
- const fetchFilteredThreads = async (threadFilter, skip, limit) => {
45
- const [threads, total] = await Promise.all([
46
- Thread.find(threadFilter)
47
- .sort({ lastMessageAt: -1 })
48
- .skip(skip)
49
- .limit(limit)
50
- .lean(),
51
- Thread.countDocuments(threadFilter)
52
- ]);
53
- return { threads, total, reviewMap: null };
54
- };
55
-
56
- // The board decides the order; only the requested page's threads are read in full.
57
- const fetchBoardThreads = async (bucket, skip, limit) => {
58
- const rows = await listReviewBoard({ bucket });
59
- const reviewMap = new Map(rows.map(({ patientCode, ...row }) => [patientCode, row]));
60
- const withThread = new Set((await Thread.find(
61
- { code: { $in: [...reviewMap.keys()] }, lastMessageAt: { $ne: null } },
62
- { code: 1 },
63
- ).lean()).map((thread) => thread.code));
64
- const ordered = rows.map((row) => row.patientCode).filter((code) => withThread.has(code));
65
- const page = ordered.slice(skip, skip + limit);
66
- const threads = page.length
67
- ? await Thread.find({ code: { $in: page }, lastMessageAt: { $ne: null } }).sort({ lastMessageAt: -1 }).lean()
68
- : [];
69
- // A patient can hold more than one thread; the most recently active one represents them.
70
- const threadByCode = new Map();
71
- threads.forEach((thread) => { if (!threadByCode.has(thread.code)) threadByCode.set(thread.code, thread); });
72
- return { threads: page.map((code) => threadByCode.get(code)).filter(Boolean), total: ordered.length, reviewMap };
73
- };
74
-
75
43
  const fetchConversationData = async (filter, skip, limit) => {
76
44
  const openTaskMap = PENDING_TASK_FILTERS.includes(filter)
77
45
  ? await getOpenTaskSummary()
@@ -86,13 +54,19 @@ const fetchConversationData = async (filter, skip, limit) => {
86
54
  requests: taskEntries.filter(([, entry]) => entry.requests > 0).map(([code]) => code),
87
55
  };
88
56
 
57
+ const threadFilter = buildThreadFilter(filter, { taskCodes });
58
+
89
59
  logger.info('[fetchConversationData] Applying filter', { filter });
90
60
  const startTime = Date.now();
91
61
 
92
- const bucket = boardBucketOfFilter(filter);
93
- const { threads, total, reviewMap } = bucket
94
- ? await fetchBoardThreads(bucket, skip, limit)
95
- : await fetchFilteredThreads(buildThreadFilter(filter, { taskCodes }), skip, limit);
62
+ const [threads, total] = await Promise.all([
63
+ Thread.find(threadFilter)
64
+ .sort({ lastMessageAt: -1 })
65
+ .skip(skip)
66
+ .limit(limit)
67
+ .lean(),
68
+ Thread.countDocuments(threadFilter)
69
+ ]);
96
70
 
97
71
  logger.info('[fetchConversationData] Completed', { duration: Date.now() - startTime, count: threads.length });
98
72
 
@@ -108,7 +82,7 @@ const fetchConversationData = async (filter, skip, limit) => {
108
82
  }
109
83
  }
110
84
 
111
- return { threads, total, airtableNameMap, openTaskMap, reviewMap };
85
+ return { threads, total, airtableNameMap, openTaskMap };
112
86
  };
113
87
 
114
88
  const getMediaType = (media) => {
@@ -119,7 +93,7 @@ const getMediaType = (media) => {
119
93
  return type === 'application' ? 'document' : subtype === 'webp' ? 'sticker' : type;
120
94
  };
121
95
 
122
- const processConversations = async (threads, airtableNameMap, openTaskMap, reviewMap = null) => {
96
+ const processConversations = async (threads, airtableNameMap, openTaskMap) => {
123
97
  const createConversation = (thread, index) => {
124
98
  const code = thread?.code || `error_${index}`;
125
99
  return {
@@ -145,8 +119,7 @@ const processConversations = async (threads, airtableNameMap, openTaskMap, revie
145
119
  lastMessageFromMe: thread?.lastMessageFromMe || false,
146
120
  isLastMessageMedia: !!thread?.lastMessageMedia,
147
121
  lastMessageType: getMediaType(thread?.lastMessageMedia),
148
- openTasks: openTaskMap?.get(code) || null,
149
- clinicalReview: reviewMap?.get(code) || null
122
+ openTasks: openTaskMap?.get(code) || null
150
123
  };
151
124
  };
152
125