@@ -4,7 +4,6 @@ import httpErrorHandler from "@middy/http-error-handler";
44import httpSecurityHeaders from "@middy/http-security-headers" ;
55import { APIGatewayProxyEvent , APIGatewayProxyResult } from "aws-lambda" ;
66
7- import { Commons } from "../lib/commons" ;
87import { type OrderPatientReference } from "../lib/db/order-db" ;
98import { createFhirErrorResponse , createFhirResponse } from "../lib/fhir-response" ;
109import { securityHeaders } from "../lib/http/security-headers" ;
@@ -16,26 +15,26 @@ import { corsOptions } from "./cors-configuration";
1615import { init } from "./init" ;
1716import { resultStatusFHIRTaskSchema } from "./schemas" ;
1817
19- function parseAndValidateTask ( body : string | null , commons : Commons ) : FHIRTask {
18+ function parseAndValidateTask ( body : string | null ) : FHIRTask {
2019 let parsedTask : unknown ;
2120
2221 if ( ! body ) {
23- commons . logError ( "result-status-lambda" , "Missing request body" ) ;
22+ console . error ( "result-status-lambda" , "Missing request body" ) ;
2423 throw new Error ( "Request body is required" ) ;
2524 }
2625
2726 try {
2827 parsedTask = JSON . parse ( body ) ;
2928 } catch ( error ) {
30- commons . logError ( "result-status-lambda" , "Invalid JSON in request body" , { error } ) ;
29+ console . error ( "result-status-lambda" , "Invalid JSON in request body" , { error } ) ;
3130 throw new Error ( "Invalid JSON in request body" , { cause : error } ) ;
3231 }
3332
3433 const validationResult = resultStatusFHIRTaskSchema . safeParse ( parsedTask ) ;
3534
3635 if ( ! validationResult . success ) {
3736 const errorDetails = generateReadableError ( validationResult . error ) ;
38- commons . logError ( "result-status-lambda" , "Task validation failed" , { error : errorDetails } ) ;
37+ console . error ( "result-status-lambda" , "Task validation failed" , { error : errorDetails } ) ;
3938 throw new Error ( `Task validation failed: ${ errorDetails } ` ) ;
4039 }
4140
@@ -78,16 +77,16 @@ function extractOrderUidFromFHIRTask(task: FHIRTask): string {
7877export const lambdaHandler = async (
7978 event : APIGatewayProxyEvent ,
8079) : Promise < APIGatewayProxyResult > => {
81- const { commons , resultService, orderService } = init ( ) ;
82- commons . logInfo ( "result-status-lambda" , "Received result status request" , {
80+ const { resultService, orderService } = init ( ) ;
81+ console . info ( "result-status-lambda" , "Received result status request" , {
8382 path : event . path ,
8483 method : event . httpMethod ,
8584 } ) ;
8685
8786 let task : FHIRTask ;
8887
8988 try {
90- task = parseAndValidateTask ( event . body , commons ) ;
89+ task = parseAndValidateTask ( event . body ) ;
9190 } catch ( error ) {
9291 const message = error instanceof Error ? error . message : "Invalid request body" ;
9392 return createFhirErrorResponse ( 400 , "invalid" , message , "error" ) ;
@@ -100,31 +99,42 @@ export const lambdaHandler = async (
10099 orderUID = extractOrderUidFromFHIRTask ( task ) ;
101100 } catch ( error ) {
102101 const message = error instanceof Error ? error . message : "Invalid identifiers" ;
103- commons . logError ( "result-status-lambda" , "Failed to extract identifiers from FHIR Task" , {
102+ console . error ( "result-status-lambda" , "Failed to extract identifiers from FHIR Task" , {
104103 error,
105104 } ) ;
106105 return createFhirErrorResponse ( 400 , "invalid" , message , "error" ) ;
107106 }
108107
108+ let correlationId : string ;
109+
110+ try {
111+ correlationId = getCorrelationIdFromEventHeaders ( event ) ;
112+ } catch ( error ) {
113+ console . error ( "result-status-lambda" , "Failed to extract correlation ID from request headers" , {
114+ error,
115+ } ) ;
116+ return createFhirErrorResponse ( 400 , "invalid" , "Invalid correlation ID in headers" , "error" ) ;
117+ }
118+
109119 let orderSummary : OrderPatientReference | null ;
110120
111121 try {
112122 orderSummary = await orderService . retrievePatientIdFromOrder ( orderUID ) ;
113123 } catch ( error ) {
114- commons . logError ( "result-status-lambda" , "Failed to retrieve order details from database" , {
124+ console . error ( "result-status-lambda" , "Failed to retrieve order details from database" , {
115125 error,
116126 orderUID,
117127 } ) ;
118128 return createFhirErrorResponse ( 500 , "exception" , "An internal error occurred" , "fatal" ) ;
119129 }
120130
121131 if ( ! orderSummary ) {
122- commons . logError ( "result-status-lambda" , "Order not found for given order UID" , { orderUID } ) ;
132+ console . error ( "result-status-lambda" , "Order not found for given order UID" , { orderUID } ) ;
123133 return createFhirErrorResponse ( 404 , "not-found" , "Order not found" , "error" ) ;
124134 }
125135
126136 if ( orderSummary . patient_uid !== patientUID ) {
127- commons . logError ( "result-status-lambda" , "Patient UID in Task does not match order record" , {
137+ console . error ( "result-status-lambda" , "Patient UID in Task does not match order record" , {
128138 orderUID,
129139 } ) ;
130140 return createFhirErrorResponse (
@@ -135,25 +145,10 @@ export const lambdaHandler = async (
135145 ) ;
136146 }
137147
138- let correlationId : string ;
139-
140- try {
141- correlationId = getCorrelationIdFromEventHeaders ( event ) ;
142- } catch ( error ) {
143- commons . logError (
144- "result-status-lambda" ,
145- "Failed to extract correlation ID from request headers" ,
146- {
147- error,
148- } ,
149- ) ;
150- return createFhirErrorResponse ( 400 , "invalid" , "Invalid correlation ID in headers" , "error" ) ;
151- }
152-
153148 try {
154149 await resultService . updateResultStatus ( orderUID , ResultStatus . Result_Available , correlationId ) ;
155150 } catch ( error ) {
156- commons . logError ( "result-status-lambda" , "Failed to update result status in database" , {
151+ console . error ( "result-status-lambda" , "Failed to update result status in database" , {
157152 error,
158153 orderUID,
159154 } ) ;
0 commit comments