Skip to content

Commit

Permalink
Merge branch 'develop' into 1040-alb-behind-api-nlb_1
Browse files Browse the repository at this point in the history
  • Loading branch information
leite08 committed Jun 19, 2024
2 parents 622cfd8 + 056427f commit 9681781
Show file tree
Hide file tree
Showing 52 changed files with 746 additions and 628 deletions.
47 changes: 24 additions & 23 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions packages/api-sdk/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@metriport/api-sdk",
"version": "9.0.1",
"version": "9.0.2-alpha.0",
"description": "Metriport helps you access and manage health and medical data, through a single open source API.",
"author": "Metriport Inc. <[email protected]>",
"homepage": "https://metriport.com/",
Expand Down Expand Up @@ -57,9 +57,9 @@
},
"dependencies": {
"@medplum/fhirtypes": "^2.0.32",
"@metriport/commonwell-sdk": "^4.15.14",
"@metriport/shared": "^0.9.7",
"axios": "^1.3.4",
"@metriport/commonwell-sdk": "^4.15.15-alpha.0",
"@metriport/shared": "^0.9.8-alpha.0",
"axios": "^1.4.0",
"dayjs": "^1.11.7",
"dotenv": "^16.3.1",
"zod": "^3.22.1"
Expand Down
2 changes: 1 addition & 1 deletion packages/api/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "api",
"version": "1.18.4",
"version": "1.18.5-alpha.0",
"description": "",
"main": "app.js",
"private": true,
Expand Down
22 changes: 15 additions & 7 deletions packages/api/src/external/commonwell/patient-external-data.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Patient } from "@metriport/core/domain/patient";
import { DiscoveryParams } from "@metriport/core/domain/patient-discovery";
import { MetriportError } from "@metriport/core/util/error/metriport-error";
import { executeWithRetriesSafe } from "@metriport/shared";
import { executeWithRetriesSafe, MetriportError } from "@metriport/shared";
import dayjs from "dayjs";
import duration from "dayjs/plugin/duration";
import { getPatientOrFail } from "../../command/medical/patient/get-patient";
Expand All @@ -14,7 +13,7 @@ import { CQLinkStatus, PatientDataCommonwell } from "./patient-shared";
dayjs.extend(duration);

const maxAttemptsToGetPatientCWData = 5;
const waitTimeBetweenAttemptsToGetPatientCWData = dayjs.duration(2, "seconds");
const waitTimeBetweenAttemptsToGetPatientCWData = dayjs.duration(1, "seconds");

