-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaggregateRequirements.ts
327 lines (305 loc) · 11.9 KB
/
aggregateRequirements.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
import fhir4, { Extension, Coding } from 'fhir/r4';
import { RuntimeSettings } from '../config/settings';
import {
KeyFilterElementForResourceType,
ResourceTypesWithoutKeyFilterElements,
getElementPathWithoutResouceTypePrefix
} from './elementDetails';
import { logger } from '../common/logger';
import * as errMsg from '../common/errors';
export type CodeFilter = {
path: string;
code?: Coding[];
valueSet?: string;
extension: Extension[];
};
export type DataReqOutputs = {
type: string;
profile: string;
extension: Extension[];
mustSupport: string[] | undefined;
_mustSupport: { extension: Extension[] }[];
codeFilter: CodeFilter[];
};
export function reorgDataRequirementWithMeasure(
bundles: {
fileName: string;
json: string;
}[],
settings: RuntimeSettings
): DataReqOutputs[] {
const dataReqOutputs: DataReqOutputs[] = [];
bundles.forEach((b: { fileName: string; json: string }) => {
const bundle = JSON.parse(b.json) as fhir4.Bundle;
let measureName: string = '';
let measureUrl: string = '';
let measureId: string = '';
let measureFlg = false;
let measureExt: Extension;
bundle.entry?.forEach(entry => {
if (entry.resource?.resourceType === 'Measure') {
if (measureFlg) {
logger.error(`Multiple measures in file ${b.fileName}`);
}
measureFlg = true;
measureName = entry.resource.name ?? '';
measureId = entry.resource.id ?? '';
if (measureId.length === 0) {
logger.warn(`Measure with no id in ${b.fileName}, will process all libraries`);
}
measureUrl = entry.resource.url ?? '';
measureExt = {
url: settings.measureExtensionURL,
valueString: entry.resource.url
};
}
});
if (measureName === '') {
logger.error(`no measure found in file ${b.fileName}`);
return dataReqOutputs;
}
logger.info('');
logger.info(`** Processing measure ${measureName} (${measureUrl}) in file ${b.fileName}`);
if (!settings.measureLink.has(measureUrl)) {
logger.warn(`Measure link details not present for measure ${measureName} (${measureUrl})`);
}
// Extract Libraries only if there's Measure in the bundle
if (measureFlg) {
bundle.entry?.forEach(entry => {
// process data requirements on the library with the same id as the measure (or all libraries if no id on the measure)
if (
entry.resource?.resourceType === 'Library' &&
(measureId.length === 0 || measureId === entry.resource?.id)
) {
logger.info(`**** Processing data requirements in library with id ${entry.resource!.id}`);
entry.resource.dataRequirement?.forEach(item => {
if (item.profile) {
const p: string = item.profile[0];
// filter check
if (!ResourceTypesWithoutKeyFilterElements.has(item.type)) {
if (KeyFilterElementForResourceType.has(item.type)) {
const filterElement = getElementPathWithoutResouceTypePrefix(
KeyFilterElementForResourceType.get(item.type)!,
item.type
);
const hasFilterOnElement: boolean = (
item.codeFilter?.map(oneCodeFilter => {
if (
oneCodeFilter.path === filterElement ||
`${oneCodeFilter.path}[x]` === filterElement
) {
return true;
} else {
return false;
}
}) ?? []
).reduce(
(wasAlreadyFound: boolean, foundHere: boolean) => wasAlreadyFound || foundHere,
false
);
if (!hasFilterOnElement) {
logger.error(errMsg.noFilterOnKeyElementMsg(item.type, p, filterElement));
}
} else {
logger.error(errMsg.keyFilterElementNotKnownMsg(item.type, p));
}
}
if (!dataReqOutputs.some(out => out.profile.includes(p))) {
// Add a new profile based Data requirement output
const ms_ext: { extension: Extension[] }[] = [];
item.mustSupport?.forEach(function () {
ms_ext.push({ extension: [measureExt] });
});
const new_cf: CodeFilter[] = getNormalizedCodeFilterList(
measureExt,
item.type,
item.codeFilter
);
let new_ms: string[] = [];
if (item.mustSupport) new_ms = item.mustSupport;
dataReqOutputs.push({
type: item.type ?? '',
profile: p,
extension: [measureExt],
mustSupport: new_ms,
_mustSupport: ms_ext,
codeFilter: new_cf
});
} else {
dataReqOutputs.forEach(dro => {
if (dro.profile === p) {
if (
!dro.extension.some(
ext =>
ext.url === settings.measureExtensionURL &&
ext.valueString === measureExt.valueString
)
) {
dro.extension.push(measureExt);
}
item.mustSupport?.forEach(ms => {
if (dro.mustSupport?.includes(ms)) {
if (
!dro._mustSupport[dro.mustSupport?.indexOf(ms)].extension.some(
ext =>
ext.url === settings.measureExtensionURL &&
ext.valueString === measureExt.valueString
)
) {
dro._mustSupport[dro.mustSupport?.indexOf(ms)].extension.push(measureExt);
}
} else {
dro.mustSupport?.push(ms);
dro._mustSupport.push({
extension: [measureExt]
});
}
});
const new_cf: CodeFilter[] = getNormalizedCodeFilterList(
measureExt,
item.type,
item.codeFilter
);
new_cf.forEach(it_cf => {
if (it_cf.valueSet) {
const matchingFilters = dro.codeFilter.filter(
oneCodeFilter =>
oneCodeFilter.valueSet === it_cf.valueSet &&
oneCodeFilter.path === it_cf.path
);
if (matchingFilters.length === 0) {
// no filter entry for this value set yet, add
dro.codeFilter?.push(it_cf);
} else if (matchingFilters.length === 1) {
if (
!matchingFilters
.at(0)!
.extension.some(
ext =>
ext.url === settings.measureExtensionURL &&
ext.valueString === measureExt.valueString
)
) {
// measure not present in the list of measure using this filter, add
matchingFilters.at(0)!.extension.push(measureExt);
}
} else {
logger.error(`multiple matching filters for value set ${it_cf.valueSet}`);
}
}
if (it_cf.code) {
const matchingFilters = dro.codeFilter.filter(
oneCodeFilter =>
oneCodeFilter.code?.at(0)?.system === it_cf.code?.at(0)!.system &&
oneCodeFilter.code?.at(0)?.code === it_cf.code?.at(0)!.code &&
oneCodeFilter.path === it_cf.path
);
if (matchingFilters.length === 0) {
// no filter entry for this value set yet, add
dro.codeFilter?.push(it_cf);
} else if (matchingFilters.length === 1) {
if (
!matchingFilters
.at(0)!
.extension.some(
ext =>
ext.url === settings.measureExtensionURL &&
ext.valueString === measureExt.valueString
)
) {
// measure not present in the list of measure using this filter, add
matchingFilters.at(0)!.extension.push(measureExt);
}
} else {
logger.error(`multiple matching filters for code ${it_cf.code.at(0)}`);
}
}
});
}
});
}
}
});
}
});
}
});
return dataReqOutputs;
}
// split out code filters so that each entry has exactly 1 code or a value set
// if any encountered that have extra codes or codes and value sets, then split into multiple entries with one each
// if any encountered with no code or value set, log an error and continue without using
function getNormalizedCodeFilterList(
measureExt: fhir4.Extension,
resourceType: string,
codeFilters?: fhir4.DataRequirementCodeFilter[]
): CodeFilter[] {
if (!codeFilters) {
return [];
}
const new_cf: CodeFilter[] = [];
codeFilters.forEach(cf => {
if (cf.valueSet && cf.code) {
logger.warn(
`code filter entry with both code and value set found, created entries with each separately for path ${cf.path} on resource type ${resourceType}`
);
new_cf.push({
path: cf.path ?? '',
valueSet: cf.valueSet,
extension: [measureExt]
});
if (cf.code.length > 1) {
logger.warn(
`code filter entry with multiple codes, created entries with each separately for path ${cf.path} on resource type ${resourceType}`
);
cf.code.forEach(oneCode => {
new_cf.push({
path: cf.path ?? '',
code: [oneCode],
extension: [measureExt]
});
});
} else {
new_cf.push({
path: cf.path ?? '',
code: cf.code,
extension: [measureExt]
});
}
} else if (cf.code) {
if (cf.code.length > 1) {
logger.warn(
`code filter entry with multiple codes, created entries with each separately for path ${cf.path} on resource type ${resourceType}`
);
cf.code.forEach(oneCode => {
new_cf.push({
path: cf.path ?? '',
code: [oneCode],
extension: [measureExt]
});
});
} else if (cf.code.length === 1) {
new_cf.push({
path: cf.path ?? '',
code: cf.code,
extension: [measureExt]
});
} else {
logger.error(
`Code filter found with no code or value set for path ${cf.path} on resource type ${resourceType}`
);
}
} else if (cf.valueSet) {
new_cf.push({
path: cf.path ?? '',
valueSet: cf.valueSet,
extension: [measureExt]
});
} else {
logger.error(
`Code filter found with no code or value set for path ${cf.path} on resource type ${resourceType}`
);
}
});
return new_cf;
}