Skip to content

Commit f33c999

Browse files
committed
Place SNS records on queue
1 parent d66ac1b commit f33c999

2 files changed

Lines changed: 18 additions & 22 deletions

File tree

lambdas/allocation/src/__tests__/allocator.test.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ describe("allocator", () => {
7979
});
8080

8181
describe("createAllocator", () => {
82-
it("should process a single SNS record and send message to SQS", async () => {
82+
it("should place the SNS event unchanged on the SQS queue", async () => {
8383
const letterEvent = createLetterEvent("id1");
8484
const snsEvent = createSNSEvent([
8585
createSNSEventRecord(JSON.stringify(letterEvent)),
@@ -92,8 +92,8 @@ describe("allocator", () => {
9292
expect.objectContaining({
9393
input: {
9494
QueueUrl: mockQueueUrl,
95-
MessageBody: JSON.stringify(letterEvent),
96-
MessageGroupId: "id1",
95+
MessageBody: JSON.stringify(snsEvent.Records[0]),
96+
MessageGroupId: expect.any(String),
9797
},
9898
}),
9999
);
@@ -120,8 +120,8 @@ describe("allocator", () => {
120120
expect.objectContaining({
121121
input: {
122122
QueueUrl: mockQueueUrl,
123-
MessageBody: JSON.stringify(letterEvent1),
124-
MessageGroupId: "id1",
123+
MessageBody: JSON.stringify(snsEvent.Records[0]),
124+
MessageGroupId: expect.any(String),
125125
},
126126
}),
127127
);
@@ -130,8 +130,8 @@ describe("allocator", () => {
130130
expect.objectContaining({
131131
input: {
132132
QueueUrl: mockQueueUrl,
133-
MessageBody: JSON.stringify(letterEvent2),
134-
MessageGroupId: "id2",
133+
MessageBody: JSON.stringify(snsEvent.Records[1]),
134+
MessageGroupId: expect.any(String),
135135
},
136136
}),
137137
);
@@ -140,8 +140,8 @@ describe("allocator", () => {
140140
expect.objectContaining({
141141
input: {
142142
QueueUrl: mockQueueUrl,
143-
MessageBody: JSON.stringify(letterEvent3),
144-
MessageGroupId: "id3",
143+
MessageBody: JSON.stringify(snsEvent.Records[2]),
144+
MessageGroupId: expect.any(String),
145145
},
146146
}),
147147
);
Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
11
import { SNSEvent, SNSEventRecord, SNSHandler } from "aws-lambda";
22
import { SendMessageCommand } from "@aws-sdk/client-sqs";
3-
import {
4-
$LetterEvent,
5-
LetterEvent,
6-
} from "@nhsdigital/nhs-notify-event-schemas-supplier-api/src";
3+
import { randomUUID } from "node:crypto";
4+
75
import { Deps } from "./deps";
86

97
export default function createAllocator(deps: Deps): SNSHandler {
108
return async (event: SNSEvent): Promise<void> => {
119
// Allocation will be done under a future ticket. For now, just place events on the queue,
1210
// adding a message group ID to permit the use of a FIFO queue
1311
const sqsCommands: SendMessageCommand[] = event.Records.map((record) =>
14-
extractLetterEvent(record),
15-
).map((letterEvent) => buildSendMessageCommand(letterEvent, deps.queueUrl));
12+
buildSendMessageCommand(record, deps.queueUrl),
13+
);
1614

1715
for (const sqsCommand of sqsCommands) {
1816
deps.logger.info({
@@ -24,14 +22,12 @@ export default function createAllocator(deps: Deps): SNSHandler {
2422
};
2523
}
2624

27-
function extractLetterEvent(record: SNSEventRecord): LetterEvent {
28-
return $LetterEvent.parse(JSON.parse(record.Sns.Message));
29-
}
30-
31-
function buildSendMessageCommand(letterEvent: LetterEvent, queueUrl: string) {
25+
function buildSendMessageCommand(snsRecord: SNSEventRecord, queueUrl: string) {
3226
return new SendMessageCommand({
3327
QueueUrl: queueUrl,
34-
MessageBody: JSON.stringify(letterEvent),
35-
MessageGroupId: letterEvent.data.domainId,
28+
MessageBody: JSON.stringify(snsRecord),
29+
// Using a random UUID here effectively means that the amendments queue is not FIFO for new pending
30+
// letters. Pragmatically this is OK because we shouldn't be getting updates from the supplier yet.
31+
MessageGroupId: randomUUID(),
3632
});
3733
}

0 commit comments

Comments
 (0)