Skip to content

Commit f86b0f8

Browse files
added more validity checks
1 parent 0a26a23 commit f86b0f8

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,13 +110,18 @@ export class SupplierConfigRepository {
110110
TableName: this.config.supplierConfigTableName,
111111
IndexName: "packSpecificationId-index",
112112
KeyConditionExpression: "#pk = :pk AND #packSpecId = :packSpecId",
113+
FilterExpression: "#status = :status AND #approval = :approval",
113114
ExpressionAttributeNames: {
114115
"#pk": "PK",
115116
"#packSpecId": "packSpecificationId",
117+
"#status": "status",
118+
"#approval": "approval",
116119
},
117120
ExpressionAttributeValues: {
118121
":pk": "SUPPLIER_PACK",
119122
":packSpecId": packSpecId,
123+
":status": "PROD",
124+
":approval": "APPROVED",
120125
},
121126
}),
122127
);

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

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
getVolumeGroupDetails,
99
} from "../supplier-config";
1010
import { Deps } from "../../config/deps";
11+
import { stat } from "node:fs";
1112

1213
function makeDeps(overrides: Partial<Deps> = {}): Deps {
1314
const logger = {
@@ -419,7 +420,11 @@ describe("supplier-config service", () => {
419420

420421
describe("getPackSpecification", () => {
421422
it("returns pack specification when found", async () => {
422-
const packSpec = { id: "spec1", name: "Pack Spec 1" } as any;
423+
const packSpec = {
424+
id: "spec1",
425+
name: "Pack Spec 1",
426+
status: "PROD",
427+
} as any;
423428
const deps = makeDeps();
424429
deps.supplierConfigRepo.getPackSpecification = jest
425430
.fn()
@@ -429,6 +434,29 @@ describe("supplier-config service", () => {
429434

430435
expect(result).toBe(packSpec);
431436
});
437+
438+
it("throws when pack specification is not active based on status", async () => {
439+
const packSpec = {
440+
id: "spec2",
441+
name: "Pack Spec 2",
442+
status: "DRAFT",
443+
} as any;
444+
const deps = makeDeps();
445+
deps.supplierConfigRepo.getPackSpecification = jest
446+
.fn()
447+
.mockResolvedValue(packSpec);
448+
449+
await expect(getPackSpecification("spec2", deps)).rejects.toThrow(
450+
/not active/,
451+
);
452+
expect(deps.logger.error).toHaveBeenCalledWith(
453+
expect.objectContaining({
454+
description: "Pack specification is not active based on status",
455+
packSpecId: "spec2",
456+
status: "DRAFT",
457+
}),
458+
);
459+
});
432460
});
433461

434462
describe("getSuppliersWithValidPack", () => {

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,14 @@ export async function getPackSpecification(
154154
): Promise<PackSpecification> {
155155
const packSpec =
156156
await deps.supplierConfigRepo.getPackSpecification(packSpecId);
157+
if (packSpec.status !== "PROD") {
158+
deps.logger.error({
159+
description: "Pack specification is not active based on status",
160+
packSpecId,
161+
status: packSpec.status,
162+
});
163+
throw new Error(`Pack specification with id ${packSpecId} is not active`);
164+
}
157165
return packSpec;
158166
}
159167

0 commit comments

Comments
 (0)