Skip to content

Commit 22d2070

Browse files
import schemas from nhs-notify-event-schemas-supplier-config
1 parent 9f8981d commit 22d2070

7 files changed

Lines changed: 228 additions & 36 deletions

File tree

internal/datastore/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"@aws-sdk/client-dynamodb": "^3.984.0",
44
"@aws-sdk/lib-dynamodb": "^3.1008.0",
55
"@internal/helpers": "*",
6+
"@nhsdigital/nhs-notify-event-schemas-supplier-config": "^1.0.1",
67
"pino": "^10.3.0",
78
"zod": "^4.1.11",
89
"zod-mermaid": "^1.0.9"

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
Supplier,
1414
SupplierAllocation,
1515
VolumeGroup,
16-
} from "./SupplierConfigDomain";
16+
} from "@nhsdigital/nhs-notify-event-schemas-supplier-config";
1717

1818
export type SupplierConfigRepositoryConfig = {
1919
supplierConfigTableName: string;

lambdas/supplier-allocator/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"@nhsdigital/nhs-notify-event-schemas-letter-rendering": "^2.0.1",
99
"@nhsdigital/nhs-notify-event-schemas-letter-rendering-v1": "npm:@nhsdigital/nhs-notify-event-schemas-letter-rendering@^1.1.5",
1010
"@nhsdigital/nhs-notify-event-schemas-supplier-api": "^1.0.8",
11+
"@nhsdigital/nhs-notify-event-schemas-supplier-config": "^1.0.1",
1112
"@types/aws-lambda": "^8.10.148",
1213
"aws-lambda": "^1.0.7",
1314
"esbuild": "^0.27.2",

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
LetterVariant,
66
SupplierAllocation,
77
VolumeGroup,
8-
} from "internal/datastore/src/SupplierConfigDomain";
8+
} from "@nhsdigital/nhs-notify-event-schemas-supplier-config";
99
import { LetterRequestPreparedEventV2 } from "@nhsdigital/nhs-notify-event-schemas-letter-rendering";
1010
import z from "zod";
1111
import {
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
import {
2+
getSupplierAllocationsForVolumeGroup,
3+
getVariantDetails,
4+
getVolumeGroupDetails,
5+
} from "../supplier-config";
6+
import { Deps } from "../../config/deps";
7+
8+
function makeDeps(overrides: Partial<Deps> = {}): Deps {
9+
const logger = {
10+
info: jest.fn(),
11+
error: jest.fn(),
12+
} as unknown as Deps["logger"];
13+
14+
const supplierConfigRepo = {
15+
getLetterVariant: jest.fn(),
16+
getVolumeGroup: jest.fn(),
17+
getSupplierAllocationsForVolumeGroup: jest.fn(),
18+
} as unknown as Deps["supplierConfigRepo"];
19+
20+
const base: Partial<Deps> = {
21+
logger: logger as any,
22+
supplierConfigRepo: supplierConfigRepo as any,
23+
};
24+
25+
return { ...(base as Deps), ...overrides } as Deps;
26+
}
27+
28+
describe("supplier-config service", () => {
29+
afterEach(() => jest.resetAllMocks());
30+
31+
describe("getVariantDetails", () => {
32+
it("returns variant details and logs when found", async () => {
33+
const variant = { id: "v1", volumeGroupId: "g1" } as any;
34+
const deps = makeDeps();
35+
deps.supplierConfigRepo.getLetterVariant = jest
36+
.fn()
37+
.mockResolvedValue(variant);
38+
39+
const result = await getVariantDetails("v1", deps);
40+
41+
expect(result).toBe(variant);
42+
expect(deps.logger.info).toHaveBeenCalled();
43+
});
44+
45+
it("logs an error and returns undefined when not found", async () => {
46+
const deps = makeDeps();
47+
deps.supplierConfigRepo.getLetterVariant = jest.fn().mockResolvedValue();
48+
49+
const result = await getVariantDetails("missing", deps);
50+
51+
expect(result).toBeUndefined();
52+
expect(deps.logger.error).toHaveBeenCalled();
53+
});
54+
});
55+
56+
describe("getVolumeGroupDetails", () => {
57+
it("returns group details when active", async () => {
58+
const group = {
59+
id: "g1",
60+
status: "PROD",
61+
startDate: "2020-01-01",
62+
} as any;
63+
const deps = makeDeps();
64+
deps.supplierConfigRepo.getVolumeGroup = jest
65+
.fn()
66+
.mockResolvedValue(group);
67+
68+
const result = await getVolumeGroupDetails("g1", deps);
69+
70+
expect(result).toBe(group);
71+
expect(deps.logger.info).toHaveBeenCalled();
72+
});
73+
74+
it("throws when group is not active based on dates/status", async () => {
75+
const future = new Date(Date.now() + 1000 * 60 * 60 * 24).toISOString();
76+
const group = { id: "g2", status: "DRAFT", startDate: future } as any;
77+
const deps = makeDeps();
78+
deps.supplierConfigRepo.getVolumeGroup = jest
79+
.fn()
80+
.mockResolvedValue(group);
81+
82+
await expect(getVolumeGroupDetails("g2", deps)).rejects.toThrow(
83+
/not active/,
84+
);
85+
expect(deps.logger.error).toHaveBeenCalled();
86+
});
87+
});
88+
89+
describe("getSupplierAllocationsForVolumeGroup", () => {
90+
const allocations = [
91+
{ supplier: "s1", variantId: "v1" },
92+
{ supplier: "s2", variantId: "v2" },
93+
] as any[];
94+
95+
it("returns all allocations when no supplierId provided", async () => {
96+
const deps = makeDeps();
97+
deps.supplierConfigRepo.getSupplierAllocationsForVolumeGroup = jest
98+
.fn()
99+
.mockResolvedValue(allocations);
100+
101+
const result = await getSupplierAllocationsForVolumeGroup("g1", "", deps);
102+
103+
expect(result).toEqual(allocations);
104+
expect(deps.logger.info).toHaveBeenCalled();
105+
});
106+
107+
it("filters by supplierId when provided", async () => {
108+
const deps = makeDeps();
109+
deps.supplierConfigRepo.getSupplierAllocationsForVolumeGroup = jest
110+
.fn()
111+
.mockResolvedValue(allocations);
112+
113+
const result = await getSupplierAllocationsForVolumeGroup(
114+
"g1",
115+
"s2",
116+
deps,
117+
);
118+
119+
expect(result).toEqual([allocations[1]]);
120+
});
121+
122+
it("throws when supplierId provided but no matching allocation", async () => {
123+
const deps = makeDeps();
124+
deps.supplierConfigRepo.getSupplierAllocationsForVolumeGroup = jest
125+
.fn()
126+
.mockResolvedValue(allocations);
127+
128+
await expect(
129+
getSupplierAllocationsForVolumeGroup("g1", "missing", deps),
130+
).rejects.toThrow(/No supplier allocations found/);
131+
expect(deps.logger.error).toHaveBeenCalled();
132+
});
133+
});
134+
});

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {
33
Supplier,
44
SupplierAllocation,
55
VolumeGroup,
6-
} from "internal/datastore/src/SupplierConfigDomain";
6+
} from "@nhsdigital/nhs-notify-event-schemas-supplier-config";
77
import { Deps } from "../config/deps";
88

99
export async function getVariantDetails(

package-lock.json

Lines changed: 89 additions & 33 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)