8000 ENG-191 Only load the bundle when converting by leite08 · Pull Request #3692 · metriport/metriport · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

ENG-191 Only load the bundle when converting #3692

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 5 additions & 26 deletions packages/api/src/command/medical/patient/consolidated-get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import { analytics, EventTypes } from "@metriport/core/external/analytics/postho
import { out } from "@metriport/core/util";
import { uuidv7 } from "@metriport/core/util/uuid-v7";
import { emptyFunction } from "@metriport/shared";
import { elapsedTimeFromNow } from "@metriport/shared/common/date";
import { SearchSetBundle } from "@metriport/shared/medical";
import dayjs from "dayjs";
import duration from "dayjs/plugin/duration";
Expand Down Expand Up @@ -52,6 +51,7 @@ type GetConsolidatedPatientData = {
resources?: ResourceTypeForConsolidation[];
dateFrom?: string;
dateTo?: string;
requestId?: string;
fromDashboard?: boolean;
// TODO 2215 Remove this when we have contributed data as part of get consolidated (from S3)
forceDataFromFhir?: boolean;
Expand Down Expand Up @@ -261,6 +261,7 @@ export async function getConsolidated({
if (!bundle) {
bundle = await getConsolidatedPatientData({
patient,
requestId,
resources,
dateFrom,
dateTo,
Expand All @@ -270,43 +271,19 @@ export async function getConsolidated({
bundle.total = bundle.entry?.length ?? 0;
const hasResources = bundle.entry && bundle.entry.length > 0;
const shouldCreateMedicalRecord = conversionType && conversionType != "json" && hasResources;
const currentConsolidatedProgress = patient.data.consolidatedQueries?.find(
q => q.requestId === requestId
);

const defaultAnalyticsProps = {
distinctId: patient.cxId,
event: EventTypes.consolidatedQuery,
properties: {
patientId: patient.id,
conversionType: "bundle",
duration: elapsedTimeFromNow(currentConsolidatedProgress?.startedAt),
resourceCount: bundle.entry?.length,
},
};

analytics(defaultAnalyticsProps);
Comment on lines -273 to -288
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved to get-snapshot-local.ts


if (shouldCreateMedicalRecord) {
// If we need to convert to medical record, we also have to update the resulting
// FHIR bundle to represent that.
bundle = await handleBundleToMedicalRecord({
bundle,
patient,
requestId,
resources,
dateFrom,
dateTo,
conversionType,
});

analytics({
...defaultAnalyticsProps,
properties: {
...defaultAnalyticsProps.properties,
duration: elapsedTimeFromNow(currentConsolidatedProgress?.startedAt),
conversionType,
},
});
Comment on lines -302 to -309
Copy link
Member Author
@leite08 leite08 Apr 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved to handleBundleToMedicalRecord - convert-fhir-bundle.ts

}

if (conversionType === "json" && hasResources) {
Expand Down Expand Up @@ -410,6 +387,7 @@ async function uploadConsolidatedJsonAndReturnUrl({
*/
export async function getConsolidatedPatientData({
patient,
requestId,
resources,
dateFrom,
dateTo,
Expand All @@ -419,6 +397,7 @@ export async function getConsolidatedPatientData({
const payload: ConsolidatedSnapshotRequestSync = {
patient,
resources,
requestId,
dateFrom,
dateTo,
isAsync: false,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { ConsolidationConversionType } from "@metriport/api-sdk";
import { deleteConsolidated } from "@metriport/core/command/consolidated/consolidated-delete";
import { ConsolidatedSnapshotRequestSync } from "@metriport/core/command/consolidated/get-snapshot";
import { buildConsolidatedSnapshotConnector } from "@metriport/core/command/consolidated/get-snapshot-factory";
import { Patient } from "@metriport/core/domain/patient";
import { processAsyncError } from "@metriport/core/util/error/shared";
import { out } from "@metriport/core/util/log";
import { ResourceDiffDirection } from "@metriport/shared/interface/external/ehr/resource-diff";
import { createResourceDiffBundles } from "../../../external/ehr/create-resource-diff-bundles";
import { getConsolidated } from "../patient/consolidated-get";
import { getConsolidated } from "./consolidated-get";

/**
* Recreates the consolidated bundle for a patient.
Expand Down Expand Up @@ -40,7 +42,16 @@ export async function recreateConsolidated({
processAsyncError(`Failed to delete consolidated bundle`, log)(err);
}
try {
await getConsolidated({ patient, conversionType });
if (conversionType) {
await getConsolidated({ patient, conversionType });
} else {
const payload: ConsolidatedSnapshotRequestSync = {
patient,
isAsync: false,
};
const connector = buildConsolidatedSnapshotConnector();
await connector.execute(payload);
}
if (isDq) {
createResourceDiffBundles({
cxId: patient.cxId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ import {
createMRSummaryFileName,
createSandboxMRSummaryFileName,
} from "@metriport/core/domain/medical-record-summary";
import { Patient } from "@metriport/core/domain/patient";
import { getConsolidatedQueryByRequestId, Patient } from "@metriport/core/domain/patient";
import { analytics, EventTypes } from "@metriport/core/external/analytics/posthog";
import { getLambdaResultPayload, makeLambdaClient } from "@metriport/core/external/aws/lambda";
import { makeS3Client, S3Utils } from "@metriport/core/external/aws/s3";
import { out } from "@metriport/core/util";
import { elapsedTimeFromNow } from "@metriport/shared/common/date";
import { SearchSetBundle } from "@metriport/shared/medical";
import dayjs from "dayjs";
import duration from "dayjs/plugin/duration";
Expand All @@ -39,13 +41,15 @@ export const emptyMetaProp = "na";
export async function handleBundleToMedicalRecord({
bundle,
patient,
requestId,
resources,
dateFrom,
dateTo,
conversionType,
}: {
bundle: Bundle<Resource>;
patient: Pick<Patient, "id" | "cxId" | "data">;
requestId?: string;
resources?: ResourceTypeForConsolidation[];
dateFrom?: string;
dateTo?: string;
Expand Down Expand Up @@ -80,6 +84,18 @@ export async function handleBundleToMedicalRecord({
newBundle.entry = [];
newBundle.total = 0;
}

const currentConsolidatedProgress = getConsolidatedQueryByRequestId(patient, requestId);
analytics({
distinctId: patient.cxId,
event: EventTypes.consolidatedQuery,
properties: {
patientId: patient.id,
conversionType,
duration: elapsedTimeFromNow(currentConsolidatedProgress?.startedAt),
resourceCount: newBundle.entry?.length,
},
});
return newBundle;
}

Expand Down
31 changes: 24 additions & 7 deletions packages/core/src/command/consolidated/get-snapshot-local.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import {
import { elapsedTimeFromNow } from "@metriport/shared/common/date";
import { SearchSetBundle } from "@metriport/shared/medical";
import axios from "axios";
import { getConsolidatedQueryByRequestId } from "../../domain/patient";
import { analytics, EventTypes } from "../../external/analytics/posthog";
import { checkBundle } from "../../external/fhir/bundle/qa";
import { getConsolidatedFhirBundle as getConsolidatedFromFhirServer } from "../../external/fhir/consolidated/consolidated";
import { deduplicate } from "../../external/fhir/consolidated/deduplicate";
Expand All @@ -33,12 +35,13 @@ export class ConsolidatedSnapshotConnectorLocal implements ConsolidatedSnapshotC
async execute(
params: ConsolidatedSnapshotRequestSync | ConsolidatedSnapshotRequestAsync
): Promise<ConsolidatedSnapshotResponse> {
const { cxId, id: patientId } = params.patient;
const { patient, requestId } = params;
const { cxId, id: patientId } = patient;
const { log } = out(`ConsolidatedSnapshotConnectorLocal cx ${cxId} pat ${patientId}`);

const originalBundle = await getBundle(params);

const fhirPatient = patientToFhir(params.patient);
const fhirPatient = patientToFhir(patient);
const patientEntry = buildBundleEntry(fhirPatient);
originalBundle.entry = [patientEntry, ...(originalBundle.entry ?? [])];
originalBundle.total = originalBundle.entry.length;
Expand All @@ -60,8 +63,10 @@ export class ConsolidatedSnapshotConnectorLocal implements ConsolidatedSnapshotC
bundle: dedupedBundle,
});

const resultBundle = normalizedBundle;

try {
checkBundle(normalizedBundle, cxId, patientId);
checkBundle(resultBundle, cxId, patientId);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (error: any) {
const msg = "Bundle contains invalid data";
Expand All @@ -72,7 +77,7 @@ export class ConsolidatedSnapshotConnectorLocal implements ConsolidatedSnapshotC
uploadConsolidatedSnapshotToS3({
...params,
s3BucketName: this.bucketName,
bundle: dedupedBundle,
bundle: resultBundle,
type: "invalid",
});
} catch (error) {
Expand All @@ -81,7 +86,7 @@ export class ConsolidatedSnapshotConnectorLocal implements ConsolidatedSnapshotC
throw new MetriportError(msg, error, additionalInfo);
}

const [, dedupedS3Info] = await Promise.all([
const [, , resultS3Info] = await Promise.all([
uploadConsolidatedSnapshotToS3({
...params,
s3BucketName: this.bucketName,
Expand All @@ -97,12 +102,12 @@ export class ConsolidatedSnapshotConnectorLocal implements ConsolidatedSnapshotC
uploadConsolidatedSnapshotToS3({
...params,
s3BucketName: this.bucketName,
bundle: normalizedBundle,
bundle: resultBundle,
type: "normalized",
}),
]);

const { bucket, key } = dedupedS3Info;
const { bucket, key } = resultS3Info;
const info = {
bundleLocation: bucket,
bundleFilename: key,
Expand All @@ -120,6 +125,18 @@ export class ConsolidatedSnapshotConnectorLocal implements ConsolidatedSnapshotC
});
}

const currentConsolidatedProgress = getConsolidatedQueryByRequestId(patient, requestId);
analytics({
distinctId: cxId,
event: EventTypes.consolidatedQuery,
properties: {
patientId: patientId,
conversionType: "bundle",
duration: elapsedTimeFromNow(currentConsolidatedProgress?.startedAt),
resourceCount: resultBundle.entry?.length,
},
});

return info;
}
}
Expand Down
7 changes: 7 additions & 0 deletions packages/core/src/domain/patient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,10 @@ export function createDriversLicensePersonalIdentifier(
};
return personalIdentifier;
}

export function getConsolidatedQueryByRequestId(
patient: Pick<Patient, "data">,
requestId: string | undefined
): ConsolidatedQuery | undefined {
return patient.data.consolidatedQueries?.find(q => q.requestId === requestId);
}
Loading
0