@@ -106,9 +106,71 @@ async function getVolumeGroupDetails(
106106 groupId,
107107 } ) ;
108108 }
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+ }
109124 return groupDetails ;
110125}
111126
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+
112174async function getSupplierFromConfig ( letterEvent : PreparedEvents , deps : Deps ) {
113175 const variantDetails : LetterVariant = await getVariantDetails (
114176 letterEvent . data . letterVariantId ,
@@ -129,8 +191,10 @@ async function getSupplierFromConfig(letterEvent: PreparedEvents, deps: Deps) {
129191 } ) ;
130192
131193 const supplierAllocations : SupplierAllocation [ ] =
132- await deps . supplierConfigRepo . getSupplierAllocationsForVolumeGroup (
194+ await getSupplierAllocationsForVolumeGroup (
133195 variantDetails . volumeGroupId ,
196+ variantDetails . supplierId ?? "" ,
197+ deps ,
134198 ) ;
135199 deps . logger . info ( {
136200 description : "Fetched supplier allocations for volume group" ,
0 commit comments