Skip to content

Commit 08074be

Browse files
refactor to supplier-config services
1 parent b766a5b commit 08074be

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
@@ -10,6 +10,11 @@ import { LetterRequestPreparedEventV2 } from "@nhsdigital/nhs-notify-event-schem
1010
import z from "zod";
1111
import { Unit } from "aws-embedded-metrics";
1212
import { MetricEntry, MetricStatus, buildEMFObject } from "@internal/helpers";
13+
import {
14+
getSupplierAllocationsForVolumeGroup,
15+
getVariantDetails,
16+
getVolumeGroupDetails,
17+
} from "../services/supplier-config";
1318
import { Deps } from "../config/deps";
1419

1520
type SupplierSpec = {
@@ -57,120 +62,6 @@ function validateType(event: unknown) {
5762
}
5863
}
5964

60-
async function getVariantDetails(
61-
variantId: string,
62-
deps: Deps,
63-
): Promise<LetterVariant> {
64-
deps.logger.info({
65-
description: "Fetching letter variant details from database",
66-
variantId,
67-
});
68-
69-
const variantDetails: LetterVariant =
70-
await deps.supplierConfigRepo.getLetterVariant(variantId);
71-
72-
if (variantDetails) {
73-
deps.logger.info({
74-
description: "Fetched letter variant details",
75-
variantId,
76-
variantDetails,
77-
});
78-
} else {
79-
deps.logger.error({
80-
description: "No letter variant found for id",
81-
variantId,
82-
});
83-
}
84-
return variantDetails;
85-
}
86-
87-
async function getVolumeGroupDetails(
88-
groupId: string,
89-
deps: Deps,
90-
): Promise<VolumeGroup> {
91-
deps.logger.info({
92-
description: "Fetching volume group details from database",
93-
groupId,
94-
});
95-
96-
const groupDetails = await deps.supplierConfigRepo.getVolumeGroup(groupId);
97-
if (groupDetails) {
98-
deps.logger.info({
99-
description: "Fetched volume group details",
100-
groupId,
101-
groupDetails,
102-
});
103-
} else {
104-
deps.logger.error({
105-
description: "No volume group found for id",
106-
groupId,
107-
});
108-
}
109-
110-
if (
111-
groupDetails.status !== "PROD" &&
112-
(new Date(groupDetails.startDate) > new Date() ||
113-
(groupDetails.endDate && new Date(groupDetails.endDate) < new Date()))
114-
) {
115-
deps.logger.error({
116-
description: "Volume group is not active based on status and dates",
117-
groupId,
118-
status: groupDetails.status,
119-
startDate: groupDetails.startDate,
120-
endDate: groupDetails.endDate,
121-
});
122-
throw new Error(`Volume group with id ${groupId} is not active`);
123-
}
124-
return groupDetails;
125-
}
126-
127-
async function getSupplierAllocationsForVolumeGroup(
128-
groupId: string,
129-
supplierId: string,
130-
deps: Deps,
131-
): Promise<SupplierAllocation[]> {
132-
deps.logger.info({
133-
description: "Fetching supplier allocations for volume group from database",
134-
groupId,
135-
});
136-
const allocations =
137-
await deps.supplierConfigRepo.getSupplierAllocationsForVolumeGroup(groupId);
138-
139-
if (allocations.length > 0) {
140-
deps.logger.info({
141-
description:
142-
"Fetched supplier allocations for volume group from database",
143-
groupId,
144-
count: allocations.length,
145-
});
146-
} else {
147-
deps.logger.error({
148-
description: "No supplier allocations found for volume group id",
149-
groupId,
150-
});
151-
}
152-
153-
if (supplierId) {
154-
const filteredAllocations = allocations.filter(
155-
(alloc) => alloc.supplier === supplierId,
156-
);
157-
if (filteredAllocations.length === 0) {
158-
deps.logger.error({
159-
description:
160-
"No supplier allocations found for variantsupplier id in volume group",
161-
groupId,
162-
supplierId,
163-
});
164-
throw new Error(
165-
`No supplier allocations found for variant supplier id ${supplierId} in volume group ${groupId}`,
166-
);
167-
}
168-
return filteredAllocations;
169-
}
170-
171-
return allocations;
172-
}
173-
17465
async function getSupplierFromConfig(letterEvent: PreparedEvents, deps: Deps) {
17566
const variantDetails: LetterVariant = await getVariantDetails(
17667
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)