@@ -130,22 +130,30 @@ export async function updateNotificationsTable(
130130 // For each callback resource, return a promise
131131 const callbackPromises = bodyData . data . map ( async ( resource ) => {
132132 let messageId : string
133- let messageStatus : string
133+ let messageStatus : string | undefined
134+ let channelStatus : string | undefined
135+ let supplierStatus : string | undefined
134136 let timestamp : string
135137
136138 if ( resource . type === CallbackType . message ) {
137139 messageId = resource . attributes . messageId
138140 messageStatus = resource . attributes . messageStatus
141+ channelStatus = resource . attributes . channels ?. [ 0 ] ?. channelStatus // If missing, undefined
142+ supplierStatus = undefined
139143 timestamp = resource . attributes . timestamp
140144 } else if ( resource . type === CallbackType . channel ) {
141145 messageId = resource . attributes . messageId
142- messageStatus = resource . attributes . channelStatus
146+ messageStatus = undefined
147+ channelStatus = resource . attributes . channelStatus
148+ supplierStatus = resource . attributes . supplierStatus
143149 timestamp = resource . attributes . timestamp
144150 } else {
145151 logger . error ( "Unknown data structure - cannot store to notifications table." , { resource} )
146152 // Set to junk data, so that when we try and update the table we will fail. This is fine, and handled later.
147153 messageId = "unknown"
148- messageStatus = "unknown"
154+ messageStatus = undefined
155+ channelStatus = undefined
156+ supplierStatus = undefined
149157 timestamp = "unknown"
150158 }
151159
@@ -190,28 +198,54 @@ export async function updateNotificationsTable(
190198 NHSNumber : item . NHSNumber ,
191199 RequestId : item . RequestId
192200 }
201+
202+ // Build UpdateExpression so undefined statuses are not written/updated.
203+ const sets : Array < string > = [
204+ "LastNotificationRequestTimestamp = :ts" ,
205+ "ExpiryTime = :et"
206+ ]
207+ const eav : Record < string , unknown > = {
208+ ":ts" : timestamp ,
209+ ":et" : newExpiry
210+ }
211+
212+ if ( messageStatus !== undefined ) {
213+ sets . push ( "MessageStatus = :ms" )
214+ eav [ ":ms" ] = messageStatus
215+ }
216+ if ( channelStatus !== undefined ) {
217+ sets . push ( "ChannelStatus = :cs" )
218+ eav [ ":cs" ] = channelStatus
219+ }
220+ if ( supplierStatus !== undefined ) {
221+ sets . push ( "SupplierStatus = :ss" )
222+ eav [ ":ss" ] = supplierStatus
223+ }
224+
225+ const UpdateExpression = `SET ${ sets . join ( ", " ) } `
226+
193227 try {
194228 await docClient . send ( new UpdateCommand ( {
195229 TableName : dynamoTable ,
196230 Key : key ,
197- UpdateExpression : [
198- "SET DeliveryStatus = :ds" ,
199- " , LastNotificationRequestTimestamp = :ts" ,
200- " , ExpiryTime = :et"
201- ] . join ( "" ) ,
202- ExpressionAttributeValues : {
203- ":ds" : messageStatus ,
204- ":ts" : timestamp ,
205- ":et" : newExpiry
206- }
231+ UpdateExpression,
232+ ExpressionAttributeValues : eav
207233 } ) )
234+
208235 logger . info (
209236 "Updated notification state" ,
210237 {
211238 NotifyMessageID : item . NotifyMessageID ,
212239 nhsNumber : item . NHSNumber ,
213240 psuRequestId : item . RequestId ,
214- newStatus : messageStatus ,
241+ // The overall delivery status is whichever of
242+ // messageStatus or channelStatus is defined (prefer messageStatus)
243+ // TODO: Update the splunk query to use the below statuses
244+ deliveryStatus : messageStatus ?? channelStatus ,
245+ // Parse to a string, or else undefined stuff doesn't get logged (thanks aws)
246+ messageStatus : `${ messageStatus } ` ,
247+ channelStatus : `${ channelStatus } ` ,
248+ supplierStatus : `${ supplierStatus } ` ,
215249 newTimestamp : timestamp ,
216250 newExpiryTime : newExpiry
217251 }
0 commit comments