diff --git a/src/workflows/botswana/LaboratoryBundle.ts b/src/workflows/botswana/LaboratoryBundle.ts index def16e5..fd6cad0 100644 --- a/src/workflows/botswana/LaboratoryBundle.ts +++ b/src/workflows/botswana/LaboratoryBundle.ts @@ -27,7 +27,33 @@ class LaboratoryBundle { } - +<<<<<<< Updated upstream + +======= + protected async addAllCodings(labBundle: ILaboratoryBundle): Promise { + try { + for (const e of labBundle.entry!) { + if ( + e.resource && + e.resource.resourceType == 'ServiceRequest' && + e.resource.code && + e.resource.code.coding && + e.resource.code.coding.length > 0 + ) { + logger.info`Translating ServiceRequest Codings` + + e.resource = await this.translateCoding(>>>>>> Stashed changes } diff --git a/src/workflows/botswana/hl7Workflows.ts b/src/workflows/botswana/hl7Workflows.ts new file mode 100644 index 0000000..588a1e0 --- /dev/null +++ b/src/workflows/botswana/hl7Workflows.ts @@ -0,0 +1,133 @@ +'use strict' + +import { R4 } from '@ahryman40k/ts-fhir-types' +import { BundleTypeKind, IBundle } from '@ahryman40k/ts-fhir-types/lib/R4' +import got from 'got/dist/source' +import config from '../../lib/config' +import logger from '../../lib/winston' +import { WorkflowHandler, topicList } from './workflowHandler' +import sleep from 'sleep-promise' + +export default class Hl7WorkflowsBw { + public static errorBundle: IBundle = { + resourceType: 'Bundle', + type: BundleTypeKind._transactionResponse, + entry: [ + { + response: { + status: '500 Server Error', + }, + }, + ], + } + + // GET Lab Orders via HL7v2 over HTTP - ORU Message + static async handleOruMessage(hl7Msg: string): Promise { + try { + const translatedBundle: R4.IBundle = await Hl7WorkflowsBw.translateBundle( + hl7Msg, + 'bwConfig:fromIpmsOruTemplate', + ) + + if (translatedBundle != this.errorBundle && translatedBundle.entry) { + WorkflowHandler.sendPayload({ bundle: translatedBundle }, topicList.HANDLE_ORU_FROM_IPMS) + return translatedBundle + } else { + return this.errorBundle + } + } catch (error: any) { + logger.error(`Could not save ORU message!\n${JSON.stringify(error)}`) + return this.errorBundle + } + } + + static async handleAdtMessage(hl7Msg: string): Promise { + try { + const translatedBundle: R4.IBundle = await Hl7WorkflowsBw.translateBundle( + hl7Msg, + 'bwConfig:fromIpmsAdtTemplate', + ) + + if (translatedBundle != this.errorBundle) { + // Save to SHR?? + // let resultBundle: R4.IBundle = await saveBundle(translatedBundle) + + WorkflowHandler.sendPayload({ bundle: translatedBundle }, topicList.SAVE_IPMS_PATIENT) + + return translatedBundle + } else { + return this.errorBundle + } + } catch (error: any) { + logger.error(`Could not translate and save ADT message!\n${JSON.stringify(error)}`) + return this.errorBundle + } + } + + private static async translateBundle(hl7Msg: string, template: string) { + let tries = 0 + let translatedBundle: R4.IBundle = this.errorBundle + + while (tries < 5 && translatedBundle == this.errorBundle) { + tries = tries + 1 + translatedBundle = await this.getHl7Translation(hl7Msg, config.get(template)) + if (translatedBundle == this.errorBundle) { + await sleep(1000) + } + } + return translatedBundle + } + + static async getHl7Translation(hl7Message: string, template: string): Promise { + try { + const translatedMessage: any = await got({ + url: `${config.get('fhirConverterUrl')}/convert/hl7v2/${template}`, + headers: { + 'content-type': 'text/plain', + }, + body: hl7Message.replace(/\r/g, '\n'), + method: 'POST', + https: { + rejectUnauthorized: false, + }, + username: config.get('mediator:client:username'), + password: config.get('mediator:client:password'), + }).json() + + return translatedMessage.fhirResource + } catch (error: any) { + logger.error( + `Could not translate HL7 message\n${hl7Message}\nwith template ${template}!\n${JSON.stringify( + error, + )}`, + ) + + return this.errorBundle + } + } + + static async getFhirTranslation(bundle: R4.IBundle, template: string): Promise { + try { + return await got({ + url: `${config.get('fhirConverterUrl')}/convert/fhir/${template}`, + headers: { + 'content-type': 'application/json', + }, + body: JSON.stringify(bundle), + method: 'POST', + https: { + rejectUnauthorized: false, + }, + username: config.get('mediator:client:username'), + password: config.get('mediator:client:password'), + }).text() + } catch (error: any) { + logger.error( + `Could not translate FHIR Bundle message\n${JSON.stringify( + bundle, + )}\n with template ${template}!\n${JSON.stringify(error)}`, + ) + return '' + } + } +}