@@ -10,6 +10,11 @@ import { LetterRequestPreparedEventV2 } from "@nhsdigital/nhs-notify-event-schem
1010import z from "zod" ;
1111import { Unit } from "aws-embedded-metrics" ;
1212import { MetricEntry , MetricStatus , buildEMFObject } from "@internal/helpers" ;
13+ import {
14+ getSupplierAllocationsForVolumeGroup ,
15+ getVariantDetails ,
16+ getVolumeGroupDetails ,
17+ } from "../services/supplier-config" ;
1318import { Deps } from "../config/deps" ;
1419
1520type 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-
17465async function getSupplierFromConfig ( letterEvent : PreparedEvents , deps : Deps ) {
17566 const variantDetails : LetterVariant = await getVariantDetails (
17667 letterEvent . data . letterVariantId ,
0 commit comments