@peopl-health/nexus 5.71.27 → 5.71.29
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/fhir/index.js
CHANGED
|
@@ -15,7 +15,7 @@ const {
|
|
|
15
15
|
recordRecommendationReview,
|
|
16
16
|
} = require('./services/recommendationService');
|
|
17
17
|
const { storeContingency, readContingencies } = require('./services/contingencyService');
|
|
18
|
-
const { readDispatchedEscalations, storeEscalationCase } = require('./services/escalationService');
|
|
18
|
+
const { readDispatchedEscalations, readEscalationCases, storeEscalationCase } = require('./services/escalationService');
|
|
19
19
|
const { storeIntervention, readInterventions } = require('./services/interventionService');
|
|
20
20
|
const { storeCluster, readClusters } = require('./services/clusterService');
|
|
21
21
|
const { storeRoutingDecision, readRoutingDecisions } = require('./services/routingService');
|
|
@@ -71,6 +71,7 @@ module.exports = {
|
|
|
71
71
|
readContingencies,
|
|
72
72
|
readDispatchedEscalations,
|
|
73
73
|
storeEscalationCase,
|
|
74
|
+
readEscalationCases,
|
|
74
75
|
storeIntervention,
|
|
75
76
|
readInterventions,
|
|
76
77
|
storeCluster,
|
|
@@ -13,6 +13,7 @@ const {
|
|
|
13
13
|
ESCALATION_CASE_SLUG,
|
|
14
14
|
ESCALATION_CASE_STATUS_SLUG,
|
|
15
15
|
ESCALATION_DECISION_SLUG,
|
|
16
|
+
SYMPTOM_CASE_SLUG,
|
|
16
17
|
} = require('../constants/projectionSlugs');
|
|
17
18
|
const { SEVERITY_TO_PRIORITY } = require('../constants/severityPriority');
|
|
18
19
|
const { caseStatusOf } = require('../constants/escalationCaseStatus');
|
|
@@ -74,7 +75,12 @@ class Task {
|
|
|
74
75
|
requester: reference('Device', deviceId),
|
|
75
76
|
executionPeriod: { start: toIso(review.createdAt), ...(review.closedAt ? { end: toIso(review.closedAt) } : {}) },
|
|
76
77
|
};
|
|
77
|
-
if (conditionIds.length)
|
|
78
|
+
if (conditionIds.length) {
|
|
79
|
+
payload.reason = conditionIds.map((id) => {
|
|
80
|
+
const symptom = { ...reference('Condition', id), type: 'Condition', identifier: identifier(SYMPTOM_CASE_SLUG, id) };
|
|
81
|
+
return { reference: symptom };
|
|
82
|
+
});
|
|
83
|
+
}
|
|
78
84
|
// Only a code reaches FHIR: the operator's cancellation note is free text.
|
|
79
85
|
if (review.status === 'cancelled') {
|
|
80
86
|
const reasonCode = String(review.cancellationReason ?? '').startsWith('no_requiere') ? 'no_requiere' : 'cancelado_por_equipo';
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
const { getDeviceId } = require('../config/fhirConfig');
|
|
2
2
|
const { fhirId } = require('../helpers/fhirHelper');
|
|
3
|
-
const {
|
|
3
|
+
const { identifierValue } = require('../helpers/fhirReadHelper');
|
|
4
|
+
const { ESCALATION_SLUG, ESCALATION_CASE_SLUG } = require('../constants/projectionSlugs');
|
|
4
5
|
const { getFhirStore } = require('../stores/fhirStore');
|
|
5
6
|
const { fromFhir } = require('../projections/dispatchedEscalationProjection');
|
|
6
7
|
const { Task } = require('../resources/Task');
|
|
@@ -43,7 +44,42 @@ async function storeEscalationCase({ review, priority, conditionIds }) {
|
|
|
43
44
|
return { ids: stored };
|
|
44
45
|
}
|
|
45
46
|
|
|
47
|
+
const CLOSED_CASE_STATUSES = ['completed', 'cancelled'];
|
|
48
|
+
|
|
49
|
+
function caseFromTask(task) {
|
|
50
|
+
return {
|
|
51
|
+
reviewId: identifierValue(task, ESCALATION_CASE_SLUG),
|
|
52
|
+
status: task.status,
|
|
53
|
+
businessStatus: task.businessStatus?.coding?.[0]?.code ?? null,
|
|
54
|
+
label: task.businessStatus?.text ?? null,
|
|
55
|
+
isOpen: !CLOSED_CASE_STATUSES.includes(task.status),
|
|
56
|
+
priority: task.priority ?? null,
|
|
57
|
+
// Only references that carry the original symptom case id; a Task written before they did reads as none.
|
|
58
|
+
conditionIds: (task.reason ?? []).map((reason) => reason.reference?.identifier?.value).filter(Boolean),
|
|
59
|
+
decision: task.output?.find((output) => output.type?.text === 'decision')?.valueCodeableConcept?.coding?.[0]?.code ?? null,
|
|
60
|
+
statusReason: task.statusReason?.concept?.text ?? null,
|
|
61
|
+
openedAt: task.authoredOn ?? null,
|
|
62
|
+
lastModified: task.lastModified ?? null,
|
|
63
|
+
closedAt: task.executionPeriod?.end ?? null,
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// A patient's escalation cases, newest first; `openOnly` keeps the ones whose Task is not completed or cancelled.
|
|
68
|
+
async function readEscalationCases({ patientId, openOnly = false }) {
|
|
69
|
+
if (typeof patientId !== 'string' || !patientId.trim()) {
|
|
70
|
+
throw new Error('readEscalationCases requires a non-empty patientId');
|
|
71
|
+
}
|
|
72
|
+
const found = await getFhirStore().find({
|
|
73
|
+
patient: `Patient/${fhirId(patientId)}`,
|
|
74
|
+
resourceType: 'Task',
|
|
75
|
+
identifierSlug: ESCALATION_CASE_SLUG,
|
|
76
|
+
});
|
|
77
|
+
const cases = found.map(caseFromTask);
|
|
78
|
+
return openOnly ? cases.filter((escalationCase) => escalationCase.isOpen) : cases;
|
|
79
|
+
}
|
|
80
|
+
|
|
46
81
|
module.exports = {
|
|
47
82
|
readDispatchedEscalations,
|
|
83
|
+
readEscalationCases,
|
|
48
84
|
storeEscalationCase,
|
|
49
85
|
};
|
|
@@ -30,10 +30,16 @@ async function storeResources(resources, { store = null, merge = null } = {}) {
|
|
|
30
30
|
return { stored };
|
|
31
31
|
}
|
|
32
32
|
|
|
33
|
-
// A plan rewrite reprojects its Tasks with no `output`, where the outcomes and
|
|
33
|
+
// A plan rewrite reprojects its Tasks as freshly `requested` with no `output`, where the outcomes and
|
|
34
|
+
// verdicts live; an outcome also moved the status, so both survive the rewrite.
|
|
34
35
|
const keepTaskOutput = {
|
|
35
36
|
merge: (prior, incoming) => (prior.resourceType === 'Task' && prior.output?.length
|
|
36
|
-
? {
|
|
37
|
+
? {
|
|
38
|
+
...incoming,
|
|
39
|
+
output: prior.output,
|
|
40
|
+
status: prior.status || incoming.status,
|
|
41
|
+
...(prior.lastModified ? { lastModified: prior.lastModified } : {}),
|
|
42
|
+
}
|
|
37
43
|
: incoming),
|
|
38
44
|
};
|
|
39
45
|
|
|
@@ -33,6 +33,10 @@ const { safetyGateExists } = require('./riskService');
|
|
|
33
33
|
const { getCatalog } = require('./terminologyService');
|
|
34
34
|
|
|
35
35
|
const OUTCOME_VALUES = ['improved', 'partial', 'no_relief', 'worsened', 'not_tried', 'unknown'];
|
|
36
|
+
// The patient tried it: the recommendation is done. not_tried / unknown leave it open. With no outcome (every
|
|
37
|
+
// triage record, and adherence-only chat records), adherence full/partial ("la siguió", "en parte") means it was tried.
|
|
38
|
+
const TRIED_OUTCOMES = new Set(['improved', 'partial', 'no_relief', 'worsened']);
|
|
39
|
+
const TRIED_ADHERENCE = new Set(['full', 'partial']);
|
|
36
40
|
const ADHERENCE_VALUES = ['full', 'partial', 'none'];
|
|
37
41
|
const CHAT_ACTIVE_WINDOW_MS = 7 * 24 * 60 * 60 * 1000;
|
|
38
42
|
const SOURCE_VALUES = ['chat', 'triage'];
|
|
@@ -162,12 +166,22 @@ async function recordRecommendationOutcome({
|
|
|
162
166
|
}
|
|
163
167
|
const target = await resolveTask({ patientId, recommendationId, ctcaeTerm });
|
|
164
168
|
if (!target) return { recorded: false };
|
|
165
|
-
const
|
|
169
|
+
const recordedAt = toIso(new Date());
|
|
170
|
+
const output = outcomeEntry({ outcome: resolvedOutcome, adherence, notes, source, recordedAt, eventKey });
|
|
166
171
|
let duplicate = false;
|
|
167
172
|
await storeResources([target.task], {
|
|
168
|
-
merge: (prior
|
|
173
|
+
merge: (prior) => {
|
|
169
174
|
duplicate = Boolean(eventKey) && hasOutcomeEvent(prior, eventKey);
|
|
170
|
-
|
|
175
|
+
if (duplicate) return prior;
|
|
176
|
+
const done = prior.status === 'completed'
|
|
177
|
+
|| TRIED_OUTCOMES.has(resolvedOutcome)
|
|
178
|
+
|| (!outcome && TRIED_ADHERENCE.has(adherence));
|
|
179
|
+
return {
|
|
180
|
+
...prior,
|
|
181
|
+
status: done ? 'completed' : 'in-progress',
|
|
182
|
+
lastModified: recordedAt,
|
|
183
|
+
output: [...(prior.output || []), output],
|
|
184
|
+
};
|
|
171
185
|
},
|
|
172
186
|
});
|
|
173
187
|
return {
|
|
@@ -224,13 +238,13 @@ async function recordRecommendationReview({
|
|
|
224
238
|
let duplicate = false;
|
|
225
239
|
let retained = null;
|
|
226
240
|
const write = await storeResources([target.task], {
|
|
227
|
-
merge: (prior
|
|
241
|
+
merge: (prior) => {
|
|
228
242
|
duplicate = Boolean(key) && hasReviewEvent(prior, key);
|
|
229
243
|
if (duplicate) {
|
|
230
244
|
retained = reviewByEvent(prior, key);
|
|
231
|
-
return
|
|
245
|
+
return prior;
|
|
232
246
|
}
|
|
233
|
-
return { ...
|
|
247
|
+
return { ...prior, output: [...(prior.output || []), output] };
|
|
234
248
|
},
|
|
235
249
|
});
|
|
236
250
|
if (write.skipped) return { recorded: false, skipped: write.skipped, recommendation_id: target.recommendationId };
|