@metriport/commonwell-cert-runner 1.4.20 → 1.4.22-alpha.0

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.
@@ -1,5 +1,5 @@
1
- #!/usr/bin/env node
2
- import { CommonWell, Document, RequestMetadata } from "@metriport/commonwell-sdk";
3
- export declare function documentConsumption(commonWell: CommonWell, queryMeta: RequestMetadata): Promise<void>;
4
- export declare function queryDocuments(commonWell: CommonWell, queryMeta: RequestMetadata): Promise<Document[]>;
5
- export declare function retrieveDocument(commonWell: CommonWell, queryMeta: RequestMetadata, doc: Document): Promise<void>;
1
+ #!/usr/bin/env node
2
+ import { CommonWell, Document, RequestMetadata } from "@metriport/commonwell-sdk";
3
+ export declare function documentConsumption(commonWell: CommonWell, queryMeta: RequestMetadata): Promise<void>;
4
+ export declare function queryDocuments(commonWell: CommonWell, queryMeta: RequestMetadata): Promise<Document[]>;
5
+ export declare function retrieveDocument(commonWell: CommonWell, queryMeta: RequestMetadata, doc: Document): Promise<void>;
@@ -1,85 +1,85 @@
1
- #!/usr/bin/env node
2
- "use strict";
3
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
4
- if (k2 === undefined) k2 = k;
5
- var desc = Object.getOwnPropertyDescriptor(m, k);
6
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
7
- desc = { enumerable: true, get: function() { return m[k]; } };
8
- }
9
- Object.defineProperty(o, k2, desc);
10
- }) : (function(o, m, k, k2) {
11
- if (k2 === undefined) k2 = k;
12
- o[k2] = m[k];
13
- }));
14
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
15
- Object.defineProperty(o, "default", { enumerable: true, value: v });
16
- }) : function(o, v) {
17
- o["default"] = v;
18
- });
19
- var __importStar = (this && this.__importStar) || function (mod) {
20
- if (mod && mod.__esModule) return mod;
21
- var result = {};
22
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
23
- __setModuleDefault(result, mod);
24
- return result;
25
- };
26
- Object.defineProperty(exports, "__esModule", { value: true });
27
- exports.retrieveDocument = exports.queryDocuments = exports.documentConsumption = void 0;
28
- const commonwell_sdk_1 = require("@metriport/commonwell-sdk");
29
- const fs = __importStar(require("fs"));
30
- const payloads_1 = require("./payloads");
31
- const shared_person_1 = require("./shared-person");
32
- const util_1 = require("./util");
33
- // Document Consumption
34
- // https://commonwellalliance.sharepoint.com/sites/ServiceAdopter/SitePages/Document-Consumption-(SOAP,-REST).aspx
35
- async function documentConsumption(commonWell, queryMeta) {
36
- const documents = await queryDocuments(commonWell, queryMeta);
37
- for (const doc of documents) {
38
- await retrieveDocument(commonWell, queryMeta, doc);
39
- }
40
- }
41
- exports.documentConsumption = documentConsumption;
42
- async function queryDocuments(commonWell, queryMeta) {
43
- // E1: Document Query
44
- console.log(`>>> E1c: Query for documents using FHIR (REST)`);
45
- const { personId, patientId } = await (0, shared_person_1.findOrCreatePerson)(commonWell, queryMeta, (0, payloads_1.makeDocPerson)({ facilityId: commonWell.oid }));
46
- if (!personId)
47
- throw new Error(`[E1c] personId is undefined before calling getNetworkLinks()`);
48
- const respLinks = await commonWell.getNetworkLinks(queryMeta, patientId);
49
- console.log(respLinks);
50
- const allLinks = respLinks._embedded.networkLink
51
- ? respLinks._embedded.networkLink.flatMap(util_1.filterTruthy)
52
- : [];
53
- const lola1Links = allLinks.filter(commonwell_sdk_1.isLOLA1);
54
- console.log(`Found ${allLinks.length} network links, ${lola1Links.length} are LOLA 1`);
55
- for (const link of lola1Links) {
56
- const upgradeURL = link._links?.upgrade?.href;
57
- if (!upgradeURL) {
58
- console.log(`[queryDocuments] missing upgrade URL for link `, link);
59
- continue;
60
- }
61
- const respUpgradeLink = await commonWell.upgradeOrDowngradeNetworkLink(queryMeta, upgradeURL);
62
- console.log(respUpgradeLink);
63
- }
64
- console.log(`>>> [E1c] Querying for docs...`);
65
- const respDocQuery = await commonWell.queryDocuments(queryMeta, patientId);
66
- return respDocQuery.entry;
67
- }
68
- exports.queryDocuments = queryDocuments;
69
- async function retrieveDocument(commonWell, queryMeta, doc) {
70
- // E2: Document Retrieve
71
- console.log(`>>> E2c: Retrieve documents using FHIR (REST)`);
72
- // store the query result as well
73
- const queryFileName = `./cw_consumption_${doc.id ?? "ID"}_${(0, payloads_1.makeId)()}.response.file`;
74
- fs.writeFileSync(queryFileName, JSON.stringify(doc));
75
- const fileName = `./cw_consumption_${doc.id ?? "ID"}_${(0, payloads_1.makeId)()}.contents.file`;
76
- // the default is UTF-8, avoid changing the encoding if we don't know the file we're downloading
77
- const outputStream = fs.createWriteStream(fileName, { encoding: undefined });
78
- console.log(`File being created at ${process.cwd()}/${fileName}`);
79
- const url = doc.content.location;
80
- if (!url)
81
- throw new Error(`[E2c] missing content.location in document ${doc.id}`);
82
- await commonWell.retrieveDocument(queryMeta, url, outputStream);
83
- }
84
- exports.retrieveDocument = retrieveDocument;
1
+ #!/usr/bin/env node
2
+ "use strict";
3
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
4
+ if (k2 === undefined) k2 = k;
5
+ var desc = Object.getOwnPropertyDescriptor(m, k);
6
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
7
+ desc = { enumerable: true, get: function() { return m[k]; } };
8
+ }
9
+ Object.defineProperty(o, k2, desc);
10
+ }) : (function(o, m, k, k2) {
11
+ if (k2 === undefined) k2 = k;
12
+ o[k2] = m[k];
13
+ }));
14
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
15
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
16
+ }) : function(o, v) {
17
+ o["default"] = v;
18
+ });
19
+ var __importStar = (this && this.__importStar) || function (mod) {
20
+ if (mod && mod.__esModule) return mod;
21
+ var result = {};
22
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
23
+ __setModuleDefault(result, mod);
24
+ return result;
25
+ };
26
+ Object.defineProperty(exports, "__esModule", { value: true });
27
+ exports.retrieveDocument = exports.queryDocuments = exports.documentConsumption = void 0;
28
+ const commonwell_sdk_1 = require("@metriport/commonwell-sdk");
29
+ const fs = __importStar(require("fs"));
30
+ const payloads_1 = require("./payloads");
31
+ const shared_person_1 = require("./shared-person");
32
+ const util_1 = require("./util");
33
+ // Document Consumption
34
+ // https://commonwellalliance.sharepoint.com/sites/ServiceAdopter/SitePages/Document-Consumption-(SOAP,-REST).aspx
35
+ async function documentConsumption(commonWell, queryMeta) {
36
+ const documents = await queryDocuments(commonWell, queryMeta);
37
+ for (const doc of documents) {
38
+ await retrieveDocument(commonWell, queryMeta, doc);
39
+ }
40
+ }
41
+ exports.documentConsumption = documentConsumption;
42
+ async function queryDocuments(commonWell, queryMeta) {
43
+ // E1: Document Query
44
+ console.log(`>>> E1c: Query for documents using FHIR (REST)`);
45
+ const { personId, patientId } = await (0, shared_person_1.findOrCreatePerson)(commonWell, queryMeta, (0, payloads_1.makeDocPerson)({ facilityId: commonWell.oid }));
46
+ if (!personId)
47
+ throw new Error(`[E1c] personId is undefined before calling getNetworkLinks()`);
48
+ const respLinks = await commonWell.getNetworkLinks(queryMeta, patientId);
49
+ console.log(respLinks);
50
+ const allLinks = respLinks._embedded.networkLink
51
+ ? respLinks._embedded.networkLink.flatMap(util_1.filterTruthy)
52
+ : [];
53
+ const lola1Links = allLinks.filter(commonwell_sdk_1.isLOLA1);
54
+ console.log(`Found ${allLinks.length} network links, ${lola1Links.length} are LOLA 1`);
55
+ for (const link of lola1Links) {
56
+ const upgradeURL = link._links?.upgrade?.href;
57
+ if (!upgradeURL) {
58
+ console.log(`[queryDocuments] missing upgrade URL for link `, link);
59
+ continue;
60
+ }
61
+ const respUpgradeLink = await commonWell.upgradeOrDowngradeNetworkLink(queryMeta, upgradeURL);
62
+ console.log(respUpgradeLink);
63
+ }
64
+ console.log(`>>> [E1c] Querying for docs...`);
65
+ const respDocQuery = await commonWell.queryDocuments(queryMeta, patientId);
66
+ return respDocQuery.entry;
67
+ }
68
+ exports.queryDocuments = queryDocuments;
69
+ async function retrieveDocument(commonWell, queryMeta, doc) {
70
+ // E2: Document Retrieve
71
+ console.log(`>>> E2c: Retrieve documents using FHIR (REST)`);
72
+ // store the query result as well
73
+ const queryFileName = `./cw_consumption_${doc.id ?? "ID"}_${(0, payloads_1.makeId)()}.response.file`;
74
+ fs.writeFileSync(queryFileName, JSON.stringify(doc));
75
+ const fileName = `./cw_consumption_${doc.id ?? "ID"}_${(0, payloads_1.makeId)()}.contents.file`;
76
+ // the default is UTF-8, avoid changing the encoding if we don't know the file we're downloading
77
+ const outputStream = fs.createWriteStream(fileName, { encoding: undefined });
78
+ console.log(`File being created at ${process.cwd()}/${fileName}`);
79
+ const url = doc.content.location;
80
+ if (!url)
81
+ throw new Error(`[E2c] missing content.location in document ${doc.id}`);
82
+ await commonWell.retrieveDocument(queryMeta, url, outputStream);
83
+ }
84
+ exports.retrieveDocument = retrieveDocument;
85
85
  //# sourceMappingURL=document-consumption.js.map
