Skip to content

Commit 3b75bb4

Browse files
get unit tests to pass
1 parent e26fa97 commit 3b75bb4

2 files changed

Lines changed: 35 additions & 18 deletions

File tree

lambdas/supplier-allocator/src/handler/__tests__/allocate-handler.test.ts

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ function createPreparedV1Event(
6969
requestItemId: "requestItem1",
7070
requestItemPlanId: "requestItemPlan1",
7171
clientId: "client1",
72-
campaignId: "campaign1",
72+
campaignId: overrides.campaignId ?? "campaign1",
7373
templateId: "template1",
7474
url: overrides.url ?? "s3://letterDataBucket/letter1.pdf",
7575
sha256Hash:
@@ -230,6 +230,37 @@ describe("createSupplierAllocatorHandler", () => {
230230
});
231231
});
232232

233+
test("parses SNS notification and sends message to SQS queue for v2 event without a campaignId", async () => {
234+
const preparedEvent = createPreparedV2Event({ campaignId: "" });
235+
const evt: SQSEvent = createSQSEvent([
236+
createSqsRecord("msg1", JSON.stringify(preparedEvent)),
237+
]);
238+
239+
setupDefaultMocks();
240+
process.env.UPSERT_LETTERS_QUEUE_URL = "https://sqs.test.queue";
241+
242+
const handler = createSupplierAllocatorHandler(mockedDeps);
243+
const result = await handler(evt, {} as any, {} as any);
244+
245+
expect(result).toBeDefined();
246+
if (!result) throw new Error("expected BatchResponse, got void");
247+
248+
expect(result.batchItemFailures).toHaveLength(0);
249+
250+
expect(mockSqsClient.send).toHaveBeenCalledTimes(1);
251+
const sendCall = (mockSqsClient.send as jest.Mock).mock.calls[0][0];
252+
expect(sendCall).toBeInstanceOf(SendMessageCommand);
253+
254+
const messageBody = JSON.parse(sendCall.input.MessageBody);
255+
expect(messageBody.letterEvent).toEqual(preparedEvent);
256+
expect(messageBody.supplierSpec).toEqual({
257+
supplierId: "supplier1",
258+
specId: "spec1",
259+
priority: 1,
260+
billingId: "billing1",
261+
});
262+
});
263+
233264
test("parses SNS notification and sends message to SQS queue for v1 event", async () => {
234265
const preparedEvent = createPreparedV1Event();
235266

lambdas/supplier-allocator/src/handler/allocate-handler.ts

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -151,12 +151,12 @@ function emitSupCampaignClientMetric(
151151
deps: Deps,
152152
) {
153153
const namespace = "supplier-allocator";
154-
const clientId = getClientId(letterEvent);
155-
const campaignId = getCampaignId(letterEvent);
154+
const { campaignId, clientId } = letterEvent.data;
155+
console.log("VLASIS and the campaignId is:", campaignId);
156156
const dimensions: Record<string, string> = {
157157
Supplier: supplier,
158158
ClientId: clientId,
159-
CampaignId: campaignId,
159+
CampaignId: campaignId || "unknown",
160160
};
161161
const metric: MetricEntry = {
162162
key: status,
@@ -166,20 +166,6 @@ function emitSupCampaignClientMetric(
166166
deps.logger.info(buildEMFObject(namespace, dimensions, metric));
167167
}
168168

169-
function getClientId(letterEvent: PreparedEvents): string {
170-
if (letterEvent && letterEvent.data && letterEvent.data.clientId) {
171-
return letterEvent.data.clientId;
172-
}
173-
return "unknown";
174-
}
175-
176-
function getCampaignId(letterEvent: PreparedEvents): string {
177-
if (letterEvent && letterEvent.data && letterEvent.data.campaignId) {
178-
return letterEvent.data.campaignId;
179-
}
180-
return "unknown";
181-
}
182-
183169
export default function createSupplierAllocatorHandler(deps: Deps): SQSHandler {
184170
return async (event: SQSEvent) => {
185171
const batchItemFailures: SQSBatchItemFailure[] = [];

0 commit comments

Comments
 (0)