Skip to content

Commit

Permalink
Merge pull request #2288 from metriport/1667-fix-async-awaits-dq-resp…
Browse files Browse the repository at this point in the history
…onse

fix(ihev2): removing async in dq response
  • Loading branch information
jonahkaye committed Jun 19, 2024
2 parents 816c4c8 + 763b55b commit f3a0ae3
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export async function sendProcessRetryDqRequest({
cxId,
index,
});
return await processDqResponse({
return processDqResponse({
response,
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,15 +132,15 @@ function parseDocumentReference({
return documentReference;
}

async function handleSuccessResponse({
function handleSuccessResponse({
extrinsicObjects,
outboundRequest,
gateway,
}: {
extrinsicObjects: ExtrinsicObject[];
outboundRequest: OutboundDocumentQueryReq;
gateway: XCAGateway;
}): Promise<OutboundDocumentQueryResp> {
}): OutboundDocumentQueryResp {
const documentReferences = extrinsicObjects.flatMap(
extrinsicObject => parseDocumentReference({ extrinsicObject, outboundRequest }) ?? []
);
Expand All @@ -163,7 +163,7 @@ export function processDqResponse({
response: { response, success, gateway, outboundRequest },
}: {
response: DQSamlClientResponse;
}): Promise<OutboundDocumentQueryResp> {
}): OutboundDocumentQueryResp {
if (success === false) {
return handleHttpErrorResponse({
httpError: response,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { getCidReference } from "../mtom/cid";
import { out } from "../../../../../../util/log";
import { errorToString, toArray } from "@metriport/shared";
import { iti39Schema, DocumentResponse } from "./schema";
import { capture } from "../../../../../../util/notifications";

const { log } = out("DR Processing");

Expand Down Expand Up @@ -88,52 +89,65 @@ async function processDocumentReference({
idMapping: Record<string, string>;
mtomResponse: MtomAttachments;
}): Promise<DocumentReference> {
const s3Utils = getS3UtilsInstance();
const { mimeType, decodedBytes } = getMtomBytesAndMimeType(documentResponse, mtomResponse);
const strippedDocUniqueId = stripUrnPrefix(documentResponse.DocumentUniqueId);
const metriportId = idMapping[strippedDocUniqueId];
if (!metriportId) {
throw new MetriportError("MetriportId not found for document");
}
try {
const s3Utils = getS3UtilsInstance();
const { mimeType, decodedBytes } = getMtomBytesAndMimeType(documentResponse, mtomResponse);
const strippedDocUniqueId = stripUrnPrefix(documentResponse.DocumentUniqueId);
const metriportId = idMapping[strippedDocUniqueId];
if (!metriportId) {
throw new MetriportError("MetriportId not found for document");
}

const filePath = createDocumentFilePath(
outboundRequest.cxId,
outboundRequest.patientId,
metriportId,
mimeType
);
const fileInfo = await s3Utils.getFileInfoFromS3(filePath, bucket);
const filePath = createDocumentFilePath(
outboundRequest.cxId,
outboundRequest.patientId,
metriportId,
mimeType
);
const fileInfo = await s3Utils.getFileInfoFromS3(filePath, bucket);

if (!fileInfo.exists) {
await s3Utils.uploadFile({
bucket,
key: filePath,
file: decodedBytes,
contentType: mimeType,
});
}

log(
`Downloaded a document with mime type: ${mimeType} for patient: ${outboundRequest.patientId} and request: ${outboundRequest.id}`
);

if (!fileInfo.exists) {
await s3Utils.uploadFile({
bucket,
key: filePath,
file: decodedBytes,
return {
url: s3Utils.buildFileUrl(bucket, filePath),
size: documentResponse.size ? parseInt(documentResponse.size) : undefined,
title: documentResponse.title,
fileName: filePath,
creation: documentResponse.creation,
language: documentResponse.language,
contentType: mimeType,
docUniqueId: documentResponse.DocumentUniqueId.toString(),
metriportId: metriportId,
fileLocation: bucket,
homeCommunityId: outboundRequest.gateway.homeCommunityId,
repositoryUniqueId: documentResponse.RepositoryUniqueId,
newDocumentUniqueId: documentResponse.NewDocumentUniqueId,
newRepositoryUniqueId: documentResponse.NewRepositoryUniqueId,
isNew: !fileInfo.exists,
};
} catch (error) {
const msg = "Error processing Document Reference";
log(`${msg}: ${error}`);
capture.error(msg, {
extra: {
error,
outboundRequest,
documentResponse,
},
});
throw new MetriportError(`Error Processing Document Reference`, error);
}

log(
`Downloaded a document with mime type: ${mimeType} for patient: ${outboundRequest.patientId} and request: ${outboundRequest.id}`
);

return {
url: s3Utils.buildFileUrl(bucket, filePath),
size: documentResponse.size ? parseInt(documentResponse.size) : undefined,
title: documentResponse.title,
fileName: filePath,
creation: documentResponse.creation,
language: documentResponse.language,
contentType: mimeType,
docUniqueId: documentResponse.DocumentUniqueId.toString(),
metriportId: metriportId,
fileLocation: bucket,
homeCommunityId: outboundRequest.gateway.homeCommunityId,
repositoryUniqueId: documentResponse.RepositoryUniqueId,
newDocumentUniqueId: documentResponse.NewDocumentUniqueId,
newRepositoryUniqueId: documentResponse.NewRepositoryUniqueId,
isNew: !fileInfo.exists,
};
}

function generateIdMapping(documentReferences: DocumentReference[]): Record<string, string> {
Expand All @@ -158,12 +172,19 @@ async function handleSuccessResponse({
}): Promise<OutboundDocumentRetrievalResp> {
try {
const idMapping = generateIdMapping(outboundRequest.documentReference);
const documentReferences = await Promise.all(
documentResponses.map(async (documentResponse: DocumentResponse) =>
const documentReferencesResults = await Promise.allSettled(
documentResponses.map((documentResponse: DocumentResponse) =>
processDocumentReference({ documentResponse, outboundRequest, idMapping, mtomResponse })
)
);

const documentReferences = documentReferencesResults
.filter(
(result): result is PromiseFulfilledResult<DocumentReference> =>
result.status === "fulfilled"
)
.map(result => result.value);

const response: OutboundDocumentRetrievalResp = {
id: outboundRequest.id,
patientId: outboundRequest.patientId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,15 @@ export function processRegistryErrorList(
return operationOutcome.issue.length > 0 ? operationOutcome : undefined;
}

export async function handleRegistryErrorResponse({
export function handleRegistryErrorResponse({
registryErrorList,
outboundRequest,
gateway,
}: {
registryErrorList: RegistryErrorList;
outboundRequest: OutboundDocumentQueryReq | OutboundDocumentRetrievalReq;
gateway: XCAGateway;
}): Promise<OutboundDocumentQueryResp | OutboundDocumentRetrievalResp> {
}): OutboundDocumentQueryResp | OutboundDocumentRetrievalResp {
const operationOutcome = processRegistryErrorList(registryErrorList, outboundRequest);
return {
id: outboundRequest.id,
Expand All @@ -84,7 +84,7 @@ export async function handleRegistryErrorResponse({
};
}

export async function handleHttpErrorResponse({
export function handleHttpErrorResponse({
httpError,
outboundRequest,
gateway,
Expand All @@ -94,7 +94,7 @@ export async function handleHttpErrorResponse({
outboundRequest: OutboundDocumentQueryReq | OutboundDocumentRetrievalReq;
gateway: XCAGateway;
attempt?: number | undefined;
}): Promise<OutboundDocumentQueryResp | OutboundDocumentRetrievalResp> {
}): OutboundDocumentQueryResp | OutboundDocumentRetrievalResp {
const operationOutcome: OperationOutcome = {
resourceType: "OperationOutcome",
id: outboundRequest.id,
Expand All @@ -121,15 +121,15 @@ export async function handleHttpErrorResponse({
};
}

export async function handleEmptyResponse({
export function handleEmptyResponse({
outboundRequest,
gateway,
text = "No documents found",
}: {
outboundRequest: OutboundDocumentQueryReq | OutboundDocumentRetrievalReq;
gateway: XCAGateway;
text?: string;
}): Promise<OutboundDocumentQueryResp | OutboundDocumentRetrievalResp> {
}): OutboundDocumentQueryResp | OutboundDocumentRetrievalResp {
const operationOutcome: OperationOutcome = {
resourceType: "OperationOutcome",
id: outboundRequest.id,
Expand All @@ -155,15 +155,15 @@ export async function handleEmptyResponse({
};
}

export async function handleSchemaErrorResponse({
export function handleSchemaErrorResponse({
outboundRequest,
gateway,
text = "Schema Error",
}: {
outboundRequest: OutboundDocumentQueryReq | OutboundDocumentRetrievalReq;
gateway: XCAGateway;
text?: string;
}): Promise<OutboundDocumentQueryResp | OutboundDocumentRetrievalResp> {
}): OutboundDocumentQueryResp | OutboundDocumentRetrievalResp {
const operationOutcome: OperationOutcome = {
resourceType: "OperationOutcome",
id: outboundRequest.id,
Expand Down

0 comments on commit f3a0ae3

Please sign in to comment.