@@ -1,7 +1,7 @@
1
- #!/usr/bin/env node
2
- import { CommonWell, RequestMetadata } from "@metriport/commonwell-sdk";
3
- export declare function documentContribution({ memberManagementApi, api: apiDefaultOrg, queryMeta, }: {
4
- memberManagementApi: CommonWell;
5
- api: CommonWell;
6
- queryMeta: RequestMetadata;
7
- }): Promise<void>;
1
+ #!/usr/bin/env node
2
+ import { CommonWell, RequestMetadata } from "@metriport/commonwell-sdk";
3
+ export declare function documentContribution({ memberManagementApi, api: apiDefaultOrg, queryMeta, }: {
4
+ memberManagementApi: CommonWell;
5
+ api: CommonWell;
6
+ queryMeta: RequestMetadata;
7
+ }): Promise<void>;
@@ -1,166 +1,166 @@
1
- #!/usr/bin/env node
2
- "use strict";
3
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
4
- if (k2 === undefined) k2 = k;
5
- var desc = Object.getOwnPropertyDescriptor(m, k);
6
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
7
- desc = { enumerable: true, get: function() { return m[k]; } };
8
- }
9
- Object.defineProperty(o, k2, desc);
10
- }) : (function(o, m, k, k2) {
11
- if (k2 === undefined) k2 = k;
12
- o[k2] = m[k];
13
- }));
14
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
15
- Object.defineProperty(o, "default", { enumerable: true, value: v });
16
- }) : function(o, v) {
17
- o["default"] = v;
18
- });
19
- var __importStar = (this && this.__importStar) || function (mod) {
20
- if (mod && mod.__esModule) return mod;
21
- var result = {};
22
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
23
- __setModuleDefault(result, mod);
24
- return result;
25
- };
26
- var __importDefault = (this && this.__importDefault) || function (mod) {
27
- return (mod && mod.__esModule) ? mod : { "default": mod };
28
- };
29
- Object.defineProperty(exports, "__esModule", { value: true });
30
- exports.documentContribution = void 0;
31
- const commonwell_sdk_1 = require("@metriport/commonwell-sdk");
32
- const axios_1 = __importDefault(require("axios"));
33
- const fs = __importStar(require("fs"));
34
- const lodash_1 = require("lodash");
35
- const payloads_1 = require("./payloads");
36
- const shared_person_1 = require("./shared-person");
37
- const util_1 = require("./util");
38
- const AXIOS_TIMEOUT_MILLIS = 20000;
39
- // Document Contribution
40
- // https://commonwellalliance.sharepoint.com/sites/ServiceAdopter/SitePages/Document-Contribution-(SOAP,-REST).aspx
41
- const commonwellPrivateKey = (0, util_1.getEnvOrFail)("COMMONWELL_ORG_PRIVATE_KEY");
42
- const commonwellCert = (0, util_1.getEnvOrFail)("COMMONWELL_ORG_CERTIFICATE");
43
- const orgIdSuffix = (0, util_1.getEnvOrFail)("DOCUMENT_CONTRIBUTION_ORGANIZATION_ID");
44
- const firstName = (0, util_1.getEnv)("DOCUMENT_CONTRIBUTION_PATIENT_FIRST_NAME");
45
- const lastName = (0, util_1.getEnv)("DOCUMENT_CONTRIBUTION_PATIENT_LAST_NAME");
46
- const dob = (0, util_1.getEnv)("DOCUMENT_CONTRIBUTION_PATIENT_DATE_OF_BIRTH");
47
- const gender = (0, util_1.getEnv)("DOCUMENT_CONTRIBUTION_PATIENT_GENDER");
48
- const zip = (0, util_1.getEnv)("DOCUMENT_CONTRIBUTION_PATIENT_ZIP");
49
- const fhirUrl = (0, util_1.getEnvOrFail)("DOCUMENT_CONTRIBUTION_FHIR_URL");
50
- const docUrl = (0, util_1.getEnvOrFail)("DOCUMENT_CONTRIBUTION_URL");
51
- const rootOid = (0, util_1.getEnvOrFail)("COMMONWELL_OID");
52
- async function documentContribution({ memberManagementApi, api: apiDefaultOrg, queryMeta, }) {
53
- console.log(`>>> E3: Query for documents served by Metriport's FHIR server`);
54
- if (!firstName) {
55
- console.log(`Skipping E3 because no first name provided`);
56
- return;
57
- }
58
- if (!lastName) {
59
- console.log(`Skipping E3 because no last name provided`);
60
- return;
61
- }
62
- if (!zip) {
63
- console.log(`Skipping E3 because no zip provided`);
64
- return;
65
- }
66
- if (!dob) {
67
- console.log(`Skipping E3 because no date of birth provided`);
68
- return;
69
- }
70
- if (!gender) {
71
- console.log(`Skipping E3 because no gender provided`);
72
- return;
73
- }
74
- const { orgAPI: apiNewOrg, orgName, orgId, } = await getOrCreateOrg(memberManagementApi, queryMeta);
75
- const person = (0, payloads_1.makeDocPerson)({
76
- firstName,
77
- lastName,
78
- zip,
79
- gender,
80
- dob,
81
- facilityId: apiDefaultOrg.oid,
82
- });
83
- console.log(`Find or create patient and person on main org`);
84
- const { personId, patientId: patientIdMainOrg } = await (0, shared_person_1.findOrCreatePerson)(apiDefaultOrg, queryMeta, person);
85
- console.log(`personId: ${personId}`);
86
- console.log(`patientId on main org: ${patientIdMainOrg}`);
87
- const newPerson = (0, lodash_1.cloneDeep)(person);
88
- newPerson.identifier = (0, payloads_1.makePatient)({ facilityId: apiNewOrg.oid }).identifier;
89
- if (newPerson.identifier) {
90
- newPerson.identifier[0].assigner = orgName;
91
- newPerson.identifier[0].label = orgName;
92
- }
93
- const { patientId: patientIdNewOrg } = await (0, shared_person_1.findOrCreatePatient)(apiNewOrg, queryMeta, newPerson, personId);
94
- console.log(`patientId: ${patientIdNewOrg}`);
95
- console.log(`Get patients links`);
96
- const respGetLinks = await apiNewOrg.getNetworkLinks(queryMeta, patientIdNewOrg);
97
- console.log(respGetLinks);
98
- const allLinks = respGetLinks._embedded.networkLink
99
- ? respGetLinks._embedded.networkLink.flatMap(util_1.filterTruthy)
100
- : [];
101
- const lola1Links = allLinks.filter(commonwell_sdk_1.isLOLA1);
102
- console.log(`Found ${allLinks.length} network links, ${lola1Links.length} are LOLA 1`);
103
- for (const link of lola1Links) {
104
- const upgradeURL = link._links?.upgrade?.href;
105
- if (!upgradeURL) {
106
- console.log(`[queryDocuments] missing upgrade URL for link `, link);
107
- continue;
108
- }
109
- const respUpgradeLink = await apiNewOrg.upgradeOrDowngradeNetworkLink(queryMeta, upgradeURL);
110
- console.log(respUpgradeLink);
111
- }
112
- console.log(`>>> [E3] Populating test data on FHIR server...`);
113
- const fhirApi = axios_1.default.create({
114
- timeout: AXIOS_TIMEOUT_MILLIS,
115
- baseURL: fhirUrl,
116
- });
117
- // TODO: #230 we could split convertPatientIdToSubjectId() in two and reuse the part that splits the CW patientId.
118
- const newPatientId = patientIdNewOrg.split("%5E%5E%5E")[0];
119
- await addOrgToFHIRServer(orgId, orgName, fhirApi);
120
- await addPatientToFHIRServer(newPatientId, fhirApi);
121
- await addDocumentRefAndBinaryToFHIRServer(newPatientId, orgId, orgName, fhirApi);
122
- console.log(`>>> [E3] Querying for docs from the main org...`);
123
- const respDocQuery = await apiDefaultOrg.queryDocuments(queryMeta, patientIdMainOrg);
124
- console.log(respDocQuery);
125
- const documents = respDocQuery.entry ?? [];
126
- for (const doc of documents) {
127
- console.log(`DOCUMENT: ${JSON.stringify(doc, undefined, 2)}`);
128
- // store the query result as well
129
- const queryFileName = `./cw_contribution_${doc.id ?? "ID"}_${(0, payloads_1.makeId)()}.response.file`;
130
- fs.writeFileSync(queryFileName, JSON.stringify(doc));
131
- const fileName = `./cw_contribution_${doc.id ?? "ID"}_${(0, payloads_1.makeId)()}.contents.file`;
132
- // the default is UTF-8, avoid changing the encoding if we don't know the file we're downloading
133
- const outputStream = fs.createWriteStream(fileName, { encoding: undefined });
134
- console.log(`File being created at ${process.cwd()}/${fileName}`);
135
- const url = doc.content?.location;
136
- if (url != null)
137
- await apiDefaultOrg.retrieveDocument(queryMeta, url, outputStream);
138
- }
139
- }
140
- exports.documentContribution = documentContribution;
141
- async function getOrCreateOrg(memberManagementApi, queryMeta) {
142
- const orgPayload = (0, payloads_1.makeDocContribOrganization)(orgIdSuffix);
143
- const orgId = orgPayload.organizationId;
144
- const orgIdWithoutNamespace = orgId.slice("urn:oid:".length);
145
- const orgName = orgPayload.name;
146
- console.log(`Get the doc org - ID ${orgId}, name ${orgName}`);
147
- const respGetOneOrg = await memberManagementApi.getOneOrg(queryMeta, orgId);
148
- console.log(respGetOneOrg);
149
- if (!respGetOneOrg) {
150
- console.log(`Doc org not found, create one`);
151
- const respCreateOrg = await memberManagementApi.createOrg(queryMeta, orgPayload);
152
- console.log(respCreateOrg);
153
- console.log(`Add certificate to doc org`);
154
- const respAddCertificateToOrg = await memberManagementApi.addCertificateToOrg(queryMeta, payloads_1.certificate, orgIdWithoutNamespace);
155
- console.log(respAddCertificateToOrg);
156
- }
157
- const orgAPI = new commonwell_sdk_1.CommonWell(commonwellCert, commonwellPrivateKey, orgName, //commonwellSandboxOrgName,
158
- orgIdWithoutNamespace, //commonwellSandboxOID,
159
- commonwell_sdk_1.APIMode.integration);
160
- return { orgAPI, orgName, orgId: orgIdWithoutNamespace };
161
- }
162
- async function addOrgToFHIRServer(orgId, orgName, fhirApi) {
163
- // TODO: #230 we could create data as a JS structure instead of string - easier for future code enhancements and maintenance.
1
+ #!/usr/bin/env node
2
+ "use strict";
3
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
4
+ if (k2 === undefined) k2 = k;
5
+ var desc = Object.getOwnPropertyDescriptor(m, k);
6
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
7
+ desc = { enumerable: true, get: function() { return m[k]; } };
8
+ }
9
+ Object.defineProperty(o, k2, desc);
10
+ }) : (function(o, m, k, k2) {
11
+ if (k2 === undefined) k2 = k;
12
+ o[k2] = m[k];
13
+ }));
14
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
15
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
16
+ }) : function(o, v) {
17
+ o["default"] = v;
18
+ });
19
+ var __importStar = (this && this.__importStar) || function (mod) {
20
+ if (mod && mod.__esModule) return mod;
21
+ var result = {};
22
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
23
+ __setModuleDefault(result, mod);
24
+ return result;
25
+ };
26
+ var __importDefault = (this && this.__importDefault) || function (mod) {
27
+ return (mod && mod.__esModule) ? mod : { "default": mod };
28
+ };
29
+ Object.defineProperty(exports, "__esModule", { value: true });
30
+ exports.documentContribution = void 0;
31
+ const commonwell_sdk_1 = require("@metriport/commonwell-sdk");
32
+ const axios_1 = __importDefault(require("axios"));
33
+ const fs = __importStar(require("fs"));
34
+ const lodash_1 = require("lodash");
35
+ const payloads_1 = require("./payloads");
36
+ const shared_person_1 = require("./shared-person");
37
+ const util_1 = require("./util");
38
+ const AXIOS_TIMEOUT_MILLIS = 20000;
39
+ // Document Contribution
40
+ // https://commonwellalliance.sharepoint.com/sites/ServiceAdopter/SitePages/Document-Contribution-(SOAP,-REST).aspx
41
+ const commonwellPrivateKey = (0, util_1.getEnvOrFail)("COMMONWELL_ORG_PRIVATE_KEY");
42
+ const commonwellCert = (0, util_1.getEnvOrFail)("COMMONWELL_ORG_CERTIFICATE");
43
+ const orgIdSuffix = (0, util_1.getEnvOrFail)("DOCUMENT_CONTRIBUTION_ORGANIZATION_ID");
44
+ const firstName = (0, util_1.getEnv)("DOCUMENT_CONTRIBUTION_PATIENT_FIRST_NAME");
45
+ const lastName = (0, util_1.getEnv)("DOCUMENT_CONTRIBUTION_PATIENT_LAST_NAME");
46
+ const dob = (0, util_1.getEnv)("DOCUMENT_CONTRIBUTION_PATIENT_DATE_OF_BIRTH");
47
+ const gender = (0, util_1.getEnv)("DOCUMENT_CONTRIBUTION_PATIENT_GENDER");
48
+ const zip = (0, util_1.getEnv)("DOCUMENT_CONTRIBUTION_PATIENT_ZIP");
49
+ const fhirUrl = (0, util_1.getEnvOrFail)("DOCUMENT_CONTRIBUTION_FHIR_URL");
50
+ const docUrl = (0, util_1.getEnvOrFail)("DOCUMENT_CONTRIBUTION_URL");
51
+ const rootOid = (0, util_1.getEnvOrFail)("COMMONWELL_OID");
52
+ async function documentContribution({ memberManagementApi, api: apiDefaultOrg, queryMeta, }) {
53
+ console.log(`>>> E3: Query for documents served by Metriport's FHIR server`);
54
+ if (!firstName) {
55
+ console.log(`Skipping E3 because no first name provided`);
56
+ return;
57
+ }
58
+ if (!lastName) {
59
+ console.log(`Skipping E3 because no last name provided`);
60
+ return;
61
+ }
62
+ if (!zip) {
63
+ console.log(`Skipping E3 because no zip provided`);
64
+ return;
65
+ }
66
+ if (!dob) {
67
+ console.log(`Skipping E3 because no date of birth provided`);
68
+ return;
69
+ }
70
+ if (!gender) {
71
+ console.log(`Skipping E3 because no gender provided`);
72
+ return;
73
+ }
74
+ const { orgAPI: apiNewOrg, orgName, orgId, } = await getOrCreateOrg(memberManagementApi, queryMeta);
75
+ const person = (0, payloads_1.makeDocPerson)({
76
+ firstName,
77
+ lastName,
78
+ zip,
79
+ gender,
80
+ dob,
81
+ facilityId: apiDefaultOrg.oid,
82
+ });
83
+ console.log(`Find or create patient and person on main org`);
84
+ const { personId, patientId: patientIdMainOrg } = await (0, shared_person_1.findOrCreatePerson)(apiDefaultOrg, queryMeta, person);
85
+ console.log(`personId: ${personId}`);
86
+ console.log(`patientId on main org: ${patientIdMainOrg}`);
87
+ const newPerson = (0, lodash_1.cloneDeep)(person);
88
+ newPerson.identifier = (0, payloads_1.makePatient)({ facilityId: apiNewOrg.oid }).identifier;
89
+ if (newPerson.identifier) {
90
+ newPerson.identifier[0].assigner = orgName;
91
+ newPerson.identifier[0].label = orgName;
92
+ }
93
+ const { patientId: patientIdNewOrg } = await (0, shared_person_1.findOrCreatePatient)(apiNewOrg, queryMeta, newPerson, personId);
94
+ console.log(`patientId: ${patientIdNewOrg}`);
95
+ console.log(`Get patients links`);
96
+ const respGetLinks = await apiNewOrg.getNetworkLinks(queryMeta, patientIdNewOrg);
97
+ console.log(respGetLinks);
98
+ const allLinks = respGetLinks._embedded.networkLink
99
+ ? respGetLinks._embedded.networkLink.flatMap(util_1.filterTruthy)
100
+ : [];
101
+ const lola1Links = allLinks.filter(commonwell_sdk_1.isLOLA1);
102
+ console.log(`Found ${allLinks.length} network links, ${lola1Links.length} are LOLA 1`);
103
+ for (const link of lola1Links) {
104
+ const upgradeURL = link._links?.upgrade?.href;
105
+ if (!upgradeURL) {
106
+ console.log(`[queryDocuments] missing upgrade URL for link `, link);
107
+ continue;
108
+ }
109
+ const respUpgradeLink = await apiNewOrg.upgradeOrDowngradeNetworkLink(queryMeta, upgradeURL);
110
+ console.log(respUpgradeLink);
111
+ }
112
+ console.log(`>>> [E3] Populating test data on FHIR server...`);
113
+ const fhirApi = axios_1.default.create({
114
+ timeout: AXIOS_TIMEOUT_MILLIS,
115
+ baseURL: fhirUrl,
116
+ });
117
+ // TODO: #230 we could split convertPatientIdToSubjectId() in two and reuse the part that splits the CW patientId.
118
+ const newPatientId = patientIdNewOrg.split("%5E%5E%5E")[0];
119
+ await addOrgToFHIRServer(orgId, orgName, fhirApi);
120
+ await addPatientToFHIRServer(newPatientId, fhirApi);
121
+ await addDocumentRefAndBinaryToFHIRServer(newPatientId, orgId, orgName, fhirApi);
122
+ console.log(`>>> [E3] Querying for docs from the main org...`);
123
+ const respDocQuery = await apiDefaultOrg.queryDocuments(queryMeta, patientIdMainOrg);
124
+ console.log(respDocQuery);
125
+ const documents = respDocQuery.entry ?? [];
126
+ for (const doc of documents) {
127
+ console.log(`DOCUMENT: ${JSON.stringify(doc, undefined, 2)}`);
128
+ // store the query result as well
129
+ const queryFileName = `./cw_contribution_${doc.id ?? "ID"}_${(0, payloads_1.makeId)()}.response.file`;
130
+ fs.writeFileSync(queryFileName, JSON.stringify(doc));
131
+ const fileName = `./cw_contribution_${doc.id ?? "ID"}_${(0, payloads_1.makeId)()}.contents.file`;
132
+ // the default is UTF-8, avoid changing the encoding if we don't know the file we're downloading
133
+ const outputStream = fs.createWriteStream(fileName, { encoding: undefined });
134
+ console.log(`File being created at ${process.cwd()}/${fileName}`);
135
+ const url = doc.content?.location;
136
+ if (url != null)
137
+ await apiDefaultOrg.retrieveDocument(queryMeta, url, outputStream);
138
+ }
139
+ }
140
+ exports.documentContribution = documentContribution;
141
+ async function getOrCreateOrg(memberManagementApi, queryMeta) {
142
+ const orgPayload = (0, payloads_1.makeDocContribOrganization)(orgIdSuffix);
143
+ const orgId = orgPayload.organizationId;
144
+ const orgIdWithoutNamespace = orgId.slice("urn:oid:".length);
145
+ const orgName = orgPayload.name;
146
+ console.log(`Get the doc org - ID ${orgId}, name ${orgName}`);
147
+ const respGetOneOrg = await memberManagementApi.getOneOrg(queryMeta, orgId);
148
+ console.log(respGetOneOrg);
149
+ if (!respGetOneOrg) {
150
+ console.log(`Doc org not found, create one`);
151
+ const respCreateOrg = await memberManagementApi.createOrg(queryMeta, orgPayload);
152
+ console.log(respCreateOrg);
153
+ console.log(`Add certificate to doc org`);
154
+ const respAddCertificateToOrg = await memberManagementApi.addCertificateToOrg(queryMeta, payloads_1.certificate, orgIdWithoutNamespace);
155
+ console.log(respAddCertificateToOrg);
156
+ }
157
+ const orgAPI = new commonwell_sdk_1.CommonWell(commonwellCert, commonwellPrivateKey, orgName, //commonwellSandboxOrgName,
158
+ orgIdWithoutNamespace, //commonwellSandboxOID,
159
+ commonwell_sdk_1.APIMode.integration);
160
+ return { orgAPI, orgName, orgId: orgIdWithoutNamespace };
161
+ }
162
+ async function addOrgToFHIRServer(orgId, orgName, fhirApi) {
163
+ // TODO: #230 we could create data as a JS structure instead of string - easier for future code enhancements and maintenance.
164
164
  const data = `{
165
165
  "resourceType": "Organization",
166
166
  "id": "${orgId}",
@@ -206,10 +206,10 @@ async function addOrgToFHIRServer(orgId, orgName, fhirApi) {
206
206
  "country": "US"
207
207
  }
208
208
  ]
209
- }`;
210
- await fhirApi.put(`/Organization/${orgId}`, JSON.parse(data));
211
- }
212
- async function addPatientToFHIRServer(patientId, fhirApi) {
209
+ }`;
210
+ await fhirApi.put(`/Organization/${orgId}`, JSON.parse(data));
211
+ }
212
+ async function addPatientToFHIRServer(patientId, fhirApi) {
213
213
  const data = `{
214
214
  "resourceType": "Patient",
215
215
  "id": "${patientId}",
@@ -412,20 +412,20 @@ async function addPatientToFHIRServer(patientId, fhirApi) {
412
412
  }
413
413
  }
