Skip to content

Commit bf8d500

Browse files
bug fixes
1 parent e1ab7cd commit bf8d500

2 files changed

Lines changed: 103 additions & 35 deletions

File tree

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

Lines changed: 72 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 = {
@@ -133,6 +122,23 @@ describe("supplier-config service", () => {
133122
);
134123
expect(deps.logger.error).toHaveBeenCalled();
135124
});
125+
it("returns group details when start date and end date are both today", async () => {
126+
const today = new Date().toISOString();
127+
const group = {
128+
id: "g4",
129+
status: "PROD",
130+
startDate: today,
131+
endDate: today,
132+
} as any;
133+
const deps = makeDeps();
134+
deps.supplierConfigRepo.getVolumeGroup = jest
135+
.fn()
136+
.mockResolvedValue(group);
137+
138+
const result = await getVolumeGroupDetails("g4", deps);
139+
140+
expect(result).toBe(group);
141+
});
136142
});
137143

138144
describe("getSupplierAllocationsForVolumeGroup", () => {
@@ -187,8 +193,8 @@ describe("supplier-config service", () => {
187193
{ supplier: "s2", variantId: "v2" },
188194
] as any[];
189195
const suppliers = [
190-
{ id: "s1", name: "Supplier 1" },
191-
{ id: "s2", name: "Supplier 2" },
196+
{ id: "s1", name: "Supplier 1", status: "PROD" },
197+
{ id: "s2", name: "Supplier 2", status: "PROD" },
192198
] as any[];
193199
const deps = makeDeps();
194200
deps.supplierConfigRepo.getSuppliersDetails = jest
@@ -223,9 +229,9 @@ describe("supplier-config service", () => {
223229
{ supplier: "s5", variantId: "v3" },
224230
] as any[];
225231
const suppliers = [
226-
{ id: "s1", name: "Supplier 1" },
227-
{ id: "s3", name: "Supplier 3" },
228-
{ id: "s5", name: "Supplier 5" },
232+
{ id: "s1", name: "Supplier 1", status: "PROD" },
233+
{ id: "s3", name: "Supplier 3", status: "PROD" },
234+
{ id: "s5", name: "Supplier 5", status: "PROD" },
229235
] as any[];
230236
const deps = makeDeps();
231237
deps.supplierConfigRepo.getSuppliersDetails = jest
@@ -248,8 +254,8 @@ describe("supplier-config service", () => {
248254
{ supplier: "s3", variantId: "v3" },
249255
] as any[];
250256
const suppliers = [
251-
{ id: "s1", name: "Supplier 1" },
252-
{ id: "s2", name: "Supplier 2" },
257+
{ id: "s1", name: "Supplier 1", status: "PROD" },
258+
{ id: "s2", name: "Supplier 2", status: "PROD" },
253259
] as any[];
254260
const deps = makeDeps();
255261
deps.supplierConfigRepo.getSuppliersDetails = jest
@@ -272,8 +278,8 @@ describe("supplier-config service", () => {
272278
{ supplier: "s2", variantId: "v2" },
273279
] as any[];
274280
const suppliers = [
275-
{ id: "s1", name: "Supplier 1" },
276-
{ id: "s2", name: "Supplier 2" },
281+
{ id: "s1", name: "Supplier 1", status: "PROD" },
282+
{ id: "s2", name: "Supplier 2", status: "PROD" },
277283
] as any[];
278284
const deps = makeDeps();
279285
deps.supplierConfigRepo.getSuppliersDetails = jest
@@ -284,4 +290,50 @@ describe("supplier-config service", () => {
284290

285291
expect(deps.logger.warn).not.toHaveBeenCalled();
286292
});
293+
294+
it("throws when no active suppliers found", async () => {
295+
const allocations = [
296+
{ supplier: "s1", variantId: "v1" },
297+
{ supplier: "s2", variantId: "v2" },
298+
] as any[];
299+
const suppliers = [
300+
{ id: "s1", name: "Supplier 1", status: "DRAFT" },
301+
{ id: "s2", name: "Supplier 2", status: "DRAFT" },
302+
] as any[];
303+
const deps = makeDeps();
304+
deps.supplierConfigRepo.getSuppliersDetails = jest
305+
.fn()
306+
.mockResolvedValue(suppliers);
307+
308+
await expect(getSupplierDetails(allocations, deps)).rejects.toThrow(
309+
/No active suppliers found/,
310+
);
311+
expect(deps.logger.error).toHaveBeenCalledWith(
312+
expect.objectContaining({
313+
description: "No active suppliers found for supplier allocations",
314+
}),
315+
);
316+
});
317+
318+
it("filters to return only active suppliers with PROD status", async () => {
319+
const allocations = [
320+
{ supplier: "s1", variantId: "v1" },
321+
{ supplier: "s2", variantId: "v2" },
322+
{ supplier: "s3", variantId: "v3" },
323+
] as any[];
324+
const suppliers = [
325+
{ id: "s1", name: "Supplier 1", status: "PROD" },
326+
{ id: "s2", name: "Supplier 2", status: "DRAFT" },
327+
{ id: "s3", name: "Supplier 3", status: "PROD" },
328+
] as any[];
329+
const deps = makeDeps();
330+
deps.supplierConfigRepo.getSuppliersDetails = jest
331+
.fn()
332+
.mockResolvedValue(suppliers);
333+
334+
const result = await getSupplierDetails(allocations, deps);
335+
336+
expect(result).toEqual([suppliers[0], suppliers[2]]);
337+
expect(result.every((s) => s.status === "PROD")).toBe(true);
338+
});
287339
});

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

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,30 @@ 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);
24+
25+
const startOfDay = new Date();
26+
startOfDay.setHours(0, 0, 0, 0);
27+
const endOfDay = new Date();
28+
endOfDay.setHours(23, 59, 59, 999);
2329

2430
if (
25-
groupDetails &&
26-
(groupDetails.status !== "PROD" ||
27-
new Date(groupDetails.startDate) > new Date() ||
28-
(groupDetails.endDate && new Date(groupDetails.endDate) < new Date()))
31+
groupDetails.status === "PROD" &&
32+
new Date(groupDetails.startDate) < endOfDay &&
33+
(!groupDetails.endDate || new Date(groupDetails.endDate) >= startOfDay)
2934
) {
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`);
35+
return groupDetails;
3836
}
39-
return groupDetails;
37+
38+
deps.logger.error({
39+
description: "Volume group is not active based on status and dates",
40+
groupId,
41+
status: groupDetails.status,
42+
startDate: groupDetails.startDate,
43+
endDate: groupDetails.endDate,
44+
});
45+
throw new Error(`Volume group with id ${groupId} is not active`);
4046
}
4147

4248
export async function getSupplierAllocationsForVolumeGroup(
@@ -99,5 +105,15 @@ export async function getSupplierDetails(
99105
missingSuppliers: missingSupplierIds,
100106
});
101107
}
102-
return supplierDetails;
108+
const activeSuppliers = supplierDetails.filter((s) => s.status === "PROD");
109+
if (activeSuppliers.length === 0) {
110+
deps.logger.error({
111+
description: "No active suppliers found for supplier allocations",
112+
supplierIds,
113+
});
114+
throw new Error(
115+
`No active suppliers found for supplier ids ${supplierIds.join(", ")}`,
116+
);
117+
}
118+
return activeSuppliers;
103119
}

0 commit comments

Comments
 (0)