@peopl-health/nexus 5.61.0-dev.11186 → 5.61.0-dev.11187
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/controllers/conversationController.js +2 -2
- package/lib/controllers/escalationReviewController.js +11 -0
- package/lib/models/escalationReviewModel.js +1 -0
- package/lib/routes/index.js +2 -0
- package/lib/services/conversationService.js +40 -13
- package/lib/services/reviewBoardService.js +78 -0
- package/package.json +1 -1
|
@@ -34,7 +34,7 @@ const getConversationController = async (req, res) => {
|
|
|
34
34
|
});
|
|
35
35
|
}
|
|
36
36
|
|
|
37
|
-
const { threads, total, airtableNameMap, openTaskMap } = await fetchConversationData(filter, skip, limit);
|
|
37
|
+
const { threads, total, airtableNameMap, openTaskMap, reviewMap } = await fetchConversationData(filter, skip, limit);
|
|
38
38
|
|
|
39
39
|
if (!threads?.length) {
|
|
40
40
|
return res.status(200).json({
|
|
@@ -44,7 +44,7 @@ const getConversationController = async (req, res) => {
|
|
|
44
44
|
});
|
|
45
45
|
}
|
|
46
46
|
|
|
47
|
-
const processedConversations = await processConversations(threads, airtableNameMap, openTaskMap);
|
|
47
|
+
const processedConversations = await processConversations(threads, airtableNameMap, openTaskMap, reviewMap);
|
|
48
48
|
const totalPages = Math.ceil(total / limit);
|
|
49
49
|
|
|
50
50
|
res.status(200).json({
|
|
@@ -19,6 +19,7 @@ const {
|
|
|
19
19
|
confirmNoteFacts,
|
|
20
20
|
syncEscalationReviewCase,
|
|
21
21
|
} = require('../services/escalationReviewService');
|
|
22
|
+
const { listReviewBoard } = require('../services/reviewBoardService');
|
|
22
23
|
|
|
23
24
|
const respondWithError = (res, error, action, context) => {
|
|
24
25
|
logger.error(`Error ${action}:`, {
|
|
@@ -72,6 +73,15 @@ const listEscalationReviewsController = async (req, res) => {
|
|
|
72
73
|
}
|
|
73
74
|
};
|
|
74
75
|
|
|
76
|
+
const listReviewBoardController = async (req, res) => {
|
|
77
|
+
try {
|
|
78
|
+
const rows = await listReviewBoard({ bucket: req.query.bucket });
|
|
79
|
+
res.status(200).json({ success: true, rows });
|
|
80
|
+
} catch (error) {
|
|
81
|
+
respondWithError(res, error, 'listing the review board', { bucket: String(req.query?.bucket || '') });
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
|
|
75
85
|
const setEscalationReviewCasesController = async (req, res) => {
|
|
76
86
|
try {
|
|
77
87
|
const { patientCaseRecordIds, triggeredBy } = req.body || {};
|
|
@@ -241,6 +251,7 @@ module.exports = {
|
|
|
241
251
|
getReviewTimelineController,
|
|
242
252
|
handOffEscalationReviewController,
|
|
243
253
|
listEscalationReviewsController,
|
|
254
|
+
listReviewBoardController,
|
|
244
255
|
openEscalationReviewController,
|
|
245
256
|
recordEscalationReviewDecisionController,
|
|
246
257
|
sendEscalationReviewIndicationsController,
|
|
@@ -200,6 +200,7 @@ const escalationReviewSchema = new mongoose.Schema({
|
|
|
200
200
|
}, { timestamps: true });
|
|
201
201
|
|
|
202
202
|
escalationReviewSchema.index({ patientCode: 1, createdAt: -1 });
|
|
203
|
+
escalationReviewSchema.index({ status: 1, closedAt: -1 });
|
|
203
204
|
escalationReviewSchema.index({ draftDueAt: 1 }, { partialFilterExpression: { draftDueAt: { $type: 'date' } } });
|
|
204
205
|
escalationReviewSchema.index({ doctorAlertDueAt: 1 }, { partialFilterExpression: { doctorAlertDueAt: { $type: 'date' } } });
|
|
205
206
|
escalationReviewSchema.index({ patientCode: 1 }, { unique: true, partialFilterExpression: { closedAt: null } });
|
package/lib/routes/index.js
CHANGED
|
@@ -120,6 +120,7 @@ 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',
|
|
125
126
|
'GET /:id/timeline': 'getReviewTimelineController',
|
|
@@ -295,6 +296,7 @@ const builtInControllers = {
|
|
|
295
296
|
// Escalation review controllers
|
|
296
297
|
openEscalationReviewController: escalationReviewController.openEscalationReviewController,
|
|
297
298
|
listEscalationReviewsController: escalationReviewController.listEscalationReviewsController,
|
|
299
|
+
listReviewBoardController: escalationReviewController.listReviewBoardController,
|
|
298
300
|
getEscalationReviewController: escalationReviewController.getEscalationReviewController,
|
|
299
301
|
getReviewTimelineController: escalationReviewController.getReviewTimelineController,
|
|
300
302
|
setEscalationReviewCasesController: escalationReviewController.setEscalationReviewCasesController,
|
|
@@ -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
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
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
|
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
const { httpError } = require('../utils/httpErrorUtils');
|
|
2
|
+
const { getEscalationReview } = require('../models/escalationReviewModel');
|
|
3
|
+
|
|
4
|
+
const DONE_WINDOW_MS = 7 * 24 * 60 * 60 * 1000;
|
|
5
|
+
const URGENCY_RANK = { stat: 4, asap: 3, urgent: 2, routine: 1 };
|
|
6
|
+
const RETURNED_STATUSES = ['returned'];
|
|
7
|
+
const FOLLOWUP_STATUSES = ['indications_sent', 'indications_queued'];
|
|
8
|
+
const DONE_STATUSES = ['closed', 'cancelled'];
|
|
9
|
+
const BOARD_BUCKETS = ['pending', 'returned', 'followup', 'done'];
|
|
10
|
+
const BOARD_FILTER_PREFIX = 'clinical-review-';
|
|
11
|
+
|
|
12
|
+
const bucketQuery = (bucket, now) => {
|
|
13
|
+
switch (bucket) {
|
|
14
|
+
case 'pending':
|
|
15
|
+
return { closedAt: null, status: { $nin: [...RETURNED_STATUSES, ...FOLLOWUP_STATUSES, ...DONE_STATUSES] } };
|
|
16
|
+
case 'returned':
|
|
17
|
+
return { closedAt: null, status: { $in: RETURNED_STATUSES } };
|
|
18
|
+
case 'followup':
|
|
19
|
+
return { closedAt: null, status: { $in: FOLLOWUP_STATUSES } };
|
|
20
|
+
case 'done':
|
|
21
|
+
return { status: { $in: DONE_STATUSES }, closedAt: { $gte: new Date(now.getTime() - DONE_WINDOW_MS) } };
|
|
22
|
+
default:
|
|
23
|
+
throw httpError(400, `bucket must be one of: ${BOARD_BUCKETS.join(', ')}`);
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
const rankOf = (alarm) => URGENCY_RANK[alarm?.urgencyLevel] || 0;
|
|
28
|
+
|
|
29
|
+
const leadingAlarm = (alarms) => alarms.reduce((lead, alarm) => (
|
|
30
|
+
!lead || rankOf(alarm) > rankOf(lead) || (rankOf(alarm) === rankOf(lead) && alarm.raisedAt > lead.raisedAt) ? alarm : lead
|
|
31
|
+
), null);
|
|
32
|
+
|
|
33
|
+
const boardRowOf = (review, bucket) => {
|
|
34
|
+
const alarms = review.alarms || [];
|
|
35
|
+
const lead = leadingAlarm(alarms);
|
|
36
|
+
const raised = alarms.map((alarm) => alarm.raisedAt).filter(Boolean);
|
|
37
|
+
const since = raised.length ? new Date(Math.min(...raised.map(Number))) : review.createdAt;
|
|
38
|
+
return {
|
|
39
|
+
reviewId: String(review._id),
|
|
40
|
+
patientCode: review.patientCode,
|
|
41
|
+
bucket,
|
|
42
|
+
status: review.status,
|
|
43
|
+
urgency: lead?.urgencyLevel || null,
|
|
44
|
+
symptom: lead?.symptom || null,
|
|
45
|
+
alarmCount: alarms.length,
|
|
46
|
+
since,
|
|
47
|
+
closedAt: review.closedAt || null,
|
|
48
|
+
};
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
const byUrgencyThenAge = (a, b) => (URGENCY_RANK[b.urgency] || 0) - (URGENCY_RANK[a.urgency] || 0)
|
|
52
|
+
|| new Date(a.since) - new Date(b.since);
|
|
53
|
+
|
|
54
|
+
const byMostRecentlyClosed = (a, b) => new Date(b.closedAt) - new Date(a.closedAt);
|
|
55
|
+
|
|
56
|
+
async function listReviewBoard({ bucket, now = new Date() } = {}) {
|
|
57
|
+
const reviews = await getEscalationReview()
|
|
58
|
+
.find(bucketQuery(bucket, now), {
|
|
59
|
+
patientCode: 1, status: 1, 'alarms.urgencyLevel': 1, 'alarms.symptom': 1, 'alarms.raisedAt': 1, createdAt: 1, closedAt: 1,
|
|
60
|
+
})
|
|
61
|
+
.lean();
|
|
62
|
+
const rows = reviews.map((review) => boardRowOf(review, bucket))
|
|
63
|
+
.sort(bucket === 'done' ? byMostRecentlyClosed : byUrgencyThenAge);
|
|
64
|
+
const seen = new Set();
|
|
65
|
+
return rows.filter((row) => !seen.has(row.patientCode) && seen.add(row.patientCode));
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const boardBucketOfFilter = (filter) => {
|
|
69
|
+
if (typeof filter !== 'string' || !filter.startsWith(BOARD_FILTER_PREFIX)) return null;
|
|
70
|
+
const bucket = filter.slice(BOARD_FILTER_PREFIX.length);
|
|
71
|
+
return BOARD_BUCKETS.includes(bucket) ? bucket : null;
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
module.exports = {
|
|
75
|
+
BOARD_BUCKETS,
|
|
76
|
+
boardBucketOfFilter,
|
|
77
|
+
listReviewBoard,
|
|
78
|
+
};
|