-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathresourceCreation.ts
More file actions
308 lines (295 loc) · 10.2 KB
/
Copy pathresourceCreation.ts
File metadata and controls
308 lines (295 loc) · 10.2 KB
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
import { v4 as uuidv4 } from 'uuid';
import { getRandomFirstName, getRandomLastName } from '../randomizer';
import _ from 'lodash';
import { getResourcePrimaryDates, jsDateToFHIRDate } from './dates';
import { getResourcePatientReference } from './patient';
import { getResourceCode } from './codes';
import { Enums } from 'fqm-execution';
export function createPatientResourceString(qicorePatient: boolean, birthDate: string): string {
const id = uuidv4();
// NOTE: should add non-binary genders in the future
const gender = Math.random() < 0.5 ? 'male' : 'female';
const pt: fhir4.Patient = {
resourceType: 'Patient',
id,
identifier: [
{
use: 'usual',
system: 'http://example.com/test-id',
value: `test-patient-${id}`
}
],
name: [
{
family: getRandomLastName(),
given: [getRandomFirstName(gender)]
}
],
gender,
birthDate
};
// if qicorePatient is true, add qicore-patient profile to the patient's meta.profile
if (qicorePatient) {
pt['meta'] = {};
pt['meta']['profile'] = ['http://hl7.org/fhir/us/qicore/StructureDefinition/qicore-patient'];
}
return JSON.stringify(pt, null, 2);
}
/**
* Creates copies of all passed in Bundle entries (without references maintained) and gives them
* new resource ids. Replaces all patient references to the patient oldPatientId with newPatientId
* @param copyResources array of fhir Bundle entries to be copied
* @param oldPatientId a patient id that the copyResources may reference
* @param newPatientId a patient id that should replace oldId in references
* @returns array of new Bundle entry copies
*/
export function createCopiedResources(
copyResources: fhir4.BundleEntry[],
oldPatientId: string,
newPatientId: string
): fhir4.BundleEntry[] {
const resources: fhir4.BundleEntry[] = copyResources.map(cr => {
let entryString = JSON.stringify(cr);
const idRegexp = new RegExp(`Patient/${oldPatientId}`, 'g');
entryString = entryString.replace(idRegexp, `Patient/${newPatientId}`);
const entry: fhir4.BundleEntry = JSON.parse(entryString);
if (entry.resource) {
const newResourceId = uuidv4();
entry.resource.id = newResourceId;
entry.fullUrl = `urn:uuid:${newResourceId}`;
}
// Note: this does not update potential cross-resource references, which we may want to support in the future
return entry;
});
return resources;
}
/**
* Creates a copy of the passed in patient object (without references maintained) and updates the
* id and identifier as well as creating a new name to differentiate the new patient copy
* @param copyPatient {fhir4.Patient} a fhir Patient object to copy
* @returns {fhir4.Patient} the new fhir patient copy
*/
export function createCopiedPatientResource(copyPatient: fhir4.Patient): fhir4.Patient {
const patient: fhir4.Patient = _.cloneDeep(copyPatient);
const identifier = patient.identifier?.find(id => id.system === 'http://example.com/test-id');
patient.id = uuidv4();
if (identifier) {
identifier.value = `test-patient-${patient.id}`;
} else {
const newIdentifier: fhir4.Identifier = {
use: 'usual',
system: 'http://example.com/test-id',
value: `test-patient-${patient.id}`
};
if (patient.identifier) {
patient.identifier.push(newIdentifier);
} else {
patient.identifier = [newIdentifier];
}
}
if (patient.name && patient.name.length > 0) {
patient.name[0] = {
family: getRandomLastName(),
given: [getRandomFirstName(patient.gender === 'male' ? 'male' : 'female')] // future should handle non-binary
};
}
return patient;
}
/**
* Creates a string representing a patient bundle resource. Creates using a patient resource and
* an array of the patient's associated resources
* @param {Object} patient FHIR Patient object
* @param {Array} entries array of FHIR BundleEntries associated with the patient
* @returns {String} representation of a FHIR patient bundle resource
*/
export function createPatientBundle(
patient: fhir4.Patient,
entries: fhir4.BundleEntry[],
fullUrl?: string,
testMeasureReport?: fhir4.MeasureReport
): fhir4.Bundle {
const bundle: fhir4.Bundle = {
type: 'transaction',
resourceType: 'Bundle',
id: uuidv4(),
entry: [
{
resource: patient,
request: {
method: 'PUT',
url: `Patient/${patient.id}`
},
fullUrl: fullUrl ?? `urn:uuid:${patient.id}`
}
]
};
entries.forEach(entry => {
bundle.entry?.push({
...entry,
request: {
method: 'PUT',
url: `${entry.resource?.resourceType}/${entry.resource?.id}`
}
});
});
if (testMeasureReport) {
bundle.entry?.push({
resource: testMeasureReport,
request: {
method: 'PUT',
url: `MeasureReport/${testMeasureReport.id}`
},
fullUrl: `urn:uuid:${testMeasureReport.id}`
});
}
return bundle;
}
/**
* Creates incomplete FHIR resource with generated ID, information populated from the provided data requirements,
* and code information populated from a randomly selected expanded ValueSet (obtained from the given measure bundle)
* @param dr FHIR DataRequirement object
* @param mb FHIR measure bundle
* @returns {String} incomplete FHIR resource that will appear as initial value in code editor
*/
export function createFHIRResourceString(
dr: fhir4.DataRequirement,
mb: fhir4.Bundle,
patientId: string | null,
mpStart: string,
mpEnd: string
): string {
const resource: any = {
resourceType: dr.type,
id: uuidv4()
};
if (dr.profile) {
resource.meta = { profile: dr.profile };
}
getResourceCode(resource, dr, mb);
getResourcePatientReference(resource, dr, patientId);
getResourcePrimaryDates(resource, dr, mpStart, mpEnd);
return JSON.stringify(resource, null, 2);
}
/**
* Creates a FHIR data exchange MeasureReport from measure and subject data to be submitted with associated patient
* https://build.fhir.org/ig/HL7/davinci-deqm/StructureDefinition-datax-measurereport-deqm.html
* @param measure FHIR Measure
* @param measurementPeriod FHIR Period representing the measurement period
* @param subjectId the patient id the MeasureReport is associated with
* @returns { fhir4.MeasureReport } a data exchange measure report used to send Measure-relevant data to a server
*/
export function createDataExchangeMeasureReport(
measure: fhir4.Measure,
measurementPeriod: fhir4.Period,
subjectId: string
): fhir4.MeasureReport {
return {
resourceType: 'MeasureReport',
id: uuidv4(),
measure: measure.url?.includes('|') ? measure.url : `${measure.url}|${measure.version}`, //canonical measure/version
period: measurementPeriod,
status: 'complete',
type: 'data-collection',
subject: { reference: `Patient/${subjectId}` },
date: jsDateToFHIRDate(new Date()),
reporter: { reference: 'Organization/fqm-testify' }, //TODO: do we need to send an organization resource?
meta: {
profile: ['http://hl7.org/fhir/us/davinci-deqm/StructureDefinition/datax-measurereport-deqm']
},
extension: [
{
url: 'http://hl7.org/fhir/us/davinci-deqm/StructureDefinition/extension-submitDataUpdateType',
valueCode: 'snapshot'
}
],
contained: [{ resourceType: 'Organization', id: 'fqm-testify' }]
};
}
/**
* Creates a FHIR cqfm test case MeasureReport from measure and subject data to be exported with associated patient
* @param mb FHIR MeasureBundle
* @param measurementPeriod FHIR Period representing the measurement period
* @param subjectId the patient id the MeasureReport is associated with
* @param desiredPopulations a list of desired population codes for the patient to fall into
* @returns {fhir4.MeasureReport} a cqfm test case measure report associated with the patient and measure
*/
export function createCQFMTestCaseMeasureReport(
mb: fhir4.Bundle,
measurementPeriod: fhir4.Period,
subjectId: string,
desiredPopulations?: string[]
): fhir4.MeasureReport {
const measure = mb?.entry?.find(e => e?.resource?.resourceType === 'Measure')?.resource as fhir4.Measure;
const testGroup = generateTestCaseMRGroup(measure, desiredPopulations);
const parametersId = uuidv4();
return {
resourceType: 'MeasureReport',
id: uuidv4(),
measure: measure.url as string,
period: measurementPeriod,
status: 'complete',
type: 'individual',
meta: {
profile: ['http://hl7.org/fhir/us/cqfmeasures/StructureDefinition/test-case-cqfm']
},
extension: [
{
url: 'http://hl7.org/fhir/us/cqfmeasures/StructureDefinition/cqfm-inputParameters',
valueReference: {
reference: `#${parametersId}`
}
}
],
modifierExtension: [
{
url: 'http://hl7.org/fhir/us/cqfmeasures/StructureDefinition/cqfm-isTestCase',
valueBoolean: true
}
],
contained: [
{
resourceType: 'Parameters',
id: parametersId,
parameter: [
{
name: 'subject',
// For now this is just the Patient id. May evolve as we learn more about cqfm-testCases
valueString: subjectId
}
]
}
],
group: testGroup
};
}
/**
* Takes in a Measure and desired populations array and produces the group property for a cqfm test case MeasureReport
* @param measure a FHIR Measure resource
* @param desiredPopulations a list of desired population codes for the patient to fall into
* @returns an Array containing an object with a measure score and population object to be used as the group property in a cqfm test case MeasureReport
*/
export function generateTestCaseMRGroup(
measure: fhir4.Measure,
desiredPopulations?: string[]
): fhir4.MeasureReportGroup[] {
let measureScore = 0;
const testPops = measure?.group?.[0].population?.map(pop => {
const newPop: fhir4.MeasureReportGroupPopulation = { code: pop.code };
const popCode = pop.code?.coding?.[0].code;
if (popCode && desiredPopulations && desiredPopulations.includes(popCode)) {
newPop.count = 1;
if (popCode === Enums.PopulationType.NUMER) {
measureScore = 1;
}
} else {
newPop.count = 0;
}
return newPop;
});
return [
{
population: testPops,
measureScore: { value: measureScore }
}
];
}