Skip to content

Commit 4b98352

Browse files
review changes
1 parent ed3cdb7 commit 4b98352

8 files changed

Lines changed: 100 additions & 152 deletions

File tree

internal/datastore/src/__test__/supplier-config-repository.test.ts

Lines changed: 2 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
import { Logger } from "pino";
21
import { PutCommand } from "@aws-sdk/lib-dynamodb";
32
import {
43
DBContext,
54
createTables,
65
deleteTables,
76
setupDynamoDBContainer,
87
} from "./db";
9-
import { LogStream, createTestLogger } from "./logs";
108
import { SupplierConfigRepository } from "../supplier-config-repository";
119

1210
function createLetterVariantItem(variantId: string) {
@@ -26,10 +24,10 @@ function createLetterVariantItem(variantId: string) {
2624
function createVolumeGroupItem(groupId: string, status = "PROD") {
2725
const startDate = new Date(Date.now() - 24 * 1000 * 60 * 60)
2826
.toISOString()
29-
.split("T")[0]; // Started an hour ago to ensure it's active based on start date. Tests can override this if needed.
27+
.split("T")[0]; // Started a day ago to ensure it's active based on start date. Tests can override this if needed.
3028
const endDate = new Date(Date.now() + 24 * 1000 * 60 * 60)
3129
.toISOString()
32-
.split("T")[0]; // Ends in an hour to ensure it's active based on end date. Tests can override this if needed.
30+
.split("T")[0]; // Ends in a day to ensure it's active based on end date. Tests can override this if needed.
3331
return {
3432
PK: "VOLUME_GROUP",
3533
SK: groupId,
@@ -75,8 +73,6 @@ jest.setTimeout(30_000);
7573
describe("SupplierConfigRepository", () => {
7674
let dbContext: DBContext;
7775
let repository: SupplierConfigRepository;
78-
let logStream: LogStream;
79-
let logger: Logger;
8076

8177
// Database tests can take longer, especially with setup and teardown
8278
beforeAll(async () => {
@@ -85,10 +81,8 @@ describe("SupplierConfigRepository", () => {
8581

8682
beforeEach(async () => {
8783
await createTables(dbContext);
88-
({ logStream, logger } = createTestLogger());
8984
repository = new SupplierConfigRepository(
9085
dbContext.docClient,
91-
logger,
9286
dbContext.config,
9387
);
9488
});
@@ -120,8 +114,6 @@ describe("SupplierConfigRepository", () => {
120114
expect(result.status).toBe("PROD");
121115
expect(result.volumeGroupId).toBe(`group-${variantId}`);
122116
expect(result.packSpecificationIds).toEqual([`pack-spec-${variantId}`]);
123-
124-
expect(logStream.logs).toEqual([]);
125117
});
126118

127119
test("getLetterVariant throws error for non-existent variant", async () => {
@@ -130,12 +122,6 @@ describe("SupplierConfigRepository", () => {
130122
await expect(repository.getLetterVariant(variantId)).rejects.toThrow(
131123
`No letter variant details found for id ${variantId}`,
132124
);
133-
134-
expect(
135-
logStream.logs.some((log) =>
136-
log.includes("No letter variant found for id"),
137-
),
138-
).toBe(true);
139125
});
140126

141127
test("getVolumeGroup returns correct details for existing group", async () => {
@@ -155,8 +141,6 @@ describe("SupplierConfigRepository", () => {
155141
expect(result.status).toBe("PROD");
156142
expect(new Date(result.startDate).getTime()).toBeLessThan(Date.now());
157143
expect(new Date(result.endDate!).getTime()).toBeGreaterThan(Date.now());
158-
159-
expect(logStream.logs).toEqual([]);
160144
});
161145

162146
test("getVolumeGroup throws error for non-existent group", async () => {
@@ -165,12 +149,6 @@ describe("SupplierConfigRepository", () => {
165149
await expect(repository.getVolumeGroup(groupId)).rejects.toThrow(
166150
`No volume group details found for id ${groupId}`,
167151
);
168-
169-
expect(
170-
logStream.logs.some((log) =>
171-
log.includes("No volume group found for id"),
172-
),
173-
).toBe(true);
174152
});
175153

176154
test("getSupplierAllocationsForVolumeGroup returns correct allocations", async () => {
@@ -197,8 +175,6 @@ describe("SupplierConfigRepository", () => {
197175
allocationPercentage: 50,
198176
},
199177
]);
200-
201-
expect(logStream.logs).toEqual([]);
202178
});
203179

204180
test("getSupplierAllocationsForVolumeGroup returns multiple allocations", async () => {
@@ -245,7 +221,6 @@ describe("SupplierConfigRepository", () => {
245221
},
246222
]),
247223
);
248-
expect(logStream.logs).toEqual([]);
249224
});
250225

251226
test("getSupplierAllocationsForVolumeGroup throws error for non-existent group", async () => {
@@ -256,12 +231,6 @@ describe("SupplierConfigRepository", () => {
256231
).rejects.toThrow(
257232
`No active supplier allocations found for volume group id ${groupId}`,
258233
);
259-
260-
expect(
261-
logStream.logs.some((log) =>
262-
log.includes("No supplier allocations found for volume group id"),
263-
),
264-
).toBe(true);
265234
});
266235

267236
test("getSuppliersDetails returns correct supplier details", async () => {
@@ -285,7 +254,6 @@ describe("SupplierConfigRepository", () => {
285254
status: "PROD",
286255
},
287256
]);
288-
expect(logStream.logs).toEqual([]);
289257
});
290258

291259
test("getSuppliersDetails throws error for non-existent supplier", async () => {
@@ -294,9 +262,5 @@ describe("SupplierConfigRepository", () => {
294262
await expect(repository.getSuppliersDetails([supplierId])).rejects.toThrow(
295263
`Supplier with id ${supplierId} not found`,
296264
);
297-
298-
expect(
299-
logStream.logs.some((log) => log.includes("No supplier found for id")),
300-
).toBe(true);
301265
});
302266
});

internal/datastore/src/supplier-config-repository.ts

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import {
33
GetCommand,
44
QueryCommand,
55
} from "@aws-sdk/lib-dynamodb";
6-
import { Logger } from "pino";
76
import {
87
$LetterVariant,
98
$Supplier,
@@ -22,7 +21,6 @@ export type SupplierConfigRepositoryConfig = {
2221
export class SupplierConfigRepository {
2322
constructor(
2423
readonly ddbClient: DynamoDBDocumentClient,
25-
readonly log: Logger,
2624
readonly config: SupplierConfigRepositoryConfig,
2725
) {}
2826

@@ -34,10 +32,6 @@ export class SupplierConfigRepository {
3432
}),
3533
);
3634
if (!result.Item) {
37-
this.log.error({
38-
description: "No letter variant found for id",
39-
variantId,
40-
});
4135
throw new Error(`No letter variant details found for id ${variantId}`);
4236
}
4337

@@ -52,10 +46,6 @@ export class SupplierConfigRepository {
5246
}),
5347
);
5448
if (!result.Item) {
55-
this.log.error({
56-
description: "No volume group found for id",
57-
groupId,
58-
});
5949
throw new Error(`No volume group details found for id ${groupId}`);
6050
}
6151
return $VolumeGroup.parse(result.Item);
@@ -83,10 +73,6 @@ export class SupplierConfigRepository {
8373
}),
8474
);
8575
if (!result.Items || result.Items.length === 0) {
86-
this.log.error({
87-
description: "No supplier allocations found for volume group id",
88-
groupId,
89-
});
9076
throw new Error(
9177
`No active supplier allocations found for volume group id ${groupId}`,
9278
);
@@ -105,10 +91,6 @@ export class SupplierConfigRepository {
10591
}),
10692
);
10793
if (!result.Item) {
108-
this.log.error({
109-
description: "No supplier found for id",
110-
supplierId,
111-
});
11294
throw new Error(`Supplier with id ${supplierId} not found`);
11395
}
11496
suppliers.push($Supplier.parse(result.Item));

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ describe("createDependenciesContainer", () => {
4747
expect(createLogger).toHaveBeenCalledTimes(1);
4848
expect(SupplierConfigRepository).toHaveBeenCalledTimes(1);
4949
const supplierConfigRepoCtorArgs = SupplierConfigRepository.mock.calls[0];
50-
expect(supplierConfigRepoCtorArgs[2]).toEqual({
50+
expect(supplierConfigRepoCtorArgs[1]).toEqual({
5151
supplierConfigTableName: "SupplierConfigTable",
5252
});
5353
expect(deps.env).toEqual(env);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ function createSupplierConfigRepository(
2727
supplierConfigTableName: envVars.SUPPLIER_CONFIG_TABLE_NAME,
2828
};
2929

30-
return new SupplierConfigRepository(createDocumentClient(), log, config);
30+
return new SupplierConfigRepository(createDocumentClient(), config);
3131
}
3232

3333
export function createDependenciesContainer(): Deps {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ async function getSupplierFromConfig(letterEvent: PreparedEvents, deps: Deps) {
7171
const supplierAllocations: SupplierAllocation[] =
7272
await getSupplierAllocationsForVolumeGroup(
7373
variantDetails.volumeGroupId,
74-
variantDetails.supplierId ?? "",
7574
deps,
75+
variantDetails.supplierId,
7676
);
7777

7878
const supplierDetails: Supplier[] = await getSupplierDetails(

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

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@ import {
55
getVolumeGroupDetails,
66
} from "../supplier-config";
77
import { Deps } from "../../config/deps";
8+
import { warn } from "node:console";
89

910
function makeDeps(overrides: Partial<Deps> = {}): Deps {
1011
const logger = {
1112
info: jest.fn(),
1213
error: jest.fn(),
14+
warn: jest.fn(),
1315
} as unknown as Deps["logger"];
1416

1517
const supplierConfigRepo = {
@@ -146,7 +148,7 @@ describe("supplier-config service", () => {
146148
.fn()
147149
.mockResolvedValue(allocations);
148150

149-
const result = await getSupplierAllocationsForVolumeGroup("g1", "", deps);
151+
const result = await getSupplierAllocationsForVolumeGroup("g1", deps);
150152

151153
expect(result).toEqual(allocations);
152154
});
@@ -159,8 +161,8 @@ describe("supplier-config service", () => {
159161

160162
const result = await getSupplierAllocationsForVolumeGroup(
161163
"g1",
162-
"s2",
163164
deps,
165+
"s2",
164166
);
165167

166168
expect(result).toEqual([allocations[1]]);
@@ -173,7 +175,7 @@ describe("supplier-config service", () => {
173175
.mockResolvedValue(allocations);
174176

175177
await expect(
176-
getSupplierAllocationsForVolumeGroup("g1", "missing", deps),
178+
getSupplierAllocationsForVolumeGroup("g1", deps, "missing"),
177179
).rejects.toThrow(/No supplier allocations found/);
178180
expect(deps.logger.error).toHaveBeenCalled();
179181
});
@@ -240,4 +242,47 @@ describe("supplier-config service", () => {
240242
]);
241243
});
242244
});
245+
it("logs a warning when supplier allocations count differs from supplier details count", async () => {
246+
const allocations = [
247+
{ supplier: "s1", variantId: "v1" },
248+
{ supplier: "s2", variantId: "v2" },
249+
{ supplier: "s3", variantId: "v3" },
250+
] as any[];
251+
const suppliers = [
252+
{ id: "s1", name: "Supplier 1" },
253+
{ id: "s2", name: "Supplier 2" },
254+
] as any[];
255+
const deps = makeDeps();
256+
deps.supplierConfigRepo.getSuppliersDetails = jest
257+
.fn()
258+
.mockResolvedValue(suppliers);
259+
260+
await getSupplierDetails(allocations, deps);
261+
262+
expect(deps.logger.warn).toHaveBeenCalledWith({
263+
description: "Mismatch between supplier allocations and supplier details",
264+
allocationsCount: 3,
265+
detailsCount: 2,
266+
missingSuppliers: ["s3"],
267+
});
268+
});
269+
270+
it("does not log a warning when counts match", async () => {
271+
const allocations = [
272+
{ supplier: "s1", variantId: "v1" },
273+
{ supplier: "s2", variantId: "v2" },
274+
] as any[];
275+
const suppliers = [
276+
{ id: "s1", name: "Supplier 1" },
277+
{ id: "s2", name: "Supplier 2" },
278+
] as any[];
279+
const deps = makeDeps();
280+
deps.supplierConfigRepo.getSuppliersDetails = jest
281+
.fn()
282+
.mockResolvedValue(suppliers);
283+
284+
await getSupplierDetails(allocations, deps);
285+
286+
expect(deps.logger.warn).not.toHaveBeenCalled();
287+
});
243288
});

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ export async function getVolumeGroupDetails(
4141

4242
export async function getSupplierAllocationsForVolumeGroup(
4343
groupId: string,
44-
supplierId: string,
4544
deps: Deps,
45+
supplierId?: string,
4646
): Promise<SupplierAllocation[]> {
4747
const allocations =
4848
await deps.supplierConfigRepo.getSupplierAllocationsForVolumeGroup(groupId);
@@ -86,5 +86,18 @@ export async function getSupplierDetails(
8686
`No supplier details found for supplier ids ${supplierIds.join(", ")}`,
8787
);
8888
}
89+
// Log a warning if some supplier details are missing compared to allocations
90+
if (supplierAllocations.length !== supplierDetails.length) {
91+
const foundSupplierIds = new Set(supplierDetails.map((s) => s.id));
92+
const missingSupplierIds = supplierIds.filter(
93+
(id) => !foundSupplierIds.has(id),
94+
);
95+
deps.logger.warn({
96+
description: "Mismatch between supplier allocations and supplier details",
97+
allocationsCount: supplierAllocations.length,
98+
detailsCount: supplierDetails.length,
99+
missingSuppliers: missingSupplierIds,
100+
});
101+
}
89102
return supplierDetails;
90103
}

0 commit comments

Comments
 (0)