Skip to content

Commit 55410d9

Browse files
committed
CCM-12614: tidy up after merging in main
1 parent 9cb6fc1 commit 55410d9

File tree

15 files changed

+633
-403
lines changed

15 files changed

+633
-403
lines changed

lambdas/pdm-poll-lambda/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"dependencies": {
33
"aws-lambda": "^1.0.7",
4+
"digital-letters-events": "^0.0.1",
45
"lodash": "^4.17.21",
56
"p-limit": "^3.1.0",
67
"utils": "^0.0.1"

lambdas/pdm-poll-lambda/src/__tests__/apis/sqs-handler.test.ts

Lines changed: 148 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,18 @@ describe('SQS Handler', () => {
4040

4141
const response = await handler(recordEvent([pdmResourceSubmittedEvent]));
4242

43-
expect(eventPublisher.sendEvents).toHaveBeenCalledWith([
44-
{
45-
...pdmResourceSubmittedEvent,
46-
id: '550e8400-e29b-41d4-a716-446655440001',
47-
time: '2023-06-20T12:00:00.250Z',
48-
recordedtime: '2023-06-20T12:00:00.250Z',
49-
type: 'uk.nhs.notify.digital.letters.pdm.resource.available.v1',
50-
},
51-
]);
43+
expect(eventPublisher.sendEvents).toHaveBeenCalledWith(
44+
[
45+
{
46+
...pdmResourceSubmittedEvent,
47+
id: '550e8400-e29b-41d4-a716-446655440001',
48+
time: '2023-06-20T12:00:00.250Z',
49+
recordedtime: '2023-06-20T12:00:00.250Z',
50+
type: 'uk.nhs.notify.digital.letters.pdm.resource.available.v1',
51+
},
52+
],
53+
expect.any(Function),
54+
);
5255
expect(logger.info).toHaveBeenCalledWith(
5356
'Received SQS Event of 1 record(s)',
5457
);
@@ -63,19 +66,22 @@ describe('SQS Handler', () => {
6366

6467
const response = await handler(recordEvent([pdmResourceSubmittedEvent]));
6568

66-
expect(eventPublisher.sendEvents).toHaveBeenCalledWith([
67-
{
68-
...pdmResourceSubmittedEvent,
69-
id: '550e8400-e29b-41d4-a716-446655440001',
70-
time: '2023-06-20T12:00:00.250Z',
71-
recordedtime: '2023-06-20T12:00:00.250Z',
72-
type: 'uk.nhs.notify.digital.letters.pdm.resource.unavailable.v1',
73-
data: {
74-
...pdmResourceSubmittedEvent.data,
75-
retryCount: 0,
69+
expect(eventPublisher.sendEvents).toHaveBeenCalledWith(
70+
[
71+
{
72+
...pdmResourceSubmittedEvent,
73+
id: '550e8400-e29b-41d4-a716-446655440001',
74+
time: '2023-06-20T12:00:00.250Z',
75+
recordedtime: '2023-06-20T12:00:00.250Z',
76+
type: 'uk.nhs.notify.digital.letters.pdm.resource.unavailable.v1',
77+
data: {
78+
...pdmResourceSubmittedEvent.data,
79+
retryCount: 0,
80+
},
7681
},
77-
},
78-
]);
82+
],
83+
expect.any(Function),
84+
);
7985
expect(logger.info).toHaveBeenCalledWith(
8086
'Received SQS Event of 1 record(s)',
8187
);
@@ -94,15 +100,18 @@ describe('SQS Handler', () => {
94100
recordEvent([pdmResourceUnavailableEvent]),
95101
);
96102

97-
expect(eventPublisher.sendEvents).toHaveBeenCalledWith([
98-
{
99-
...pdmResourceUnavailableEvent,
100-
id: '550e8400-e29b-41d4-a716-446655440001',
101-
time: '2023-06-20T12:00:00.250Z',
102-
recordedtime: '2023-06-20T12:00:00.250Z',
103-
type: 'uk.nhs.notify.digital.letters.pdm.resource.available.v1',
104-
},
105-
]);
103+
expect(eventPublisher.sendEvents).toHaveBeenCalledWith(
104+
[
105+
{
106+
...pdmResourceUnavailableEvent,
107+
id: '550e8400-e29b-41d4-a716-446655440001',
108+
time: '2023-06-20T12:00:00.250Z',
109+
recordedtime: '2023-06-20T12:00:00.250Z',
110+
type: 'uk.nhs.notify.digital.letters.pdm.resource.available.v1',
111+
},
112+
],
113+
expect.any(Function),
114+
);
106115
expect(logger.info).toHaveBeenCalledWith(
107116
'Received SQS Event of 1 record(s)',
108117
);
@@ -119,19 +128,22 @@ describe('SQS Handler', () => {
119128
recordEvent([pdmResourceUnavailableEvent]),
120129
);
121130

122-
expect(eventPublisher.sendEvents).toHaveBeenCalledWith([
123-
{
124-
...pdmResourceUnavailableEvent,
125-
id: '550e8400-e29b-41d4-a716-446655440001',
126-
time: '2023-06-20T12:00:00.250Z',
127-
recordedtime: '2023-06-20T12:00:00.250Z',
128-
type: 'uk.nhs.notify.digital.letters.pdm.resource.unavailable.v1',
129-
data: {
130-
...pdmResourceSubmittedEvent.data,
131-
retryCount: 2,
131+
expect(eventPublisher.sendEvents).toHaveBeenCalledWith(
132+
[
133+
{
134+
...pdmResourceUnavailableEvent,
135+
id: '550e8400-e29b-41d4-a716-446655440001',
136+
time: '2023-06-20T12:00:00.250Z',
137+
recordedtime: '2023-06-20T12:00:00.250Z',
138+
type: 'uk.nhs.notify.digital.letters.pdm.resource.unavailable.v1',
139+
data: {
140+
...pdmResourceSubmittedEvent.data,
141+
retryCount: 2,
142+
},
132143
},
133-
},
134-
]);
144+
],
145+
expect.any(Function),
146+
);
135147
expect(logger.info).toHaveBeenCalledWith(
136148
'Received SQS Event of 1 record(s)',
137149
);
@@ -154,19 +166,22 @@ describe('SQS Handler', () => {
154166

155167
const response = await handler(recordEvent([testEvent]));
156168

157-
expect(eventPublisher.sendEvents).toHaveBeenCalledWith([
158-
{
159-
...pdmResourceUnavailableEvent,
160-
id: '550e8400-e29b-41d4-a716-446655440001',
161-
time: '2023-06-20T12:00:00.250Z',
162-
recordedtime: '2023-06-20T12:00:00.250Z',
163-
type: 'uk.nhs.notify.digital.letters.pdm.resource.retries.exceeded.v1',
164-
data: {
165-
...pdmResourceSubmittedEvent.data,
166-
retryCount: 10,
169+
expect(eventPublisher.sendEvents).toHaveBeenCalledWith(
170+
[
171+
{
172+
...pdmResourceUnavailableEvent,
173+
id: '550e8400-e29b-41d4-a716-446655440001',
174+
time: '2023-06-20T12:00:00.250Z',
175+
recordedtime: '2023-06-20T12:00:00.250Z',
176+
type: 'uk.nhs.notify.digital.letters.pdm.resource.retries.exceeded.v1',
177+
data: {
178+
...pdmResourceSubmittedEvent.data,
179+
retryCount: 10,
180+
},
167181
},
168-
},
169-
]);
182+
],
183+
expect.any(Function),
184+
);
170185
expect(logger.info).toHaveBeenCalledWith(
171186
'Received SQS Event of 1 record(s)',
172187
);
@@ -178,16 +193,91 @@ describe('SQS Handler', () => {
178193
});
179194

180195
describe('errors', () => {
181-
it('should return failed items to the queue if an error occurs while processing them', async () => {
196+
it('should return failed SQS records to the queue if an error occurs while calling PDM', async () => {
197+
pdm.poll.mockRejectedValueOnce(new Error('PDM error'));
182198
const event = recordEvent([pdmResourceSubmittedEvent]);
183-
event.Records[0].body = 'not-json';
184199

185200
const result = await handler(event);
186201

187202
expect(logger.warn).toHaveBeenCalledWith({
188-
error: `Unexpected token 'o', "not-json" is not valid JSON`,
203+
err: 'PDM error',
189204
description: 'Failed processing message',
190-
messageId: '1',
205+
});
206+
207+
expect(logger.info).toHaveBeenCalledWith(
208+
'0 of 1 records processed successfully',
209+
);
210+
211+
expect(result).toEqual({
212+
batchItemFailures: [{ itemIdentifier: '1' }],
213+
});
214+
});
215+
216+
it('should return failed SQS records to the queue if an error occurs while processing them', async () => {
217+
const event = recordEvent([pdmResourceSubmittedEvent]);
218+
event.Records[0].body = 'not-json';
219+
220+
const result = await handler(event);
221+
222+
expect(logger.warn).toHaveBeenCalledWith({
223+
err: new SyntaxError(
224+
`Unexpected token 'o', "not-json" is not valid JSON`,
225+
),
226+
description: 'Error parsing SQS record',
227+
});
228+
229+
expect(logger.info).toHaveBeenCalledWith(
230+
'0 of 1 records processed successfully',
231+
);
232+
233+
expect(result).toEqual({
234+
batchItemFailures: [{ itemIdentifier: '1' }],
235+
});
236+
});
237+
238+
it('should return failed items to the queue if an invalid pdm.resource.submitted event is received', async () => {
239+
const invalidSubmittedEvent = {
240+
...pdmResourceSubmittedEvent,
241+
source: 'invalid pdm.resource.submitted source',
242+
};
243+
const event = recordEvent([invalidSubmittedEvent]);
244+
245+
const result = await handler(event);
246+
247+
expect(logger.warn).toHaveBeenCalledWith({
248+
err: expect.arrayContaining([
249+
expect.objectContaining({
250+
instancePath: '/source',
251+
}),
252+
]),
253+
description: 'Error parsing queue entry',
254+
});
255+
256+
expect(logger.info).toHaveBeenCalledWith(
257+
'0 of 1 records processed successfully',
258+
);
259+
260+
expect(result).toEqual({
261+
batchItemFailures: [{ itemIdentifier: '1' }],
262+
});
263+
});
264+
265+
it('should return failed items to the queue if an invalid pdm.resource.unavailable event is received', async () => {
266+
const invalidSubmittedEvent = {
267+
...pdmResourceUnavailableEvent,
268+
source: 'invalid pdm.resource.unavailable source',
269+
};
270+
const event = recordEvent([invalidSubmittedEvent]);
271+
272+
const result = await handler(event);
273+
274+
expect(logger.warn).toHaveBeenCalledWith({
275+
err: expect.arrayContaining([
276+
expect.objectContaining({
277+
instancePath: '/source',
278+
}),
279+
]),
280+
description: 'Error parsing queue entry',
191281
});
192282

193283
expect(logger.info).toHaveBeenCalledWith(

lambdas/pdm-poll-lambda/src/__tests__/test-data.ts

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import { SQSEvent, SQSRecord } from 'aws-lambda';
2-
import { CloudEvent } from 'utils';
2+
import {
3+
PDMResourceSubmitted,
4+
PDMResourceUnavailable,
5+
} from 'digital-letters-events';
36

47
const baseEvent = {
5-
profileversion: '1.0.0',
6-
profilepublished: '2025-10',
78
id: '550e8400-e29b-41d4-a716-446655440001',
89
specversion: '1.0',
9-
source: '/nhs/england/notify/production/primary/data-plane/digital-letters',
10+
source:
11+
'/nhs/england/notify/production/primary/data-plane/digitalletters/pdm',
1012
subject:
1113
'customer/920fca11-596a-4eca-9c47-99f624614658/recipient/769acdd4-6a47-496f-999f-76a6fd2c3959',
1214
type: 'uk.nhs.notify.digital.letters.pdm.resource.submitted.v1',
@@ -17,11 +19,9 @@ const baseEvent = {
1719
datacontenttype: 'application/json',
1820
dataschema:
1921
'https://notify.nhs.uk/cloudevents/schemas/digital-letters/2025-10/digital-letter-base-data.schema.json',
20-
dataschemaversion: '1.0',
2122
severitytext: 'INFO',
2223
data: {
2324
resourceId: 'a2bcbb42-ab7e-42b6-88d6-74f8d3ca4a09',
24-
'digital-letter-id': '123e4567-e89b-12d3-a456-426614174000',
2525
messageReference: 'ref1',
2626
senderId: 'sender1',
2727
},
@@ -31,19 +31,19 @@ export const pdmResourceSubmittedEvent = {
3131
...baseEvent,
3232
type: 'uk.nhs.notify.digital.letters.pdm.resource.submitted.v1',
3333
dataschema:
34-
'https://notify.nhs.uk/cloudevents/schemas/digital-letters/2025-10/digital-letter-base-data.schema.json',
35-
} as CloudEvent;
34+
'https://notify.nhs.uk/cloudevents/schemas/digital-letters/2025-10-draft/data/digital-letters-pdm-resource-submitted-data.schema.json',
35+
} as PDMResourceSubmitted;
3636

3737
export const pdmResourceUnavailableEvent = {
3838
...baseEvent,
3939
type: 'uk.nhs.notify.digital.letters.pdm.resource.unavailable.v1',
4040
dataschema:
41-
'https://notify.nhs.uk/cloudevents/schemas/digital-letters/2025-10/digital-letter-base-data.schema.json',
41+
'https://notify.nhs.uk/cloudevents/schemas/digital-letters/2025-10-draft/data/digital-letters-pdm-resource-unavailable-data.schema.json',
4242
data: {
4343
...baseEvent.data,
4444
retryCount: 1,
4545
},
46-
} as CloudEvent;
46+
} as PDMResourceUnavailable;
4747

4848
const busEvent = {
4949
version: '0',
@@ -66,7 +66,9 @@ const sqsRecord = {
6666
awsRegion: '',
6767
} as SQSRecord;
6868

69-
export const recordEvent = (events: CloudEvent[]): SQSEvent => ({
69+
export const recordEvent = (
70+
events: (PDMResourceSubmitted | PDMResourceUnavailable)[],
71+
): SQSEvent => ({
7072
Records: events.map((event, i) => ({
7173
...sqsRecord,
7274
messageId: String(i + 1),

0 commit comments

Comments
 (0)