forked from smart-on-fhir/bulk-data-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExportManager.js
963 lines (837 loc) · 31.1 KB
/
ExportManager.js
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
const crypto = require("crypto");
const moment = require("moment");
const fs = require("fs");
const base64url = require("base64-url");
const zlib = require("zlib");
const config = require("./config");
const lib = require("./lib");
const QueryBuilder = require("./QueryBuilder");
const getDB = require("./db");
const toNdjson = require("./transforms/dbRowToNdjson");
const toCSV = require("./transforms/dbRowToCSV");
const fhirStream = require("./FhirStream");
const translator = require("./transforms/dbRowTranslator");
const supportedFormats = {
"application/fhir+ndjson" : "ndjson",
"application/ndjson" : "ndjson",
"ndjson" : "ndjson",
"text/csv" : "csv",
"csv" : "csv"
};
const exportTypes = {
ndjson: {
fileExtension: "ndjson",
contentType : "application/fhir+ndjson",
transform : toNdjson
},
csv: {
fileExtension: "csv",
contentType : "text/csv; charset=UTF-8; header=present",
transform : toCSV
}
};
function getExportParam(req, name)
{
if (req.method == "GET") {
return req.query[name];
}
if (req.method == "POST") {
const out = [];
(req.body.parameter || []).forEach(x => {
if (x.name === name) {
const valueX = Object.keys(x).find(key => key.indexOf("value") === 0);
if (valueX) {
out.push(x[valueX]);
}
}
});
if (out.length) {
return out.length === 1 ? out[0] : out;
}
}
return null;
}
function isFile(path)
{
try {
const stat = fs.statSync(path);
return stat.isFile();
} catch {
return false;
}
}
function deleteFileIfExists(path)
{
try {
if (isFile(path)) {
fs.unlinkSync(path);
}
} catch (ex) {
console.error(ex);
return false;
}
return true;
}
class ExportManager
{
/**
* Simulated error (if any)
* @type {string}
*/
simulatedError = "";
/**
* Simulated export duration
* @type {number}
*/
simulatedExportDuration;
/**
* Database size multiplier
* @type {number}
*/
databaseMultiplier = 1;
/**
* FHIR version as integer (2|3|4)
* @type {number}
*/
stu = 4;
/**
* How many FHIR resources to include in one file
* @type {number}
*/
resourcesPerFile = config.defaultPageSize;
/**
* Access Token LifeTime in minutes
* @type {number}
*/
accessTokenLifeTime = config.defaultTokenLifeTime;
/**
* An array of resourceTypes (from the _type parameter)
* @type {string[]}
*/
resourceTypes;
/**
* An array of FHIR element paths (from the _elements parameter)
* @type {string[]}
*/
fhirElements;
/**
* Unique ID for this job
* @type {string}
*/
id;
/**
* When was the export started? JS timestamp.
* @type {number}
*/
requestStart;
/**
* True if an authorization header has been passed to kick-off
* @type {boolean}
*/
secure;
/**
* File extension (ndjson|csv). Based on the _outputFormat parameter.
* @type {string}
*/
outputFormat;
/**
* The group ID (if any)
* @type {string}
*/
group;
/**
* The kick-off request URL
* @type {string}
*/
request;
/**
* The modified since FHIR instant (the _since parameter)
* @type {string}
*/
since;
/**
* true for system-level exports and false otherwise
* @type {boolean}
*/
systemLevel;
/**
* Array of patient IDs to filter by (from the patient parameter)
* @type {string[]}
*/
patients;
/**
* Generated file download error (if any)
* @type {string}
*/
fileError;
/**
* The status of this job
*/
jobStatus = "UNDEFINED";
/**
* When a _since timestamp is supplied in the export request, a portion of
* the resources (expressed as percentage here) will be reported as deleted
* using the deleted field in the output JSON.
*/
simulateDeletedPct = 0;
extended = false;
/**
* An array to hold kickoff errors that should be included in the errors
* payload property if lenient handling is preferred
*/
kickoffErrors;
/**
*
* @param {string} id
* @returns {Promise<ExportManager>}
*/
static find(id)
{
return lib.readJSON(`${__dirname}/jobs/${id}.json`).then(
state => new ExportManager(state)
);
}
static createKickOffHandler(system = false)
{
return function(req, res) {
const job = new ExportManager({
simulatedError : req.sim.err,
simulatedExportDuration: req.sim.dur,
databaseMultiplier : req.sim.m,
// stu : req.sim.stu,
resourcesPerFile : req.sim.page,
accessTokenLifeTime : req.sim.tlt,
fileError : req.sim.fileError,
simulateDeletedPct : req.sim.del
});
job.kickOff(req, res, system);
}
}
static createCancelHandler()
{
return function cancelFlow(req, res) {
return ExportManager.find(req.params.id).then(
job => job.cancel(res),
() => lib.outcomes.cancelNotFound(res)
);
};
}
static createStatusHandler()
{
return function handleStatus(req, res) {
return ExportManager.find(req.params.id).then(
job => job.handleStatus(req, res),
err => lib.operationOutcome(res, err.message, { httpCode: 400 })
);
}
};
static createDownloadHandler()
{
return function handleFileDownload(req, res, next) {
ExportManager.find(req.sim.id).then(
job => job.download(req, res).catch(next),
() => lib.outcomes.exportDeleted(res)
);
}
}
static cleanUp()
{
return lib.forEachFile({
dir: __dirname + "/jobs/",
filter: path => path.endsWith(".json")
}, (path, fileStats, next) => {
return lib.readJSON(path).then(state => {
if (/*state.jobStatus === "EXPORTED" &&*/ Date.now() - state.createdAt > config.maxExportAge * 60000) {
fs.unlink(path, err => {
/* istanbul ignore if */
if (err) {
console.error(err);
}
next();
});
}
else {
next();
}
});
}).then(() => {
/* istanbul ignore if */
if (process.env.NODE_ENV != "test") {
setTimeout(ExportManager.cleanUp, 5000).unref();
}
});
}
constructor(options = {})
{
this.id = crypto.randomBytes(16).toString("hex");
this.kickoffErrors = options.kickoffErrors || [];
this.setSimulatedError(options.simulatedError)
.setSimulatedExportDuration(options.simulatedExportDuration)
.setDatabaseMultiplier(options.databaseMultiplier)
.setSTU(options.stu)
.setResourcesPerFile(options.resourcesPerFile)
.setAccessTokenLifeTime(options.accessTokenLifeTime)
.setSystemLevel(options.systemLevel)
.setGroup(options.group)
// .setPatients(options.patients)
.setSimulateDeletedPct(options.simulateDeletedPct)
.setSince(options.since);
["resourceTypes", "fhirElements", "id", "requestStart", "secure", "patients",
"outputFormat", "request", "fileError","jobStatus", "extended", "createdAt",
"ignoreTransientError"].forEach(key => {
if (key in options) {
this[key] = options[key];
}
});
if (!this.createdAt) {
this.createdAt = Date.now();
}
this.save()
}
save()
{
fs.writeFileSync(
`${__dirname}/jobs/${this.id}.json`,
JSON.stringify(this.toJSON(), null, 4)
);
}
delete()
{
deleteFileIfExists(`${__dirname}/jobs/${this.id}.json`);
}
toJSON()
{
return {
simulatedError : this.simulatedError,
simulatedExportDuration: this.simulatedExportDuration,
databaseMultiplier : this.databaseMultiplier,
stu : this.stu,
resourcesPerFile : this.resourcesPerFile,
accessTokenLifeTime : this.accessTokenLifeTime,
resourceTypes : this.resourceTypes,
fhirElements : this.fhirElements,
id : this.id,
requestStart : this.requestStart,
secure : this.secure,
outputFormat : this.outputFormat,
group : this.group,
request : this.request,
since : this.since,
systemLevel : this.systemLevel,
patients : this.patients,
fileError : this.fileError,
jobStatus : this.jobStatus,
extended : this.extended,
createdAt : this.createdAt,
ignoreTransientError : this.ignoreTransientError,
simulateDeletedPct : this.simulateDeletedPct,
kickoffErrors : this.kickoffErrors
};
}
async kickOff(req, res, system)
{
const isLenient = !!String(req.headers.prefer || "").match(/\bhandling\s*=\s*lenient\b/i);
// Verify that the POST body contains a Parameters resource ------------
if (req.method == "POST" && req.body.resourceType !== "Parameters") {
return lib.operationOutcome(res, "The POST body should be a Parameters resource", { httpCode: 400 });
}
// Validate the accept header ------------------------------------------
// let accept = req.headers.accept;
// if (!accept || accept == "*/*") {
// accept = "application/fhir+ndjson"
// }
// if (accept != "application/fhir+ndjson" && accept != "application/fhir+json") {
// return lib.outcomes.invalidAccept(res, accept);
// }
// Simulate file_generation_failed error if requested ------------------
if (this.simulatedError == "file_generation_failed") {
return lib.outcomes.fileGenerationFailed(res);
}
try {
this.setSTU(req.sim.stu);
} catch (ex) {
return lib.operationOutcome(res, ex.message, { httpCode: 400 });
}
this.setGroup(req.params.groupId);
this.setSystemLevel(system);
this.extended = !!req.sim.extended;
const _type = getExportParam(req, "_type") || "";
const _patient = getExportParam(req, "patient") || "";
const _since = getExportParam(req, "_since") || "";
const _outputFormat = getExportParam(req, "_outputFormat") || "application/fhir+ndjson";
const _elements = getExportParam(req, "_elements") || "";
const _typeFilter = getExportParam(req, "_typeFilter") || "";
const _includeAssociatedData = getExportParam(req, "_includeAssociatedData") || "";
if (_includeAssociatedData) {
const outcome = lib.createOperationOutcome(`The "_includeAssociatedData" parameter is not supported by this server`);
if (!isLenient) {
this.delete();
return res.status(400).json(outcome);
}
this.kickoffErrors.push(outcome);
}
if (_typeFilter) {
const outcome = lib.createOperationOutcome(`The "_typeFilter" parameter is not supported by this server`);
if (!isLenient) {
this.delete();
return res.status(400).json(outcome);
}
this.kickoffErrors.push(outcome);
}
if (_patient && req.method != "POST") {
return lib.operationOutcome(res, `The "patient" parameter is only available in POST requests`, { httpCode: 400 });
}
try {
await this.setResourceTypes(_type);
} catch (ex) {
return lib.operationOutcome(res, ex.message, { httpCode: 400 });
}
try {
this.setPatients(_patient);
} catch (ex) {
return lib.operationOutcome(res, ex.message, { httpCode: 400 });
}
try {
this.setSince(_since);
} catch (ex) {
return lib.outcomes.invalidSinceParameter(res, _since);
}
try {
await this.setFHIRElements(_elements);
} catch (ex) {
return lib.operationOutcome(res, ex.message, { httpCode: 400 });
}
if (!supportedFormats.hasOwnProperty(_outputFormat)) {
return lib.operationOutcome(res, `The "${_outputFormat}" _outputFormat is not supported`, { httpCode: 400 });
}
this.outputFormat = supportedFormats[_outputFormat];
this.requestStart = Date.now();
this.secure = !!req.headers.authorization;
const proto = req.headers["x-forwarded-proto"] || req.protocol;
this.request = proto + "://" + req.headers.host + req.originalUrl;
// Prepare the status URL
let url = config.baseUrl + req.originalUrl.split("?").shift().replace(
/(\/[^/]+)?\/fhir\/.*/,
`/fhir/bulkstatus/${this.id}`
);
this.jobStatus = "STARTED";
this.save();
// Instead of generating the response, and then returning it, the server
// returns a 202 Accepted header, and a Content-Location at which the
// client can use to access the response.
// HTTP/1.1 202 Accepted
res.set("Content-Location", url);
lib.outcomes.exportAccepted(res, url);
}
async handleStatus(req, res) {
if (this.secure && !req.headers.authorization) {
return lib.operationOutcome(res, "Not authorized", { httpCode: 401 });
}
if (this.jobStatus === "EXPORTED") {
return lib.outcomes.cancelCompleted(res);
}
if (!this.ignoreTransientError && this.simulatedError == "transient_error") {
this.ignoreTransientError = true;
this.save();
return lib.operationOutcome(res, "An unknown error ocurred (transient_error). Please try again.", {
httpCode : 500,
issueCode: "transient"
});
}
// ensure requestStart param is present
let requestStart = moment(this.requestStart);
// check if the user should (continue to) wait
let endTime = moment(requestStart).add(this.simulatedExportDuration, "seconds");
let now = moment();
// If waiting - show progress and exit
if (endTime.isAfter(now, "second")) {
let diff = (+now - +requestStart)/1000;
let pct = Math.round((diff / this.simulatedExportDuration) * 100);
return res.set({
"X-Progress" : pct + "%",
"Retry-After": Math.ceil(this.simulatedExportDuration - diff)
}).status(202).end();
}
// ---------------------------------------------------------------------
// Now the simulated file generation is complete!
// ---------------------------------------------------------------------
this.jobStatus = "EXPORTED";
this.save();
// Count all the requested resources in the database.
let builder = new QueryBuilder({
type : this.resourceTypes,
patients : this.patients,
group : this.group,
systemLevel: this.systemLevel,
start : this.since
});
let { sql, params } = builder.compileCount();
const DB = getDB(this.stu);
DB.promise("all", sql, params).then(rows => {
// console.log(sql, rows, this)
// Finally generate those download links
let len = rows.length;
let linksArr = [];
let errorArr = [...this.kickoffErrors];
let deletedArr = [];
let linksLen = 0;
let baseUrl = config.baseUrl //+ req.originalUrl.split("?").shift().replace(/\/[^/]+\/fhir\/.*/, "");
for(let y = 0; y < len; y++ ) { // for each selected resource
let row = rows[y];
let n = Math.ceil((row.rowCount * this.databaseMultiplier)/this.resourcesPerFile); // how many files for this resource
for (let i = 0; i < n; i++) { // generate each file path
if (linksLen > config.maxFiles) {
return res.status(413).send("Too many files");
}
// console.log(y, y % 2, this.simulatedError)
if (this.simulatedError == "some_file_generation_failed" && i % 2) {
errorArr.push({
type : "OperationOutcome",
url: lib.buildUrlPath(
baseUrl,
base64url.encode(JSON.stringify({
id: this.id,
secure: this.secure,
fileError: `Failed to export ${i + 1}.${row.fhir_type}.${this.outputFormat}`
})),
"/fhir/bulkfiles/",
`${i + 1}.${row.fhir_type}.${this.outputFormat}`
)
});
}
else {
let offset = this.resourcesPerFile * i;
let count = Math.min(
this.resourcesPerFile,
row.rowCount * this.databaseMultiplier - offset
);
// Here we know we have a list of {count} resources that
// we can put into a file by generating the proper link
// to it. However, if {this.simulateDeletedPct} is set,
// certain percentage of them should go into the
// "deleted" array instead!
if (this.simulateDeletedPct && this.since) {
let cnt = Math.round(count/100 * this.simulateDeletedPct);
cnt && deletedArr.push({
type: row.fhir_type,
count: cnt,
url: lib.buildUrlPath(
baseUrl,
base64url.encode(JSON.stringify({
id : this.id,
limit : cnt,
del : 1,
secure: this.secure,
offset
})),
"/fhir/bulkfiles/",
`${i + 1}.${row.fhir_type}.${this.outputFormat}`
)
});
count -= cnt;
offset += cnt;
}
linksLen = linksArr.push({
type: row.fhir_type,
count: count,
url: lib.buildUrlPath(
baseUrl,
base64url.encode(JSON.stringify({
id : this.id,
offset,
limit : count,
secure: this.secure,
})),
"/fhir/bulkfiles/",
`${i + 1}.${row.fhir_type}.${this.outputFormat}`
)
});
}
}
}
res.set({
"Expires": new Date(this.createdAt + config.maxExportAge * 60000).toUTCString()
}).json({
// a FHIR instant type that indicates the server's time when the
// query is run. No resources that have a modified data after this
// instant should be in the response.
"transactionTime": requestStart,
// the full url of the original bulk data kick-off request
"request" : this.request,
// boolean value indicating whether downloading the generated files
// will require an authentication token. Note: This may be false in
// the case of signed S3 urls or an internal file server within an
// organization's firewall.
"requiresAccessToken": this.secure,
// array of bulk data file items with one entry for each generated
// file. Note: If no data is returned from the kick-off request,
// the server should return an empty array.
"output" : linksArr,
// When a _since timestamp is supplied in the export request,
// this array SHALL be populated with output files containing
// FHIR Transaction Bundles that indicate which FHIR resources
// would have been returned, but have been deleted subsequent to
// that date. If no resources have been deleted or the _since
// parameter was not supplied, the server MAY omit this key
// or MAY return an empty array.
"deleted": deletedArr,
// If no errors occurred, the server should return an empty array
"error": errorArr
}).end();
});
};
async download(req, res)
{
if (this.secure && !req.headers.authorization) {
return lib.operationOutcome(res, "Not authorized", { httpCode: 401 });
}
if (this.secure) {
const grantedScopes = lib.getGrantedScopes(req)
const resourceType = req.params.file.split(".")[1]
const hasAccess = lib.hasAccessToResourceType(grantedScopes, resourceType, "read")
if (!hasAccess) {
return lib.operationOutcome(res, "Permission denied", { httpCode: 403 });
}
}
if (this.jobStatus !== "EXPORTED") {
return lib.outcomes.exportNotCompleted(res);
}
// console.log(req.sim, this)
const fileError = req.sim.fileError;
// early exit in case simulated errors
if (this.simulatedError == "file_expired") {
return lib.outcomes.fileExpired(res);
}
// early exit in case simulated file errors
if (fileError) {
return res.set({ "Content-Type": "application/fhir+ndjson" })
.end(JSON.stringify(lib.createOperationOutcome(req.sim.fileError)));
}
const acceptEncoding = req.headers["accept-encoding"] || "";
const shouldDeflate = (/\bdeflate\b/.test(acceptEncoding));
const shouldGzip = (/\bgzip\b/.test(acceptEncoding));
// set the response headers
res.set({
"Content-Type": exportTypes[this.outputFormat].contentType,
"Content-Disposition": "attachment"
});
/* istanbul ignore else */
if (shouldGzip) {
res.set({ "Content-Encoding": "gzip" });
}
else if (shouldDeflate) {
res.set({ "Content-Encoding": "deflate" });
}
let input = new fhirStream({
fileName : req.params.file,
stu : this.stu,
databaseMultiplier: this.databaseMultiplier,
extended : this.extended,
group : this.group,
limit : req.sim.limit,
offset : req.sim.offset,
since : this.since,
systemLevel: this.systemLevel,
patients : this.patients
});
input.on("error", error => {
console.error(error);
return res.status(500).end();
});
input.init().then(() => {
let pipeline = input.pipe(translator({
_elements : this.fhirElements,
err : this.fileError,
deleted : !!req.sim.del,
secure : this.secure
}));
const transform = exportTypes[this.outputFormat].transform;
if (transform) {
pipeline = pipeline.pipe(transform({ extended: this.extended }));
}
/* istanbul ignore else */
if (shouldGzip) {
pipeline = pipeline.pipe(zlib.createGzip())
}
else if (shouldDeflate) {
pipeline = pipeline.pipe(zlib.createDeflate())
}
pipeline.pipe(res);
});
}
/**
* After a bulk data request has been started, a client MAY send a DELETE
* request to the URL provided in the Content-Location header to cancel the
* request.
*
* If the request has been completed, a server MAY use the request as a
* signal that a client is done retrieving files and that it is safe for the
* sever to remove those from storage.
*
* Following the delete request, when subsequent requests are made to the
* polling location, the server SHALL return a 404 error and an associated
* FHIR OperationOutcome in JSON format.
*/
cancel(res)
{
this.delete();
return lib.outcomes.cancelAccepted(res);
}
// SETTERS
// -------------------------------------------------------------------------
/**
* @param {string} errorId
*/
setSimulatedError(errorId = "")
{
this.simulatedError = String(errorId || "").trim();
return this;
}
/**
* Sets the simulated file generation duration in seconds
* @param {number} duration
*/
setSimulatedExportDuration(duration = config.defaultWaitTime)
{
this.simulatedExportDuration = lib.uInt(duration, config.defaultWaitTime);
return this;
}
/**
* Sets the database size multiplier
* @param {number} multiplier
*/
setDatabaseMultiplier(multiplier = 1)
{
this.databaseMultiplier = lib.uInt(multiplier, 1);
return this;
}
/**
* Sets the numeric FHIR version
* @param {number} version
*/
setSTU(version = 4)
{
const ver = lib.uInt(version, 4);
if (ver < 2 || ver > 4) {
throw new Error(`Invalid FHIR version "${version}". Must be 2, 3 or 4`);
}
this.stu = ver;
return this;
}
/**
* Sets the resourcesPerFile
* @param {number} count
*/
setResourcesPerFile(count = config.defaultPageSize)
{
this.resourcesPerFile = lib.uInt(count, config.defaultPageSize);
return this;
}
/**
* Sets the Access Token LifeTime in minutes
* @param {number} minutes
*/
setAccessTokenLifeTime(minutes = config.defaultTokenLifeTime)
{
this.accessTokenLifeTime = lib.uInt(minutes, config.defaultTokenLifeTime);
return this;
}
/**
* Set what percentage of the output resources should be reported as deleted
* @param {number} pct
*/
setSimulateDeletedPct(pct = 0)
{
this.simulateDeletedPct = lib.uInt(pct, 0);
return this;
}
/**
*
* @param {boolean} isSystemLevel
*/
setSystemLevel(isSystemLevel)
{
this.systemLevel = lib.bool(isSystemLevel);
return this;
}
/**
*
* @param {string} groupId
*/
setGroup(groupId = "")
{
this.group = String(groupId || "").trim();
return this;
}
/**
* Sets the array of resource types to be exported
* @param {string|string[]} types Comma-separated list or array of strings
*/
async setResourceTypes(types)
{
const requestedTypes = lib.makeArray(types || "").map(t => String(t || "").trim()).filter(Boolean);
const availableTypes = await lib.getAvailableResourceTypes(this.stu);
if (availableTypes.indexOf("OperationDefinition") === -1) {
availableTypes.push("OperationDefinition");
}
const badParam = requestedTypes.find(type => availableTypes.indexOf(type) == -1);
if (badParam) {
this.resourceTypes = [];
throw new Error(`The requested resource type "${badParam}" is not available on this server`);
}
this.resourceTypes = requestedTypes;
}
/**
*
* @param {{reference:string}[]} patients
*/
setPatients(patients)
{
const arr = lib.makeArray(patients).filter(Boolean);
if (this.systemLevel && arr.length) {
throw new Error("The patient parameter is not available in system-level export requests");
}
this.patients = arr.map(ref => ref.reference ?
ref.reference.replace(/^\/?Patient\//i, "") :
null
).filter(Boolean);
return this;
}
/**
* Sets the _since moment and makes sure it is in the future
* @param {string} since FHIR Instant
*/
setSince(since = "")
{
this.since = since ? lib.fhirDateTime(since, true) : "";
return this;
}
/**
*
* @param {string|string[]} elements
*/
async setFHIRElements(elements)
{
const _elements = lib.makeArray(elements || "").map(t => String(t || "").trim()).filter(Boolean);
const availableTypes = await lib.getAvailableResourceTypes(this.stu);
for (let element of _elements) {
const match = element.match(/^([a-zA-Z]+)(\.([a-zA-Z]+))?$/);
if (!match || !match[1]) {
throw new Error(`The _elements parameter should contain entries of the form "[element]" or "[ResourceType].[element]". Found "${element}".`);
}
if (match[3]) {
const badParam = availableTypes.indexOf(match[1]) == -1;
if (badParam) {
throw new Error(`The _elements parameter includes a resource type "${match[1]}" which is not available on this server.`);
}
}
}
this.fhirElements = _elements;
return this;
}
};
module.exports = ExportManager;
/* istanbul ignore if */
if (process.env.NODE_ENV != "test") {
ExportManager.cleanUp();
}