Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
],
"priority": 99,
"status": "INT",
"supplierId": "supplier1",
"type": "STANDARD",
"volumeGroupId": "volumeGroup-test3"
"volumeGroupId": "volumeGroup-test1"
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,26 @@ data "aws_iam_policy_document" "upsert_letter_lambda" {

resources = [
aws_dynamodb_table.letters.arn,
aws_dynamodb_table.idempotency.arn,
"${aws_dynamodb_table.letters.arn}/index/supplierStatus-index"
]
}

statement {
sid = "AllowIdempotencyTableWrite"
effect = "Allow"

actions = [
"dynamodb:PutItem",
"dynamodb:GetItem",
"dynamodb:UpdateItem",
"dynamodb:DeleteItem"
]

resources = [
aws_dynamodb_table.idempotency.arn,
]
}

statement {
sid = "AllowSQSRead"
effect = "Allow"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,37 @@ describe("eligibleSuppliers", () => {
"Supplier service error",
);
});
it("should filter allocations by letterVariantSupplierId if provided", async () => {
(
supplierConfigService.getSupplierAllocationsForVolumeGroup as jest.Mock
).mockResolvedValue(mockSupplierAllocations);
(supplierConfigService.getSupplierDetails as jest.Mock).mockResolvedValue(
mockSuppliers,
);

const letterVariantSupplierId = "supplier-1";
await eligibleSuppliers(mockVolumeGroup, mockDeps, letterVariantSupplierId);
expect(supplierConfigService.getSupplierDetails).toHaveBeenCalledWith(
["supplier-1"],
mockDeps,
);
});
it("should log a warning if no allocations found for specified letter variant supplier", async () => {
(
supplierConfigService.getSupplierAllocationsForVolumeGroup as jest.Mock
).mockResolvedValue([]);
(supplierConfigService.getSupplierDetails as jest.Mock).mockResolvedValue(
[],
);

const letterVariantSupplierId = "supplier-1";
await eligibleSuppliers(mockVolumeGroup, mockDeps, letterVariantSupplierId);
expect(mockDeps.logger.warn).toHaveBeenCalledWith({
description: "No allocations found for specified letter variant supplier",
volumeGroupId: mockVolumeGroup.id,
letterVariantSupplierId,
});
});
});

describe("preferredSupplierPack", () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ async function getSupplierFromConfig(
);

const { supplierAllocations, suppliers: allocatedSuppliers } =
await eligibleSuppliers(volumeGroup, deps);
await eligibleSuppliers(volumeGroup, deps, letterVariant.supplierId);

const preferredPack: PackSpecification = await preferredSupplierPack(
letterEvent,
Expand Down Expand Up @@ -391,6 +391,9 @@ export default function createSupplierAllocatorHandler(deps: Deps): SQSHandler {

({ priority, supplier } = supplierAllocationResult);
} catch (error) {
console.log(
`Error processing allocation of record ${record.messageId}: ${error}`,
);
deps.logger.error({
description: "Error processing allocation of record",
err: error,
Expand Down
27 changes: 27 additions & 0 deletions lambdas/supplier-allocator/src/handler/allocation-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { PreparedEvents } from "./types";
export async function eligibleSuppliers(
volumeGroup: VolumeGroup,
deps: Deps,
letterVariantSupplierId?: string,
): Promise<{
supplierAllocations: SupplierAllocation[];
suppliers: Supplier[];
Expand All @@ -31,6 +32,32 @@ export async function eligibleSuppliers(
volumeGroup.id,
deps,
);
if (letterVariantSupplierId) {
deps.logger.info({
description: "Filtering allocations for letter variant supplier",
volumeGroupId: volumeGroup.id,
letterVariantSupplierId,
});
const filteredAllocations = supplierAllocations.filter(
(alloc) => alloc.supplier === letterVariantSupplierId,
);
if (filteredAllocations.length === 0) {
deps.logger.warn({
description:
"No allocations found for specified letter variant supplier",
volumeGroupId: volumeGroup.id,
letterVariantSupplierId,
});
}
return {
supplierAllocations: filteredAllocations,
suppliers: await getSupplierDetails(
filteredAllocations.map((alloc) => alloc.supplier),
deps,
),
};
}

const allocationPercentageSum = supplierAllocations.reduce(
(sum, alloc) => sum + alloc.allocationPercentage,
0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ test.describe("Supplier Allocation Tests", () => {
logger.info(
`New total daily allocation for date ${allocationDate}: ${newTotalDailyAllocation}`,
);
expect(newTotalDailyAllocation).toBe(originalTotalDailyAllocation + 1);
expect(newTotalDailyAllocation).toBeGreaterThanOrEqual(
originalTotalDailyAllocation + 1,
);
});
});
Loading