@@ -3,10 +3,13 @@ import {SQSClient, SendMessageBatchCommand} from "@aws-sdk/client-sqs"
33
44import { createHmac } from "crypto"
55
6- import { DataItem } from "../updatePrescriptionStatus"
6+ import { PSUDataItem } from "@PrescriptionStatusUpdate_common/commonTypes"
7+
8+ import { checkSiteOrSystemIsNotifyEnabled } from "../validation/notificationSiteAndSystemFilters"
79
810const sqsUrl : string | undefined = process . env . NHS_NOTIFY_PRESCRIPTIONS_SQS_QUEUE_URL
9- const sqsSalt : string = process . env . SQS_SALT ?? "DEVSALT"
11+ const fallbackSalt = "DEV SALT"
12+ const sqsSalt : string = process . env . SQS_SALT ?? fallbackSalt
1013
1114// The AWS_REGION is always defined in lambda environments
1215const sqs = new SQSClient ( { region : process . env . AWS_REGION } )
@@ -33,26 +36,26 @@ function chunkArray<T>(arr: Array<T>, size: number): Array<Array<T>> {
3336 * @param hashFunction - Which hash function to use. HMAC compatible. Defaults to SHA-256
3437 * @returns - A hex encoded string of the hash
3538 */
36- export function saltedHash ( input : string , hashFunction : string = "sha256" ) : string {
37- if ( sqsSalt === "DEVSALT" ) {
38- console . warn ( "Using the fallback salt value - please update the environment variable `SQS_SALT` to a random value." )
39+ export function saltedHash ( logger : Logger , input : string , hashFunction : string = "sha256" ) : string {
40+ if ( sqsSalt === fallbackSalt ) {
41+ logger . warn ( "Using the fallback salt value - please update the environment variable `SQS_SALT` to a random value." )
3942 }
4043 return createHmac ( hashFunction , sqsSalt )
4144 . update ( input , "utf8" )
4245 . digest ( "hex" )
4346}
4447
4548/**
46- * Pushes an array of DataItems to the notifications SQS queue
49+ * Pushes an array of PSUDataItem to the notifications SQS queue
4750 * Uses SendMessageBatch to send up to 10 at a time
4851 *
4952 * @param requestId - The x-request-id header from the incoming event
50- * @param data - Array of DataItems to send to SQS
53+ * @param data - Array of PSUDataItem to send to SQS
5154 * @param logger - Logger instance
5255 */
5356export async function pushPrescriptionToNotificationSQS (
5457 requestId : string ,
55- data : Array < DataItem > ,
58+ data : Array < PSUDataItem > ,
5659 logger : Logger
5760) {
5861 logger . info ( "Checking if any items require notifications" , { numItemsToBeChecked : data . length , sqsUrl} )
@@ -62,8 +65,17 @@ export async function pushPrescriptionToNotificationSQS(
6265 throw new Error ( "Notifications SQS URL not configured" )
6366 }
6467
68+ // Only allow through sites and systems that are allowedSitesAndSystems
69+ const allowedSitesAndSystemsData = checkSiteOrSystemIsNotifyEnabled ( data )
70+ logger . info (
71+ "Filtered out sites and suppliers that are not enabled, or are explicitly disabled" ,
72+ {
73+ numItemsAllowed : allowedSitesAndSystemsData . length
74+ }
75+ )
76+
6577 // SQS batch calls are limited to 10 messages per request, so chunk the data
66- const batches = chunkArray ( data , 10 )
78+ const batches = chunkArray ( allowedSitesAndSystemsData , 10 )
6779
6880 // Only these statuses will be pushed to the SQS
6981 const updateStatuses : Array < string > = [
@@ -80,7 +92,7 @@ export async function pushPrescriptionToNotificationSQS(
8092 MessageBody : JSON . stringify ( item ) ,
8193 // FIFO
8294 // We dedupe on both nhs number and ods code
83- MessageDeduplicationId : saltedHash ( `${ item . PatientNHSNumber } :${ item . PharmacyODSCode } ` ) ,
95+ MessageDeduplicationId : saltedHash ( logger , `${ item . PatientNHSNumber } :${ item . PharmacyODSCode } ` ) ,
8496 MessageGroupId : requestId
8597 } ) )
8698 // We could do a round of deduplications here, but benefits would be minimal and AWS SQS will do it for us anyway.
0 commit comments