@@ -117,6 +117,80 @@ test.describe('Digital Letters - NHSApp Status Handler', () => {
117117 ] ) ;
118118 } ) ;
119119
120+ test ( 'should handle duplicate event for the same TTL record' , async ( ) => {
121+ const event = {
122+ ...baseEvent ,
123+ data : {
124+ ...baseEvent . data ,
125+ messageReference : uuidv4 ( ) ,
126+ } ,
127+ } ;
128+
129+ const concatedReference = `${ event . data . senderId } _${ event . data . messageReference } ` ;
130+
131+ const ttlItem = {
132+ PK : concatedReference ,
133+ SK : 'TTL' ,
134+ dateOfExpiry : '2023-12-31#0' ,
135+ event,
136+ ttl : Date . now ( ) / 1000 + 3600 ,
137+ } ;
138+
139+ const channelStatusPublishedEvent = {
140+ source : '/nhs/england/notify/comms-mgr-dev/dev/data-plane/messaging' ,
141+ type : 'uk.nhs.notify.channel.status.PUBLISHED.v1' ,
142+ data : {
143+ messageReference : concatedReference ,
144+ supplierStatus : 'paper_letter_opted_out' ,
145+ } ,
146+ } ;
147+
148+ const putResponseCode = await putTtl ( ttlItem ) ;
149+ expect ( putResponseCode ) . toBe ( 200 ) ;
150+
151+ await eventPublisher . sendEvents < any > (
152+ [ channelStatusPublishedEvent , channelStatusPublishedEvent ] ,
153+ ( ) => true ,
154+ ) ;
155+
156+ await expectToPassEventually ( async ( ) => {
157+ const ttl = await getTtl (
158+ event . data . senderId ,
159+ event . data . messageReference ,
160+ ) ;
161+
162+ expect ( ttl . length ) . toBe ( 1 ) ;
163+ expect ( ttl [ 0 ] ) . toHaveProperty ( 'withdrawn' , true ) ;
164+ } ) ;
165+
166+ await Promise . all ( [
167+ expectToPassEventually ( async ( ) => {
168+ const eventLogEntry = await getLogsFromCloudwatch (
169+ `/aws/vendedlogs/events/event-bus/nhs-${ ENV } -dl` ,
170+ [
171+ '$.message_type = "EVENT_RECEIPT"' ,
172+ '$.details.detail_type = "uk.nhs.notify.digital.letters.queue.digital.letter.read.v1"' ,
173+ `$.details.event_detail = "*\\"messageReference\\":\\"${ event . data . messageReference } \\"*"` ,
174+ ] ,
175+ ) ;
176+
177+ expect ( eventLogEntry . length ) . toEqual ( 2 ) ;
178+ } ) ,
179+
180+ expectToPassEventually ( async ( ) => {
181+ const eventLogEntry = await getLogsFromCloudwatch (
182+ NHSAPP_STATUS_HANDLER_LAMBDA_LOG_GROUP_NAME ,
183+ [
184+ '$.message.description = "TTL record marked as withdrawn"' ,
185+ `$.message.messageReference = "${ concatedReference } "` ,
186+ ] ,
187+ ) ;
188+
189+ expect ( eventLogEntry . length ) . toEqual ( 2 ) ;
190+ } , 150 ) ,
191+ ] ) ;
192+ } ) ;
193+
120194 test ( 'should handle missing TTL record' , async ( ) => {
121195 const concatedReference = `${ uuidv4 ( ) } _${ uuidv4 ( ) } ` ;
122196
0 commit comments