Skip to content

Commit feda92f

Browse files
Fix to error if the VARIANT map does not contain the letter variant
1 parent b5fe212 commit feda92f

2 files changed

Lines changed: 39 additions & 1 deletion

File tree

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,35 @@ describe("createSupplierAllocatorHandler", () => {
344344
);
345345
});
346346

347+
test("returns batch failure when variant mapping is missing", async () => {
348+
const preparedEvent = createPreparedV2Event();
349+
preparedEvent.data.letterVariantId = "missing-variant";
350+
351+
const evt: SQSEvent = createSQSEvent([
352+
createSqsRecord("msg1", JSON.stringify(preparedEvent)),
353+
]);
354+
355+
process.env.UPSERT_LETTERS_QUEUE_URL = "https://sqs.test.queue";
356+
357+
// Override variant map to be empty for this test
358+
mockedDeps.env.VARIANT_MAP = {} as any;
359+
360+
const handler = createSupplierAllocatorHandler(mockedDeps);
361+
const result = await handler(evt, {} as any, {} as any);
362+
if (!result) throw new Error("expected BatchResponse, got void");
363+
364+
expect(result.batchItemFailures).toHaveLength(1);
365+
expect(result.batchItemFailures[0].itemIdentifier).toBe("msg1");
366+
expect(
367+
(mockedDeps.logger.error as jest.Mock).mock.calls.length,
368+
).toBeGreaterThan(0);
369+
expect((mockedDeps.logger.error as jest.Mock).mock.calls[0][0]).toEqual(
370+
expect.objectContaining({
371+
description: "No supplier mapping found for variant",
372+
}),
373+
);
374+
});
375+
347376
test("handles SQS send errors and returns batch failure", async () => {
348377
const preparedEvent = createPreparedV2Event();
349378

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,16 @@ function resolveSupplierForVariant(
2020
description: "Resolving supplier for letter variant",
2121
variantId,
2222
});
23-
return deps.env.VARIANT_MAP[variantId];
23+
const supplier = deps.env.VARIANT_MAP[variantId];
24+
if (!supplier) {
25+
deps.logger.error({
26+
description: "No supplier mapping found for variant",
27+
variantId,
28+
});
29+
throw new Error(`No supplier mapping for variantId: ${variantId}`);
30+
}
31+
32+
return supplier;
2433
}
2534

2635
function validateType(event: unknown) {

0 commit comments

Comments
 (0)