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

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.
@@ -120,8 +120,10 @@ const traceRouteDefinitions = {
120
120
 
121
121
  const escalationReviewRouteDefinitions = {
122
122
  'POST /': 'openEscalationReviewController',
123
+ 'GET /': 'listReviewBoardController',
123
124
  'GET /by-patient/:code': 'listEscalationReviewsController',
124
125
  'GET /:id': 'getEscalationReviewController',
126
+ 'GET /:id/timeline': 'getReviewTimelineController',
125
127
  'POST /:id/cases': 'setEscalationReviewCasesController',
126
128
  'POST /:id/call-logs': 'addEscalationReviewCallLogController',
127
129
  'POST /:id/draft-suggestions': 'suggestEscalationReviewDraftController',
@@ -294,7 +296,9 @@ const builtInControllers = {
294
296
  // Escalation review controllers
295
297
  openEscalationReviewController: escalationReviewController.openEscalationReviewController,
296
298
  listEscalationReviewsController: escalationReviewController.listEscalationReviewsController,
299
+ listReviewBoardController: escalationReviewController.listReviewBoardController,
297
300
  getEscalationReviewController: escalationReviewController.getEscalationReviewController,
301
+ getReviewTimelineController: escalationReviewController.getReviewTimelineController,
298
302
  setEscalationReviewCasesController: escalationReviewController.setEscalationReviewCasesController,
299
303
  addEscalationReviewCallLogController: escalationReviewController.addEscalationReviewCallLogController,
300
304
  suggestEscalationReviewDraftController: escalationReviewController.suggestEscalationReviewDraftController,
@@ -6,6 +6,7 @@ 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');
9
10
 
10
11
  const { sendMessage } = require('../core/NexusMessaging');
11
12
  const { ensurePatientResource } = require('./patientResourceService');
@@ -40,6 +41,37 @@ const buildThreadFilter = (filter, { taskCodes }) => {
40
41
  }
41
42
  };
42
43
 
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
+
43
75
  const fetchConversationData = async (filter, skip, limit) => {
44
76
  const openTaskMap = PENDING_TASK_FILTERS.includes(filter)
45
77
  ? await getOpenTaskSummary()
@@ -54,19 +86,13 @@ const fetchConversationData = async (filter, skip, limit) => {
54
86
  requests: taskEntries.filter(([, entry]) => entry.requests > 0).map(([code]) => code),
55
87
  };
56
88
 
57
- const threadFilter = buildThreadFilter(filter, { taskCodes });
58
-
59
89
  logger.info('[fetchConversationData] Applying filter', { filter });
60
90
  const startTime = Date.now();
61
91
 
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
- ]);
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);
70
96
 
71
97
  logger.info('[fetchConversationData] Completed', { duration: Date.now() - startTime, count: threads.length });
72
98
 
@@ -82,7 +108,7 @@ const fetchConversationData = async (filter, skip, limit) => {
82
108
  }
83
109
  }
84
110
 
85
- return { threads, total, airtableNameMap, openTaskMap };
111
+ return { threads, total, airtableNameMap, openTaskMap, reviewMap };
86
112
  };
87
113
 
88
114
  const getMediaType = (media) => {
@@ -93,7 +119,7 @@ const getMediaType = (media) => {
93
119
  return type === 'application' ? 'document' : subtype === 'webp' ? 'sticker' : type;
94
120
  };
95
121
 
96
- const processConversations = async (threads, airtableNameMap, openTaskMap) => {
122
+ const processConversations = async (threads, airtableNameMap, openTaskMap, reviewMap = null) => {
97
123
  const createConversation = (thread, index) => {
98
124
  const code = thread?.code || `error_${index}`;
99
125
  return {
@@ -119,7 +145,8 @@ const processConversations = async (threads, airtableNameMap, openTaskMap) => {
119
145
  lastMessageFromMe: thread?.lastMessageFromMe || false,
120
146
  isLastMessageMedia: !!thread?.lastMessageMedia,
121
147
  lastMessageType: getMediaType(thread?.lastMessageMedia),
122
- openTasks: openTaskMap?.get(code) || null
148
+ openTasks: openTaskMap?.get(code) || null,
149
+ clinicalReview: reviewMap?.get(code) || null
123
150
  };
124
151
  };
125
152