@metriport/commonwell-sdk 4.3.3 → 4.3.4
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/LICENSE +20 -20
- package/README.md +40 -40
- package/lib/client/commonwell-api.d.ts +49 -49
- package/lib/client/commonwell-api.js +2 -2
- package/lib/client/commonwell.d.ts +425 -425
- package/lib/client/commonwell.js +739 -739
- package/lib/client/document.d.ts +7 -7
- package/lib/client/document.js +71 -71
- package/lib/common/commonwell-error.d.ts +12 -12
- package/lib/common/commonwell-error.js +34 -34
- package/lib/common/fileDownload.d.ts +11 -11
- package/lib/common/fileDownload.js +43 -43
- package/lib/common/make-jwt.d.ts +24 -24
- package/lib/common/make-jwt.js +90 -90
- package/lib/common/metriport-error.d.ts +4 -4
- package/lib/common/metriport-error.js +33 -33
- package/lib/common/util.d.ts +25 -25
- package/lib/common/util.js +70 -70
- package/lib/common/validate-npi.d.ts +9 -9
- package/lib/common/validate-npi.js +39 -39
- package/lib/index.d.ts +20 -20
- package/lib/index.js +54 -54
- package/lib/models/address.d.ts +50 -50
- package/lib/models/address.js +28 -28
- package/lib/models/certificates.d.ts +156 -156
- package/lib/models/certificates.js +26 -26
- package/lib/models/contact.d.ts +50 -50
- package/lib/models/contact.js +39 -39
- package/lib/models/demographics.d.ts +292 -292
- package/lib/models/demographics.js +34 -34
- package/lib/models/document.d.ts +4421 -4421
- package/lib/models/document.js +151 -151
- package/lib/models/enrollment-summary.d.ts +18 -18
- package/lib/models/enrollment-summary.js +14 -14
- package/lib/models/facility.d.ts +75 -75
- package/lib/models/facility.js +10 -10
- package/lib/models/human-name.d.ts +53 -53
- package/lib/models/human-name.js +37 -37
- package/lib/models/identifier.d.ts +79 -79
- package/lib/models/identifier.js +36 -36
- package/lib/models/iso-date.d.ts +3 -3
- package/lib/models/iso-date.js +6 -6
- package/lib/models/iso-datetime.d.ts +3 -3
- package/lib/models/iso-datetime.js +8 -8
- package/lib/models/link.d.ts +642 -642
- package/lib/models/link.js +50 -50
- package/lib/models/organization.d.ts +763 -763
- package/lib/models/organization.js +83 -83
- package/lib/models/patient.d.ts +3617 -3617
- package/lib/models/patient.js +49 -49
- package/lib/models/period.d.ts +12 -12
- package/lib/models/period.js +12 -12
- package/lib/models/person.d.ts +2440 -2440
- package/lib/models/person.js +39 -39
- package/lib/models/purpose-of-use.d.ts +29 -29
- package/lib/models/purpose-of-use.js +34 -34
- package/package.json +2 -2
package/lib/client/commonwell.js
CHANGED
|
@@ -1,740 +1,740 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
-
if (mod && mod.__esModule) return mod;
|
|
20
|
-
var result = {};
|
|
21
|
-
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
-
__setModuleDefault(result, mod);
|
|
23
|
-
return result;
|
|
24
|
-
};
|
|
25
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
26
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
|
-
};
|
|
28
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
-
exports.CommonWell = exports.APIMode = void 0;
|
|
30
|
-
const axios_1 = __importDefault(require("axios"));
|
|
31
|
-
const httpStatus = __importStar(require("http-status"));
|
|
32
|
-
const https_1 = require("https");
|
|
33
|
-
const make_jwt_1 = require("../common/make-jwt");
|
|
34
|
-
const metriport_error_1 = __importDefault(require("../common/metriport-error"));
|
|
35
|
-
const certificates_1 = require("../models/certificates");
|
|
36
|
-
const link_1 = require("../models/link");
|
|
37
|
-
const organization_1 = require("../models/organization");
|
|
38
|
-
const patient_1 = require("../models/patient");
|
|
39
|
-
const person_1 = require("../models/person");
|
|
40
|
-
const document = __importStar(require("./document"));
|
|
41
|
-
const DEFAULT_AXIOS_TIMEOUT_SECONDS = 120;
|
|
42
|
-
var APIMode;
|
|
43
|
-
(function (APIMode) {
|
|
44
|
-
APIMode["integration"] = "integration";
|
|
45
|
-
APIMode["production"] = "production";
|
|
46
|
-
})(APIMode = exports.APIMode || (exports.APIMode = {}));
|
|
47
|
-
class CommonWell {
|
|
48
|
-
/**
|
|
49
|
-
* Creates a new instance of the CommonWell API client pertaining to an
|
|
50
|
-
* organization to make requests on behalf of.
|
|
51
|
-
*
|
|
52
|
-
* @param orgCert The certificate (public key) for the organization.
|
|
53
|
-
* @param rsaPrivateKey An RSA key corresponding to the specified orgCert.
|
|
54
|
-
* @param apiMode The mode the client will be running.
|
|
55
|
-
* @param apiMode The mode the client will be running.
|
|
56
|
-
* @param options Optional parameters
|
|
57
|
-
* @param options.timeout Connection timeout in milliseconds, default 120 seconds.
|
|
58
|
-
*/
|
|
59
|
-
constructor(orgCert, rsaPrivateKey, orgName, oid, apiMode, options = {}) {
|
|
60
|
-
this.rsaPrivateKey = rsaPrivateKey;
|
|
61
|
-
this.httpsAgent = new https_1.Agent({ cert: orgCert, key: rsaPrivateKey });
|
|
62
|
-
this.api = axios_1.default.create({
|
|
63
|
-
timeout: options?.timeout ?? DEFAULT_AXIOS_TIMEOUT_SECONDS * 1000,
|
|
64
|
-
baseURL: apiMode === APIMode.production ? CommonWell.productionUrl : CommonWell.integrationUrl,
|
|
65
|
-
httpsAgent: this.httpsAgent,
|
|
66
|
-
});
|
|
67
|
-
this.api.interceptors.response.use(this.axiosSuccessfulResponse(this), this.axiosErrorResponse(this));
|
|
68
|
-
this.orgName = orgName;
|
|
69
|
-
this._oid = oid;
|
|
70
|
-
}
|
|
71
|
-
get oid() {
|
|
72
|
-
return this._oid;
|
|
73
|
-
}
|
|
74
|
-
/**
|
|
75
|
-
* Returns the `CW-Reference` header from the last request.
|
|
76
|
-
*/
|
|
77
|
-
get lastReferenceHeader() {
|
|
78
|
-
return this._lastReferenceHeader;
|
|
79
|
-
}
|
|
80
|
-
// Being extra safe with these bc a failure here fails the actual request
|
|
81
|
-
postRequest(response) {
|
|
82
|
-
this._lastReferenceHeader =
|
|
83
|
-
response && response.headers ? response.headers["cw-reference"] : undefined;
|
|
84
|
-
}
|
|
85
|
-
axiosSuccessfulResponse(_this) {
|
|
86
|
-
return (response) => {
|
|
87
|
-
_this && _this.postRequest(response);
|
|
88
|
-
return response;
|
|
89
|
-
};
|
|
90
|
-
}
|
|
91
|
-
axiosErrorResponse(_this) {
|
|
92
|
-
//eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
93
|
-
return (error) => {
|
|
94
|
-
_this && _this.postRequest(error.response);
|
|
95
|
-
throw error;
|
|
96
|
-
};
|
|
97
|
-
}
|
|
98
|
-
// TODO: #322 handle errors in API calls as per
|
|
99
|
-
// https://specification.commonwellalliance.org/services/rest-api-reference (8.6.1 Error)
|
|
100
|
-
// Note that also sometimes these calls 404 when things aren't found and etc
|
|
101
|
-
//--------------------------------------------------------------------------------------------
|
|
102
|
-
// Org Management
|
|
103
|
-
//--------------------------------------------------------------------------------------------
|
|
104
|
-
/**
|
|
105
|
-
* Create an org.
|
|
106
|
-
* See: https://commonwellalliance.sharepoint.com/sites/ServiceAdopter/SitePages/Organization-Management-API---Overview-and-Summary.aspx#post-a-new-organization
|
|
107
|
-
*
|
|
108
|
-
* @param meta Metadata about the request.
|
|
109
|
-
* @param organization The org to create.
|
|
110
|
-
* @returns
|
|
111
|
-
*/
|
|
112
|
-
async createOrg(meta, organization) {
|
|
113
|
-
const headers = await this.buildQueryHeaders(meta);
|
|
114
|
-
const resp = await this.api.post(`${CommonWell.MEMBER_ENDPOINT}/${this.oid}/org`, organization, {
|
|
115
|
-
headers,
|
|
116
|
-
});
|
|
117
|
-
return organization_1.organizationSchema.parse(resp.data);
|
|
118
|
-
}
|
|
119
|
-
/**
|
|
120
|
-
* Update an org.
|
|
121
|
-
* See: https://commonwellalliance.sharepoint.com/sites/ServiceAdopter/SitePages/Organization-Management-API---Overview-and-Summary.aspx#put-new-information-into-an-organization
|
|
122
|
-
*
|
|
123
|
-
* @param meta Metadata about the request.
|
|
124
|
-
* @param organization The org to update.
|
|
125
|
-
* @returns
|
|
126
|
-
*/
|
|
127
|
-
async updateOrg(meta, organization, id) {
|
|
128
|
-
const headers = await this.buildQueryHeaders(meta);
|
|
129
|
-
const resp = await this.api.put(`${CommonWell.MEMBER_ENDPOINT}/${this.oid}/org/${id}/`, organization, {
|
|
130
|
-
headers,
|
|
131
|
-
});
|
|
132
|
-
return organization_1.organizationSchema.parse(resp.data);
|
|
133
|
-
}
|
|
134
|
-
/**
|
|
135
|
-
* Get list of orgs.
|
|
136
|
-
* See: https://commonwellalliance.sharepoint.com/sites/ServiceAdopter/SitePages/Organization-Management-API---Overview-and-Summary.aspx#get-a-list-of-all-organizations
|
|
137
|
-
*
|
|
138
|
-
* @param meta Metadata about the request.
|
|
139
|
-
* @param summary Returns only summary data
|
|
140
|
-
* @param offset Sets an offset number from which recorded returns will begin
|
|
141
|
-
* @param limit Limits the number of returned records
|
|
142
|
-
* @param sort Specifies sort order
|
|
143
|
-
* @returns
|
|
144
|
-
*/
|
|
145
|
-
async getAllOrgs(meta, summary, offset, limit, sort) {
|
|
146
|
-
const headers = await this.buildQueryHeaders(meta);
|
|
147
|
-
const resp = await this.api.get(`${CommonWell.MEMBER_ENDPOINT}/${this.oid}/org`, {
|
|
148
|
-
headers,
|
|
149
|
-
params: { summary, offset, limit, sort },
|
|
150
|
-
});
|
|
151
|
-
return organization_1.organizationListSchema.parse(resp.data);
|
|
152
|
-
}
|
|
153
|
-
/**
|
|
154
|
-
* Get one org.
|
|
155
|
-
* See: https://commonwellalliance.sharepoint.com/sites/ServiceAdopter/SitePages/Organization-Management-API---Overview-and-Summary.aspx#get-a-single-organization
|
|
156
|
-
*
|
|
157
|
-
* @param meta Metadata about the request.
|
|
158
|
-
* @param id The org to be found
|
|
159
|
-
* @returns
|
|
160
|
-
*/
|
|
161
|
-
async getOneOrg(meta, id) {
|
|
162
|
-
const headers = await this.buildQueryHeaders(meta);
|
|
163
|
-
const resp = await this.api.get(`${CommonWell.MEMBER_ENDPOINT}/${this.oid}/org/${id}/`, {
|
|
164
|
-
headers,
|
|
165
|
-
validateStatus: null, // don't throw on status code > 299
|
|
166
|
-
});
|
|
167
|
-
const status = resp.status;
|
|
168
|
-
if (status === httpStatus.NOT_FOUND)
|
|
169
|
-
return undefined;
|
|
170
|
-
if (httpStatus[`${status}_CLASS`] === httpStatus.classes.SUCCESSFUL) {
|
|
171
|
-
return organization_1.organizationSchema.parse(resp.data);
|
|
172
|
-
}
|
|
173
|
-
throw new metriport_error_1.default(`Failed to retrieve Organization`, status);
|
|
174
|
-
}
|
|
175
|
-
/**
|
|
176
|
-
* Add certificate to org.
|
|
177
|
-
* See: https://commonwellalliance.sharepoint.com/sites/ServiceAdopter/SitePages/Organization-Management-API---Overview-and-Summary.aspx#post-new-certificates-to-organizations
|
|
178
|
-
*
|
|
179
|
-
* @param meta Metadata about the request.
|
|
180
|
-
* @param certificate The certificate to add to the org
|
|
181
|
-
* @param id The org to add a certificate too
|
|
182
|
-
* @returns
|
|
183
|
-
*/
|
|
184
|
-
async addCertificateToOrg(meta, certificate, id) {
|
|
185
|
-
const headers = await this.buildQueryHeaders(meta);
|
|
186
|
-
const resp = await this.api.post(`${CommonWell.MEMBER_ENDPOINT}/${this.oid}/org/${id}/certificate`, certificate, {
|
|
187
|
-
headers,
|
|
188
|
-
});
|
|
189
|
-
return certificates_1.certificateRespSchema.parse(resp.data);
|
|
190
|
-
}
|
|
191
|
-
/**
|
|
192
|
-
* Replace certificate for org.
|
|
193
|
-
* See: https://commonwellalliance.sharepoint.com/sites/ServiceAdopter/SitePages/Organization-Management-API---Overview-and-Summary.aspx#put-a-list-of-certificates-into-an-organization
|
|
194
|
-
*
|
|
195
|
-
* @param meta Metadata about the request.
|
|
196
|
-
* @param certificate The certificate to replace for the org
|
|
197
|
-
* @param id The org to replace a certificate for
|
|
198
|
-
* @returns
|
|
199
|
-
*/
|
|
200
|
-
async replaceCertificateForOrg(meta, certificate, id) {
|
|
201
|
-
const headers = await this.buildQueryHeaders(meta);
|
|
202
|
-
const resp = await this.api.put(`${CommonWell.MEMBER_ENDPOINT}/${this.oid}/org/${id}/certificate`, certificate, {
|
|
203
|
-
headers,
|
|
204
|
-
});
|
|
205
|
-
return certificates_1.certificateRespSchema.parse(resp.data);
|
|
206
|
-
}
|
|
207
|
-
/**
|
|
208
|
-
* Delete certificate from org.
|
|
209
|
-
* See: https://commonwellalliance.sharepoint.com/sites/ServiceAdopter/SitePages/Organization-Management-API---Overview-and-Summary.aspx#delete-certificates-by-thumbprint
|
|
210
|
-
*
|
|
211
|
-
* @param meta Metadata about the request.
|
|
212
|
-
* @param id The org to delete a certificate from
|
|
213
|
-
* @param thumbprint The thumbprint from the certificate
|
|
214
|
-
* @param purpose The purpose from the certificate
|
|
215
|
-
* @returns
|
|
216
|
-
*/
|
|
217
|
-
async deleteCertificateFromOrg(meta, id, thumbprint, purpose) {
|
|
218
|
-
const headers = await this.buildQueryHeaders(meta);
|
|
219
|
-
await this.api.delete(`${CommonWell.MEMBER_ENDPOINT}/${this.oid}/org/${id}/certificate/${thumbprint}/purpose/${purpose}`, {
|
|
220
|
-
headers,
|
|
221
|
-
});
|
|
222
|
-
return;
|
|
223
|
-
}
|
|
224
|
-
/**
|
|
225
|
-
* Get certificate from org.
|
|
226
|
-
* See: https://commonwellalliance.sharepoint.com/sites/ServiceAdopter/SitePages/Organization-Management-API---Overview-and-Summary.aspx#get-certificates-for-an-organization
|
|
227
|
-
*
|
|
228
|
-
* @param meta Metadata about the request.
|
|
229
|
-
* @param certificate The certificate to add to the org
|
|
230
|
-
* @param id The org to get a certificate from
|
|
231
|
-
* @param thumbprint The thumbprint from the certificate
|
|
232
|
-
* @param purpose The purpose from the certificate
|
|
233
|
-
* @returns
|
|
234
|
-
*/
|
|
235
|
-
async getCertificatesFromOrg(meta, id, thumbprint, purpose) {
|
|
236
|
-
const headers = await this.buildQueryHeaders(meta);
|
|
237
|
-
const resp = await this.api.get(`${CommonWell.MEMBER_ENDPOINT}/${this.oid}/org/${id}/certificate`, {
|
|
238
|
-
headers,
|
|
239
|
-
params: { thumbprint, purpose },
|
|
240
|
-
});
|
|
241
|
-
return certificates_1.certificateRespSchema.parse(resp.data);
|
|
242
|
-
}
|
|
243
|
-
/**
|
|
244
|
-
* Get certificate from org (by thumbprint).
|
|
245
|
-
* See: https://commonwellalliance.sharepoint.com/sites/ServiceAdopter/SitePages/Organization-Management-API---Overview-and-Summary.aspx#get-certificates-by-thumbprint
|
|
246
|
-
*
|
|
247
|
-
* @param meta Metadata about the request.
|
|
248
|
-
* @param certificate The certificate to add to the org
|
|
249
|
-
* @param id The org to get a certificate from
|
|
250
|
-
* @param thumbprint The thumbprint from the certificate
|
|
251
|
-
* @param purpose The purpose from the certificate
|
|
252
|
-
* @returns
|
|
253
|
-
*/
|
|
254
|
-
async getCertificatesFromOrgByThumbprint(meta, id, thumbprint, purpose) {
|
|
255
|
-
const headers = await this.buildQueryHeaders(meta);
|
|
256
|
-
const resp = await this.api.get(`${CommonWell.MEMBER_ENDPOINT}/${this.oid}/org/${id}/certificate/${thumbprint}`, {
|
|
257
|
-
headers,
|
|
258
|
-
params: { purpose },
|
|
259
|
-
});
|
|
260
|
-
return certificates_1.certificateRespSchema.parse(resp.data);
|
|
261
|
-
}
|
|
262
|
-
/**
|
|
263
|
-
* Get certificate from org (by thumbprint & purpose).
|
|
264
|
-
* See: https://commonwellalliance.sharepoint.com/sites/ServiceAdopter/SitePages/Organization-Management-API---Overview-and-Summary.aspx#get-certificates-by-thumbprint-and-purpose
|
|
265
|
-
*
|
|
266
|
-
* @param meta Metadata about the request.
|
|
267
|
-
* @param certificate The certificate to add to the org
|
|
268
|
-
* @param id The org to get a certificate from
|
|
269
|
-
* @param thumbprint The thumbprint from the certificate
|
|
270
|
-
* @param purpose The purpose from the certificate
|
|
271
|
-
* @returns
|
|
272
|
-
*/
|
|
273
|
-
async getCertificatesFromOrgByThumbprintAndPurpose(meta, id, thumbprint, purpose) {
|
|
274
|
-
const headers = await this.buildQueryHeaders(meta);
|
|
275
|
-
const resp = await this.api.get(`${CommonWell.MEMBER_ENDPOINT}/${this.oid}/org/${id}/certificate/${thumbprint}/purpose/${purpose}`, {
|
|
276
|
-
headers,
|
|
277
|
-
});
|
|
278
|
-
return certificates_1.certificateRespSchema.parse(resp.data);
|
|
279
|
-
}
|
|
280
|
-
//--------------------------------------------------------------------------------------------
|
|
281
|
-
// Person Management
|
|
282
|
-
//--------------------------------------------------------------------------------------------
|
|
283
|
-
/**
|
|
284
|
-
* Enrolls a new person.
|
|
285
|
-
* See: https://specification.commonwellalliance.org/services/patient-identity-and-linking/protocol-operations#8716-adding-a-new-person
|
|
286
|
-
*
|
|
287
|
-
* @param meta Metadata about the request.
|
|
288
|
-
* @param person The person to enroll.
|
|
289
|
-
* @returns
|
|
290
|
-
*/
|
|
291
|
-
async enrollPerson(meta, person) {
|
|
292
|
-
const headers = await this.buildQueryHeaders(meta);
|
|
293
|
-
const resp = await this.api.post(CommonWell.PERSON_ENDPOINT, person, {
|
|
294
|
-
headers,
|
|
295
|
-
});
|
|
296
|
-
return person_1.personSchema.parse(resp.data);
|
|
297
|
-
}
|
|
298
|
-
/**
|
|
299
|
-
* Searches for a person based on the specified strong ID.
|
|
300
|
-
* See: https://specification.commonwellalliance.org/services/patient-identity-and-linking/protocol-operations#8711-search-for-a-person
|
|
301
|
-
*
|
|
302
|
-
* @param meta Metadata about the request.
|
|
303
|
-
* @param key The ID's value, for example the driver's license ID.
|
|
304
|
-
* @param system The ID's uri to specify type, for example "urn:oid:2.16.840.1.113883.4.3.6" is a CA DL.
|
|
305
|
-
* @returns
|
|
306
|
-
*/
|
|
307
|
-
async searchPerson(meta, key, system) {
|
|
308
|
-
const headers = await this.buildQueryHeaders(meta);
|
|
309
|
-
const resp = await this.api.get(CommonWell.PERSON_ENDPOINT, {
|
|
310
|
-
headers,
|
|
311
|
-
params: { key, system },
|
|
312
|
-
});
|
|
313
|
-
return person_1.personSearchRespSchema.parse(resp.data);
|
|
314
|
-
}
|
|
315
|
-
/**
|
|
316
|
-
* Searches for a person based on patient demo.
|
|
317
|
-
* See: https://specification.commonwellalliance.org/services/patient-identity-and-linking/protocol-operations#8713-find-persons-matching-patient-demographics
|
|
318
|
-
*
|
|
319
|
-
* @param meta Metadata about the request.
|
|
320
|
-
* @param patientId The patient ID.
|
|
321
|
-
* @returns
|
|
322
|
-
*/
|
|
323
|
-
async searchPersonByPatientDemo(meta, patientId) {
|
|
324
|
-
const headers = await this.buildQueryHeaders(meta);
|
|
325
|
-
const resp = await this.api.get(`${CommonWell.ORG_ENDPOINT}/${this.oid}/patient/${patientId}/person`, {
|
|
326
|
-
headers,
|
|
327
|
-
});
|
|
328
|
-
return person_1.personSearchRespSchema.parse(resp.data);
|
|
329
|
-
}
|
|
330
|
-
/**
|
|
331
|
-
* Gets a person based on person id.
|
|
332
|
-
* See: https://specification.commonwellalliance.org/services/patient-identity-and-linking/protocol-operations#8713-find-persons-matching-patient-demographics
|
|
333
|
-
*
|
|
334
|
-
* @param meta Metadata about the request.
|
|
335
|
-
* @param personId The person ID.
|
|
336
|
-
* @returns
|
|
337
|
-
*/
|
|
338
|
-
async getPersonById(meta, personId) {
|
|
339
|
-
const headers = await this.buildQueryHeaders(meta);
|
|
340
|
-
const resp = await this.api.get(`${CommonWell.PERSON_ENDPOINT}/${personId}`, {
|
|
341
|
-
headers,
|
|
342
|
-
});
|
|
343
|
-
return person_1.personSchema.parse(resp.data);
|
|
344
|
-
}
|
|
345
|
-
/**
|
|
346
|
-
* Updates a person.
|
|
347
|
-
* See: https://specification.commonwellalliance.org/services/patient-identity-and-linking/protocol-operations#8741-updating-person-information
|
|
348
|
-
*
|
|
349
|
-
* @param meta Metadata about the request.
|
|
350
|
-
* @param person The data to update.
|
|
351
|
-
* @param id The person to be updated.
|
|
352
|
-
* @returns
|
|
353
|
-
*/
|
|
354
|
-
async updatePerson(meta, person, id) {
|
|
355
|
-
const headers = await this.buildQueryHeaders(meta);
|
|
356
|
-
const resp = await this.api.post(`${CommonWell.PERSON_ENDPOINT}/${id}`, person, {
|
|
357
|
-
headers,
|
|
358
|
-
});
|
|
359
|
-
return person_1.personSchema.parse(resp.data);
|
|
360
|
-
}
|
|
361
|
-
/**
|
|
362
|
-
* Matches a person to a patient.
|
|
363
|
-
* See: https://specification.commonwellalliance.org/services/patient-identity-and-linking/protocol-operations#8732-retrieve-patient-matches
|
|
364
|
-
*
|
|
365
|
-
* @param meta Metadata about the request.
|
|
366
|
-
* @param id The person to be matched.
|
|
367
|
-
* @returns
|
|
368
|
-
*/
|
|
369
|
-
async patientMatch(meta, id) {
|
|
370
|
-
const headers = await this.buildQueryHeaders(meta);
|
|
371
|
-
const resp = await this.api.get(`${CommonWell.PERSON_ENDPOINT}/${id}/patientMatch`, {
|
|
372
|
-
headers,
|
|
373
|
-
params: { orgId: this.oid },
|
|
374
|
-
});
|
|
375
|
-
return patient_1.patientSearchRespSchema.parse(resp.data);
|
|
376
|
-
}
|
|
377
|
-
/**
|
|
378
|
-
* @deprecated use addPatientLink() instead
|
|
379
|
-
*/
|
|
380
|
-
async patientLink(meta, personId, patientUri, patientStrongId) {
|
|
381
|
-
return this.addPatientLink(meta, personId, patientUri, patientStrongId);
|
|
382
|
-
}
|
|
383
|
-
/**
|
|
384
|
-
* Add patient link to person.
|
|
385
|
-
* See: https://specification.commonwellalliance.org/services/patient-identity-and-linking/protocol-operations#8721
|
|
386
|
-
*
|
|
387
|
-
* @param meta Metadata about the request.
|
|
388
|
-
* @param personId The person id to be link to a patient.
|
|
389
|
-
* @param patientUri The patient uri to be link to a person.
|
|
390
|
-
* @param [patientStrongId] The patient's strong ID, if available (optional).
|
|
391
|
-
* @returns {PatientLink}
|
|
392
|
-
*/
|
|
393
|
-
async addPatientLink(meta, personId, patientUri, patientStrongId) {
|
|
394
|
-
const headers = await this.buildQueryHeaders(meta);
|
|
395
|
-
const resp = await this.api.post(`${CommonWell.PERSON_ENDPOINT}/${personId}/patientLink`, {
|
|
396
|
-
patient: patientUri,
|
|
397
|
-
...(patientStrongId ? { identifier: patientStrongId } : undefined),
|
|
398
|
-
}, { headers });
|
|
399
|
-
return person_1.patientLinkSchema.parse(resp.data);
|
|
400
|
-
}
|
|
401
|
-
/**
|
|
402
|
-
* Re-enrolls a person.
|
|
403
|
-
* See: https://specification.commonwellalliance.org/services/patient-identity-and-linking/protocol-operations#875-person-unenrollment
|
|
404
|
-
*
|
|
405
|
-
* @param meta Metadata about the request.
|
|
406
|
-
* @param id The person to be re-enrolled.
|
|
407
|
-
* @returns Person with enrollment information
|
|
408
|
-
*/
|
|
409
|
-
async reenrollPerson(meta, id) {
|
|
410
|
-
const headers = await this.buildQueryHeaders(meta);
|
|
411
|
-
const resp = await this.api.put(`${CommonWell.PERSON_ENDPOINT}/${id}/enroll`, {}, { headers });
|
|
412
|
-
return person_1.personSchema.parse(resp.data);
|
|
413
|
-
}
|
|
414
|
-
/**
|
|
415
|
-
* Unenrolls a person.
|
|
416
|
-
* See: https://specification.commonwellalliance.org/services/patient-identity-and-linking/protocol-operations#875-person-unenrollment
|
|
417
|
-
*
|
|
418
|
-
* @param meta Metadata about the request.
|
|
419
|
-
* @param id The person to be unenrolled.
|
|
420
|
-
* @returns
|
|
421
|
-
*/
|
|
422
|
-
async unenrollPerson(meta, id) {
|
|
423
|
-
const headers = await this.buildQueryHeaders(meta);
|
|
424
|
-
const resp = await this.api.put(`${CommonWell.PERSON_ENDPOINT}/${id}/unenroll`, {}, {
|
|
425
|
-
headers,
|
|
426
|
-
});
|
|
427
|
-
return person_1.personSchema.parse(resp.data);
|
|
428
|
-
}
|
|
429
|
-
/**
|
|
430
|
-
* Deletes a person.
|
|
431
|
-
* See: https://specification.commonwellalliance.org/services/patient-identity-and-linking/protocol-operations#8742-deleting-a-person
|
|
432
|
-
*
|
|
433
|
-
* @param meta Metadata about the request.
|
|
434
|
-
* @param id The person to be deleted.
|
|
435
|
-
* @returns
|
|
436
|
-
*/
|
|
437
|
-
async deletePerson(meta, id) {
|
|
438
|
-
const headers = await this.buildQueryHeaders(meta);
|
|
439
|
-
await this.api.delete(`${CommonWell.PERSON_ENDPOINT}/${id}`, { headers });
|
|
440
|
-
return;
|
|
441
|
-
}
|
|
442
|
-
//--------------------------------------------------------------------------------------------
|
|
443
|
-
// Patient Management
|
|
444
|
-
//--------------------------------------------------------------------------------------------
|
|
445
|
-
/**
|
|
446
|
-
* Register a new patient.
|
|
447
|
-
* See: https://specification.commonwellalliance.org/services/patient-identity-and-linking/protocol-operations#8762-adding-a-local-patient-record
|
|
448
|
-
*
|
|
449
|
-
* @param meta Metadata about the request.
|
|
450
|
-
* @param patient The patient to register.
|
|
451
|
-
* @returns
|
|
452
|
-
*/
|
|
453
|
-
async registerPatient(meta, patient) {
|
|
454
|
-
const headers = await this.buildQueryHeaders(meta);
|
|
455
|
-
const resp = await this.api.post(`${CommonWell.ORG_ENDPOINT}/${this.oid}/patient`, patient, {
|
|
456
|
-
headers,
|
|
457
|
-
});
|
|
458
|
-
return patient_1.patientSchema.parse(resp.data);
|
|
459
|
-
}
|
|
460
|
-
/**
|
|
461
|
-
* Returns a patient based on its ID.
|
|
462
|
-
*
|
|
463
|
-
* @param meta Metadata about the request.
|
|
464
|
-
* @param id Patient's ID.
|
|
465
|
-
* @returns {Promise<Patient>}
|
|
466
|
-
*/
|
|
467
|
-
async getPatient(meta, id) {
|
|
468
|
-
const headers = await this.buildQueryHeaders(meta);
|
|
469
|
-
const suffix = id.endsWith("/") ? "" : "/";
|
|
470
|
-
const resp = await this.api.get(`${CommonWell.ORG_ENDPOINT}/${this.oid}/patient/${id}${suffix}`, { headers });
|
|
471
|
-
return patient_1.patientSchema.parse(resp.data);
|
|
472
|
-
}
|
|
473
|
-
/**
|
|
474
|
-
* Searches for a patient based on params.
|
|
475
|
-
* See: https://specification.commonwellalliance.org/services/patient-identity-and-linking/protocol-operations#8761-search-for-a-patient
|
|
476
|
-
*
|
|
477
|
-
* @param meta Metadata about the request.
|
|
478
|
-
* @param fname Patient's first name.
|
|
479
|
-
* @param lname Patient's last name.
|
|
480
|
-
* @param dob Patient's date of birth.
|
|
481
|
-
* @param gender Patient's gender.
|
|
482
|
-
* @param zip Patient's zip code.
|
|
483
|
-
* @returns
|
|
484
|
-
*/
|
|
485
|
-
async searchPatient(meta, fname, lname, dob, gender, zip) {
|
|
486
|
-
const headers = await this.buildQueryHeaders(meta);
|
|
487
|
-
const resp = await this.api.get(`${CommonWell.ORG_ENDPOINT}/${this.oid}/patient`, {
|
|
488
|
-
headers,
|
|
489
|
-
params: { fname, lname, dob, gender, zip },
|
|
490
|
-
});
|
|
491
|
-
return patient_1.patientSearchRespSchema.parse(resp.data);
|
|
492
|
-
}
|
|
493
|
-
/**
|
|
494
|
-
* Updates a patient.
|
|
495
|
-
* See: https://specification.commonwellalliance.org/services/patient-identity-and-linking/protocol-operations#8763-updating-a-local-patient-record
|
|
496
|
-
*
|
|
497
|
-
* @param meta Metadata about the request.
|
|
498
|
-
* @param patient The data to update.
|
|
499
|
-
* @param id The patient to be updated.
|
|
500
|
-
* @returns
|
|
501
|
-
*/
|
|
502
|
-
async updatePatient(meta, patient, id) {
|
|
503
|
-
const headers = await this.buildQueryHeaders(meta);
|
|
504
|
-
const resp = await this.api.post(`${CommonWell.ORG_ENDPOINT}/${this.oid}/patient/${id}/`, patient, {
|
|
505
|
-
headers,
|
|
506
|
-
});
|
|
507
|
-
return patient_1.patientSchema.parse(resp.data);
|
|
508
|
-
}
|
|
509
|
-
/**
|
|
510
|
-
* Merges patients.
|
|
511
|
-
* See: https://specification.commonwellalliance.org/services/patient-identity-and-linking/protocol-operations#8765-merging-local-patient-records
|
|
512
|
-
*
|
|
513
|
-
* @param meta Metadata about the request.
|
|
514
|
-
* @param nonSurvivingPatientId The local Patient Identifier of the non-surviving Patient Record (This patient gets replaced)
|
|
515
|
-
* @param referencePatientLink The patient link for the patient that will replace the non surviving patient
|
|
516
|
-
* @returns
|
|
517
|
-
*/
|
|
518
|
-
async mergePatients(meta, nonSurvivingPatientId, referencePatientLink) {
|
|
519
|
-
const headers = await this.buildQueryHeaders(meta);
|
|
520
|
-
await this.api.put(`${CommonWell.ORG_ENDPOINT}/${this.oid}/patient/${nonSurvivingPatientId}/merge`, {
|
|
521
|
-
link: {
|
|
522
|
-
other: {
|
|
523
|
-
reference: referencePatientLink,
|
|
524
|
-
},
|
|
525
|
-
type: "replace",
|
|
526
|
-
},
|
|
527
|
-
}, {
|
|
528
|
-
headers,
|
|
529
|
-
});
|
|
530
|
-
return;
|
|
531
|
-
}
|
|
532
|
-
/**
|
|
533
|
-
* Get Patient's Network Links.
|
|
534
|
-
* See: https://specification.commonwellalliance.org/services/record-locator-service/protocol-operations-record-locator-service#8771-retrieving-network-links
|
|
535
|
-
*
|
|
536
|
-
* @param meta Metadata about the request.
|
|
537
|
-
* @param patientId Patient for which to get the network links.
|
|
538
|
-
* @returns
|
|
539
|
-
*/
|
|
540
|
-
async getNetworkLinks(meta, patientId) {
|
|
541
|
-
const headers = await this.buildQueryHeaders(meta);
|
|
542
|
-
// Error handling: https://github.com/metriport/metriport-internal/issues/322
|
|
543
|
-
try {
|
|
544
|
-
const resp = await this.api.get(`${CommonWell.ORG_ENDPOINT}/${this.oid}/patient/${patientId}/networkLink`, {
|
|
545
|
-
headers,
|
|
546
|
-
});
|
|
547
|
-
return patient_1.patientNetworkLinkRespSchema.parse(resp.data);
|
|
548
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
549
|
-
}
|
|
550
|
-
catch (err) {
|
|
551
|
-
// when there's no NetworkLink, CW's API return 412
|
|
552
|
-
if (err.response?.status === 412)
|
|
553
|
-
return { _embedded: { networkLink: [] } };
|
|
554
|
-
throw err;
|
|
555
|
-
}
|
|
556
|
-
}
|
|
557
|
-
/**
|
|
558
|
-
* Deletes a patient.
|
|
559
|
-
* See: https://specification.commonwellalliance.org/services/patient-identity-and-linking/protocol-operations#8764-deleting-a-local-patient-record
|
|
560
|
-
*
|
|
561
|
-
* @param meta Metadata about the request.
|
|
562
|
-
* @param id The patient to be updated.
|
|
563
|
-
* @returns
|
|
564
|
-
*/
|
|
565
|
-
async deletePatient(meta, id) {
|
|
566
|
-
const headers = await this.buildQueryHeaders(meta);
|
|
567
|
-
await this.api.delete(`${CommonWell.ORG_ENDPOINT}/${this.oid}/patient/${id}/`, {
|
|
568
|
-
headers,
|
|
569
|
-
});
|
|
570
|
-
}
|
|
571
|
-
//--------------------------------------------------------------------------------------------
|
|
572
|
-
// Document Management
|
|
573
|
-
//--------------------------------------------------------------------------------------------
|
|
574
|
-
/**
|
|
575
|
-
* Queries a patient's Documents.
|
|
576
|
-
*
|
|
577
|
-
* @param meta Metadata about the request.
|
|
578
|
-
* @param patientId The patient's ID.
|
|
579
|
-
* @returns {Promise<DocumentQueryResponse>}
|
|
580
|
-
* @see {@link https://specification.commonwellalliance.org/services/data-broker/cha-broker-api-reference#104-document-query|Use case}
|
|
581
|
-
* @see {@link https://specification.commonwellalliance.org/services/data-broker/protocol-operations-data-broker#8781-find-documents|API spec}
|
|
582
|
-
*/
|
|
583
|
-
async queryDocuments(meta, patientId) {
|
|
584
|
-
const headers = await this.buildQueryHeaders(meta);
|
|
585
|
-
return document.query(this.api, headers, patientId);
|
|
586
|
-
}
|
|
587
|
-
/**
|
|
588
|
-
* Queries a patient's Documents - including other possible results.
|
|
589
|
-
*
|
|
590
|
-
* @param meta Metadata about the request.
|
|
591
|
-
* @param patientId The patient's ID.
|
|
592
|
-
* @returns The DocumentReferences of a patient's available documents and/or OperationOutcomes denoting problems with the query.
|
|
593
|
-
* @see {@link https://specification.commonwellalliance.org/services/data-broker/cha-broker-api-reference#104-document-query|Use case}
|
|
594
|
-
* @see {@link https://specification.commonwellalliance.org/services/data-broker/protocol-operations-data-broker#8781-find-documents|API spec}
|
|
595
|
-
*/
|
|
596
|
-
async queryDocumentsFull(meta, patientId) {
|
|
597
|
-
const headers = await this.buildQueryHeaders(meta);
|
|
598
|
-
return document.queryFull(this.api, headers, patientId);
|
|
599
|
-
}
|
|
600
|
-
/**
|
|
601
|
-
* Retrieve a Document and pipe its bytes into the outputStream.
|
|
602
|
-
*
|
|
603
|
-
* @param {string} inputUrl - The URL of the file to be downloaded.
|
|
604
|
-
* @param {fs.WriteStream} outputStream - The stream to receive the downloaded file's bytes.
|
|
605
|
-
* @returns {Promise<void>}
|
|
606
|
-
* @see {@link https://specification.commonwellalliance.org/services/data-broker/cha-broker-api-reference#106-document-retrieval|Use case}
|
|
607
|
-
* @see {@link https://specification.commonwellalliance.org/services/data-broker/protocol-operations-data-broker#8782-retrieve-document|API spec}
|
|
608
|
-
*/
|
|
609
|
-
async retrieveDocument(meta, inputUrl, outputStream) {
|
|
610
|
-
const headers = await this.buildQueryHeaders(meta);
|
|
611
|
-
return document.retrieve(this.api, headers, inputUrl, outputStream);
|
|
612
|
-
}
|
|
613
|
-
//--------------------------------------------------------------------------------------------
|
|
614
|
-
// Link Management
|
|
615
|
-
//--------------------------------------------------------------------------------------------
|
|
616
|
-
/**
|
|
617
|
-
* Upgrade or downgrade network link.
|
|
618
|
-
* See: https://specification.commonwellalliance.org/services/record-locator-service/protocol-operations-record-locator-service#8772-upgrading-a-network-link
|
|
619
|
-
*
|
|
620
|
-
* @param meta Metadata about the request.
|
|
621
|
-
* @param href The href of network link to be upgraded or downgraded
|
|
622
|
-
* @param proxy The proxy for the patient link action.
|
|
623
|
-
* @returns
|
|
624
|
-
*/
|
|
625
|
-
async upgradeOrDowngradeNetworkLink(meta, href, proxy) {
|
|
626
|
-
const headers = await this.buildQueryHeaders(meta);
|
|
627
|
-
const resp = await this.api.post(href, {
|
|
628
|
-
proxy,
|
|
629
|
-
}, {
|
|
630
|
-
headers,
|
|
631
|
-
});
|
|
632
|
-
return link_1.networkLinkSchema.parse(resp.data);
|
|
633
|
-
}
|
|
634
|
-
/**
|
|
635
|
-
* Update a patient link.
|
|
636
|
-
* See: https://specification.commonwellalliance.org/services/patient-identity-and-linking/protocol-operations#8722-updating-a-patient-link
|
|
637
|
-
*
|
|
638
|
-
* @param meta Metadata about the request.
|
|
639
|
-
* @param patientLinkUri The uri of patient link to be updated
|
|
640
|
-
* @param patientUri The uri of patient that belongs to this link
|
|
641
|
-
* @param identifier Add identifier information to the patient link
|
|
642
|
-
* @returns
|
|
643
|
-
*/
|
|
644
|
-
async updatePatientLink(meta, patientLinkUri, patientUri, identifier) {
|
|
645
|
-
const headers = await this.buildQueryHeaders(meta);
|
|
646
|
-
const resp = await this.api.post(patientLinkUri, {
|
|
647
|
-
patient: patientUri,
|
|
648
|
-
identifier: identifier,
|
|
649
|
-
}, {
|
|
650
|
-
headers,
|
|
651
|
-
});
|
|
652
|
-
return person_1.patientLinkSchema.parse(resp.data);
|
|
653
|
-
}
|
|
654
|
-
/**
|
|
655
|
-
* Get a person's links to patients.
|
|
656
|
-
* See: https://specification.commonwellalliance.org/services/patient-identity-and-linking/protocol-operations#8721
|
|
657
|
-
*
|
|
658
|
-
* @param meta Metadata about the request.
|
|
659
|
-
* @param personId The person id to be link to a patient.
|
|
660
|
-
* @param [limitToOrg=true] Whether to limit the search to the current organization (optional).
|
|
661
|
-
* @returns Response with list of links to Patients
|
|
662
|
-
*/
|
|
663
|
-
async getPatientLinks(meta, personId, limitToOrg = true) {
|
|
664
|
-
const headers = await this.buildQueryHeaders(meta);
|
|
665
|
-
const resp = await this.api.get(`${CommonWell.PERSON_ENDPOINT}/${personId}/patientLink`, {
|
|
666
|
-
headers,
|
|
667
|
-
params: {
|
|
668
|
-
...(limitToOrg ? { orgId: this.oid } : undefined),
|
|
669
|
-
},
|
|
670
|
-
});
|
|
671
|
-
return person_1.patientLinkSearchRespSchema.parse(resp.data);
|
|
672
|
-
}
|
|
673
|
-
/**
|
|
674
|
-
* Gets a patient link.
|
|
675
|
-
* See: https://specification.commonwellalliance.org/services/patient-identity-and-linking/protocol-operations#8723-getting-a-patient-link
|
|
676
|
-
*
|
|
677
|
-
* @param meta Metadata about the request.
|
|
678
|
-
* @param personId Person that is linked
|
|
679
|
-
* @param patientId Patient that is linked
|
|
680
|
-
* @returns
|
|
681
|
-
*/
|
|
682
|
-
async getPatientLink(meta, personId, patientId) {
|
|
683
|
-
const headers = await this.buildQueryHeaders(meta);
|
|
684
|
-
const resp = await this.api.get(`/v1/person/${personId}/patientLink/${patientId}/`, {
|
|
685
|
-
headers,
|
|
686
|
-
});
|
|
687
|
-
return patient_1.patientLinkRespSchema.parse(resp.data);
|
|
688
|
-
}
|
|
689
|
-
/**
|
|
690
|
-
* Deletes a patient link - the link will be moved to LOLA 0 and cannot be used again.
|
|
691
|
-
* See: https://specification.commonwellalliance.org/services/patient-identity-and-linking/protocol-operations#8724-deleting-a-patient-link
|
|
692
|
-
*
|
|
693
|
-
* WARNING: This shouldn't be used except under the explicit request of a person.
|
|
694
|
-
*
|
|
695
|
-
* @param meta Metadata about the request.
|
|
696
|
-
* @param patientLinkUri The uri of patient link to be deleted
|
|
697
|
-
* @returns
|
|
698
|
-
*/
|
|
699
|
-
async deletePatientLink(meta, patientLinkUri) {
|
|
700
|
-
const headers = await this.buildQueryHeaders(meta);
|
|
701
|
-
await this.api.delete(patientLinkUri, {
|
|
702
|
-
headers,
|
|
703
|
-
});
|
|
704
|
-
return;
|
|
705
|
-
}
|
|
706
|
-
/**
|
|
707
|
-
* Resets a patient link - the link will be moved to LOLA 1 and can be relinked later.
|
|
708
|
-
* See: https://specification.commonwellalliance.org/services/patient-identity-and-linking/protocol-operations#8725-resetting-a-patient-link
|
|
709
|
-
*
|
|
710
|
-
* @param meta Metadata about the request.
|
|
711
|
-
* @param personId Person that is linked
|
|
712
|
-
* @param patientId Patient that is linked
|
|
713
|
-
* @returns
|
|
714
|
-
*/
|
|
715
|
-
async resetPatientLink(meta, personId, patientId) {
|
|
716
|
-
const headers = await this.buildQueryHeaders(meta);
|
|
717
|
-
await this.api.put(`/v1/person/${personId}/patientLink/${patientId}/reset`, {}, {
|
|
718
|
-
headers,
|
|
719
|
-
});
|
|
720
|
-
return;
|
|
721
|
-
}
|
|
722
|
-
//--------------------------------------------------------------------------------------------
|
|
723
|
-
// Private Methods
|
|
724
|
-
//--------------------------------------------------------------------------------------------
|
|
725
|
-
async buildQueryHeaders(meta) {
|
|
726
|
-
const jwt = await (0, make_jwt_1.makeJwt)(this.rsaPrivateKey, meta.role, meta.subjectId, this.orgName, this.oid, meta.purposeOfUse, meta.npi, meta.payloadHash);
|
|
727
|
-
return { Authorization: `Bearer ${jwt}` };
|
|
728
|
-
}
|
|
729
|
-
}
|
|
730
|
-
exports.CommonWell = CommonWell;
|
|
731
|
-
CommonWell.integrationUrl = "https://integration.rest.api.commonwellalliance.org";
|
|
732
|
-
CommonWell.productionUrl = "https://rest.api.commonwellalliance.org";
|
|
733
|
-
// V1
|
|
734
|
-
CommonWell.PERSON_ENDPOINT = "/v1/person";
|
|
735
|
-
CommonWell.ORG_ENDPOINT = "/v1/org";
|
|
736
|
-
CommonWell.PATIENT_ENDPOINT = "/v1/patient";
|
|
737
|
-
CommonWell.MEMBER_ENDPOINT = "/v1/member";
|
|
738
|
-
// V2
|
|
739
|
-
CommonWell.DOCUMENT_QUERY_ENDPOINT = "/v2/documentReference";
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
26
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
|
+
};
|
|
28
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
+
exports.CommonWell = exports.APIMode = void 0;
|
|
30
|
+
const axios_1 = __importDefault(require("axios"));
|
|
31
|
+
const httpStatus = __importStar(require("http-status"));
|
|
32
|
+
const https_1 = require("https");
|
|
33
|
+
const make_jwt_1 = require("../common/make-jwt");
|
|
34
|
+
const metriport_error_1 = __importDefault(require("../common/metriport-error"));
|
|
35
|
+
const certificates_1 = require("../models/certificates");
|
|
36
|
+
const link_1 = require("../models/link");
|
|
37
|
+
const organization_1 = require("../models/organization");
|
|
38
|
+
const patient_1 = require("../models/patient");
|
|
39
|
+
const person_1 = require("../models/person");
|
|
40
|
+
const document = __importStar(require("./document"));
|
|
41
|
+
const DEFAULT_AXIOS_TIMEOUT_SECONDS = 120;
|
|
42
|
+
var APIMode;
|
|
43
|
+
(function (APIMode) {
|
|
44
|
+
APIMode["integration"] = "integration";
|
|
45
|
+
APIMode["production"] = "production";
|
|
46
|
+
})(APIMode = exports.APIMode || (exports.APIMode = {}));
|
|
47
|
+
class CommonWell {
|
|
48
|
+
/**
|
|
49
|
+
* Creates a new instance of the CommonWell API client pertaining to an
|
|
50
|
+
* organization to make requests on behalf of.
|
|
51
|
+
*
|
|
52
|
+
* @param orgCert The certificate (public key) for the organization.
|
|
53
|
+
* @param rsaPrivateKey An RSA key corresponding to the specified orgCert.
|
|
54
|
+
* @param apiMode The mode the client will be running.
|
|
55
|
+
* @param apiMode The mode the client will be running.
|
|
56
|
+
* @param options Optional parameters
|
|
57
|
+
* @param options.timeout Connection timeout in milliseconds, default 120 seconds.
|
|
58
|
+
*/
|
|
59
|
+
constructor(orgCert, rsaPrivateKey, orgName, oid, apiMode, options = {}) {
|
|
60
|
+
this.rsaPrivateKey = rsaPrivateKey;
|
|
61
|
+
this.httpsAgent = new https_1.Agent({ cert: orgCert, key: rsaPrivateKey });
|
|
62
|
+
this.api = axios_1.default.create({
|
|
63
|
+
timeout: options?.timeout ?? DEFAULT_AXIOS_TIMEOUT_SECONDS * 1000,
|
|
64
|
+
baseURL: apiMode === APIMode.production ? CommonWell.productionUrl : CommonWell.integrationUrl,
|
|
65
|
+
httpsAgent: this.httpsAgent,
|
|
66
|
+
});
|
|
67
|
+
this.api.interceptors.response.use(this.axiosSuccessfulResponse(this), this.axiosErrorResponse(this));
|
|
68
|
+
this.orgName = orgName;
|
|
69
|
+
this._oid = oid;
|
|
70
|
+
}
|
|
71
|
+
get oid() {
|
|
72
|
+
return this._oid;
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Returns the `CW-Reference` header from the last request.
|
|
76
|
+
*/
|
|
77
|
+
get lastReferenceHeader() {
|
|
78
|
+
return this._lastReferenceHeader;
|
|
79
|
+
}
|
|
80
|
+
// Being extra safe with these bc a failure here fails the actual request
|
|
81
|
+
postRequest(response) {
|
|
82
|
+
this._lastReferenceHeader =
|
|
83
|
+
response && response.headers ? response.headers["cw-reference"] : undefined;
|
|
84
|
+
}
|
|
85
|
+
axiosSuccessfulResponse(_this) {
|
|
86
|
+
return (response) => {
|
|
87
|
+
_this && _this.postRequest(response);
|
|
88
|
+
return response;
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
axiosErrorResponse(_this) {
|
|
92
|
+
//eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
93
|
+
return (error) => {
|
|
94
|
+
_this && _this.postRequest(error.response);
|
|
95
|
+
throw error;
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
// TODO: #322 handle errors in API calls as per
|
|
99
|
+
// https://specification.commonwellalliance.org/services/rest-api-reference (8.6.1 Error)
|
|
100
|
+
// Note that also sometimes these calls 404 when things aren't found and etc
|
|
101
|
+
//--------------------------------------------------------------------------------------------
|
|
102
|
+
// Org Management
|
|
103
|
+
//--------------------------------------------------------------------------------------------
|
|
104
|
+
/**
|
|
105
|
+
* Create an org.
|
|
106
|
+
* See: https://commonwellalliance.sharepoint.com/sites/ServiceAdopter/SitePages/Organization-Management-API---Overview-and-Summary.aspx#post-a-new-organization
|
|
107
|
+
*
|
|
108
|
+
* @param meta Metadata about the request.
|
|
109
|
+
* @param organization The org to create.
|
|
110
|
+
* @returns
|
|
111
|
+
*/
|
|
112
|
+
async createOrg(meta, organization) {
|
|
113
|
+
const headers = await this.buildQueryHeaders(meta);
|
|
114
|
+
const resp = await this.api.post(`${CommonWell.MEMBER_ENDPOINT}/${this.oid}/org`, organization, {
|
|
115
|
+
headers,
|
|
116
|
+
});
|
|
117
|
+
return organization_1.organizationSchema.parse(resp.data);
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Update an org.
|
|
121
|
+
* See: https://commonwellalliance.sharepoint.com/sites/ServiceAdopter/SitePages/Organization-Management-API---Overview-and-Summary.aspx#put-new-information-into-an-organization
|
|
122
|
+
*
|
|
123
|
+
* @param meta Metadata about the request.
|
|
124
|
+
* @param organization The org to update.
|
|
125
|
+
* @returns
|
|
126
|
+
*/
|
|
127
|
+
async updateOrg(meta, organization, id) {
|
|
128
|
+
const headers = await this.buildQueryHeaders(meta);
|
|
129
|
+
const resp = await this.api.put(`${CommonWell.MEMBER_ENDPOINT}/${this.oid}/org/${id}/`, organization, {
|
|
130
|
+
headers,
|
|
131
|
+
});
|
|
132
|
+
return organization_1.organizationSchema.parse(resp.data);
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Get list of orgs.
|
|
136
|
+
* See: https://commonwellalliance.sharepoint.com/sites/ServiceAdopter/SitePages/Organization-Management-API---Overview-and-Summary.aspx#get-a-list-of-all-organizations
|
|
137
|
+
*
|
|
138
|
+
* @param meta Metadata about the request.
|
|
139
|
+
* @param summary Returns only summary data
|
|
140
|
+
* @param offset Sets an offset number from which recorded returns will begin
|
|
141
|
+
* @param limit Limits the number of returned records
|
|
142
|
+
* @param sort Specifies sort order
|
|
143
|
+
* @returns
|
|
144
|
+
*/
|
|
145
|
+
async getAllOrgs(meta, summary, offset, limit, sort) {
|
|
146
|
+
const headers = await this.buildQueryHeaders(meta);
|
|
147
|
+
const resp = await this.api.get(`${CommonWell.MEMBER_ENDPOINT}/${this.oid}/org`, {
|
|
148
|
+
headers,
|
|
149
|
+
params: { summary, offset, limit, sort },
|
|
150
|
+
});
|
|
151
|
+
return organization_1.organizationListSchema.parse(resp.data);
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* Get one org.
|
|
155
|
+
* See: https://commonwellalliance.sharepoint.com/sites/ServiceAdopter/SitePages/Organization-Management-API---Overview-and-Summary.aspx#get-a-single-organization
|
|
156
|
+
*
|
|
157
|
+
* @param meta Metadata about the request.
|
|
158
|
+
* @param id The org to be found
|
|
159
|
+
* @returns
|
|
160
|
+
*/
|
|
161
|
+
async getOneOrg(meta, id) {
|
|
162
|
+
const headers = await this.buildQueryHeaders(meta);
|
|
163
|
+
const resp = await this.api.get(`${CommonWell.MEMBER_ENDPOINT}/${this.oid}/org/${id}/`, {
|
|
164
|
+
headers,
|
|
165
|
+
validateStatus: null, // don't throw on status code > 299
|
|
166
|
+
});
|
|
167
|
+
const status = resp.status;
|
|
168
|
+
if (status === httpStatus.NOT_FOUND)
|
|
169
|
+
return undefined;
|
|
170
|
+
if (httpStatus[`${status}_CLASS`] === httpStatus.classes.SUCCESSFUL) {
|
|
171
|
+
return organization_1.organizationSchema.parse(resp.data);
|
|
172
|
+
}
|
|
173
|
+
throw new metriport_error_1.default(`Failed to retrieve Organization`, status);
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* Add certificate to org.
|
|
177
|
+
* See: https://commonwellalliance.sharepoint.com/sites/ServiceAdopter/SitePages/Organization-Management-API---Overview-and-Summary.aspx#post-new-certificates-to-organizations
|
|
178
|
+
*
|
|
179
|
+
* @param meta Metadata about the request.
|
|
180
|
+
* @param certificate The certificate to add to the org
|
|
181
|
+
* @param id The org to add a certificate too
|
|
182
|
+
* @returns
|
|
183
|
+
*/
|
|
184
|
+
async addCertificateToOrg(meta, certificate, id) {
|
|
185
|
+
const headers = await this.buildQueryHeaders(meta);
|
|
186
|
+
const resp = await this.api.post(`${CommonWell.MEMBER_ENDPOINT}/${this.oid}/org/${id}/certificate`, certificate, {
|
|
187
|
+
headers,
|
|
188
|
+
});
|
|
189
|
+
return certificates_1.certificateRespSchema.parse(resp.data);
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* Replace certificate for org.
|
|
193
|
+
* See: https://commonwellalliance.sharepoint.com/sites/ServiceAdopter/SitePages/Organization-Management-API---Overview-and-Summary.aspx#put-a-list-of-certificates-into-an-organization
|
|
194
|
+
*
|
|
195
|
+
* @param meta Metadata about the request.
|
|
196
|
+
* @param certificate The certificate to replace for the org
|
|
197
|
+
* @param id The org to replace a certificate for
|
|
198
|
+
* @returns
|
|
199
|
+
*/
|
|
200
|
+
async replaceCertificateForOrg(meta, certificate, id) {
|
|
201
|
+
const headers = await this.buildQueryHeaders(meta);
|
|
202
|
+
const resp = await this.api.put(`${CommonWell.MEMBER_ENDPOINT}/${this.oid}/org/${id}/certificate`, certificate, {
|
|
203
|
+
headers,
|
|
204
|
+
});
|
|
205
|
+
return certificates_1.certificateRespSchema.parse(resp.data);
|
|
206
|
+
}
|
|
207
|
+
/**
|
|
208
|
+
* Delete certificate from org.
|
|
209
|
+
* See: https://commonwellalliance.sharepoint.com/sites/ServiceAdopter/SitePages/Organization-Management-API---Overview-and-Summary.aspx#delete-certificates-by-thumbprint
|
|
210
|
+
*
|
|
211
|
+
* @param meta Metadata about the request.
|
|
212
|
+
* @param id The org to delete a certificate from
|
|
213
|
+
* @param thumbprint The thumbprint from the certificate
|
|
214
|
+
* @param purpose The purpose from the certificate
|
|
215
|
+
* @returns
|
|
216
|
+
*/
|
|
217
|
+
async deleteCertificateFromOrg(meta, id, thumbprint, purpose) {
|
|
218
|
+
const headers = await this.buildQueryHeaders(meta);
|
|
219
|
+
await this.api.delete(`${CommonWell.MEMBER_ENDPOINT}/${this.oid}/org/${id}/certificate/${thumbprint}/purpose/${purpose}`, {
|
|
220
|
+
headers,
|
|
221
|
+
});
|
|
222
|
+
return;
|
|
223
|
+
}
|
|
224
|
+
/**
|
|
225
|
+
* Get certificate from org.
|
|
226
|
+
* See: https://commonwellalliance.sharepoint.com/sites/ServiceAdopter/SitePages/Organization-Management-API---Overview-and-Summary.aspx#get-certificates-for-an-organization
|
|
227
|
+
*
|
|
228
|
+
* @param meta Metadata about the request.
|
|
229
|
+
* @param certificate The certificate to add to the org
|
|
230
|
+
* @param id The org to get a certificate from
|
|
231
|
+
* @param thumbprint The thumbprint from the certificate
|
|
232
|
+
* @param purpose The purpose from the certificate
|
|
233
|
+
* @returns
|
|
234
|
+
*/
|
|
235
|
+
async getCertificatesFromOrg(meta, id, thumbprint, purpose) {
|
|
236
|
+
const headers = await this.buildQueryHeaders(meta);
|
|
237
|
+
const resp = await this.api.get(`${CommonWell.MEMBER_ENDPOINT}/${this.oid}/org/${id}/certificate`, {
|
|
238
|
+
headers,
|
|
239
|
+
params: { thumbprint, purpose },
|
|
240
|
+
});
|
|
241
|
+
return certificates_1.certificateRespSchema.parse(resp.data);
|
|
242
|
+
}
|
|
243
|
+
/**
|
|
244
|
+
* Get certificate from org (by thumbprint).
|
|
245
|
+
* See: https://commonwellalliance.sharepoint.com/sites/ServiceAdopter/SitePages/Organization-Management-API---Overview-and-Summary.aspx#get-certificates-by-thumbprint
|
|
246
|
+
*
|
|
247
|
+
* @param meta Metadata about the request.
|
|
248
|
+
* @param certificate The certificate to add to the org
|
|
249
|
+
* @param id The org to get a certificate from
|
|
250
|
+
* @param thumbprint The thumbprint from the certificate
|
|
251
|
+
* @param purpose The purpose from the certificate
|
|
252
|
+
* @returns
|
|
253
|
+
*/
|
|
254
|
+
async getCertificatesFromOrgByThumbprint(meta, id, thumbprint, purpose) {
|
|
255
|
+
const headers = await this.buildQueryHeaders(meta);
|
|
256
|
+
const resp = await this.api.get(`${CommonWell.MEMBER_ENDPOINT}/${this.oid}/org/${id}/certificate/${thumbprint}`, {
|
|
257
|
+
headers,
|
|
258
|
+
params: { purpose },
|
|
259
|
+
});
|
|
260
|
+
return certificates_1.certificateRespSchema.parse(resp.data);
|
|
261
|
+
}
|
|
262
|
+
/**
|
|
263
|
+
* Get certificate from org (by thumbprint & purpose).
|
|
264
|
+
* See: https://commonwellalliance.sharepoint.com/sites/ServiceAdopter/SitePages/Organization-Management-API---Overview-and-Summary.aspx#get-certificates-by-thumbprint-and-purpose
|
|
265
|
+
*
|
|
266
|
+
* @param meta Metadata about the request.
|
|
267
|
+
* @param certificate The certificate to add to the org
|
|
268
|
+
* @param id The org to get a certificate from
|
|
269
|
+
* @param thumbprint The thumbprint from the certificate
|
|
270
|
+
* @param purpose The purpose from the certificate
|
|
271
|
+
* @returns
|
|
272
|
+
*/
|
|
273
|
+
async getCertificatesFromOrgByThumbprintAndPurpose(meta, id, thumbprint, purpose) {
|
|
274
|
+
const headers = await this.buildQueryHeaders(meta);
|
|
275
|
+
const resp = await this.api.get(`${CommonWell.MEMBER_ENDPOINT}/${this.oid}/org/${id}/certificate/${thumbprint}/purpose/${purpose}`, {
|
|
276
|
+
headers,
|
|
277
|
+
});
|
|
278
|
+
return certificates_1.certificateRespSchema.parse(resp.data);
|
|
279
|
+
}
|
|
280
|
+
//--------------------------------------------------------------------------------------------
|
|
281
|
+
// Person Management
|
|
282
|
+
//--------------------------------------------------------------------------------------------
|
|
283
|
+
/**
|
|
284
|
+
* Enrolls a new person.
|
|
285
|
+
* See: https://specification.commonwellalliance.org/services/patient-identity-and-linking/protocol-operations#8716-adding-a-new-person
|
|
286
|
+
*
|
|
287
|
+
* @param meta Metadata about the request.
|
|
288
|
+
* @param person The person to enroll.
|
|
289
|
+
* @returns
|
|
290
|
+
*/
|
|
291
|
+
async enrollPerson(meta, person) {
|
|
292
|
+
const headers = await this.buildQueryHeaders(meta);
|
|
293
|
+
const resp = await this.api.post(CommonWell.PERSON_ENDPOINT, person, {
|
|
294
|
+
headers,
|
|
295
|
+
});
|
|
296
|
+
return person_1.personSchema.parse(resp.data);
|
|
297
|
+
}
|
|
298
|
+
/**
|
|
299
|
+
* Searches for a person based on the specified strong ID.
|
|
300
|
+
* See: https://specification.commonwellalliance.org/services/patient-identity-and-linking/protocol-operations#8711-search-for-a-person
|
|
301
|
+
*
|
|
302
|
+
* @param meta Metadata about the request.
|
|
303
|
+
* @param key The ID's value, for example the driver's license ID.
|
|
304
|
+
* @param system The ID's uri to specify type, for example "urn:oid:2.16.840.1.113883.4.3.6" is a CA DL.
|
|
305
|
+
* @returns
|
|
306
|
+
*/
|
|
307
|
+
async searchPerson(meta, key, system) {
|
|
308
|
+
const headers = await this.buildQueryHeaders(meta);
|
|
309
|
+
const resp = await this.api.get(CommonWell.PERSON_ENDPOINT, {
|
|
310
|
+
headers,
|
|
311
|
+
params: { key, system },
|
|
312
|
+
});
|
|
313
|
+
return person_1.personSearchRespSchema.parse(resp.data);
|
|
314
|
+
}
|
|
315
|
+
/**
|
|
316
|
+
* Searches for a person based on patient demo.
|
|
317
|
+
* See: https://specification.commonwellalliance.org/services/patient-identity-and-linking/protocol-operations#8713-find-persons-matching-patient-demographics
|
|
318
|
+
*
|
|
319
|
+
* @param meta Metadata about the request.
|
|
320
|
+
* @param patientId The patient ID.
|
|
321
|
+
* @returns
|
|
322
|
+
*/
|
|
323
|
+
async searchPersonByPatientDemo(meta, patientId) {
|
|
324
|
+
const headers = await this.buildQueryHeaders(meta);
|
|
325
|
+
const resp = await this.api.get(`${CommonWell.ORG_ENDPOINT}/${this.oid}/patient/${patientId}/person`, {
|
|
326
|
+
headers,
|
|
327
|
+
});
|
|
328
|
+
return person_1.personSearchRespSchema.parse(resp.data);
|
|
329
|
+
}
|
|
330
|
+
/**
|
|
331
|
+
* Gets a person based on person id.
|
|
332
|
+
* See: https://specification.commonwellalliance.org/services/patient-identity-and-linking/protocol-operations#8713-find-persons-matching-patient-demographics
|
|
333
|
+
*
|
|
334
|
+
* @param meta Metadata about the request.
|
|
335
|
+
* @param personId The person ID.
|
|
336
|
+
* @returns
|
|
337
|
+
*/
|
|
338
|
+
async getPersonById(meta, personId) {
|
|
339
|
+
const headers = await this.buildQueryHeaders(meta);
|
|
340
|
+
const resp = await this.api.get(`${CommonWell.PERSON_ENDPOINT}/${personId}`, {
|
|
341
|
+
headers,
|
|
342
|
+
});
|
|
343
|
+
return person_1.personSchema.parse(resp.data);
|
|
344
|
+
}
|
|
345
|
+
/**
|
|
346
|
+
* Updates a person.
|
|
347
|
+
* See: https://specification.commonwellalliance.org/services/patient-identity-and-linking/protocol-operations#8741-updating-person-information
|
|
348
|
+
*
|
|
349
|
+
* @param meta Metadata about the request.
|
|
350
|
+
* @param person The data to update.
|
|
351
|
+
* @param id The person to be updated.
|
|
352
|
+
* @returns
|
|
353
|
+
*/
|
|
354
|
+
async updatePerson(meta, person, id) {
|
|
355
|
+
const headers = await this.buildQueryHeaders(meta);
|
|
356
|
+
const resp = await this.api.post(`${CommonWell.PERSON_ENDPOINT}/${id}`, person, {
|
|
357
|
+
headers,
|
|
358
|
+
});
|
|
359
|
+
return person_1.personSchema.parse(resp.data);
|
|
360
|
+
}
|
|
361
|
+
/**
|
|
362
|
+
* Matches a person to a patient.
|
|
363
|
+
* See: https://specification.commonwellalliance.org/services/patient-identity-and-linking/protocol-operations#8732-retrieve-patient-matches
|
|
364
|
+
*
|
|
365
|
+
* @param meta Metadata about the request.
|
|
366
|
+
* @param id The person to be matched.
|
|
367
|
+
* @returns
|
|
368
|
+
*/
|
|
369
|
+
async patientMatch(meta, id) {
|
|
370
|
+
const headers = await this.buildQueryHeaders(meta);
|
|
371
|
+
const resp = await this.api.get(`${CommonWell.PERSON_ENDPOINT}/${id}/patientMatch`, {
|
|
372
|
+
headers,
|
|
373
|
+
params: { orgId: this.oid },
|
|
374
|
+
});
|
|
375
|
+
return patient_1.patientSearchRespSchema.parse(resp.data);
|
|
376
|
+
}
|
|
377
|
+
/**
|
|
378
|
+
* @deprecated use addPatientLink() instead
|
|
379
|
+
*/
|
|
380
|
+
async patientLink(meta, personId, patientUri, patientStrongId) {
|
|
381
|
+
return this.addPatientLink(meta, personId, patientUri, patientStrongId);
|
|
382
|
+
}
|
|
383
|
+
/**
|
|
384
|
+
* Add patient link to person.
|
|
385
|
+
* See: https://specification.commonwellalliance.org/services/patient-identity-and-linking/protocol-operations#8721
|
|
386
|
+
*
|
|
387
|
+
* @param meta Metadata about the request.
|
|
388
|
+
* @param personId The person id to be link to a patient.
|
|
389
|
+
* @param patientUri The patient uri to be link to a person.
|
|
390
|
+
* @param [patientStrongId] The patient's strong ID, if available (optional).
|
|
391
|
+
* @returns {PatientLink}
|
|
392
|
+
*/
|
|
393
|
+
async addPatientLink(meta, personId, patientUri, patientStrongId) {
|
|
394
|
+
const headers = await this.buildQueryHeaders(meta);
|
|
395
|
+
const resp = await this.api.post(`${CommonWell.PERSON_ENDPOINT}/${personId}/patientLink`, {
|
|
396
|
+
patient: patientUri,
|
|
397
|
+
...(patientStrongId ? { identifier: patientStrongId } : undefined),
|
|
398
|
+
}, { headers });
|
|
399
|
+
return person_1.patientLinkSchema.parse(resp.data);
|
|
400
|
+
}
|
|
401
|
+
/**
|
|
402
|
+
* Re-enrolls a person.
|
|
403
|
+
* See: https://specification.commonwellalliance.org/services/patient-identity-and-linking/protocol-operations#875-person-unenrollment
|
|
404
|
+
*
|
|
405
|
+
* @param meta Metadata about the request.
|
|
406
|
+
* @param id The person to be re-enrolled.
|
|
407
|
+
* @returns Person with enrollment information
|
|
408
|
+
*/
|
|
409
|
+
async reenrollPerson(meta, id) {
|
|
410
|
+
const headers = await this.buildQueryHeaders(meta);
|
|
411
|
+
const resp = await this.api.put(`${CommonWell.PERSON_ENDPOINT}/${id}/enroll`, {}, { headers });
|
|
412
|
+
return person_1.personSchema.parse(resp.data);
|
|
413
|
+
}
|
|
414
|
+
/**
|
|
415
|
+
* Unenrolls a person.
|
|
416
|
+
* See: https://specification.commonwellalliance.org/services/patient-identity-and-linking/protocol-operations#875-person-unenrollment
|
|
417
|
+
*
|
|
418
|
+
* @param meta Metadata about the request.
|
|
419
|
+
* @param id The person to be unenrolled.
|
|
420
|
+
* @returns
|
|
421
|
+
*/
|
|
422
|
+
async unenrollPerson(meta, id) {
|
|
423
|
+
const headers = await this.buildQueryHeaders(meta);
|
|
424
|
+
const resp = await this.api.put(`${CommonWell.PERSON_ENDPOINT}/${id}/unenroll`, {}, {
|
|
425
|
+
headers,
|
|
426
|
+
});
|
|
427
|
+
return person_1.personSchema.parse(resp.data);
|
|
428
|
+
}
|
|
429
|
+
/**
|
|
430
|
+
* Deletes a person.
|
|
431
|
+
* See: https://specification.commonwellalliance.org/services/patient-identity-and-linking/protocol-operations#8742-deleting-a-person
|
|
432
|
+
*
|
|
433
|
+
* @param meta Metadata about the request.
|
|
434
|
+
* @param id The person to be deleted.
|
|
435
|
+
* @returns
|
|
436
|
+
*/
|
|
437
|
+
async deletePerson(meta, id) {
|
|
438
|
+
const headers = await this.buildQueryHeaders(meta);
|
|
439
|
+
await this.api.delete(`${CommonWell.PERSON_ENDPOINT}/${id}`, { headers });
|
|
440
|
+
return;
|
|
441
|
+
}
|
|
442
|
+
//--------------------------------------------------------------------------------------------
|
|
443
|
+
// Patient Management
|
|
444
|
+
//--------------------------------------------------------------------------------------------
|
|
445
|
+
/**
|
|
446
|
+
* Register a new patient.
|
|
447
|
+
* See: https://specification.commonwellalliance.org/services/patient-identity-and-linking/protocol-operations#8762-adding-a-local-patient-record
|
|
448
|
+
*
|
|
449
|
+
* @param meta Metadata about the request.
|
|
450
|
+
* @param patient The patient to register.
|
|
451
|
+
* @returns
|
|
452
|
+
*/
|
|
453
|
+
async registerPatient(meta, patient) {
|
|
454
|
+
const headers = await this.buildQueryHeaders(meta);
|
|
455
|
+
const resp = await this.api.post(`${CommonWell.ORG_ENDPOINT}/${this.oid}/patient`, patient, {
|
|
456
|
+
headers,
|
|
457
|
+
});
|
|
458
|
+
return patient_1.patientSchema.parse(resp.data);
|
|
459
|
+
}
|
|
460
|
+
/**
|
|
461
|
+
* Returns a patient based on its ID.
|
|
462
|
+
*
|
|
463
|
+
* @param meta Metadata about the request.
|
|
464
|
+
* @param id Patient's ID.
|
|
465
|
+
* @returns {Promise<Patient>}
|
|
466
|
+
*/
|
|
467
|
+
async getPatient(meta, id) {
|
|
468
|
+
const headers = await this.buildQueryHeaders(meta);
|
|
469
|
+
const suffix = id.endsWith("/") ? "" : "/";
|
|
470
|
+
const resp = await this.api.get(`${CommonWell.ORG_ENDPOINT}/${this.oid}/patient/${id}${suffix}`, { headers });
|
|
471
|
+
return patient_1.patientSchema.parse(resp.data);
|
|
472
|
+
}
|
|
473
|
+
/**
|
|
474
|
+
* Searches for a patient based on params.
|
|
475
|
+
* See: https://specification.commonwellalliance.org/services/patient-identity-and-linking/protocol-operations#8761-search-for-a-patient
|
|
476
|
+
*
|
|
477
|
+
* @param meta Metadata about the request.
|
|
478
|
+
* @param fname Patient's first name.
|
|
479
|
+
* @param lname Patient's last name.
|
|
480
|
+
* @param dob Patient's date of birth.
|
|
481
|
+
* @param gender Patient's gender.
|
|
482
|
+
* @param zip Patient's zip code.
|
|
483
|
+
* @returns
|
|
484
|
+
*/
|
|
485
|
+
async searchPatient(meta, fname, lname, dob, gender, zip) {
|
|
486
|
+
const headers = await this.buildQueryHeaders(meta);
|
|
487
|
+
const resp = await this.api.get(`${CommonWell.ORG_ENDPOINT}/${this.oid}/patient`, {
|
|
488
|
+
headers,
|
|
489
|
+
params: { fname, lname, dob, gender, zip },
|
|
490
|
+
});
|
|
491
|
+
return patient_1.patientSearchRespSchema.parse(resp.data);
|
|
492
|
+
}
|
|
493
|
+
/**
|
|
494
|
+
* Updates a patient.
|
|
495
|
+
* See: https://specification.commonwellalliance.org/services/patient-identity-and-linking/protocol-operations#8763-updating-a-local-patient-record
|
|
496
|
+
*
|
|
497
|
+
* @param meta Metadata about the request.
|
|
498
|
+
* @param patient The data to update.
|
|
499
|
+
* @param id The patient to be updated.
|
|
500
|
+
* @returns
|
|
501
|
+
*/
|
|
502
|
+
async updatePatient(meta, patient, id) {
|
|
503
|
+
const headers = await this.buildQueryHeaders(meta);
|
|
504
|
+
const resp = await this.api.post(`${CommonWell.ORG_ENDPOINT}/${this.oid}/patient/${id}/`, patient, {
|
|
505
|
+
headers,
|
|
506
|
+
});
|
|
507
|
+
return patient_1.patientSchema.parse(resp.data);
|
|
508
|
+
}
|
|
509
|
+
/**
|
|
510
|
+
* Merges patients.
|
|
511
|
+
* See: https://specification.commonwellalliance.org/services/patient-identity-and-linking/protocol-operations#8765-merging-local-patient-records
|
|
512
|
+
*
|
|
513
|
+
* @param meta Metadata about the request.
|
|
514
|
+
* @param nonSurvivingPatientId The local Patient Identifier of the non-surviving Patient Record (This patient gets replaced)
|
|
515
|
+
* @param referencePatientLink The patient link for the patient that will replace the non surviving patient
|
|
516
|
+
* @returns
|
|
517
|
+
*/
|
|
518
|
+
async mergePatients(meta, nonSurvivingPatientId, referencePatientLink) {
|
|
519
|
+
const headers = await this.buildQueryHeaders(meta);
|
|
520
|
+
await this.api.put(`${CommonWell.ORG_ENDPOINT}/${this.oid}/patient/${nonSurvivingPatientId}/merge`, {
|
|
521
|
+
link: {
|
|
522
|
+
other: {
|
|
523
|
+
reference: referencePatientLink,
|
|
524
|
+
},
|
|
525
|
+
type: "replace",
|
|
526
|
+
},
|
|
527
|
+
}, {
|
|
528
|
+
headers,
|
|
529
|
+
});
|
|
530
|
+
return;
|
|
531
|
+
}
|
|
532
|
+
/**
|
|
533
|
+
* Get Patient's Network Links.
|
|
534
|
+
* See: https://specification.commonwellalliance.org/services/record-locator-service/protocol-operations-record-locator-service#8771-retrieving-network-links
|
|
535
|
+
*
|
|
536
|
+
* @param meta Metadata about the request.
|
|
537
|
+
* @param patientId Patient for which to get the network links.
|
|
538
|
+
* @returns
|
|
539
|
+
*/
|
|
540
|
+
async getNetworkLinks(meta, patientId) {
|
|
541
|
+
const headers = await this.buildQueryHeaders(meta);
|
|
542
|
+
// Error handling: https://github.com/metriport/metriport-internal/issues/322
|
|
543
|
+
try {
|
|
544
|
+
const resp = await this.api.get(`${CommonWell.ORG_ENDPOINT}/${this.oid}/patient/${patientId}/networkLink`, {
|
|
545
|
+
headers,
|
|
546
|
+
});
|
|
547
|
+
return patient_1.patientNetworkLinkRespSchema.parse(resp.data);
|
|
548
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
549
|
+
}
|
|
550
|
+
catch (err) {
|
|
551
|
+
// when there's no NetworkLink, CW's API return 412
|
|
552
|
+
if (err.response?.status === 412)
|
|
553
|
+
return { _embedded: { networkLink: [] } };
|
|
554
|
+
throw err;
|
|
555
|
+
}
|
|
556
|
+
}
|
|
557
|
+
/**
|
|
558
|
+
* Deletes a patient.
|
|
559
|
+
* See: https://specification.commonwellalliance.org/services/patient-identity-and-linking/protocol-operations#8764-deleting-a-local-patient-record
|
|
560
|
+
*
|
|
561
|
+
* @param meta Metadata about the request.
|
|
562
|
+
* @param id The patient to be updated.
|
|
563
|
+
* @returns
|
|
564
|
+
*/
|
|
565
|
+
async deletePatient(meta, id) {
|
|
566
|
+
const headers = await this.buildQueryHeaders(meta);
|
|
567
|
+
await this.api.delete(`${CommonWell.ORG_ENDPOINT}/${this.oid}/patient/${id}/`, {
|
|
568
|
+
headers,
|
|
569
|
+
});
|
|
570
|
+
}
|
|
571
|
+
//--------------------------------------------------------------------------------------------
|
|
572
|
+
// Document Management
|
|
573
|
+
//--------------------------------------------------------------------------------------------
|
|
574
|
+
/**
|
|
575
|
+
* Queries a patient's Documents.
|
|
576
|
+
*
|
|
577
|
+
* @param meta Metadata about the request.
|
|
578
|
+
* @param patientId The patient's ID.
|
|
579
|
+
* @returns {Promise<DocumentQueryResponse>}
|
|
580
|
+
* @see {@link https://specification.commonwellalliance.org/services/data-broker/cha-broker-api-reference#104-document-query|Use case}
|
|
581
|
+
* @see {@link https://specification.commonwellalliance.org/services/data-broker/protocol-operations-data-broker#8781-find-documents|API spec}
|
|
582
|
+
*/
|
|
583
|
+
async queryDocuments(meta, patientId) {
|
|
584
|
+
const headers = await this.buildQueryHeaders(meta);
|
|
585
|
+
return document.query(this.api, headers, patientId);
|
|
586
|
+
}
|
|
587
|
+
/**
|
|
588
|
+
* Queries a patient's Documents - including other possible results.
|
|
589
|
+
*
|
|
590
|
+
* @param meta Metadata about the request.
|
|
591
|
+
* @param patientId The patient's ID.
|
|
592
|
+
* @returns The DocumentReferences of a patient's available documents and/or OperationOutcomes denoting problems with the query.
|
|
593
|
+
* @see {@link https://specification.commonwellalliance.org/services/data-broker/cha-broker-api-reference#104-document-query|Use case}
|
|
594
|
+
* @see {@link https://specification.commonwellalliance.org/services/data-broker/protocol-operations-data-broker#8781-find-documents|API spec}
|
|
595
|
+
*/
|
|
596
|
+
async queryDocumentsFull(meta, patientId) {
|
|
597
|
+
const headers = await this.buildQueryHeaders(meta);
|
|
598
|
+
return document.queryFull(this.api, headers, patientId);
|
|
599
|
+
}
|
|
600
|
+
/**
|
|
601
|
+
* Retrieve a Document and pipe its bytes into the outputStream.
|
|
602
|
+
*
|
|
603
|
+
* @param {string} inputUrl - The URL of the file to be downloaded.
|
|
604
|
+
* @param {fs.WriteStream} outputStream - The stream to receive the downloaded file's bytes.
|
|
605
|
+
* @returns {Promise<void>}
|
|
606
|
+
* @see {@link https://specification.commonwellalliance.org/services/data-broker/cha-broker-api-reference#106-document-retrieval|Use case}
|
|
607
|
+
* @see {@link https://specification.commonwellalliance.org/services/data-broker/protocol-operations-data-broker#8782-retrieve-document|API spec}
|
|
608
|
+
*/
|
|
609
|
+
async retrieveDocument(meta, inputUrl, outputStream) {
|
|
610
|
+
const headers = await this.buildQueryHeaders(meta);
|
|
611
|
+
return document.retrieve(this.api, headers, inputUrl, outputStream);
|
|
612
|
+
}
|
|
613
|
+
//--------------------------------------------------------------------------------------------
|
|
614
|
+
// Link Management
|
|
615
|
+
//--------------------------------------------------------------------------------------------
|
|
616
|
+
/**
|
|
617
|
+
* Upgrade or downgrade network link.
|
|
618
|
+
* See: https://specification.commonwellalliance.org/services/record-locator-service/protocol-operations-record-locator-service#8772-upgrading-a-network-link
|
|
619
|
+
*
|
|
620
|
+
* @param meta Metadata about the request.
|
|
621
|
+
* @param href The href of network link to be upgraded or downgraded
|
|
622
|
+
* @param proxy The proxy for the patient link action.
|
|
623
|
+
* @returns
|
|
624
|
+
*/
|
|
625
|
+
async upgradeOrDowngradeNetworkLink(meta, href, proxy) {
|
|
626
|
+
const headers = await this.buildQueryHeaders(meta);
|
|
627
|
+
const resp = await this.api.post(href, {
|
|
628
|
+
proxy,
|
|
629
|
+
}, {
|
|
630
|
+
headers,
|
|
631
|
+
});
|
|
632
|
+
return link_1.networkLinkSchema.parse(resp.data);
|
|
633
|
+
}
|
|
634
|
+
/**
|
|
635
|
+
* Update a patient link.
|
|
636
|
+
* See: https://specification.commonwellalliance.org/services/patient-identity-and-linking/protocol-operations#8722-updating-a-patient-link
|
|
637
|
+
*
|
|
638
|
+
* @param meta Metadata about the request.
|
|
639
|
+
* @param patientLinkUri The uri of patient link to be updated
|
|
640
|
+
* @param patientUri The uri of patient that belongs to this link
|
|
641
|
+
* @param identifier Add identifier information to the patient link
|
|
642
|
+
* @returns
|
|
643
|
+
*/
|
|
644
|
+
async updatePatientLink(meta, patientLinkUri, patientUri, identifier) {
|
|
645
|
+
const headers = await this.buildQueryHeaders(meta);
|
|
646
|
+
const resp = await this.api.post(patientLinkUri, {
|
|
647
|
+
patient: patientUri,
|
|
648
|
+
identifier: identifier,
|
|
649
|
+
}, {
|
|
650
|
+
headers,
|
|
651
|
+
});
|
|
652
|
+
return person_1.patientLinkSchema.parse(resp.data);
|
|
653
|
+
}
|
|
654
|
+
/**
|
|
655
|
+
* Get a person's links to patients.
|
|
656
|
+
* See: https://specification.commonwellalliance.org/services/patient-identity-and-linking/protocol-operations#8721
|
|
657
|
+
*
|
|
658
|
+
* @param meta Metadata about the request.
|
|
659
|
+
* @param personId The person id to be link to a patient.
|
|
660
|
+
* @param [limitToOrg=true] Whether to limit the search to the current organization (optional).
|
|
661
|
+
* @returns Response with list of links to Patients
|
|
662
|
+
*/
|
|
663
|
+
async getPatientLinks(meta, personId, limitToOrg = true) {
|
|
664
|
+
const headers = await this.buildQueryHeaders(meta);
|
|
665
|
+
const resp = await this.api.get(`${CommonWell.PERSON_ENDPOINT}/${personId}/patientLink`, {
|
|
666
|
+
headers,
|
|
667
|
+
params: {
|
|
668
|
+
...(limitToOrg ? { orgId: this.oid } : undefined),
|
|
669
|
+
},
|
|
670
|
+
});
|
|
671
|
+
return person_1.patientLinkSearchRespSchema.parse(resp.data);
|
|
672
|
+
}
|
|
673
|
+
/**
|
|
674
|
+
* Gets a patient link.
|
|
675
|
+
* See: https://specification.commonwellalliance.org/services/patient-identity-and-linking/protocol-operations#8723-getting-a-patient-link
|
|
676
|
+
*
|
|
677
|
+
* @param meta Metadata about the request.
|
|
678
|
+
* @param personId Person that is linked
|
|
679
|
+
* @param patientId Patient that is linked
|
|
680
|
+
* @returns
|
|
681
|
+
*/
|
|
682
|
+
async getPatientLink(meta, personId, patientId) {
|
|
683
|
+
const headers = await this.buildQueryHeaders(meta);
|
|
684
|
+
const resp = await this.api.get(`/v1/person/${personId}/patientLink/${patientId}/`, {
|
|
685
|
+
headers,
|
|
686
|
+
});
|
|
687
|
+
return patient_1.patientLinkRespSchema.parse(resp.data);
|
|
688
|
+
}
|
|
689
|
+
/**
|
|
690
|
+
* Deletes a patient link - the link will be moved to LOLA 0 and cannot be used again.
|
|
691
|
+
* See: https://specification.commonwellalliance.org/services/patient-identity-and-linking/protocol-operations#8724-deleting-a-patient-link
|
|
692
|
+
*
|
|
693
|
+
* WARNING: This shouldn't be used except under the explicit request of a person.
|
|
694
|
+
*
|
|
695
|
+
* @param meta Metadata about the request.
|
|
696
|
+
* @param patientLinkUri The uri of patient link to be deleted
|
|
697
|
+
* @returns
|
|
698
|
+
*/
|
|
699
|
+
async deletePatientLink(meta, patientLinkUri) {
|
|
700
|
+
const headers = await this.buildQueryHeaders(meta);
|
|
701
|
+
await this.api.delete(patientLinkUri, {
|
|
702
|
+
headers,
|
|
703
|
+
});
|
|
704
|
+
return;
|
|
705
|
+
}
|
|
706
|
+
/**
|
|
707
|
+
* Resets a patient link - the link will be moved to LOLA 1 and can be relinked later.
|
|
708
|
+
* See: https://specification.commonwellalliance.org/services/patient-identity-and-linking/protocol-operations#8725-resetting-a-patient-link
|
|
709
|
+
*
|
|
710
|
+
* @param meta Metadata about the request.
|
|
711
|
+
* @param personId Person that is linked
|
|
712
|
+
* @param patientId Patient that is linked
|
|
713
|
+
* @returns
|
|
714
|
+
*/
|
|
715
|
+
async resetPatientLink(meta, personId, patientId) {
|
|
716
|
+
const headers = await this.buildQueryHeaders(meta);
|
|
717
|
+
await this.api.put(`/v1/person/${personId}/patientLink/${patientId}/reset`, {}, {
|
|
718
|
+
headers,
|
|
719
|
+
});
|
|
720
|
+
return;
|
|
721
|
+
}
|
|
722
|
+
//--------------------------------------------------------------------------------------------
|
|
723
|
+
// Private Methods
|
|
724
|
+
//--------------------------------------------------------------------------------------------
|
|
725
|
+
async buildQueryHeaders(meta) {
|
|
726
|
+
const jwt = await (0, make_jwt_1.makeJwt)(this.rsaPrivateKey, meta.role, meta.subjectId, this.orgName, this.oid, meta.purposeOfUse, meta.npi, meta.payloadHash);
|
|
727
|
+
return { Authorization: `Bearer ${jwt}` };
|
|
728
|
+
}
|
|
729
|
+
}
|
|
730
|
+
exports.CommonWell = CommonWell;
|
|
731
|
+
CommonWell.integrationUrl = "https://integration.rest.api.commonwellalliance.org";
|
|
732
|
+
CommonWell.productionUrl = "https://rest.api.commonwellalliance.org";
|
|
733
|
+
// V1
|
|
734
|
+
CommonWell.PERSON_ENDPOINT = "/v1/person";
|
|
735
|
+
CommonWell.ORG_ENDPOINT = "/v1/org";
|
|
736
|
+
CommonWell.PATIENT_ENDPOINT = "/v1/patient";
|
|
737
|
+
CommonWell.MEMBER_ENDPOINT = "/v1/member";
|
|
738
|
+
// V2
|
|
739
|
+
CommonWell.DOCUMENT_QUERY_ENDPOINT = "/v2/documentReference";
|
|
740
740
|
//# sourceMappingURL=commonwell.js.map
|