@@ -8,6 +8,11 @@ import {
88} from "internal/datastore/src/SupplierConfigDomain" ;
99import { LetterRequestPreparedEventV2 } from "@nhsdigital/nhs-notify-event-schemas-letter-rendering" ;
1010import z from "zod" ;
11+ import {
12+ getSupplierAllocationsForVolumeGroup ,
13+ getVariantDetails ,
14+ getVolumeGroupDetails ,
15+ } from "../services/supplier-config" ;
1116import { Deps } from "../config/deps" ;
1217
1318type 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-
16758async function getSupplierFromConfig ( letterEvent : PreparedEvents , deps : Deps ) {
16859 const variantDetails : LetterVariant = await getVariantDetails (
16960 letterEvent . data . letterVariantId ,
0 commit comments