@@ -11,9 +11,16 @@ import {
1111 MessageBatchItem
1212} from "./types"
1313import { loadConfig } from "./ssm"
14+ import { loadSecrets , NotifySecrets } from "./secrets"
1415import { tokenExchange } from "./auth"
1516import { NOTIFY_REQUEST_MAX_BYTES , NOTIFY_REQUEST_MAX_ITEMS , DUMMY_NOTIFY_DELAY_MS } from "./constants"
1617
18+ export interface NotifyConfig {
19+ routingPlanId : string
20+ notifyApiBaseUrl : string
21+ notifySecrets : NotifySecrets
22+ }
23+
1724/**
1825 * Returns the original array, chunked in batches of up to <size>
1926 *
@@ -52,8 +59,6 @@ export async function handleNotifyRequests(
5259 return [ ]
5360 }
5461
55- const configPromise = loadConfig ( )
56-
5762 // Map the NotifyDataItems into the structure needed for notify
5863 const messages : Array < MessageBatchItem > = data . flatMap ( item => {
5964 // Ignore messages with missing deduplication IDs (the field is possibly undefined)
@@ -70,15 +75,20 @@ export async function handleNotifyRequests(
7075 } ]
7176 } )
7277
73- // Check if we should make real requests
74- const { makeRealNotifyRequestsFlag, notifyApiBaseUrlRaw} = await configPromise
75- if ( ! makeRealNotifyRequestsFlag || ! notifyApiBaseUrlRaw ) return await makeFakeNotifyRequest ( logger , data , messages )
76-
77- if ( ! notifyApiBaseUrlRaw ) throw new Error ( "NOTIFY_API_BASE_URL is not defined in the environment variables!" )
78- // Just to be safe, trim any whitespace. Also, secrets may be bytes, so make sure it's a string
79- const notifyBaseUrl = notifyApiBaseUrlRaw . trim ( )
80-
81- return await makeRealNotifyRequest ( logger , routingPlanId , notifyBaseUrl , data , messages )
78+ const { makeRealNotifyRequestsFlag, notifyApiBaseUrl} = await loadConfig ( )
79+ if ( ! makeRealNotifyRequestsFlag ) {
80+ return await makeFakeNotifyRequest ( logger , data , messages )
81+ } else if ( ! notifyApiBaseUrl ) {
82+ throw new Error ( "NOTIFY_API_BASE_URL is not defined in the environment variables!" )
83+ } else {
84+ const notifySecrets = await loadSecrets ( )
85+ const config : NotifyConfig = {
86+ routingPlanId,
87+ notifyApiBaseUrl,
88+ notifySecrets
89+ }
90+ return await makeRealNotifyRequest ( logger , config , data , messages )
91+ }
8292}
8393
8494/**
@@ -90,7 +100,6 @@ async function makeFakeNotifyRequest(
90100 data : Array < NotifyDataItemMessage > ,
91101 messages : Array < MessageBatchItem >
92102) : Promise < Array < NotifyDataItemMessage > > {
93-
94103 logger . info ( "Not doing real Notify requests. Simply waiting for some time and returning success on all messages" )
95104 await new Promise ( f => setTimeout ( f , DUMMY_NOTIFY_DELAY_MS ) )
96105
@@ -146,13 +155,15 @@ function logNotificationRequest(logger: Logger,
146155 * Handles splitting large batches into smaller ones as needed.
147156 *
148157 * @param logger - AWS logging object
149- * @param routingPlanId - The Notify routing plan ID with which to process the data
150- * @param data - The details for the notification
158+ * @param config - configuration for talking to NHS Notify
159+ * @param data - PSU SQS messages to process
160+ * @param messages - The data being sent to NHS Notify
161+ * @param bearerToken - lazy initialised Bearer token to communicate with Notify
162+ * @param axiosInstance - lazy initialised HTTP client
151163 */
152164export async function makeRealNotifyRequest (
153165 logger : Logger ,
154- routingPlanId : string ,
155- notifyBaseUrl : string ,
166+ config : NotifyConfig ,
156167 data : Array < NotifyDataItemMessage > ,
157168 messages : Array < MessageBatchItem > ,
158169 bearerToken ?: string ,
@@ -166,16 +177,16 @@ export async function makeRealNotifyRequest(
166177 data : {
167178 type : "MessageBatch" as const ,
168179 attributes : {
169- routingPlanId,
180+ routingPlanId : config . routingPlanId ,
170181 messageBatchReference,
171182 messages
172183 }
173184 }
174185 }
175186
176187 // Lazily get the bearer token and axios instance, so we only do it once even if we recurse
177- axiosInstance ??= setupAxios ( logger , notifyBaseUrl )
178- bearerToken ??= await tokenExchange ( logger , axiosInstance , notifyBaseUrl )
188+ axiosInstance ??= setupAxios ( logger , config . notifyApiBaseUrl )
189+ bearerToken ??= await tokenExchange ( logger , axiosInstance , config . notifyApiBaseUrl , config . notifySecrets )
179190
180191 // Recursive split if too large
181192 if ( messages . length >= NOTIFY_REQUEST_MAX_ITEMS || estimateSize ( body ) > NOTIFY_REQUEST_MAX_BYTES ) {
@@ -188,13 +199,17 @@ export async function makeRealNotifyRequest(
188199
189200 // send both halves in parallel
190201 const [ res1 , res2 ] = await Promise . all ( [
191- makeRealNotifyRequest ( logger , routingPlanId , notifyBaseUrl , data , firstHalf , bearerToken , axiosInstance ) ,
192- makeRealNotifyRequest ( logger , routingPlanId , notifyBaseUrl , data , secondHalf , bearerToken , axiosInstance )
202+ makeRealNotifyRequest (
203+ logger , config , data , firstHalf , bearerToken , axiosInstance
204+ ) ,
205+ makeRealNotifyRequest (
206+ logger , config , data , secondHalf , bearerToken , axiosInstance
207+ )
193208 ] )
194209 return [ ...res1 , ...res2 ]
195210 }
196211
197- logger . info ( "Making a request for notifications to NHS notify" , { count : messages . length , routingPlanId} )
212+ logger . info ( "Request notifications of NHS notify" , { count : messages . length , routingPlanId : config . routingPlanId } )
198213
199214 try {
200215 const headers = {
0 commit comments