Skip to content

Commit ef6ff09

Browse files
add check for illogical allocation percentage
1 parent 9937ac0 commit ef6ff09

2 files changed

Lines changed: 100 additions & 5 deletions

File tree

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

Lines changed: 77 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@ describe("eligibleSuppliers", () => {
5050
allocationPercentage: 30,
5151
status: "PROD",
5252
} as SupplierAllocation,
53+
{
54+
id: "allocation-3",
55+
volumeGroup: "volume-group-1",
56+
supplier: "supplier-3",
57+
allocationPercentage: 20,
58+
status: "PROD",
59+
} as SupplierAllocation,
5360
];
5461

5562
mockSuppliers = [
@@ -66,7 +73,7 @@ describe("eligibleSuppliers", () => {
6673
];
6774

6875
mockDeps = {
69-
logger: { info: jest.fn(), error: jest.fn() },
76+
logger: { info: jest.fn(), error: jest.fn(), warn: jest.fn() },
7077
} as unknown as jest.Mocked<Deps>;
7178
});
7279

@@ -110,7 +117,7 @@ describe("eligibleSuppliers", () => {
110117
await eligibleSuppliers(mockVolumeGroup, mockDeps);
111118

112119
expect(supplierConfigService.getSupplierDetails).toHaveBeenCalledWith(
113-
["supplier-1", "supplier-2"],
120+
["supplier-1", "supplier-2", "supplier-3"],
114121
mockDeps,
115122
);
116123
});
@@ -133,6 +140,38 @@ describe("eligibleSuppliers", () => {
133140
);
134141
});
135142

143+
it("should log a warning if allocation percentages do not sum to 100%", async () => {
144+
const invalidAllocations = [
145+
{
146+
id: "allocation-1",
147+
volumeGroup: "volume-group-1",
148+
supplier: "supplier-1",
149+
allocationPercentage: 40,
150+
status: "PROD",
151+
} as SupplierAllocation,
152+
{
153+
id: "allocation-2",
154+
volumeGroup: "volume-group-1",
155+
supplier: "supplier-2",
156+
157+
allocationPercentage: 30,
158+
status: "PROD",
159+
} as SupplierAllocation,
160+
];
161+
(
162+
supplierConfigService.getSupplierAllocationsForVolumeGroup as jest.Mock
163+
).mockResolvedValue(invalidAllocations);
164+
(supplierConfigService.getSupplierDetails as jest.Mock).mockResolvedValue(
165+
mockSuppliers,
166+
);
167+
await eligibleSuppliers(mockVolumeGroup, mockDeps);
168+
expect(mockDeps.logger.warn).toHaveBeenCalledWith({
169+
description: "Supplier allocations do not sum to 100%",
170+
volumeGroupId: "volume-group-1",
171+
allocationPercentageSum: 70,
172+
});
173+
});
174+
136175
it("should propagate errors from getSupplierAllocationsForVolumeGroup", async () => {
137176
const error = new Error("Database error");
138177
(
@@ -804,6 +843,42 @@ describe("selectSupplierByFactor", () => {
804843
expect(result).toBe("supplier-2");
805844
});
806845

846+
it("should log an error if a supplier allocation has zero percentage and exclude it from factor calculation", async () => {
847+
const allocationsWithZeroPercentage = [
848+
mockSupplierAllocations[0],
849+
{
850+
id: "allocation-zero",
851+
volumeGroup: "volume-group-1",
852+
supplier: "supplier-2",
853+
allocationPercentage: 0,
854+
status: "PROD",
855+
} as SupplierAllocation,
856+
mockSupplierAllocations[2],
857+
];
858+
859+
const mockSupplierFactors = [
860+
{ supplierId: "supplier-1", factor: 0.5 },
861+
{ supplierId: "supplier-3", factor: 0.8 },
862+
];
863+
864+
(
865+
supplierQuotasService.calculateSupplierAllocatedFactor as jest.Mock
866+
).mockResolvedValue(mockSupplierFactors);
867+
868+
const result = await selectSupplierByFactor(
869+
mockSuppliers,
870+
allocationsWithZeroPercentage,
871+
mockDeps,
872+
);
873+
874+
expect(mockDeps.logger.error).toHaveBeenCalledWith({
875+
description: "Supplier allocation has zero percentage",
876+
supplierId: "supplier-2",
877+
allocationPercentage: 0,
878+
});
879+
expect(result).toBe("supplier-1");
880+
});
881+
807882
it("should return first supplier when all factors are equal", async () => {
808883
const mockSupplierFactors = [
809884
{ supplierId: "supplier-1", factor: 0.5 },

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

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,18 @@ export async function eligibleSuppliers(
3131
volumeGroup.id,
3232
deps,
3333
);
34+
const allocationPercentageSum = supplierAllocations.reduce(
35+
(sum, alloc) => sum + alloc.allocationPercentage,
36+
0,
37+
);
38+
console.log("Allocation percentage sum:", allocationPercentageSum);
39+
if (allocationPercentageSum !== 100) {
40+
deps.logger.warn({
41+
description: "Supplier allocations do not sum to 100%",
42+
volumeGroupId: volumeGroup.id,
43+
allocationPercentageSum,
44+
});
45+
}
3446
const supplierIds = supplierAllocations.map((alloc) => alloc.supplier);
3547

3648
const suppliers = await getSupplierDetails(supplierIds, deps);
@@ -103,9 +115,17 @@ export async function selectSupplierByFactor(
103115
supplierAllocations: SupplierAllocation[],
104116
deps: Deps,
105117
): Promise<string> {
106-
const supplierAllocationsForPack = supplierAllocations.filter((alloc) =>
107-
suppliers.some((supplier) => supplier.id === alloc.supplier),
108-
);
118+
const supplierAllocationsForPack = supplierAllocations.filter((alloc) => {
119+
if (alloc.allocationPercentage === 0) {
120+
deps.logger.error({
121+
description: "Supplier allocation has zero percentage",
122+
supplierId: alloc.supplier,
123+
allocationPercentage: alloc.allocationPercentage,
124+
});
125+
return false;
126+
}
127+
return suppliers.some((supplier) => supplier.id === alloc.supplier);
128+
});
109129
const supplierFactors: { supplierId: string; factor: number }[] =
110130
await calculateSupplierAllocatedFactor(supplierAllocationsForPack, deps);
111131

0 commit comments

Comments
 (0)