@@ -99,9 +99,71 @@ async function getVolumeGroupDetails(
9999 groupId,
100100 } ) ;
101101 }
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+ }
102117 return groupDetails ;
103118}
104119
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+
105167async function getSupplierFromConfig ( letterEvent : PreparedEvents , deps : Deps ) {
106168 const variantDetails : LetterVariant = await getVariantDetails (
107169 letterEvent . data . letterVariantId ,
@@ -122,8 +184,10 @@ async function getSupplierFromConfig(letterEvent: PreparedEvents, deps: Deps) {
122184 } ) ;
123185
124186 const supplierAllocations : SupplierAllocation [ ] =
125- await deps . supplierConfigRepo . getSupplierAllocationsForVolumeGroup (
187+ await getSupplierAllocationsForVolumeGroup (
126188 variantDetails . volumeGroupId ,
189+ variantDetails . supplierId ?? "" ,
190+ deps ,
127191 ) ;
128192 deps . logger . info ( {
129193 description : "Fetched supplier allocations for volume group" ,
0 commit comments