88} from "@aws-sdk/client-sqs"
99import { DynamoDBClient } from "@aws-sdk/client-dynamodb"
1010import { DynamoDBDocumentClient , GetCommand , PutCommand } from "@aws-sdk/lib-dynamodb"
11- import { getParameter } from "@aws-lambda-powertools/parameters/ssm"
11+ import { SSMProvider } from "@aws-lambda-powertools/parameters/ssm"
1212import { getSecret } from "@aws-lambda-powertools/parameters/secrets"
1313
1414import { NotifyDataItem } from "@PrescriptionStatusUpdate_common/commonTypes"
@@ -30,7 +30,6 @@ const DUMMY_NOTIFY_DELAY_MS = 150
3030// these are only ever changed by a deployment
3131const dynamoTable = process . env . TABLE_NAME
3232const sqsUrl = process . env . NHS_NOTIFY_PRESCRIPTIONS_SQS_QUEUE_URL
33- const MAKE_REAL_NOTIFY_REQUESTS_PARAM = process . env . MAKE_REAL_NOTIFY_REQUESTS_PARAM
3433
3534// AWS clients
3635const sqs = new SQSClient ( { region : process . env . AWS_REGION } )
@@ -44,6 +43,25 @@ const marshallOptions = {
4443const dynamo = new DynamoDBClient ( { region : process . env . AWS_REGION } )
4544const docClient = DynamoDBDocumentClient . from ( dynamo , { marshallOptions} )
4645
46+ const ssm = new SSMProvider ( )
47+ const paramNames = {
48+ [ process . env . MAKE_REAL_NOTIFY_REQUESTS_PARAM ! ] : { maxAge : 60 } ,
49+ [ process . env . NOTIFY_API_BASE_URL_PARAM ! ] : { maxAge : 60 }
50+ }
51+ const configPromise = ssm . getParametersByName ( paramNames )
52+
53+ async function loadConfig ( ) : Promise < {
54+ makeRealNotifyRequests : boolean ,
55+ notifyApiBaseUrlRaw : string
56+ } > {
57+ const all = await configPromise
58+
59+ return {
60+ makeRealNotifyRequests : all [ process . env . MAKE_REAL_NOTIFY_REQUESTS_PARAM ! ] === "true" ,
61+ notifyApiBaseUrlRaw : all [ process . env . NOTIFY_API_BASE_URL_PARAM ! ] as string
62+ }
63+ }
64+
4765/**
4866 * Returns the original array, chunked in batches of up to <size>
4967 *
@@ -94,7 +112,7 @@ export async function reportQueueStatus(logger: Logger): Promise<void> {
94112// and helps track the nhs notify results
95113export interface NotifyDataItemMessage extends Message {
96114 PSUDataItem : NotifyDataItem
97- success ?: boolean
115+ deliveryStatus ?: string
98116 messageBatchReference ?: string ,
99117 // message reference is our internal UUID for the message
100118 messageReference : string
@@ -309,7 +327,7 @@ export async function addPrescriptionMessagesToNotificationStateStore(
309327 RequestId : data . PSUDataItem . RequestID ,
310328 SQSMessageID : data . MessageId ,
311329 LastNotifiedPrescriptionStatus : data . PSUDataItem . Status ,
312- DeliveryStatus : data . success ? "requested" : "notify request failed" ,
330+ DeliveryStatus : data . deliveryStatus ?? "unknown" , // Fall back to unknown if not set
313331 NotifyMessageID : data . notifyMessageId , // This is a GSI, but leaving it blank is fine
314332 NotifyMessageReference : data . messageReference ,
315333 NotifyMessageBatchReference : data . messageBatchReference , // Will be undefined when request fails
@@ -413,12 +431,11 @@ export async function makeBatchNotifyRequest(
413431 routingPlanId : string ,
414432 data : Array < NotifyDataItemMessage >
415433) : Promise < Array < NotifyDataItemMessage > > {
416- // Fetch secrets and parameters
417- if ( ! process . env . NOTIFY_API_BASE_URL_PARAM || ! process . env . API_KEY_SECRET ) {
434+ if ( ! process . env . API_KEY_SECRET ) {
418435 throw new Error ( "Environment configuration error" )
419436 }
420437
421- const notifyApiBaseUrlRaw = await getParameter ( process . env . NOTIFY_API_BASE_URL_PARAM )
438+ const { makeRealNotifyRequests , notifyApiBaseUrlRaw} = await loadConfig ( )
422439 const apiKeyRaw = await getSecret ( process . env . API_KEY_SECRET )
423440
424441 if ( ! notifyApiBaseUrlRaw ) throw new Error ( "NOTIFY_API_BASE_URL is not defined in the environment variables!" )
@@ -479,6 +496,21 @@ export async function makeBatchNotifyRequest(
479496 return [ ...res1 , ...res2 ]
480497 }
481498
499+ if ( ! makeRealNotifyRequests ) {
500+ logger . info ( "Not doing real Notify requests. Simply waiting for some time and returning success on all messages" )
501+ await new Promise ( f => setTimeout ( f , DUMMY_NOTIFY_DELAY_MS ) )
502+
503+ // Map each input item to a "successful" NotifyDataItemMessage
504+ return data . map ( item => {
505+ return {
506+ ...item ,
507+ messageBatchReference,
508+ deliveryStatus : "silent running" ,
509+ notifyMessageId : v4 ( ) // Create a dummy UUID
510+ }
511+ } )
512+ }
513+
482514 logger . info ( "Making a request for notifications to NHS notify" , { count : data . length , routingPlanId} )
483515
484516 // Create an axios instance configured for Notify
@@ -502,24 +534,6 @@ export async function makeBatchNotifyRequest(
502534 onRetry : onAxiosRetry
503535 } )
504536
505- let doRealRequest : boolean = false
506- if ( MAKE_REAL_NOTIFY_REQUESTS_PARAM ) doRealRequest = await getParameter ( MAKE_REAL_NOTIFY_REQUESTS_PARAM ) === "true"
507-
508- if ( ! doRealRequest ) {
509- logger . info ( "Not doing real Notify requests. Simply waiting for some time and returning success on all messages" )
510- await new Promise ( f => setTimeout ( f , DUMMY_NOTIFY_DELAY_MS ) )
511-
512- // Map each input item to a "successful" NotifyDataItemMessage
513- return data . map ( item => {
514- return {
515- ...item ,
516- messageBatchReference,
517- success : true ,
518- notifyMessageId : v4 ( ) // Create a dummy UUID
519- }
520- } )
521- }
522-
523537 try {
524538 const resp = await axiosInstance . post < CreateMessageBatchResponse > ( "/v1/message-batches" , body )
525539
@@ -528,7 +542,7 @@ export async function makeBatchNotifyRequest(
528542 logger . info ( "Requested notifications OK!" , {
529543 messageBatchReference,
530544 messageReferences : messages . map ( e => e . messageReference ) ,
531- success : "Requested Success "
545+ deliveryStatus : "requested "
532546 } )
533547
534548 // Map each input item to a NotifyDataItemMessage, marking success and attaching the notify ID
@@ -541,7 +555,7 @@ export async function makeBatchNotifyRequest(
541555 return {
542556 ...item ,
543557 messageBatchReference,
544- success : ! ! match ,
558+ deliveryStatus : match ? "requested" : "notify request failed" ,
545559 notifyMessageId : match ?. id
546560 }
547561 } )
@@ -552,7 +566,7 @@ export async function makeBatchNotifyRequest(
552566 statusText : resp . statusText ,
553567 messageBatchReference,
554568 messageReferences : messages . map ( e => e . messageReference ) ,
555- success : "Requested Failed "
569+ deliveryStatus : "notify request failed "
556570 } )
557571 throw new Error ( "Notify batch request failed" )
558572 }
@@ -561,7 +575,7 @@ export async function makeBatchNotifyRequest(
561575 logger . error ( "Notify batch request failed" , { error} )
562576 return data . map ( item => ( {
563577 ...item ,
564- success : false ,
578+ deliveryStatus : "notify request failed" ,
565579 messageBatchReference,
566580 notifyMessageId : undefined
567581 } ) )
0 commit comments