Skip to content

Commit 27ea63b

Browse files
committed
copilot feedback
1 parent 6b95f48 commit 27ea63b

File tree

9 files changed

+72
-118
lines changed

9 files changed

+72
-118
lines changed

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 & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { Commons } from "../commons";
21
import { OrderStatus, ResultStatus } from "../types/status";
32
import { DBClient } from "./db-client";
43

@@ -18,10 +17,8 @@ export interface OrderPatientReference {
1817

1918
export class OrderService {
2019
private readonly dbClient: DBClient;
21-
private readonly commons: Commons;
22-
constructor(dbClient: DBClient, commons: Commons) {
20+
constructor(dbClient: DBClient) {
2321
this.dbClient = dbClient;
24-
this.commons = commons;
2522
}
2623

2724
async retrieveOrderDetails(orderUid: string): Promise<OrderResultSummary | null> {
@@ -45,7 +42,7 @@ export class OrderService {
4542
const result = await this.dbClient.query<OrderResultSummary, [string]>(query, [orderUid]);
4643
return result.rows[0] || null;
4744
} catch (error) {
48-
this.commons.logError("order-db", "Failed to retrieve order details", { error, orderUid });
45+
console.error("order-db", "Failed to retrieve order details", { error, orderUid });
4946
throw error;
5047
}
5148
}
@@ -64,7 +61,7 @@ export class OrderService {
6461
const result = await this.dbClient.query<OrderPatientReference, [string]>(query, [orderUid]);
6562
return result.rows[0] || null;
6663
} catch (error) {
67-
this.commons.logError("order-db", "Failed to retrieve order-patient association", {
64+
console.error("order-db", "Failed to retrieve order-patient association", {
6865
error,
6966
orderUid,
7067
});
@@ -99,7 +96,7 @@ export class OrderService {
9996
await dbClient.query(resultStatusQuery, [orderUid, resultStatus, correlationId]);
10097
});
10198
} catch (error) {
102-
this.commons.logError("order-db", "Failed to update order and result status", {
99+
console.error("order-db", "Failed to update order and result status", {
103100
error,
104101
orderUid,
105102
});

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: 22 additions & 24 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
},
@@ -40,6 +32,7 @@ jest.mock("../lib/fhir-response", () => ({
4032
const VALID_ORDER_UUID = "123a1234-a12b-1234-abcd-1234567890ab";
4133
const VALID_PATIENT_UUID = "123a1234-a12b-1234-abcd-1234567890ab";
4234
const VALID_CORRELATION_ID = "123a1234-a12b-1234-abcd-1234567890ab";
35+
const VALID_HEADERS = { "X-Correlation-ID": VALID_CORRELATION_ID };
4336

4437
const validTask = {
4538
resourceType: "Task",
@@ -66,9 +59,6 @@ describe("result-status-lambda handler", () => {
6659
beforeEach(() => {
6760
jest.clearAllMocks();
6861

69-
mockLogInfo.mockReset();
70-
mockLogDebug.mockReset();
71-
mockLogError.mockReset();
7262
mockUpdateResultStatus.mockReset();
7363
mockRetrievePatientIdFromOrder.mockReset();
7464

@@ -81,7 +71,7 @@ describe("result-status-lambda handler", () => {
8171

8272
describe("request body parsing", () => {
8373
it("returns 400 when body is null", async () => {
84-
const res = await lambdaHandler(makeEvent(null));
74+
const res = await lambdaHandler(makeEvent(null, VALID_HEADERS));
8575

8676
expect(res.statusCode).toBe(400);
8777
expect(createFhirErrorResponse).toHaveBeenCalledWith(
@@ -93,7 +83,7 @@ describe("result-status-lambda handler", () => {
9383
});
9484

9585
it("returns 400 when body is invalid JSON", async () => {
96-
const res = await lambdaHandler(makeEvent("not-valid-json"));
86+
const res = await lambdaHandler(makeEvent("not-valid-json", VALID_HEADERS));
9787

9888
expect(res.statusCode).toBe(400);
9989
expect(createFhirErrorResponse).toHaveBeenCalledWith(
@@ -117,7 +107,9 @@ describe("result-status-lambda handler", () => {
117107
});
118108

119109
it("returns 400 when task fails schema validation", async () => {
120-
const res = await lambdaHandler(makeEvent(JSON.stringify({ resourceType: "Task" })));
110+
const res = await lambdaHandler(
111+
makeEvent(JSON.stringify({ resourceType: "Task" }), VALID_HEADERS),
112+
);
121113

122114
expect(res.statusCode).toBe(400);
123115
expect(createFhirErrorResponse).toHaveBeenCalledWith(
@@ -131,7 +123,7 @@ describe("result-status-lambda handler", () => {
131123
it("returns 400 when resourceType is not Task", async () => {
132124
const invalidTask = { ...validTask, resourceType: "Observation" };
133125

134-
const res = await lambdaHandler(makeEvent(JSON.stringify(invalidTask)));
126+
const res = await lambdaHandler(makeEvent(JSON.stringify(invalidTask), VALID_HEADERS));
135127

136128
expect(res.statusCode).toBe(400);
137129
});
@@ -142,15 +134,15 @@ describe("result-status-lambda handler", () => {
142134
businessStatus: { coding: [{ code: ResultStatus.Result_Withheld }] },
143135
};
144136

145-
const res = await lambdaHandler(makeEvent(JSON.stringify(invalidTask)));
137+
const res = await lambdaHandler(makeEvent(JSON.stringify(invalidTask), VALID_HEADERS));
146138

147139
expect(res.statusCode).toBe(400);
148140
});
149141

150142
it("returns 400 when identifier array is empty", async () => {
151143
const invalidTask = { ...validTask, identifier: [] };
152144

153-
const res = await lambdaHandler(makeEvent(JSON.stringify(invalidTask)));
145+
const res = await lambdaHandler(makeEvent(JSON.stringify(invalidTask), VALID_HEADERS));
154146

155147
expect(res.statusCode).toBe(400);
156148
});
@@ -160,7 +152,7 @@ describe("result-status-lambda handler", () => {
160152
it("returns 400 when for.reference has no slash (single segment)", async () => {
161153
const invalidTask = { ...validTask, for: { reference: VALID_PATIENT_UUID } };
162154

163-
const res = await lambdaHandler(makeEvent(JSON.stringify(invalidTask)));
155+
const res = await lambdaHandler(makeEvent(JSON.stringify(invalidTask), VALID_HEADERS));
164156

165157
expect(res.statusCode).toBe(400);
166158
expect(createFhirErrorResponse).toHaveBeenCalledWith(
@@ -177,7 +169,7 @@ describe("result-status-lambda handler", () => {
177169
for: { reference: `Patient/${VALID_PATIENT_UUID}/extra` },
178170
};
179171

180-
const res = await lambdaHandler(makeEvent(JSON.stringify(invalidTask)));
172+
const res = await lambdaHandler(makeEvent(JSON.stringify(invalidTask), VALID_HEADERS));
181173

182174
expect(res.statusCode).toBe(400);
183175
expect(createFhirErrorResponse).toHaveBeenCalledWith(
@@ -191,7 +183,7 @@ describe("result-status-lambda handler", () => {
191183
it("returns 400 when patient ID in for.reference is not a valid UUID", async () => {
192184
const invalidTask = { ...validTask, for: { reference: "Patient/not-a-uuid" } };
193185

194-
const res = await lambdaHandler(makeEvent(JSON.stringify(invalidTask)));
186+
const res = await lambdaHandler(makeEvent(JSON.stringify(invalidTask), VALID_HEADERS));
195187

196188
expect(res.statusCode).toBe(400);
197189
expect(createFhirErrorResponse).toHaveBeenCalledWith(
@@ -205,7 +197,7 @@ describe("result-status-lambda handler", () => {
205197
it("returns 400 when order identifier value is not a valid UUID", async () => {
206198
const invalidTask = { ...validTask, identifier: [{ value: "not-a-uuid" }] };
207199

208-
const res = await lambdaHandler(makeEvent(JSON.stringify(invalidTask)));
200+
const res = await lambdaHandler(makeEvent(JSON.stringify(invalidTask), VALID_HEADERS));
209201

210202
expect(res.statusCode).toBe(400);
211203
expect(createFhirErrorResponse).toHaveBeenCalledWith(
@@ -221,7 +213,9 @@ describe("result-status-lambda handler", () => {
221213
it("returns 500 when orderService.retrievePatientIdFromOrder throws", async () => {
222214
mockRetrievePatientIdFromOrder.mockRejectedValueOnce(new Error("DB connection failed"));
223215

224-
const res = await lambdaHandler(makeEvent(JSON.stringify(validTask)));
216+
const res = await lambdaHandler(
217+
makeEvent(JSON.stringify(validTask), { "X-Correlation-ID": VALID_CORRELATION_ID }),
218+
);
225219

226220
expect(res.statusCode).toBe(500);
227221
expect(createFhirErrorResponse).toHaveBeenCalledWith(
@@ -235,7 +229,9 @@ describe("result-status-lambda handler", () => {
235229
it("returns 404 when order is not found", async () => {
236230
mockRetrievePatientIdFromOrder.mockResolvedValueOnce(null);
237231

238-
const res = await lambdaHandler(makeEvent(JSON.stringify(validTask)));
232+
const res = await lambdaHandler(
233+
makeEvent(JSON.stringify(validTask), { "X-Correlation-ID": VALID_CORRELATION_ID }),
234+
);
239235

240236
expect(res.statusCode).toBe(404);
241237
expect(createFhirErrorResponse).toHaveBeenCalledWith(
@@ -254,7 +250,9 @@ describe("result-status-lambda handler", () => {
254250
patient_uid: "a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11",
255251
});
256252

257-
const res = await lambdaHandler(makeEvent(JSON.stringify(validTask)));
253+
const res = await lambdaHandler(
254+
makeEvent(JSON.stringify(validTask), { "X-Correlation-ID": VALID_CORRELATION_ID }),
255+
);
258256

259257
expect(res.statusCode).toBe(403);
260258
expect(createFhirErrorResponse).toHaveBeenCalledWith(

0 commit comments

Comments
 (0)