Skip to content

Commit 6a7dc75

Browse files
bug fixes
1 parent e1ab7cd commit 6a7dc75

2 files changed

Lines changed: 81 additions & 35 deletions

File tree

lambdas/supplier-allocator/src/services/__tests__/supplier-config.test.ts

Lines changed: 55 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -72,17 +72,6 @@ describe("supplier-config service", () => {
7272

7373
expect(result).toBe(group);
7474
});
75-
it("logs an error and returns group details when not found", async () => {
76-
const group = undefined;
77-
const deps = makeDeps();
78-
deps.supplierConfigRepo.getVolumeGroup = jest
79-
.fn()
80-
.mockResolvedValue(group);
81-
82-
const result = await getVolumeGroupDetails("missing", deps);
83-
84-
expect(result).toBeUndefined();
85-
});
8675

8776
it("throws when group is not active based on status", async () => {
8877
const group = {
@@ -187,8 +176,8 @@ describe("supplier-config service", () => {
187176
{ supplier: "s2", variantId: "v2" },
188177
] as any[];
189178
const suppliers = [
190-
{ id: "s1", name: "Supplier 1" },
191-
{ id: "s2", name: "Supplier 2" },
179+
{ id: "s1", name: "Supplier 1", status: "PROD" },
180+
{ id: "s2", name: "Supplier 2", status: "PROD" },
192181
] as any[];
193182
const deps = makeDeps();
194183
deps.supplierConfigRepo.getSuppliersDetails = jest
@@ -223,9 +212,9 @@ describe("supplier-config service", () => {
223212
{ supplier: "s5", variantId: "v3" },
224213
] as any[];
225214
const suppliers = [
226-
{ id: "s1", name: "Supplier 1" },
227-
{ id: "s3", name: "Supplier 3" },
228-
{ id: "s5", name: "Supplier 5" },
215+
{ id: "s1", name: "Supplier 1", status: "PROD" },
216+
{ id: "s3", name: "Supplier 3", status: "PROD" },
217+
{ id: "s5", name: "Supplier 5", status: "PROD" },
229218
] as any[];
230219
const deps = makeDeps();
231220
deps.supplierConfigRepo.getSuppliersDetails = jest
@@ -248,8 +237,8 @@ describe("supplier-config service", () => {
248237
{ supplier: "s3", variantId: "v3" },
249238
] as any[];
250239
const suppliers = [
251-
{ id: "s1", name: "Supplier 1" },
252-
{ id: "s2", name: "Supplier 2" },
240+
{ id: "s1", name: "Supplier 1", status: "PROD" },
241+
{ id: "s2", name: "Supplier 2", status: "PROD" },
253242
] as any[];
254243
const deps = makeDeps();
255244
deps.supplierConfigRepo.getSuppliersDetails = jest
@@ -272,8 +261,8 @@ describe("supplier-config service", () => {
272261
{ supplier: "s2", variantId: "v2" },
273262
] as any[];
274263
const suppliers = [
275-
{ id: "s1", name: "Supplier 1" },
276-
{ id: "s2", name: "Supplier 2" },
264+
{ id: "s1", name: "Supplier 1", status: "PROD" },
265+
{ id: "s2", name: "Supplier 2", status: "PROD" },
277266
] as any[];
278267
const deps = makeDeps();
279268
deps.supplierConfigRepo.getSuppliersDetails = jest
@@ -284,4 +273,50 @@ describe("supplier-config service", () => {
284273

285274
expect(deps.logger.warn).not.toHaveBeenCalled();
286275
});
276+
277+
it("throws when no active suppliers found", async () => {
278+
const allocations = [
279+
{ supplier: "s1", variantId: "v1" },
280+
{ supplier: "s2", variantId: "v2" },
281+
] as any[];
282+
const suppliers = [
283+
{ id: "s1", name: "Supplier 1", status: "DRAFT" },
284+
{ id: "s2", name: "Supplier 2", status: "DRAFT" },
285+
] as any[];
286+
const deps = makeDeps();
287+
deps.supplierConfigRepo.getSuppliersDetails = jest
288+
.fn()
289+
.mockResolvedValue(suppliers);
290+
291+
await expect(getSupplierDetails(allocations, deps)).rejects.toThrow(
292+
/No active suppliers found/,
293+
);
294+
expect(deps.logger.error).toHaveBeenCalledWith(
295+
expect.objectContaining({
296+
description: "No active suppliers found for supplier allocations",
297+
}),
298+
);
299+
});
300+
301+
it("filters to return only active suppliers with PROD status", async () => {
302+
const allocations = [
303+
{ supplier: "s1", variantId: "v1" },
304+
{ supplier: "s2", variantId: "v2" },
305+
{ supplier: "s3", variantId: "v3" },
306+
] as any[];
307+
const suppliers = [
308+
{ id: "s1", name: "Supplier 1", status: "PROD" },
309+
{ id: "s2", name: "Supplier 2", status: "DRAFT" },
310+
{ id: "s3", name: "Supplier 3", status: "PROD" },
311+
] as any[];
312+
const deps = makeDeps();
313+
deps.supplierConfigRepo.getSuppliersDetails = jest
314+
.fn()
315+
.mockResolvedValue(suppliers);
316+
317+
const result = await getSupplierDetails(allocations, deps);
318+
319+
expect(result).toEqual([suppliers[0], suppliers[2]]);
320+
expect(result.every((s) => s.status === "PROD")).toBe(true);
321+
});
287322
});

lambdas/supplier-allocator/src/services/supplier-config.ts

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,25 @@ export async function getVolumeGroupDetails(
1919
groupId: string,
2020
deps: Deps,
2121
): Promise<VolumeGroup> {
22-
const groupDetails = await deps.supplierConfigRepo.getVolumeGroup(groupId);
22+
const groupDetails: VolumeGroup =
23+
await deps.supplierConfigRepo.getVolumeGroup(groupId);
2324

2425
if (
25-
groupDetails &&
26-
(groupDetails.status !== "PROD" ||
27-
new Date(groupDetails.startDate) > new Date() ||
28-
(groupDetails.endDate && new Date(groupDetails.endDate) < new Date()))
26+
groupDetails.status === "PROD" &&
27+
new Date(groupDetails.startDate) <= new Date() &&
28+
(!groupDetails.endDate || new Date(groupDetails.endDate) >= new Date())
2929
) {
30-
deps.logger.error({
31-
description: "Volume group is not active based on status and dates",
32-
groupId,
33-
status: groupDetails.status,
34-
startDate: groupDetails.startDate,
35-
endDate: groupDetails.endDate,
36-
});
37-
throw new Error(`Volume group with id ${groupId} is not active`);
30+
return groupDetails;
3831
}
39-
return groupDetails;
32+
33+
deps.logger.error({
34+
description: "Volume group is not active based on status and dates",
35+
groupId,
36+
status: groupDetails.status,
37+
startDate: groupDetails.startDate,
38+
endDate: groupDetails.endDate,
39+
});
40+
throw new Error(`Volume group with id ${groupId} is not active`);
4041
}
4142

4243
export async function getSupplierAllocationsForVolumeGroup(
@@ -99,5 +100,15 @@ export async function getSupplierDetails(
99100
missingSuppliers: missingSupplierIds,
100101
});
101102
}
102-
return supplierDetails;
103+
const activeSuppliers = supplierDetails.filter((s) => s.status === "PROD");
104+
if (activeSuppliers.length === 0) {
105+
deps.logger.error({
106+
description: "No active suppliers found for supplier allocations",
107+
supplierIds,
108+
});
109+
throw new Error(
110+
`No active suppliers found for supplier ids ${supplierIds.join(", ")}`,
111+
);
112+
}
113+
return activeSuppliers;
103114
}

0 commit comments

Comments
 (0)