export type PatientWithCWData = Patient & {
data: { externalData: { COMMONWELL: PatientDataCommonwell } };
Expand All @@ -23,8 +22,8 @@ export type PatientWithCWData = Patient & {
const _getPatientWithCWData = async ({
id,
cxId,
}: Pick<Patient, "id" | "cxId">): Promise<PatientWithCWData | undefined> => {
const patientDB: Patient = await getPatientOrFail({
}: Pick<Patient, "id" | "cxId">): Promise<PatientWithCWData> => {
const patientDB = await getPatientOrFail({
id,
cxId,
});
Expand All @@ -33,7 +32,17 @@ const _getPatientWithCWData = async ({
if (!cwData) throw new MetriportError(`Missing CW data on patient`);
if (!cwData.patientId) throw new MetriportError(`Missing CW patientId`);

return patientDB as PatientWithCWData;
const patient = patientDB.dataValues;
return {
...patient,
data: {
...patient.data,
externalData: {
...patient.data.externalData,
COMMONWELL: cwData,
},
},
};
};

export async function getPatientWithCWData(
Expand All @@ -42,7 +51,6 @@ export async function getPatientWithCWData(
return executeWithRetriesSafe(() => _getPatientWithCWData(patient), {
maxAttempts: maxAttemptsToGetPatientCWData,
initialDelay: waitTimeBetweenAttemptsToGetPatientCWData.asMilliseconds(),
backoffMultiplier: 0, // no backoff
});
}

Expand Down
70 changes: 8 additions & 62 deletions packages/api/src/external/fhir/document/get-documents.ts
Original file line number Diff line number Diff line change
@@ -1,65 +1,11 @@
import { DocumentReference } from "@medplum/fhirtypes";
import { chunk } from "lodash";
import { capture } from "../../../shared/notifications";
import { makeFhirApi } from "../api/api-factory";
import {
isoDateToFHIRDateQueryFrom,
isoDateToFHIRDateQueryTo,
} from "@metriport/core/external/fhir/shared/index";
import { getDocuments } from "@metriport/core/external/fhir/document/get-documents";

export async function getDocumentsFromFHIR({
cxId,
patientId,
from,
to,
documentIds,
}: {
cxId: string;
patientId?: string | string[];
from?: string;
to?: string;
documentIds?: string[];
}): Promise<DocumentReference[]> {
try {
const api = makeFhirApi(cxId);
const docs: DocumentReference[] = [];
const chunksDocIds = documentIds && documentIds.length > 0 ? chunk(documentIds, 150) : [[]];

for (const docIds of chunksDocIds) {
const filtersAsStr = getFilters({ patientId, documentIds: docIds, from, to });
for await (const page of api.searchResourcePages("DocumentReference", filtersAsStr)) {
docs.push(...page);
}
}
return docs;
} catch (error) {
const msg = `Error getting documents from FHIR server`;
console.log(`${msg} - patientId: ${patientId}, error: ${error}`);
capture.message(msg, { extra: { patientId, error }, level: "error" });
throw error;
}
}

export function getFilters({
patientId: patientIdParam,
documentIds = [],
from,
to,
}: {
patientId?: string | string[];
documentIds?: string[];
from?: string;
to?: string;
} = {}) {
const filters = new URLSearchParams();
const patientIds = Array.isArray(patientIdParam) ? patientIdParam : [patientIdParam];
const patientIdsFiltered = patientIds.flatMap(id =>
id && id.trim().length > 0 ? id.trim() : []
);
patientIdsFiltered.length && filters.append("patient", patientIdsFiltered.join(","));
documentIds.length && filters.append(`_id`, documentIds.join(","));
from && filters.append("date", isoDateToFHIRDateQueryFrom(from));
to && filters.append("date", isoDateToFHIRDateQueryTo(to));
const filtersAsStr = filters.toString();
return filtersAsStr;
/**
* @deprecated Use `getDocuments()` from `@metriport/core/external/fhir/document/get-documents` instead.
*/
export async function getDocumentsFromFHIR(
...params: Parameters<typeof getDocuments>
): Promise<DocumentReference[]> {
return getDocuments(...params);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Bundle, DocumentReference } from "@medplum/fhirtypes";
import { out } from "@metriport/core/util/log";
import { executeWithRetries } from "@metriport/shared";
import { executeWithNetworkRetries } from "@metriport/shared";
import { errorToString } from "../../../shared/log";
import { makeFhirApi } from "../api/api-factory";

Expand All @@ -14,10 +14,9 @@ export const upsertDocumentToFHIRServer = async (
): Promise<void> => {
const fhir = makeFhirApi(cxId);
try {
await executeWithRetries(async () => await fhir.updateResource(docRef), {
await executeWithNetworkRetries(async () => await fhir.updateResource(docRef), {
maxAttempts,
initialDelay: waitTimeBetweenAttemptsInMillis,
backoffMultiplier: 0, // no backoff
log,
});
} catch (err) {
Expand All @@ -33,10 +32,9 @@ export const upsertDocumentsToFHIRServer = async (
): Promise<void> => {
const fhir = makeFhirApi(cxId);
try {
await executeWithRetries(async () => await fhir.executeBatch(transactionBundle), {
await executeWithNetworkRetries(async () => await fhir.executeBatch(transactionBundle), {
maxAttempts,
initialDelay: waitTimeBetweenAttemptsInMillis,
backoffMultiplier: 0, // no backoff
log,
});
} catch (error) {
Expand Down
Loading

0 comments on commit 9681781

Please sign in to comment.