|
| 1 | +import {IFunction} from "aws-cdk-lib/aws-lambda" |
| 2 | +import {LambdaInvoke} from "aws-cdk-lib/aws-stepfunctions-tasks" |
| 3 | +import {Construct} from "constructs" |
| 4 | +import { |
| 5 | + Chain, |
| 6 | + Choice, |
| 7 | + Condition, |
| 8 | + IChainable, |
| 9 | + Pass, |
| 10 | + TaskInput |
| 11 | +} from "aws-cdk-lib/aws-stepfunctions" |
| 12 | + |
| 13 | +export interface Format1UpdatePrescriptionsStatusDefinitionProps { |
| 14 | + readonly convertRequestToFhirFormatFunction: IFunction |
| 15 | + readonly updatePrescriptionStatusFunction: IFunction |
| 16 | +} |
| 17 | + |
| 18 | +export class Format1UpdatePrescriptionsStatusDefinition extends Construct { |
| 19 | + public readonly definition: IChainable |
| 20 | + |
| 21 | + public constructor( |
| 22 | + scope: Construct, id: string, props: Format1UpdatePrescriptionsStatusDefinitionProps |
| 23 | + ) { |
| 24 | + super(scope, id) |
| 25 | + |
| 26 | + const catchAllError = new Pass(this, "CatchAllError", { |
| 27 | + result: { |
| 28 | + value: { |
| 29 | + Payload: { |
| 30 | + statusCode: 500, |
| 31 | + headers: { |
| 32 | + "Content-Type": "application/fhir+json", |
| 33 | + "Cache-Control": "no-cache" |
| 34 | + }, |
| 35 | + body: JSON.stringify({ |
| 36 | + resourceType: "OperationOutcome", |
| 37 | + issue: [{severity: "error", code: "processing", diagnostics: "System error"}] |
| 38 | + }) |
| 39 | + } |
| 40 | + } |
| 41 | + } |
| 42 | + }) |
| 43 | + |
| 44 | + const callConvertRequestToFhirFormat = new LambdaInvoke( |
| 45 | + this, "Call Convert Request To Fhir Format", { |
| 46 | + lambdaFunction: props.convertRequestToFhirFormatFunction, |
| 47 | + assign: { |
| 48 | + convertStatusCode: "{% $states.result.Payload.statusCode %}", |
| 49 | + convertHeaders: "{% $states.result.Payload.headers %}", |
| 50 | + convertBody: "{% $parse($states.result.Payload.body) %}" |
| 51 | + } |
| 52 | + } |
| 53 | + ) |
| 54 | + callConvertRequestToFhirFormat.addCatch(catchAllError) |
| 55 | + |
| 56 | + const failedConvertRequestToFhir = new Pass(this, "Failed Convert Request to FHIR", { |
| 57 | + outputs: { |
| 58 | + Payload: { |
| 59 | + statusCode: "{% $convertStatusCode %}", |
| 60 | + headers: "{% $convertHeaders %}", |
| 61 | + body: "{% $string($convertBody) %}" |
| 62 | + } |
| 63 | + } |
| 64 | + }) |
| 65 | + |
| 66 | + const callUpdatePrescriptionStatus = new LambdaInvoke( |
| 67 | + this, "Call Update Prescription Status", { |
| 68 | + lambdaFunction: props.updatePrescriptionStatusFunction, |
| 69 | + payload: TaskInput.fromObject({ |
| 70 | + body: "{% $string($convertBody) %}", |
| 71 | + headers: "{% $convertHeaders %}" |
| 72 | + }), |
| 73 | + assign: { |
| 74 | + updateStatusCode: "{% $states.result.Payload.statusCode %}", |
| 75 | + updatePayload: "{% $states.result.Payload %}" |
| 76 | + } |
| 77 | + } |
| 78 | + ) |
| 79 | + callUpdatePrescriptionStatus.addCatch(catchAllError) |
| 80 | + |
| 81 | + const translate409To202 = new Pass(this, "Translate 409 to 202", { |
| 82 | + result: { |
| 83 | + value: { |
| 84 | + Payload: { |
| 85 | + statusCode: 202, |
| 86 | + headers: { |
| 87 | + "Content-Type": "application/fhir+json", |
| 88 | + "Cache-Control": "no-cache" |
| 89 | + }, |
| 90 | + body: JSON.stringify({ |
| 91 | + resourceType: "OperationOutcome", |
| 92 | + issue: [{ |
| 93 | + severity: "information", |
| 94 | + code: "informational", |
| 95 | + diagnostics: |
| 96 | + "Duplicate update detected. The message was valid but did not result in an update." |
| 97 | + }] |
| 98 | + }) |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + }) |
| 103 | + |
| 104 | + const endState = new Pass(this, "End State") |
| 105 | + |
| 106 | + const checkConvertResult = new Choice(this, "Convert Request to FHIR result") |
| 107 | + const convertNotOk = Condition.jsonata("{% $convertStatusCode != 200 %}") |
| 108 | + |
| 109 | + const checkUpdateResult = new Choice(this, "Check Update Prescription Status Result") |
| 110 | + const updateIs409 = Condition.jsonata("{% $updateStatusCode = 409 %}") |
| 111 | + |
| 112 | + this.definition = Chain |
| 113 | + .start(callConvertRequestToFhirFormat) |
| 114 | + .next( |
| 115 | + checkConvertResult |
| 116 | + .when(convertNotOk, failedConvertRequestToFhir) |
| 117 | + .otherwise( |
| 118 | + callUpdatePrescriptionStatus |
| 119 | + .next( |
| 120 | + checkUpdateResult |
| 121 | + .when(updateIs409, translate409To202) |
| 122 | + .otherwise(endState) |
| 123 | + ) |
| 124 | + ) |
| 125 | + ) |
| 126 | + } |
| 127 | +} |
0 commit comments