11import { SQSBatchItemFailure , SQSEvent , SQSHandler } from "aws-lambda" ;
22import { SendMessageCommand } from "@aws-sdk/client-sqs" ;
33import { LetterRequestPreparedEvent } from "@nhsdigital/nhs-notify-event-schemas-letter-rendering-v1" ;
4- import { LetterVariant } from "internal/datastore/src/SupplierConfigDomain" ;
4+ import {
5+ LetterVariant ,
6+ SupplierAllocation ,
7+ VolumeGroup ,
8+ } from "internal/datastore/src/SupplierConfigDomain" ;
59import { LetterRequestPreparedEventV2 } from "@nhsdigital/nhs-notify-event-schemas-letter-rendering" ;
610import z from "zod" ;
711import { Deps } from "../config/deps" ;
@@ -46,23 +50,90 @@ function validateType(event: unknown) {
4650 }
4751}
4852
49- async function getVariantDetails ( variantId : string , deps : Deps ) {
53+ async function getVariantDetails (
54+ variantId : string ,
55+ deps : Deps ,
56+ ) : Promise < LetterVariant > {
5057 deps . logger . info ( {
5158 description : "Fetching letter variant details from database" ,
5259 variantId,
5360 } ) ;
5461
5562 const variantDetails : LetterVariant =
5663 await deps . supplierConfigRepo . getLetterVariant ( variantId ) ;
64+ if ( ! variantDetails ) {
65+ throw new Error ( `No letter variant found for id: ${ variantId } ` ) ;
66+ }
67+
5768 deps . logger . info ( {
5869 description : "Fetched letter variant details" ,
5970 variantId,
6071 variantDetails,
6172 } ) ;
73+ return variantDetails ;
74+ }
75+
76+ async function getVolumeGroupDetails (
77+ groupId : string ,
78+ deps : Deps ,
79+ ) : Promise < VolumeGroup > {
80+ deps . logger . info ( {
81+ description : "Fetching volume group details from database" ,
82+ groupId,
83+ } ) ;
84+
85+ const groupDetails = await deps . supplierConfigRepo . getVolumeGroup ( groupId ) ;
86+ if ( ! groupDetails ) {
87+ throw new Error ( `No volume group found for id: ${ groupId } ` ) ;
88+ }
89+
90+ deps . logger . info ( {
91+ description : "Fetched volume group details" ,
92+ groupId,
93+ groupDetails,
94+ } ) ;
95+ return groupDetails ;
96+ }
97+
98+ async function getSupplierFromConfig ( letterEvent : PreparedEvents , deps : Deps ) {
99+ if ( ! letterEvent . data . letterVariantId ) {
100+ throw new Error ( "letterVariantId is required in event data" ) ;
101+ }
102+ const variantDetails : LetterVariant = await getVariantDetails (
103+ letterEvent . data . letterVariantId ,
104+ deps ,
105+ ) ;
106+ deps . logger . info ( {
107+ description : "Fetched variant details for letter variant" ,
108+ variantDetails,
109+ } ) ;
110+
111+ if ( ! variantDetails . volumeGroupId ) {
112+ throw new Error (
113+ `No volume group id for variantId: ${ letterEvent . data . letterVariantId } ` ,
114+ ) ;
115+ }
116+
117+ const volumeGroupDetails : VolumeGroup = await getVolumeGroupDetails (
118+ variantDetails . volumeGroupId ,
119+ deps ,
120+ ) ;
121+ deps . logger . info ( {
122+ description : "Fetched volume group details for letter variant" ,
123+ volumeGroupDetails,
124+ } ) ;
125+
126+ const supplierAllocations : SupplierAllocation [ ] =
127+ await deps . supplierConfigRepo . getSupplierAllocationsForVolumeGroup (
128+ variantDetails . volumeGroupId ,
129+ ) ;
130+ deps . logger . info ( {
131+ description : "Fetched supplier allocations for volume group" ,
132+ supplierAllocations,
133+ } ) ;
62134}
63135
64136function getSupplier ( letterEvent : PreparedEvents , deps : Deps ) : SupplierSpec {
65- getVariantDetails ( letterEvent . data . letterVariantId , deps ) ;
66137 return resolveSupplierForVariant ( letterEvent . data . letterVariantId , deps ) ;
67138}
68139
@@ -81,6 +152,8 @@ export default function createSupplierAllocatorHandler(deps: Deps): SQSHandler {
81152
82153 validateType ( letterEvent ) ;
83154
155+ await getSupplierFromConfig ( letterEvent as PreparedEvents , deps ) ;
156+
84157 const supplierSpec = getSupplier ( letterEvent as PreparedEvents , deps ) ;
85158
86159 deps . logger . info ( {
0 commit comments