Skip to content

Commit 62ce880

Browse files
refactor to supplier-config services
1 parent 8ffd8ee commit 62ce880

2 files changed

Lines changed: 125 additions & 114 deletions

File tree

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

Lines changed: 5 additions & 114 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+
getSupplierAllocationsForVolumeGroup,
13+
getVariantDetails,
14+
getVolumeGroupDetails,
15+
} from "../services/supplier-config";
1116
import { Deps } from "../config/deps";
1217

1318
type SupplierSpec = { supplierId: string; specId: string };
@@ -50,120 +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-
120-
async function getSupplierAllocationsForVolumeGroup(
121-
groupId: string,
122-
supplierId: string,
123-
deps: Deps,
124-
): Promise<SupplierAllocation[]> {
125-
deps.logger.info({
126-
description: "Fetching supplier allocations for volume group from database",
127-
groupId,
128-
});
129-
const allocations =
130-
await deps.supplierConfigRepo.getSupplierAllocationsForVolumeGroup(groupId);
131-
132-
if (allocations.length > 0) {
133-
deps.logger.info({
134-
description:
135-
"Fetched supplier allocations for volume group from database",
136-
groupId,
137-
count: allocations.length,
138-
});
139-
} else {
140-
deps.logger.error({
141-
description: "No supplier allocations found for volume group id",
142-
groupId,
143-
});
144-
}
145-
146-
if (supplierId) {
147-
const filteredAllocations = allocations.filter(
148-
(alloc) => alloc.supplier === supplierId,
149-
);
150-
if (filteredAllocations.length === 0) {
151-
deps.logger.error({
152-
description:
153-
"No supplier allocations found for variantsupplier id in volume group",
154-
groupId,
155-
supplierId,
156-
});
157-
throw new Error(
158-
`No supplier allocations found for variant supplier id ${supplierId} in volume group ${groupId}`,
159-
);
160-
}
161-
return filteredAllocations;
162-
}
163-
164-
return allocations;
165-
}
166-
16758
async function getSupplierFromConfig(letterEvent: PreparedEvents, deps: Deps) {
16859
const variantDetails: LetterVariant = await getVariantDetails(
16960
letterEvent.data.letterVariantId,
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
import {
2+
LetterVariant,
3+
SupplierAllocation,
4+
VolumeGroup,
5+
} from "internal/datastore/src/SupplierConfigDomain";
6+
import { Deps } from "../config/deps";
7+
8+
export async function getVariantDetails(
9+
variantId: string,
10+
deps: Deps,
11+
): Promise<LetterVariant> {
12+
deps.logger.info({
13+
description: "Fetching letter variant details from database",
14+
variantId,
15+
});
16+
17+
const variantDetails: LetterVariant =
18+
await deps.supplierConfigRepo.getLetterVariant(variantId);
19+
20+
if (variantDetails) {
21+
deps.logger.info({
22+
description: "Fetched letter variant details",
23+
variantId,
24+
variantDetails,
25+
});
26+
} else {
27+
deps.logger.error({
28+
description: "No letter variant found for id",
29+
variantId,
30+
});
31+
}
32+
return variantDetails;
33+
}
34+
35+
export async function getVolumeGroupDetails(
36+
groupId: string,
37+
deps: Deps,
38+
): Promise<VolumeGroup> {
39+
deps.logger.info({
40+
description: "Fetching volume group details from database",
41+
groupId,
42+
});
43+
44+
const groupDetails = await deps.supplierConfigRepo.getVolumeGroup(groupId);
45+
if (groupDetails) {
46+
deps.logger.info({
47+
description: "Fetched volume group details",
48+
groupId,
49+
groupDetails,
50+
});
51+
} else {
52+
deps.logger.error({
53+
description: "No volume group found for id",
54+
groupId,
55+
});
56+
}
57+
58+
if (
59+
groupDetails.status !== "PROD" &&
60+
(new Date(groupDetails.startDate) > new Date() ||
61+
(groupDetails.endDate && new Date(groupDetails.endDate) < new Date()))
62+
) {
63+
deps.logger.error({
64+
description: "Volume group is not active based on status and dates",
65+
groupId,
66+
status: groupDetails.status,
67+
startDate: groupDetails.startDate,
68+
endDate: groupDetails.endDate,
69+
});
70+
throw new Error(`Volume group with id ${groupId} is not active`);
71+
}
72+
return groupDetails;
73+
}
74+
75+
export async function getSupplierAllocationsForVolumeGroup(
76+
groupId: string,
77+
supplierId: string,
78+
deps: Deps,
79+
): Promise<SupplierAllocation[]> {
80+
deps.logger.info({
81+
description: "Fetching supplier allocations for volume group from database",
82+
groupId,
83+
});
84+
const allocations =
85+
await deps.supplierConfigRepo.getSupplierAllocationsForVolumeGroup(groupId);
86+
87+
if (allocations.length > 0) {
88+
deps.logger.info({
89+
description:
90+
"Fetched supplier allocations for volume group from database",
91+
groupId,
92+
count: allocations.length,
93+
});
94+
} else {
95+
deps.logger.error({
96+
description: "No supplier allocations found for volume group id",
97+
groupId,
98+
});
99+
}
100+
101+
if (supplierId) {
102+
const filteredAllocations = allocations.filter(
103+
(alloc) => alloc.supplier === supplierId,
104+
);
105+
if (filteredAllocations.length === 0) {
106+
deps.logger.error({
107+
description:
108+
"No supplier allocations found for variantsupplier id in volume group",
109+
groupId,
110+
supplierId,
111+
});
112+
throw new Error(
113+
`No supplier allocations found for variant supplier id ${supplierId} in volume group ${groupId}`,
114+
);
115+
}
116+
return filteredAllocations;
117+
}
118+
119+
return allocations;
120+
}

0 commit comments

Comments
 (0)