Skip to content

Commit 8449e04

Browse files
make stats idempotent too
1 parent 6de3928 commit 8449e04

1 file changed

Lines changed: 31 additions & 27 deletions

File tree

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

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ import {
3232

3333
import { Deps } from "../config/deps";
3434
import { PreparedEventSchema, PreparedEvents, SupplierDetails } from "./types";
35-
import { domain } from "zod/v4/core/regexes.cjs";
3635

3736
const idempotencyConfig = new IdempotencyConfig({
3837
eventKeyJmesPath: "id",
@@ -270,9 +269,6 @@ async function saveAllocations(
270269
type SupplierAllocationResult = {
271270
supplier: string;
272271
priority: string;
273-
perAllocationSuccess: AllocationMetrics;
274-
perAllocationFailure: AllocationMetrics;
275-
volumeGroupAllocations: VolumeGroupAllocation;
276272
};
277273

278274
async function processSupplierAllocation(
@@ -342,22 +338,42 @@ async function processSupplierAllocation(
342338
return {
343339
supplier,
344340
priority,
345-
perAllocationSuccess,
346-
perAllocationFailure,
347-
volumeGroupAllocations,
348341
};
349342
}
350343

351344
export default function createSupplierAllocatorHandler(deps: Deps): SQSHandler {
352-
const getSupplierIdempotently = makeIdempotent(processSupplierAllocation, {
353-
persistenceStore: deps.idempotencyLayer,
354-
config: idempotencyConfig,
355-
});
345+
const getSupplierIdempotently = (
346+
perAllocationSuccess: AllocationMetrics,
347+
perAllocationFailure: AllocationMetrics,
348+
volumeGroupAllocations: VolumeGroupAllocation,
349+
) => {
350+
return makeIdempotent(
351+
(letterEvent: PreparedEvents, depsInner: Deps) =>
352+
processSupplierAllocation(
353+
letterEvent,
354+
depsInner,
355+
perAllocationSuccess,
356+
perAllocationFailure,
357+
volumeGroupAllocations,
358+
),
359+
{
360+
persistenceStore: deps.idempotencyLayer,
361+
config: idempotencyConfig,
362+
},
363+
);
364+
};
356365
return async (event: SQSEvent, context: Context) => {
357366
const batchItemFailures: SQSBatchItemFailure[] = [];
358-
let perAllocationSuccess: AllocationMetrics = new Map();
359-
let perAllocationFailure: AllocationMetrics = new Map();
360-
let volumeGroupAllocations: VolumeGroupAllocation = new Map();
367+
const perAllocationSuccess: AllocationMetrics = new Map();
368+
const perAllocationFailure: AllocationMetrics = new Map();
369+
const volumeGroupAllocations: VolumeGroupAllocation = new Map();
370+
371+
// create an idempotent function bound to this handler's batchItemFailures
372+
const boundGetSupplierIdempotently = getSupplierIdempotently(
373+
perAllocationSuccess,
374+
perAllocationFailure,
375+
volumeGroupAllocations,
376+
);
361377

362378
const tasks = event.Records.map(async (record) => {
363379
let supplier = "unknown";
@@ -376,29 +392,17 @@ export default function createSupplierAllocatorHandler(deps: Deps): SQSHandler {
376392
idempotencyConfig.registerLambdaContext(context);
377393

378394
const supplierAllocationResult: SupplierAllocationResult =
379-
await getSupplierIdempotently(
380-
letterEvent,
381-
deps,
382-
perAllocationSuccess,
383-
perAllocationFailure,
384-
volumeGroupAllocations,
385-
);
395+
await boundGetSupplierIdempotently(letterEvent, deps);
386396
deps.logger.info({
387397
description: "Completed supplier allocation for letter event",
388398
eventId: letterEvent.id,
389399
domainId: letterEvent.data.domainId,
390400
letterVariantId: letterEvent.data.letterVariantId,
391401
supplier: supplierAllocationResult.supplier,
392402
priority: supplierAllocationResult.priority,
393-
updatedVolumeGroupAllocations:
394-
supplierAllocationResult.volumeGroupAllocations,
395403
});
396404
supplier = supplierAllocationResult.supplier;
397405
priority = supplierAllocationResult.priority;
398-
perAllocationSuccess = supplierAllocationResult.perAllocationSuccess;
399-
perAllocationFailure = supplierAllocationResult.perAllocationFailure;
400-
volumeGroupAllocations =
401-
supplierAllocationResult.volumeGroupAllocations;
402406
deps.logger.info({
403407
description:
404408
"Completed processing supplier allocation for letter event",

0 commit comments

Comments
 (0)