@peopl-health/nexus 5.55.0-dev.8407 → 5.55.0-dev.8409
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,6 +12,7 @@ const {
|
|
|
12
12
|
openEscalationReview,
|
|
13
13
|
recordDoctorDecision,
|
|
14
14
|
sendApprovedIndications,
|
|
15
|
+
setReviewCases,
|
|
15
16
|
suggestDraft,
|
|
16
17
|
syncEscalationReviewCase,
|
|
17
18
|
} = require('../services/escalationReviewService');
|
|
@@ -27,8 +28,14 @@ const respondWithError = (res, error, action, context) => {
|
|
|
27
28
|
|
|
28
29
|
const openEscalationReviewController = async (req, res) => {
|
|
29
30
|
try {
|
|
30
|
-
const { code, patientCaseRecordId = null, triggeredBy } = req.body || {};
|
|
31
|
-
const { review, created } = await openEscalationReview({
|
|
31
|
+
const { code, patientCaseRecordId = null, patientCaseRecordIds = null, triggeredBy } = req.body || {};
|
|
32
|
+
const { review, created } = await openEscalationReview({
|
|
33
|
+
code,
|
|
34
|
+
patientCaseRecordId,
|
|
35
|
+
patientCaseRecordIds,
|
|
36
|
+
openedBy: triggeredBy,
|
|
37
|
+
requestId: req.requestId,
|
|
38
|
+
});
|
|
32
39
|
res.status(created ? 201 : 200).json({ success: true, review });
|
|
33
40
|
} catch (error) {
|
|
34
41
|
respondWithError(res, error, 'opening escalation review', { code: redactDirectIdentifiers(String(req.body?.code || '')) });
|
|
@@ -53,6 +60,16 @@ const listEscalationReviewsController = async (req, res) => {
|
|
|
53
60
|
}
|
|
54
61
|
};
|
|
55
62
|
|
|
63
|
+
const setEscalationReviewCasesController = async (req, res) => {
|
|
64
|
+
try {
|
|
65
|
+
const { patientCaseRecordIds, triggeredBy } = req.body || {};
|
|
66
|
+
const review = await setReviewCases({ id: req.params.id, patientCaseRecordIds, updatedBy: triggeredBy, requestId: req.requestId });
|
|
67
|
+
res.status(200).json({ success: true, review });
|
|
68
|
+
} catch (error) {
|
|
69
|
+
respondWithError(res, error, 'selecting escalation review cases', { reviewId: req.params?.id });
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
|
|
56
73
|
const addEscalationReviewCallLogController = async (req, res) => {
|
|
57
74
|
try {
|
|
58
75
|
const { outcome, notes, calledAt, triggeredBy } = req.body || {};
|
|
@@ -86,8 +103,8 @@ const addEscalationReviewDraftController = async (req, res) => {
|
|
|
86
103
|
const handOffEscalationReviewController = async (req, res) => {
|
|
87
104
|
try {
|
|
88
105
|
const { mode, physicianEmail = null, triggeredBy } = req.body || {};
|
|
89
|
-
const { review, caseMirror } = await handOffToDoctor({ id: req.params.id, mode, physicianEmail, sentBy: triggeredBy, requestId: req.requestId });
|
|
90
|
-
res.status(201).json({ success: true, review, caseMirror });
|
|
106
|
+
const { review, caseMirror, caseMirrors } = await handOffToDoctor({ id: req.params.id, mode, physicianEmail, sentBy: triggeredBy, requestId: req.requestId });
|
|
107
|
+
res.status(201).json({ success: true, review, caseMirror, caseMirrors });
|
|
91
108
|
} catch (error) {
|
|
92
109
|
respondWithError(res, error, 'handing escalation review to doctor', { reviewId: req.params?.id });
|
|
93
110
|
}
|
|
@@ -124,8 +141,8 @@ const recordEscalationReviewDecisionController = async (req, res) => {
|
|
|
124
141
|
const sendEscalationReviewIndicationsController = async (req, res) => {
|
|
125
142
|
try {
|
|
126
143
|
const { outsideWindowRoute = null, triggeredBy } = req.body || {};
|
|
127
|
-
const { review, caseMirror } = await sendApprovedIndications({ id: req.params.id, outsideWindowRoute, sentBy: triggeredBy, requestId: req.requestId });
|
|
128
|
-
res.status(201).json({ success: true, review, caseMirror });
|
|
144
|
+
const { review, caseMirror, caseMirrors } = await sendApprovedIndications({ id: req.params.id, outsideWindowRoute, sentBy: triggeredBy, requestId: req.requestId });
|
|
145
|
+
res.status(201).json({ success: true, review, caseMirror, caseMirrors });
|
|
129
146
|
} catch (error) {
|
|
130
147
|
respondWithError(res, error, 'sending escalation review indications', { reviewId: req.params?.id });
|
|
131
148
|
}
|
|
@@ -134,14 +151,14 @@ const sendEscalationReviewIndicationsController = async (req, res) => {
|
|
|
134
151
|
const closeEscalationReviewController = async (req, res) => {
|
|
135
152
|
try {
|
|
136
153
|
const { triggeredBy, deliveryConfirmed = false, interruptionNotes = null } = req.body || {};
|
|
137
|
-
const { review, caseMirror } = await closeEscalationReview({
|
|
154
|
+
const { review, caseMirror, caseMirrors } = await closeEscalationReview({
|
|
138
155
|
id: req.params.id,
|
|
139
156
|
closedBy: triggeredBy,
|
|
140
157
|
deliveryConfirmed,
|
|
141
158
|
interruptionNotes,
|
|
142
159
|
requestId: req.requestId,
|
|
143
160
|
});
|
|
144
|
-
res.status(200).json({ success: true, review, caseMirror });
|
|
161
|
+
res.status(200).json({ success: true, review, caseMirror, caseMirrors });
|
|
145
162
|
} catch (error) {
|
|
146
163
|
respondWithError(res, error, 'closing escalation review', { reviewId: req.params?.id });
|
|
147
164
|
}
|
|
@@ -160,12 +177,12 @@ const cancelEscalationReviewController = async (req, res) => {
|
|
|
160
177
|
const syncEscalationReviewCaseController = async (req, res) => {
|
|
161
178
|
try {
|
|
162
179
|
const { triggeredBy } = req.body || {};
|
|
163
|
-
const { review, caseMirror } = await syncEscalationReviewCase({ id: req.params.id, syncedBy: triggeredBy, requestId: req.requestId });
|
|
164
|
-
if (caseMirror === 'failed') {
|
|
165
|
-
res.status(502).json({ success: false, error: 'The patient case could not be updated', review, caseMirror });
|
|
180
|
+
const { review, caseMirror, caseMirrors } = await syncEscalationReviewCase({ id: req.params.id, syncedBy: triggeredBy, requestId: req.requestId });
|
|
181
|
+
if (caseMirror === 'failed' || caseMirror === 'partial') {
|
|
182
|
+
res.status(502).json({ success: false, error: 'The patient case could not be updated', review, caseMirror, caseMirrors });
|
|
166
183
|
return;
|
|
167
184
|
}
|
|
168
|
-
res.status(200).json({ success: true, review, caseMirror });
|
|
185
|
+
res.status(200).json({ success: true, review, caseMirror, caseMirrors });
|
|
169
186
|
} catch (error) {
|
|
170
187
|
respondWithError(res, error, 'syncing escalation review case', { reviewId: req.params?.id });
|
|
171
188
|
}
|
|
@@ -182,6 +199,7 @@ module.exports = {
|
|
|
182
199
|
openEscalationReviewController,
|
|
183
200
|
recordEscalationReviewDecisionController,
|
|
184
201
|
sendEscalationReviewIndicationsController,
|
|
202
|
+
setEscalationReviewCasesController,
|
|
185
203
|
suggestEscalationReviewDraftController,
|
|
186
204
|
syncEscalationReviewCaseController,
|
|
187
205
|
};
|
|
@@ -56,6 +56,7 @@ const patientDeliverySchema = new mongoose.Schema({
|
|
|
56
56
|
}, { timestamps: { createdAt: true, updatedAt: false } });
|
|
57
57
|
|
|
58
58
|
const pendingCaseMirrorSchema = new mongoose.Schema({
|
|
59
|
+
recordId: { type: String, required: true },
|
|
59
60
|
changes: { type: mongoose.Schema.Types.Mixed, required: true },
|
|
60
61
|
}, { timestamps: { createdAt: true, updatedAt: false } });
|
|
61
62
|
|
|
@@ -67,7 +68,7 @@ const interruptionResolutionSchema = new mongoose.Schema({
|
|
|
67
68
|
const escalationReviewSchema = new mongoose.Schema({
|
|
68
69
|
patientCode: { type: String, required: true },
|
|
69
70
|
patientCaseRecordId: { type: String, default: null },
|
|
70
|
-
|
|
71
|
+
patientCaseRecordIds: { type: [String], default: [] },
|
|
71
72
|
operationClaim: { type: String, default: null },
|
|
72
73
|
status: {
|
|
73
74
|
type: String,
|
|
@@ -101,7 +102,6 @@ const escalationReviewSchema = new mongoose.Schema({
|
|
|
101
102
|
}, { timestamps: true });
|
|
102
103
|
|
|
103
104
|
escalationReviewSchema.index({ patientCode: 1, createdAt: -1 });
|
|
104
|
-
escalationReviewSchema.index({ activeCaseKey: 1 }, { unique: true, sparse: true });
|
|
105
105
|
escalationReviewSchema.index({ patientCode: 1 }, { unique: true, partialFilterExpression: { closedAt: null } });
|
|
106
106
|
|
|
107
107
|
const getEscalationReview = () => {
|
package/lib/routes/index.js
CHANGED
|
@@ -120,6 +120,7 @@ const escalationReviewRouteDefinitions = {
|
|
|
120
120
|
'POST /': 'openEscalationReviewController',
|
|
121
121
|
'GET /by-patient/:code': 'listEscalationReviewsController',
|
|
122
122
|
'GET /:id': 'getEscalationReviewController',
|
|
123
|
+
'POST /:id/cases': 'setEscalationReviewCasesController',
|
|
123
124
|
'POST /:id/call-logs': 'addEscalationReviewCallLogController',
|
|
124
125
|
'POST /:id/draft-suggestions': 'suggestEscalationReviewDraftController',
|
|
125
126
|
'POST /:id/drafts': 'addEscalationReviewDraftController',
|
|
@@ -281,6 +282,7 @@ const builtInControllers = {
|
|
|
281
282
|
openEscalationReviewController: escalationReviewController.openEscalationReviewController,
|
|
282
283
|
listEscalationReviewsController: escalationReviewController.listEscalationReviewsController,
|
|
283
284
|
getEscalationReviewController: escalationReviewController.getEscalationReviewController,
|
|
285
|
+
setEscalationReviewCasesController: escalationReviewController.setEscalationReviewCasesController,
|
|
284
286
|
addEscalationReviewCallLogController: escalationReviewController.addEscalationReviewCallLogController,
|
|
285
287
|
suggestEscalationReviewDraftController: escalationReviewController.suggestEscalationReviewDraftController,
|
|
286
288
|
addEscalationReviewDraftController: escalationReviewController.addEscalationReviewDraftController,
|
|
@@ -66,7 +66,9 @@ const configured = (key) => {
|
|
|
66
66
|
|
|
67
67
|
const statusAfter = (decision) => (decision === 'edited_approved' ? 'approved' : decision);
|
|
68
68
|
|
|
69
|
-
const
|
|
69
|
+
const caseIdsOf = (review) => (Array.isArray(review.patientCaseRecordIds)
|
|
70
|
+
? review.patientCaseRecordIds
|
|
71
|
+
: (review.patientCaseRecordId ? [review.patientCaseRecordId] : []));
|
|
70
72
|
|
|
71
73
|
const redactedError = (error) => redactDirectIdentifiers(String(error?.message || ''));
|
|
72
74
|
|
|
@@ -82,9 +84,17 @@ const audit = (review, updatedBy, requestId, changes) => logPatientChange({
|
|
|
82
84
|
changes: { escalationReviewId: String(review._id), ...changes },
|
|
83
85
|
});
|
|
84
86
|
|
|
85
|
-
const withCaseMirror = (review, update, changes) =>
|
|
86
|
-
|
|
87
|
-
|
|
87
|
+
const withCaseMirror = (review, update, changes) => {
|
|
88
|
+
const recordIds = caseIdsOf(review);
|
|
89
|
+
if (!recordIds.length) return update;
|
|
90
|
+
return {
|
|
91
|
+
...update,
|
|
92
|
+
$push: {
|
|
93
|
+
...update.$push,
|
|
94
|
+
pendingCaseMirrors: { $each: recordIds.map((recordId) => ({ recordId, changes })) },
|
|
95
|
+
},
|
|
96
|
+
};
|
|
97
|
+
};
|
|
88
98
|
|
|
89
99
|
async function updateReview(id, allowedStatuses, update, extraFilter = {}) {
|
|
90
100
|
const EscalationReview = getEscalationReview();
|
|
@@ -140,71 +150,102 @@ async function completeOperation(id, claim, fromStatus, recordUpdate, nextStatus
|
|
|
140
150
|
throw httpError(409, STATUS_CONFLICT);
|
|
141
151
|
}
|
|
142
152
|
|
|
143
|
-
async function
|
|
144
|
-
if (
|
|
145
|
-
const
|
|
146
|
-
{
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
return released || review;
|
|
153
|
+
async function supersededCaseIds(EscalationReview, review) {
|
|
154
|
+
if (!review.closedAt) return new Set();
|
|
155
|
+
const owner = await EscalationReview
|
|
156
|
+
.findOne({ patientCode: review.patientCode, closedAt: null }, { patientCaseRecordIds: 1, patientCaseRecordId: 1 })
|
|
157
|
+
.lean();
|
|
158
|
+
if (!owner || String(owner._id) === String(review._id)) return new Set();
|
|
159
|
+
return new Set(caseIdsOf(owner));
|
|
151
160
|
}
|
|
152
161
|
|
|
162
|
+
const summarise = (outcomes) => {
|
|
163
|
+
if (!outcomes.length) return 'skipped';
|
|
164
|
+
if (outcomes.every((outcome) => outcome.status === 'superseded')) return 'superseded';
|
|
165
|
+
if (outcomes.every((outcome) => outcome.status === 'failed')) return 'failed';
|
|
166
|
+
if (outcomes.some((outcome) => outcome.status === 'failed')) return 'partial';
|
|
167
|
+
return 'updated';
|
|
168
|
+
};
|
|
169
|
+
|
|
153
170
|
async function syncCase(review, operator, requestId) {
|
|
154
|
-
|
|
171
|
+
const recordIds = caseIdsOf(review);
|
|
172
|
+
if (!recordIds.length) return { review, caseMirror: 'skipped', caseMirrors: [] };
|
|
155
173
|
const EscalationReview = getEscalationReview();
|
|
156
174
|
const pending = review.pendingCaseMirrors || [];
|
|
157
|
-
if (!pending.length) return { review
|
|
158
|
-
|
|
159
|
-
const
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
175
|
+
if (!pending.length) return { review, caseMirror: 'updated', caseMirrors: [] };
|
|
176
|
+
|
|
177
|
+
const superseded = await supersededCaseIds(EscalationReview, review);
|
|
178
|
+
const covered = new Set(recordIds);
|
|
179
|
+
const byRecord = new Map();
|
|
180
|
+
const abandoned = [];
|
|
181
|
+
for (const mirror of pending) {
|
|
182
|
+
const recordId = mirror.recordId ?? review.patientCaseRecordId;
|
|
183
|
+
if (recordId && !covered.has(recordId)) {
|
|
184
|
+
abandoned.push(mirror._id);
|
|
185
|
+
continue;
|
|
186
|
+
}
|
|
187
|
+
if (!recordId) {
|
|
188
|
+
logger.error('[EscalationReview] A queued case change names no case and the review has none', {
|
|
189
|
+
escalationReviewId: String(review._id),
|
|
190
|
+
});
|
|
191
|
+
continue;
|
|
192
|
+
}
|
|
193
|
+
const entries = byRecord.get(recordId) || [];
|
|
194
|
+
entries.push(mirror);
|
|
195
|
+
byRecord.set(recordId, entries);
|
|
173
196
|
}
|
|
174
197
|
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
198
|
+
const applied = await Promise.all([...byRecord].map(async ([recordId, entries]) => {
|
|
199
|
+
const ids = entries.map((entry) => entry._id);
|
|
200
|
+
if (superseded.has(recordId)) {
|
|
201
|
+
logger.warn('[EscalationReview] Dropping case changes a newer review has superseded', {
|
|
202
|
+
escalationReviewId: String(review._id),
|
|
203
|
+
recordId,
|
|
204
|
+
});
|
|
205
|
+
return { outcome: { recordId, status: 'superseded' }, ids };
|
|
206
|
+
}
|
|
207
|
+
try {
|
|
208
|
+
await updatePatientTask({
|
|
209
|
+
code: review.patientCode,
|
|
210
|
+
kind: 'cases',
|
|
211
|
+
recordId,
|
|
212
|
+
changes: Object.assign({}, ...entries.map((entry) => entry.changes)),
|
|
213
|
+
updatedBy: operator,
|
|
214
|
+
source: AUDIT_SOURCE,
|
|
215
|
+
requestId,
|
|
216
|
+
});
|
|
217
|
+
return { outcome: { recordId, status: 'updated' }, ids };
|
|
218
|
+
} catch (error) {
|
|
219
|
+
logger.error('[EscalationReview] Patient case mirror failed', {
|
|
220
|
+
escalationReviewId: String(review._id),
|
|
221
|
+
recordId,
|
|
222
|
+
error: redactedError(error),
|
|
223
|
+
stack: redactedStack(error),
|
|
224
|
+
});
|
|
225
|
+
return { outcome: { recordId, status: 'failed' }, ids: [] };
|
|
226
|
+
}
|
|
227
|
+
}));
|
|
228
|
+
|
|
229
|
+
const outcomes = applied.map((entry) => entry.outcome);
|
|
230
|
+
const settled = [...abandoned, ...applied.flatMap((entry) => entry.ids)];
|
|
231
|
+
|
|
232
|
+
const caseMirror = summarise(outcomes);
|
|
233
|
+
if (!settled.length) return { review, caseMirror, caseMirrors: outcomes };
|
|
193
234
|
|
|
194
235
|
try {
|
|
195
236
|
const synced = await EscalationReview.findOneAndUpdate(
|
|
196
237
|
{ _id: review._id },
|
|
197
|
-
{ $pull: { pendingCaseMirrors: { _id: { $in:
|
|
238
|
+
{ $pull: { pendingCaseMirrors: { _id: { $in: settled } } } },
|
|
198
239
|
{ new: true },
|
|
199
240
|
).lean();
|
|
200
|
-
return { review:
|
|
241
|
+
return { review: synced || review, caseMirror, caseMirrors: outcomes };
|
|
201
242
|
} catch (error) {
|
|
202
243
|
logger.error('[EscalationReview] Could not record the applied case changes', {
|
|
203
244
|
escalationReviewId: String(review._id),
|
|
204
245
|
error: redactedError(error),
|
|
205
246
|
stack: redactedStack(error),
|
|
206
247
|
});
|
|
207
|
-
return { review, caseMirror:
|
|
248
|
+
return { review, caseMirror, caseMirrors: outcomes };
|
|
208
249
|
}
|
|
209
250
|
}
|
|
210
251
|
|
|
@@ -268,20 +309,36 @@ function ensureActiveReviewIndex(EscalationReview) {
|
|
|
268
309
|
|
|
269
310
|
const _resetEscalationReviewIndexes = () => { indexesReady = new WeakMap(); };
|
|
270
311
|
|
|
271
|
-
async function
|
|
312
|
+
async function requestedCaseIds({ patientCode, patientCaseRecordId, patientCaseRecordIds }) {
|
|
313
|
+
const requested = patientCaseRecordIds ?? (patientCaseRecordId === null ? [] : [patientCaseRecordId]);
|
|
314
|
+
if (!Array.isArray(requested)) throw httpError(400, 'patientCaseRecordIds must be an array of case ids');
|
|
315
|
+
if (requested.some((recordId) => !String(recordId ?? '').trim())) {
|
|
316
|
+
throw httpError(400, 'patientCaseRecordIds must not contain blank case ids');
|
|
317
|
+
}
|
|
318
|
+
const recordIds = [...new Set(requested.map((recordId) => String(recordId ?? '').trim()).filter(Boolean))];
|
|
319
|
+
if (!recordIds.length) return [];
|
|
320
|
+
|
|
321
|
+
const { cases } = await getPatientTasks(patientCode);
|
|
322
|
+
const known = new Set(cases.map((patientCase) => patientCase.recordId));
|
|
323
|
+
const stranger = recordIds.find((recordId) => !known.has(recordId));
|
|
324
|
+
if (stranger) throw httpError(404, 'Patient case not found for this patient');
|
|
325
|
+
return recordIds;
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
async function openEscalationReview({
|
|
329
|
+
code,
|
|
330
|
+
patientCaseRecordId = null,
|
|
331
|
+
patientCaseRecordIds = null,
|
|
332
|
+
openedBy,
|
|
333
|
+
requestId = null,
|
|
334
|
+
} = {}) {
|
|
272
335
|
const patientCode = patientCodeOf(code);
|
|
273
336
|
const operator = requiredText(openedBy, 'openedBy');
|
|
274
337
|
const patient = await getPatientInformation(patientCode);
|
|
275
338
|
if (!patient) throw httpError(404, 'Patient not found');
|
|
276
339
|
|
|
277
|
-
const
|
|
278
|
-
|
|
279
|
-
const { cases } = await getPatientTasks(patientCode);
|
|
280
|
-
if (!cases.some((patientCase) => patientCase.recordId === patientCaseRecordId)) {
|
|
281
|
-
throw httpError(404, 'Patient case not found for this patient');
|
|
282
|
-
}
|
|
283
|
-
Object.assign(fields, { patientCaseRecordId, activeCaseKey: activeCaseKeyOf(patientCode, patientCaseRecordId) });
|
|
284
|
-
}
|
|
340
|
+
const recordIds = await requestedCaseIds({ patientCode, patientCaseRecordId, patientCaseRecordIds });
|
|
341
|
+
const fields = { patientCode, openedBy: operator, patientCaseRecordIds: recordIds };
|
|
285
342
|
|
|
286
343
|
const EscalationReview = getEscalationReview();
|
|
287
344
|
await ensureActiveReviewIndex(EscalationReview);
|
|
@@ -291,10 +348,10 @@ async function openEscalationReview({ code, patientCaseRecordId = null, openedBy
|
|
|
291
348
|
} catch (error) {
|
|
292
349
|
if (error?.code !== DUPLICATE_KEY_ERROR) throw error;
|
|
293
350
|
const existing = await EscalationReview.findOne({ patientCode, closedAt: null }).lean();
|
|
294
|
-
if (!existing) throw httpError(409, '
|
|
351
|
+
if (!existing) throw httpError(409, 'This patient already has a review being opened; try again in a moment');
|
|
295
352
|
return { review: existing, created: false };
|
|
296
353
|
}
|
|
297
|
-
await audit(review, operator, requestId, { action: 'opened',
|
|
354
|
+
await audit(review, operator, requestId, { action: 'opened', patientCaseRecordIds: recordIds });
|
|
298
355
|
return { review, created: true };
|
|
299
356
|
}
|
|
300
357
|
|
|
@@ -343,6 +400,55 @@ async function appendKeepingDraft(id, entry) {
|
|
|
343
400
|
throw httpError(409, STATUS_CONFLICT);
|
|
344
401
|
}
|
|
345
402
|
|
|
403
|
+
async function setReviewCases({ id, patientCaseRecordIds, updatedBy, requestId = null } = {}) {
|
|
404
|
+
const reviewId = reviewIdOf(id);
|
|
405
|
+
const operator = requiredText(updatedBy, 'updatedBy');
|
|
406
|
+
if (!Array.isArray(patientCaseRecordIds)) throw httpError(400, 'patientCaseRecordIds must be an array of case ids');
|
|
407
|
+
const current = await loadReview(reviewId);
|
|
408
|
+
if (!DOCUMENTING_STATUSES.includes(current.status)) throw httpError(409, STATUS_CONFLICT);
|
|
409
|
+
|
|
410
|
+
const recordIds = await requestedCaseIds({ patientCode: current.patientCode, patientCaseRecordIds });
|
|
411
|
+
|
|
412
|
+
const review = await updateReview(reviewId, DOCUMENTING_STATUSES, [{
|
|
413
|
+
$set: {
|
|
414
|
+
patientCaseRecordIds: recordIds,
|
|
415
|
+
status: {
|
|
416
|
+
$cond: [
|
|
417
|
+
{
|
|
418
|
+
$and: [
|
|
419
|
+
{ $eq: ['$status', 'draft_ready'] },
|
|
420
|
+
{
|
|
421
|
+
$not: [{
|
|
422
|
+
$setEquals: [
|
|
423
|
+
{
|
|
424
|
+
$ifNull: [
|
|
425
|
+
'$patientCaseRecordIds',
|
|
426
|
+
{ $cond: [{ $ifNull: ['$patientCaseRecordId', false] }, ['$patientCaseRecordId'], []] },
|
|
427
|
+
],
|
|
428
|
+
},
|
|
429
|
+
recordIds,
|
|
430
|
+
],
|
|
431
|
+
}],
|
|
432
|
+
},
|
|
433
|
+
],
|
|
434
|
+
},
|
|
435
|
+
'documenting',
|
|
436
|
+
'$status',
|
|
437
|
+
],
|
|
438
|
+
},
|
|
439
|
+
pendingCaseMirrors: {
|
|
440
|
+
$filter: {
|
|
441
|
+
input: { $ifNull: ['$pendingCaseMirrors', []] },
|
|
442
|
+
as: 'mirror',
|
|
443
|
+
cond: { $in: [{ $ifNull: ['$$mirror.recordId', null] }, [...recordIds, null]] },
|
|
444
|
+
},
|
|
445
|
+
},
|
|
446
|
+
},
|
|
447
|
+
}]);
|
|
448
|
+
await audit(review, operator, requestId, { action: 'cases_selected', patientCaseRecordIds: recordIds });
|
|
449
|
+
return review;
|
|
450
|
+
}
|
|
451
|
+
|
|
346
452
|
async function addCallLog({ id, outcome, notes = null, calledAt = null, loggedBy, requestId = null } = {}) {
|
|
347
453
|
const reviewId = reviewIdOf(id);
|
|
348
454
|
const operator = requiredText(loggedBy, 'loggedBy');
|
|
@@ -599,7 +705,6 @@ async function cancelEscalationReview({ id, cancelledBy, reason = null, requestI
|
|
|
599
705
|
const operator = requiredText(cancelledBy, 'cancelledBy');
|
|
600
706
|
const review = await updateReview(reviewId, CANCELLABLE_STATUSES, {
|
|
601
707
|
$set: { status: 'cancelled', closedBy: operator, closedAt: new Date() },
|
|
602
|
-
$unset: { activeCaseKey: '' },
|
|
603
708
|
});
|
|
604
709
|
await audit(review, operator, requestId, { action: 'cancelled', reason: optionalText(reason) });
|
|
605
710
|
return review;
|
|
@@ -623,6 +728,7 @@ module.exports = {
|
|
|
623
728
|
openEscalationReview,
|
|
624
729
|
recordDoctorDecision,
|
|
625
730
|
sendApprovedIndications,
|
|
731
|
+
setReviewCases,
|
|
626
732
|
suggestDraft,
|
|
627
733
|
syncEscalationReviewCase,
|
|
628
734
|
};
|