Skip to content

Commit

Permalink
Merge pull request #2311 from metriport/1892-generate-ccd
Browse files Browse the repository at this point in the history
feat(fhir-to-cda): inbound dq results in proper ccd
  • Loading branch information
RamilGaripov committed Jun 21, 2024
2 parents ff9448a + 4006186 commit 7610b6c
Show file tree
Hide file tree
Showing 23 changed files with 529 additions and 457 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ export type SetDocQueryProgress = {

/**
* Update a single patient's consolidated query progress.
* Keeps existing sibling properties when those are not provided
* @returns the updated Patient
* Keeps existing sibling properties when those are not provided.
*/
export async function updateConsolidatedQueryProgress({
patient,
Expand Down
17 changes: 7 additions & 10 deletions packages/api/src/command/medical/patient/convert-fhir-to-cda.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,26 @@ import { Bundle } from "../../../routes/medical/schemas/fhir";

export async function convertFhirToCda({
cxId,
patientId,
docId,
validatedBundle,
splitCompositions = true,
}: {
cxId: string;
patientId: string;
docId: string;
validatedBundle: Bundle;
}): Promise<void> {
const { log } = out(`convertFhirToCda - cxId: ${cxId}, patientId: ${patientId}`);
splitCompositions?: boolean;
}): Promise<string[]> {
const { log } = out(`convertFhirToCda - cxId: ${cxId}`);
const cdaConverter = makeFhirToCdaConverter();

try {
await cdaConverter.requestConvert({
return cdaConverter.requestConvert({
cxId,
patientId,
docId,
bundle: validatedBundle,
splitCompositions,
});
} catch (error) {
const msg = `Error converting FHIR to CDA`;
log(`${msg} - error: ${error}`);
capture.error(msg, { extra: { error, cxId, patientId } });
capture.error(msg, { extra: { error, cxId, splitCompositions } });
throw error;
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { createUploadFilePath } from "@metriport/core/domain/document/upload";
import { Patient } from "@metriport/core/domain/patient";
import { toFHIR as toFhirPatient } from "@metriport/core/external/fhir/patient/index";
import { createUploadFilePath } from "@metriport/core/domain/document/upload";
import { uploadFhirBundleToS3 } from "@metriport/core/fhir-to-cda/upload";
import { uploadCdaDocuments, uploadFhirBundleToS3 } from "@metriport/core/fhir-to-cda/upload";
import { uuidv7 } from "@metriport/core/util/uuid-v7";
import BadRequestError from "../../../errors/bad-request";
import { processCcdRequest } from "../../../external/cda/process-ccd-request";
import { toFHIR as toFhirOrganization } from "../../../external/fhir/organization";
import { countResources } from "../../../external/fhir/patient/count-resources";
import { hydrateBundle } from "../../../external/fhir/shared/hydrate-bundle";
Expand Down Expand Up @@ -51,26 +52,35 @@ export async function handleDataContribution({
fhirBundle: validatedBundle,
destinationKey: fhirBundleDestinationKey,
});
const patientDataPromise = async () => {
return createOrUpdateConsolidatedPatientData({
cxId,
patientId: patient.id,
fhirBundle: validatedBundle,
});
};
const consolidatedDataUploadResults = await createOrUpdateConsolidatedPatientData({
cxId,
patientId: patient.id,
fhirBundle: validatedBundle,
});

const convertAndUploadCdaPromise = async () => {
const isValidForCdaConversion = hasCompositionResource(validatedBundle);
if (isValidForCdaConversion) {
await convertFhirToCda({
const converted = await convertFhirToCda({
cxId,
validatedBundle,
});
await uploadCdaDocuments({
cxId,
patientId,
cdaBundles: converted,
organization: fhirOrganization,
docId,
validatedBundle,
});
}
};
const createAndUploadCcdPromise = async () => {
// TODO: To minimize generating CCDs, make it a delayed job (run it ~5min after it was initiated, only once for all requests within that time window)
await processCcdRequest(patient, fhirOrganization);
};

return Promise.all([patientDataPromise(), convertAndUploadCdaPromise()]);
await Promise.all([createAndUploadCcdPromise(), convertAndUploadCdaPromise()]);
return consolidatedDataUploadResults;
}

async function checkResourceLimit(incomingAmount: number, patient: Patient) {
Expand Down
Loading

0 comments on commit 7610b6c

Please sign in to comment.