Skip to content

Commit 430dc4e

Browse files
CCM-20032: Letter Variant SupplierId Not Honoured
1 parent a714c75 commit 430dc4e

3 files changed

Lines changed: 69 additions & 1 deletion

File tree

lambdas/supplier-allocator/src/handler/__tests__/allocation-config.test.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,37 @@ describe("eligibleSuppliers", () => {
196196
"Supplier service error",
197197
);
198198
});
199+
it("should filter allocations by letterVariantSupplierId if provided", async () => {
200+
(
201+
supplierConfigService.getSupplierAllocationsForVolumeGroup as jest.Mock
202+
).mockResolvedValue(mockSupplierAllocations);
203+
(supplierConfigService.getSupplierDetails as jest.Mock).mockResolvedValue(
204+
mockSuppliers,
205+
);
206+
207+
const letterVariantSupplierId = "supplier-1";
208+
await eligibleSuppliers(mockVolumeGroup, mockDeps, letterVariantSupplierId);
209+
expect(supplierConfigService.getSupplierDetails).toHaveBeenCalledWith(
210+
["supplier-1"],
211+
mockDeps,
212+
);
213+
});
214+
it("should log a warning if no allocations found for specified letter variant supplier", async () => {
215+
(
216+
supplierConfigService.getSupplierAllocationsForVolumeGroup as jest.Mock
217+
).mockResolvedValue([]);
218+
(supplierConfigService.getSupplierDetails as jest.Mock).mockResolvedValue(
219+
[],
220+
);
221+
222+
const letterVariantSupplierId = "supplier-1";
223+
await eligibleSuppliers(mockVolumeGroup, mockDeps, letterVariantSupplierId);
224+
expect(mockDeps.logger.warn).toHaveBeenCalledWith({
225+
description: "No allocations found for specified letter variant supplier",
226+
volumeGroupId: mockVolumeGroup.id,
227+
letterVariantSupplierId,
228+
});
229+
});
199230
});
200231

201232
describe("preferredSupplierPack", () => {

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,18 @@ async function getSupplierFromConfig(
9292
letterVariant.volumeGroupId,
9393
deps,
9494
);
95+
console.log(
96+
`Volume group details for ${letterVariant.volumeGroupId}: ${JSON.stringify(volumeGroup)}`,
97+
);
9598

9699
const { supplierAllocations, suppliers: allocatedSuppliers } =
97-
await eligibleSuppliers(volumeGroup, deps);
100+
await eligibleSuppliers(volumeGroup, deps, letterVariant.supplierId);
101+
102+
console.log(
103+
`Eligible suppliers for volume group ${volumeGroup.id}: ${JSON.stringify(
104+
allocatedSuppliers,
105+
)}`,
106+
);
98107

99108
const preferredPack: PackSpecification = await preferredSupplierPack(
100109
letterEvent,
@@ -156,6 +165,9 @@ async function getSupplierFromConfig(
156165
volumeGroup.id,
157166
);
158167
} catch (error) {
168+
console.log(
169+
`Error fetching supplier from config for letter variant ${letterEvent.data.letterVariantId}: ${error}`,
170+
);
159171
deps.logger.error({
160172
description: "Error fetching supplier from config",
161173
err: error,
@@ -391,6 +403,9 @@ export default function createSupplierAllocatorHandler(deps: Deps): SQSHandler {
391403

392404
({ priority, supplier } = supplierAllocationResult);
393405
} catch (error) {
406+
console.log(
407+
`Error processing allocation of record ${record.messageId}: ${error}`,
408+
);
394409
deps.logger.error({
395410
description: "Error processing allocation of record",
396411
err: error,

lambdas/supplier-allocator/src/handler/allocation-config.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import { PreparedEvents } from "./types";
2323
export async function eligibleSuppliers(
2424
volumeGroup: VolumeGroup,
2525
deps: Deps,
26+
letterVariantSupplierId?: string,
2627
): Promise<{
2728
supplierAllocations: SupplierAllocation[];
2829
suppliers: Supplier[];
@@ -31,6 +32,27 @@ export async function eligibleSuppliers(
3132
volumeGroup.id,
3233
deps,
3334
);
35+
if (letterVariantSupplierId) {
36+
const filteredAllocations = supplierAllocations.filter(
37+
(alloc) => alloc.supplier === letterVariantSupplierId,
38+
);
39+
if (filteredAllocations.length === 0) {
40+
deps.logger.warn({
41+
description:
42+
"No allocations found for specified letter variant supplier",
43+
volumeGroupId: volumeGroup.id,
44+
letterVariantSupplierId,
45+
});
46+
}
47+
return {
48+
supplierAllocations: filteredAllocations,
49+
suppliers: await getSupplierDetails(
50+
filteredAllocations.map((alloc) => alloc.supplier),
51+
deps,
52+
),
53+
};
54+
}
55+
3456
const allocationPercentageSum = supplierAllocations.reduce(
3557
(sum, alloc) => sum + alloc.allocationPercentage,
3658
0,

0 commit comments

Comments
 (0)