414
414
  ]
415
- }`;
416
- await fhirApi.put(`/Patient/${patientId}`, JSON.parse(data));
417
- }
418
- async function addDocumentRefAndBinaryToFHIRServer(patientId, orgId, orgName, fhirApi) {
419
- const binaryId = `${orgId}.969696`;
420
- const payload = fs.readFileSync("./data/doc-contrib-payload");
415
+ }`;
416
+ await fhirApi.put(`/Patient/${patientId}`, JSON.parse(data));
417
+ }
418
+ async function addDocumentRefAndBinaryToFHIRServer(patientId, orgId, orgName, fhirApi) {
419
+ const binaryId = `${orgId}.969696`;
420
+ const payload = fs.readFileSync("./data/doc-contrib-payload");
421
421
  const binaryData = `{
422
422
  "resourceType": "Binary",
423
423
  "id": "${binaryId}",
424
424
  "contentType": "application/xml",
425
425
  "data": "${payload}"
426
- }`;
427
- await fhirApi.put(`/Binary/${binaryId}`, JSON.parse(binaryData));
428
- const docRefId = `${orgId}.696969`;
426
+ }`;
427
+ await fhirApi.put(`/Binary/${binaryId}`, JSON.parse(binaryData));
428
+ const docRefId = `${orgId}.696969`;
429
429
  const data = `{
430
430
  "resourceType": "DocumentReference",
431
431
  "id": "${docRefId}",
@@ -507,8 +507,8 @@ async function addDocumentRefAndBinaryToFHIRServer(patientId, orgId, orgName, fh
507
507
  "type": "Patient"
508
508
  }
509
509
  }
510
- }`;
511
- await fhirApi.put(`/DocumentReference/${docRefId}`, JSON.parse(data));
512
- return { docRefId, binaryId };
513
- }
510
+ }`;
511
+ await fhirApi.put(`/DocumentReference/${docRefId}`, JSON.parse(data));
512
+ return { docRefId, binaryId };
513
+ }
514
514
  //# sourceMappingURL=document-contribution.js.map
package/lib/index.d.ts CHANGED
@@ -1,3 +1,3 @@
1
- #!/usr/bin/env node
2
- import { Command } from "commander";
3
- export declare const program: Command;
1
+ #!/usr/bin/env node
2
+ import { Command } from "commander";
3
+ export declare const program: Command;