Skip to content

Commit 06d9ab8

Browse files
refactor to supplier-config services
1 parent 9cae7fc commit 06d9ab8

2 files changed

Lines changed: 126 additions & 67 deletions

File tree

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

Lines changed: 5 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ import {
88
} from "internal/datastore/src/SupplierConfigDomain";
99
import { LetterRequestPreparedEventV2 } from "@nhsdigital/nhs-notify-event-schemas-letter-rendering";
1010
import z from "zod";
11+
import {
12+
getVariantDetails,
13+
getVolumeGroupDetails,
14+
getSupplierAllocationsForVolumeGroup,
15+
} from "../services/supplier-config";
1116
import { Deps } from "../config/deps";
1217

1318
type SupplierSpec = { supplierId: string; specId: string };
@@ -50,73 +55,6 @@ function validateType(event: unknown) {
5055
}
5156
}
5257

53-
async function getVariantDetails(
54-
variantId: string,
55-
deps: Deps,
56-
): Promise<LetterVariant> {
57-
deps.logger.info({
58-
description: "Fetching letter variant details from database",
59-
variantId,
60-
});
61-
62-
const variantDetails: LetterVariant =
63-
await deps.supplierConfigRepo.getLetterVariant(variantId);
64-
65-
if (variantDetails) {
66-
deps.logger.info({
67-
description: "Fetched letter variant details",
68-
variantId,
69-
variantDetails,
70-
});
71-
} else {
72-
deps.logger.error({
73-
description: "No letter variant found for id",
74-
variantId,
75-
});
76-
}
77-
return variantDetails;
78-
}
79-
80-
async function getVolumeGroupDetails(
81-
groupId: string,
82-
deps: Deps,
83-
): Promise<VolumeGroup> {
84-
deps.logger.info({
85-
description: "Fetching volume group details from database",
86-
groupId,
87-
});
88-
89-
const groupDetails = await deps.supplierConfigRepo.getVolumeGroup(groupId);
90-
if (groupDetails) {
91-
deps.logger.info({
92-
description: "Fetched volume group details",
93-
groupId,
94-
groupDetails,
95-
});
96-
} else {
97-
deps.logger.error({
98-
description: "No volume group found for id",
99-
groupId,
100-
});
101-
}
102-
103-
if (
104-
groupDetails.status !== "PROD" &&
105-
(new Date(groupDetails.startDate) > new Date() ||
106-
(groupDetails.endDate && new Date(groupDetails.endDate) < new Date()))
107-
) {
108-
deps.logger.error({
109-
description: "Volume group is not active based on status and dates",
110-
groupId,
111-
status: groupDetails.status,
112-
startDate: groupDetails.startDate,
113-
endDate: groupDetails.endDate,
114-
});
115-
throw new Error(`Volume group with id ${groupId} is not active`);
116-
}
117-
return groupDetails;
118-
}
119-
12058
async function getSupplierAllocationsForVolumeGroup(
12159
groupId: string,
12260
supplierId: string,
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
import {
2+
LetterVariant,
3+
SupplierAllocation,
4+
VolumeGroup,
5+
} from "internal/datastore/src/SupplierConfigDomain";
6+
import { Deps } from "../config/deps";
7+
8+
9+
export async function getVariantDetails(
10+
variantId: string,
11+
deps: Deps,
12+
): Promise<LetterVariant> {
13+
deps.logger.info({
14+
description: "Fetching letter variant details from database",
15+
variantId,
16+
});
17+
18+
const variantDetails: LetterVariant =
19+
await deps.supplierConfigRepo.getLetterVariant(variantId);
20+
21+
if (variantDetails) {
22+
deps.logger.info({
23+
description: "Fetched letter variant details",
24+
variantId,
25+
variantDetails,
26+
});
27+
} else {
28+
deps.logger.error({
29+
description: "No letter variant found for id",
30+
variantId,
31+
});
32+
}
33+
return variantDetails;
34+
}
35+
36+
export async function getVolumeGroupDetails(
37+
groupId: string,
38+
deps: Deps,
39+
): Promise<VolumeGroup> {
40+
deps.logger.info({
41+
description: "Fetching volume group details from database",
42+
groupId,
43+
});
44+
45+
const groupDetails = await deps.supplierConfigRepo.getVolumeGroup(groupId);
46+
if (groupDetails) {
47+
deps.logger.info({
48+
description: "Fetched volume group details",
49+
groupId,
50+
groupDetails,
51+
});
52+
} else {
53+
deps.logger.error({
54+
description: "No volume group found for id",
55+
groupId,
56+
});
57+
}
58+
59+
if (
60+
groupDetails.status !== "PROD" &&
61+
(new Date(groupDetails.startDate) > new Date() ||
62+
(groupDetails.endDate && new Date(groupDetails.endDate) < new Date()))
63+
) {
64+
deps.logger.error({
65+
description: "Volume group is not active based on status and dates",
66+
groupId,
67+
status: groupDetails.status,
68+
startDate: groupDetails.startDate,
69+
endDate: groupDetails.endDate,
70+
});
71+
throw new Error(`Volume group with id ${groupId} is not active`);
72+
}
73+
return groupDetails;
74+
}
75+
76+
export async function getSupplierAllocationsForVolumeGroup(
77+
groupId: string,
78+
supplierId: string,
79+
deps: Deps,
80+
): Promise<SupplierAllocation[]> {
81+
deps.logger.info({
82+
description: "Fetching supplier allocations for volume group from database",
83+
groupId,
84+
});
85+
const allocations =
86+
await deps.supplierConfigRepo.getSupplierAllocationsForVolumeGroup(groupId);
87+
88+
if (allocations.length > 0) {
89+
deps.logger.info({
90+
description:
91+
"Fetched supplier allocations for volume group from database",
92+
groupId,
93+
count: allocations.length,
94+
});
95+
} else {
96+
deps.logger.error({
97+
description: "No supplier allocations found for volume group id",
98+
groupId,
99+
});
100+
}
101+
102+
if (supplierId) {
103+
const filteredAllocations = allocations.filter(
104+
(alloc) => alloc.supplier === supplierId,
105+
);
106+
if (filteredAllocations.length === 0) {
107+
deps.logger.error({
108+
description:
109+
"No supplier allocations found for variantsupplier id in volume group",
110+
groupId,
111+
supplierId,
112+
});
113+
throw new Error(
114+
`No supplier allocations found for variant supplier id ${supplierId} in volume group ${groupId}`,
115+
);
116+
}
117+
return filteredAllocations;
118+
}
119+
120+
return allocations;
121+
}

0 commit comments

Comments
 (0)