Skip to content

Commit 85e3399

Browse files
committed
copilot feedback
1 parent 6b95f48 commit 85e3399

9 files changed

Lines changed: 49 additions & 107 deletions

File tree

lambdas/src/lib/db/order-db.test.ts

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,18 @@
1-
import { Commons } from "../commons";
21
import { OrderStatus, ResultStatus } from "../types/status";
32
import { OrderResultSummary, OrderService } from "./order-db";
43

54
const normalizeWhitespace = (sql: string): string => sql.replace(/\s+/g, " ").trim();
65

76
describe("OrderService", () => {
87
let dbClient: any;
9-
let commons: Pick<Commons, "logError">;
108
let orderService: OrderService;
119

1210
beforeEach(() => {
1311
dbClient = {
1412
query: jest.fn(),
1513
withTransaction: jest.fn(),
1614
};
17-
commons = {
18-
logError: jest.fn(),
19-
};
20-
orderService = new OrderService(dbClient, commons as any as Commons);
15+
orderService = new OrderService(dbClient);
2116
});
2217

2318
describe("retrieveOrderDetails", () => {
@@ -82,14 +77,10 @@ describe("OrderService", () => {
8277
normalizeWhitespace(expectedRetrieveOrderDetailsQuery),
8378
);
8479
expect(dbClient.query.mock.calls[0][1]).toEqual(["order-500"]);
85-
expect(commons.logError).toHaveBeenCalledWith(
86-
"order-db",
87-
"Failed to retrieve order details",
88-
{
89-
error,
90-
orderUid: "order-500",
91-
},
92-
);
80+
expect(console.error).toHaveBeenCalledWith("order-db", "Failed to retrieve order details", {
81+
error,
82+
orderUid: "order-500",
83+
});
9384
});
9485
});
9586

@@ -208,7 +199,7 @@ describe("OrderService", () => {
208199
),
209200
).rejects.toThrow(error);
210201

211-
expect(commons.logError).toHaveBeenCalledWith(
202+
expect(console.error).toHaveBeenCalledWith(
212203
"order-db",
213204
"Failed to update order and result status",
214205
{

lambdas/src/lib/db/order-db.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,8 @@ export interface OrderPatientReference {
1818

1919
export class OrderService {
2020
private readonly dbClient: DBClient;
21-
private readonly commons: Commons;
22-
constructor(dbClient: DBClient, commons: Commons) {
21+
constructor(dbClient: DBClient) {
2322
this.dbClient = dbClient;
24-
this.commons = commons;
2523
}
2624

2725
async retrieveOrderDetails(orderUid: string): Promise<OrderResultSummary | null> {
@@ -45,7 +43,7 @@ export class OrderService {
4543
const result = await this.dbClient.query<OrderResultSummary, [string]>(query, [orderUid]);
4644
return result.rows[0] || null;
4745
} catch (error) {
48-
this.commons.logError("order-db", "Failed to retrieve order details", { error, orderUid });
46+
console.error("order-db", "Failed to retrieve order details", { error, orderUid });
4947
throw error;
5048
}
5149
}
@@ -64,7 +62,7 @@ export class OrderService {
6462
const result = await this.dbClient.query<OrderPatientReference, [string]>(query, [orderUid]);
6563
return result.rows[0] || null;
6664
} catch (error) {
67-
this.commons.logError("order-db", "Failed to retrieve order-patient association", {
65+
console.error("order-db", "Failed to retrieve order-patient association", {
6866
error,
6967
orderUid,
7068
});
@@ -99,7 +97,7 @@ export class OrderService {
9997
await dbClient.query(resultStatusQuery, [orderUid, resultStatus, correlationId]);
10098
});
10199
} catch (error) {
102-
this.commons.logError("order-db", "Failed to update order and result status", {
100+
console.error("order-db", "Failed to update order and result status", {
103101
error,
104102
orderUid,
105103
});

lambdas/src/lib/db/result-db.test.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { Commons } from "../commons";
21
import { ResultStatus } from "../types/status";
32
import { DBClient } from "./db-client";
43
import { ResultService } from "./result-db";
@@ -11,12 +10,6 @@ const mockDbClient: DBClient = {
1110
withTransaction: jest.fn(),
1211
};
1312

14-
const mockCommons: Commons = {
15-
logError: jest.fn(),
16-
logInfo: jest.fn(),
17-
logDebug: jest.fn(),
18-
};
19-
2013
const orderUid = "order-123";
2114
const correlationId = "corr-xyz";
2215

@@ -26,7 +19,7 @@ describe("ResultService", () => {
2619
beforeEach(() => {
2720
jest.clearAllMocks();
2821
mockQuery.mockReset();
29-
resultService = new ResultService(mockDbClient, mockCommons);
22+
resultService = new ResultService(mockDbClient);
3023
});
3124

3225
describe("updateResultStatus", () => {

lambdas/src/lib/db/result-db.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,16 @@
1-
import { Commons } from "../commons";
21
import { ResultStatus } from "../types/status";
32
import { DBClient } from "./db-client";
43

54
export class ResultService {
65
private readonly dbClient: DBClient;
7-
private readonly commons: Commons;
8-
constructor(dbClient: DBClient, commons: Commons) {
6+
constructor(dbClient: DBClient) {
97
this.dbClient = dbClient;
10-
this.commons = commons;
118
}
129

1310
async updateResultStatus(
1411
orderUid: string,
1512
status: ResultStatus,
16-
correlationId: string | null,
13+
correlationId: string,
1714
): Promise<void> {
1815
const query = `
1916
INSERT INTO result_status (order_uid, status, correlation_id)
@@ -23,7 +20,7 @@ export class ResultService {
2320
try {
2421
await this.dbClient.query(query, [orderUid, status, correlationId]);
2522
} catch (error) {
26-
this.commons.logError("result-db", "Failed to update result status", {
23+
console.error("Failed to update result status", {
2724
error,
2825
orderUid,
2926
status,

lambdas/src/order-result-lambda/init.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export function buildEnvironment(): Environment {
2626

2727
const secretsClient = new AwsSecretsClient(awsRegion);
2828
const dbClient = new PostgresDbClient(postgresConfigFromEnv(secretsClient));
29-
const orderService = new OrderService(dbClient, commons);
29+
const orderService = new OrderService(dbClient);
3030
const orderStatusDb = new OrderStatusService(dbClient);
3131
const patientDbClient = new PatientDbClient(dbClient);
3232
const orderDbClient = new OrderDbClient(dbClient);

lambdas/src/result-status-lambda/index.test.ts

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,11 @@ import { createFhirErrorResponse, createFhirResponse } from "../lib/fhir-respons
44
import { ResultStatus } from "../lib/types/status";
55
import { lambdaHandler } from "./index";
66

7-
const mockLogInfo = jest.fn();
8-
const mockLogDebug = jest.fn();
9-
const mockLogError = jest.fn();
107
const mockUpdateResultStatus = jest.fn();
118
const mockRetrievePatientIdFromOrder = jest.fn();
129

1310
jest.mock("./init", () => ({
1411
init: jest.fn(() => ({
15-
commons: {
16-
logInfo: mockLogInfo,
17-
logDebug: mockLogDebug,
18-
logError: mockLogError,
19-
},
2012
resultService: {
2113
updateResultStatus: mockUpdateResultStatus,
2214
},
@@ -66,9 +58,6 @@ describe("result-status-lambda handler", () => {
6658
beforeEach(() => {
6759
jest.clearAllMocks();
6860

69-
mockLogInfo.mockReset();
70-
mockLogDebug.mockReset();
71-
mockLogError.mockReset();
7261
mockUpdateResultStatus.mockReset();
7362
mockRetrievePatientIdFromOrder.mockReset();
7463

@@ -221,7 +210,9 @@ describe("result-status-lambda handler", () => {
221210
it("returns 500 when orderService.retrievePatientIdFromOrder throws", async () => {
222211
mockRetrievePatientIdFromOrder.mockRejectedValueOnce(new Error("DB connection failed"));
223212

224-
const res = await lambdaHandler(makeEvent(JSON.stringify(validTask)));
213+
const res = await lambdaHandler(
214+
makeEvent(JSON.stringify(validTask), { "X-Correlation-ID": VALID_CORRELATION_ID }),
215+
);
225216

226217
expect(res.statusCode).toBe(500);
227218
expect(createFhirErrorResponse).toHaveBeenCalledWith(
@@ -235,7 +226,9 @@ describe("result-status-lambda handler", () => {
235226
it("returns 404 when order is not found", async () => {
236227
mockRetrievePatientIdFromOrder.mockResolvedValueOnce(null);
237228

238-
const res = await lambdaHandler(makeEvent(JSON.stringify(validTask)));
229+
const res = await lambdaHandler(
230+
makeEvent(JSON.stringify(validTask), { "X-Correlation-ID": VALID_CORRELATION_ID }),
231+
);
239232

240233
expect(res.statusCode).toBe(404);
241234
expect(createFhirErrorResponse).toHaveBeenCalledWith(
@@ -254,7 +247,9 @@ describe("result-status-lambda handler", () => {
254247
patient_uid: "a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11",
255248
});
256249

257-
const res = await lambdaHandler(makeEvent(JSON.stringify(validTask)));
250+
const res = await lambdaHandler(
251+
makeEvent(JSON.stringify(validTask), { "X-Correlation-ID": VALID_CORRELATION_ID }),
252+
);
258253

259254
expect(res.statusCode).toBe(403);
260255
expect(createFhirErrorResponse).toHaveBeenCalledWith(

lambdas/src/result-status-lambda/index.ts

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import httpErrorHandler from "@middy/http-error-handler";
44
import httpSecurityHeaders from "@middy/http-security-headers";
55
import { APIGatewayProxyEvent, APIGatewayProxyResult } from "aws-lambda";
66

7-
import { Commons } from "../lib/commons";
87
import { type OrderPatientReference } from "../lib/db/order-db";
98
import { createFhirErrorResponse, createFhirResponse } from "../lib/fhir-response";
109
import { securityHeaders } from "../lib/http/security-headers";
@@ -16,26 +15,26 @@ import { corsOptions } from "./cors-configuration";
1615
import { init } from "./init";
1716
import { 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 {
7877
export 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

Comments
 (0)