@@ -2,7 +2,11 @@ import { mock } from 'jest-mock-extended';
22import { randomUUID } from 'node:crypto' ;
33import { createHandler } from 'apis/sqs-handler' ;
44import { EventPublisher , Logger } from 'utils' ;
5- import { acceptedLetterEvent , recordEvent } from '__tests__/test-data' ;
5+ import {
6+ acceptedLetterEvent ,
7+ failedLetterEvent ,
8+ recordEvent ,
9+ } from '__tests__/test-data' ;
610
711const logger = mock < Logger > ( ) ;
812const eventPublisher = mock < EventPublisher > ( ) ;
@@ -63,6 +67,85 @@ describe('SQS Handler', () => {
6367 ) ;
6468 expect ( response ) . toEqual ( { batchItemFailures : [ ] } ) ;
6569 } ) ;
70+
71+ it ( 'should include reasonCode and reasonText when provided in letter.FAILED event' , async ( ) => {
72+ const response = await handler ( recordEvent ( [ failedLetterEvent ] ) ) ;
73+
74+ expect ( eventPublisher . sendEvents ) . toHaveBeenCalledWith (
75+ [
76+ {
77+ ...failedLetterEvent ,
78+ id : '550e8400-e29b-41d4-a716-446655440001' ,
79+ time : '2023-06-20T12:00:00.250Z' ,
80+ recordedtime : '2023-06-20T12:00:00.250Z' ,
81+ dataschema :
82+ 'https://notify.nhs.uk/cloudevents/schemas/digital-letters/2025-10-draft/data/digital-letters-print-letter-transitioned-data.schema.json' ,
83+ type : 'uk.nhs.notify.digital.letters.print.letter.transitioned.v1' ,
84+ source :
85+ '/nhs/england/notify/production/primary/data-plane/digitalletters/print' ,
86+ data : {
87+ senderId : failedLetterEvent . data . origin . subject . split ( '/' ) [ 1 ] ,
88+ messageReference :
89+ failedLetterEvent . data . origin . subject . split ( '/' ) [ 3 ] ,
90+ specificationId : failedLetterEvent . data . specificationId ,
91+ status : failedLetterEvent . data . status ,
92+ supplierId : failedLetterEvent . data . supplierId ,
93+ time : failedLetterEvent . time ,
94+ reasonCode : 'FAILURE001' ,
95+ reasonText : 'Letter has too many pages' ,
96+ } ,
97+ } ,
98+ ] ,
99+ expect . any ( Function ) ,
100+ ) ;
101+ expect ( logger . info ) . toHaveBeenCalledWith (
102+ 'Received SQS Event of 1 record(s)' ,
103+ ) ;
104+ expect ( logger . info ) . toHaveBeenCalledWith (
105+ '1 of 1 records processed successfully' ,
106+ ) ;
107+ expect ( response ) . toEqual ( { batchItemFailures : [ ] } ) ;
108+ } ) ;
109+
110+ it ( 'should not include reasonCode when only reasonText is provided' , async ( ) => {
111+ const eventWithReasonTextOnly = {
112+ ...failedLetterEvent ,
113+ data : {
114+ ...failedLetterEvent . data ,
115+ reasonCode : undefined ,
116+ reasonText : 'Letter has too many pages' ,
117+ } ,
118+ } ;
119+
120+ const response = await handler ( recordEvent ( [ eventWithReasonTextOnly ] ) ) ;
121+
122+ const publishedEvent = ( eventPublisher . sendEvents as jest . Mock ) . mock
123+ . calls [ 0 ] [ 0 ] [ 0 ] ;
124+
125+ expect ( publishedEvent . data ) . not . toHaveProperty ( 'reasonCode' ) ;
126+ expect ( publishedEvent . data . reasonText ) . toBe ( 'Letter has too many pages' ) ;
127+ expect ( response ) . toEqual ( { batchItemFailures : [ ] } ) ;
128+ } ) ;
129+
130+ it ( 'should not include reasonText when only reasonCode is provided' , async ( ) => {
131+ const eventWithReasonCodeOnly = {
132+ ...failedLetterEvent ,
133+ data : {
134+ ...failedLetterEvent . data ,
135+ reasonCode : 'FAILURE001' ,
136+ reasonText : undefined ,
137+ } ,
138+ } ;
139+
140+ const response = await handler ( recordEvent ( [ eventWithReasonCodeOnly ] ) ) ;
141+
142+ const publishedEvent = ( eventPublisher . sendEvents as jest . Mock ) . mock
143+ . calls [ 0 ] [ 0 ] [ 0 ] ;
144+
145+ expect ( publishedEvent . data . reasonCode ) . toBe ( 'FAILURE001' ) ;
146+ expect ( publishedEvent . data ) . not . toHaveProperty ( 'reasonText' ) ;
147+ expect ( response ) . toEqual ( { batchItemFailures : [ ] } ) ;
148+ } ) ;
66149 } ) ;
67150
68151 describe ( 'errors' , ( ) => {
0 commit comments