Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ module "letter_updates_transformer" {
log_subscription_role_arn = local.acct.log_subscription_role_arn

lambda_env_vars = merge(local.common_lambda_env_vars, {
EVENTPUB_SNS_TOPIC_ARN = "${module.eventpub.sns_topic.arn}"
EVENTPUB_SNS_TOPIC_ARN = "${module.eventpub.sns_topic.arn}",
EVENT_SOURCE = "/data-plane/supplier-api/${var.group}/${var.environment}/letters"
})
}

Expand Down
2 changes: 1 addition & 1 deletion internal/events/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,5 @@
"typecheck": "tsc --noEmit"
},
"types": "dist/index.d.ts",
"version": "1.0.5"
"version": "1.0.6"
}
2 changes: 1 addition & 1 deletion internal/events/schemas/examples/letter.ACCEPTED.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"recordedtime": "2025-08-28T08:45:00.000Z",
"severitynumber": 2,
"severitytext": "INFO",
"source": "/data-plane/supplier-api/prod/update-status",
"source": "/data-plane/supplier-api/nhs-supplier-api-prod/main/update-status",
"specversion": "1.0",
"subject": "letter-origin/letter-rendering/letter/f47ac10b-58cc-4372-a567-0e02b2c3d479",
"time": "2025-08-28T08:45:00.000Z",
Expand Down
2 changes: 1 addition & 1 deletion internal/events/schemas/examples/letter.FORWARDED.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"recordedtime": "2025-08-28T08:45:00.000Z",
"severitynumber": 2,
"severitytext": "INFO",
"source": "/data-plane/supplier-api/prod/update-status",
"source": "/data-plane/supplier-api/nhs-supplier-api-prod/main/update-status",
"specversion": "1.0",
"subject": "letter-origin/letter-rendering/letter/f47ac10b-58cc-4372-a567-0e02b2c3d479",
"time": "2025-08-28T08:45:00.000Z",
Expand Down
2 changes: 1 addition & 1 deletion internal/events/schemas/examples/letter.RETURNED.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"recordedtime": "2025-08-28T08:45:00.000Z",
"severitynumber": 2,
"severitytext": "INFO",
"source": "/data-plane/supplier-api/prod/update-status",
"source": "/data-plane/supplier-api/nhs-supplier-api-prod/main/update-status",
"specversion": "1.0",
"subject": "letter-origin/letter-rendering/letter/f47ac10b-58cc-4372-a567-0e02b2c3d479",
"time": "2025-08-28T08:45:00.000Z",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,18 @@ jest.mock("crypto", () => ({
randomBytes: (size: number) => randomBytes[String(size)],
}));

describe("letter-updates-transformer Lambda", () => {
const mockedDeps: jest.Mocked<Deps> = {
const eventSource = "/data-plane/supplier-api/nhs-supplier-api-dev/main/letters";
const mockedDeps: jest.Mocked<Deps> = {
snsClient: { send: jest.fn() } as unknown as SNSClient,
logger: { info: jest.fn(), error: jest.fn() } as unknown as pino.Logger,
env: {
EVENTPUB_SNS_TOPIC_ARN: "arn:aws:sns:region:account:topic",
EVENT_SOURCE: "/data-plane/supplier-api/nhs-supplier-api-dev/main/letters"
} as unknown as EnvVars,
Comment thread
masl2 marked this conversation as resolved.
Outdated
} as Deps;

describe("letter-updates-transformer Lambda", () => {

beforeEach(() => {
jest.useFakeTimers();
});
Expand All @@ -50,7 +53,7 @@ describe("letter-updates-transformer Lambda", () => {
const newLetter = generateLetter("PRINTED");
const expectedEntries = [
expect.objectContaining({
Message: JSON.stringify(mapLetterToCloudEvent(newLetter)),
Message: JSON.stringify(mapLetterToCloudEvent(newLetter, eventSource)),
}),
];

Expand All @@ -76,7 +79,7 @@ describe("letter-updates-transformer Lambda", () => {
newLetter.reasonCode = "R1";
const expectedEntries = [
expect.objectContaining({
Message: JSON.stringify(mapLetterToCloudEvent(newLetter)),
Message: JSON.stringify(mapLetterToCloudEvent(newLetter, eventSource)),
}),
];

Expand All @@ -103,7 +106,7 @@ describe("letter-updates-transformer Lambda", () => {
newLetter.reasonCode = "R2";
const expectedEntries = [
expect.objectContaining({
Message: JSON.stringify(mapLetterToCloudEvent(newLetter)),
Message: JSON.stringify(mapLetterToCloudEvent(newLetter, eventSource)),
}),
];

Expand Down Expand Up @@ -168,7 +171,7 @@ describe("letter-updates-transformer Lambda", () => {
const newLetters = generateLetters(10, "PRINTED");
const expectedEntries = newLetters.map((letter) =>
expect.objectContaining({
Message: JSON.stringify(mapLetterToCloudEvent(letter)),
Message: JSON.stringify(mapLetterToCloudEvent(letter, eventSource)),
}),
);

Expand Down Expand Up @@ -197,19 +200,19 @@ describe("letter-updates-transformer Lambda", () => {
newLetters.slice(0, 10).map((letter, index) =>
expect.objectContaining({
Id: expect.stringMatching(new RegExp(`-${index}$`)),
Message: JSON.stringify(mapLetterToCloudEvent(letter)),
Message: JSON.stringify(mapLetterToCloudEvent(letter, eventSource)),
}),
),
newLetters.slice(10, 20).map((letter, index) =>
expect.objectContaining({
Id: expect.stringMatching(new RegExp(`-${index}$`)),
Message: JSON.stringify(mapLetterToCloudEvent(letter)),
Message: JSON.stringify(mapLetterToCloudEvent(letter, eventSource)),
}),
),
newLetters.slice(20).map((letter, index) =>
expect.objectContaining({
Id: expect.stringMatching(new RegExp(`-${index}$`)),
Message: JSON.stringify(mapLetterToCloudEvent(letter)),
Message: JSON.stringify(mapLetterToCloudEvent(letter, eventSource)),
}),
),
];
Expand Down
1 change: 1 addition & 0 deletions lambdas/letter-updates-transformer/src/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { z } from "zod";

const EnvVarsSchema = z.object({
EVENTPUB_SNS_TOPIC_ARN: z.string(),
EVENT_SOURCE: z.string(),
});

export type EnvVars = z.infer<typeof EnvVarsSchema>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ export default function createHandler(deps: Deps): Handler<KinesisStreamEvent> {
isChanged(record, "status") || isChanged(record, "reasonCode"),
)
.map((element) => extractNewLetter(element))
.map((element) => mapLetterToCloudEvent(element));
.map((element) => mapLetterToCloudEvent(element, deps.env.EVENT_SOURCE));

for (const batch of generateBatches(cloudEvents)) {
deps.logger.info({
description: "Publishing batch",
description: "Publishing batch",
Comment thread
masl2 marked this conversation as resolved.
Outdated
Comment thread
masl2 marked this conversation as resolved.
Outdated
size: batch.length,
letterEvents: batch,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,16 @@ describe("letter-mapper", () => {
reasonText: "Reason text",
updatedAt: "2025-11-24T15:55:18.000Z",
} as Letter;
const event = mapLetterToCloudEvent(letter);
const source = "/data-plane/supplier-api/nhs-supplier-api-dev/main/letters";
const event = mapLetterToCloudEvent(letter, source);

// Check it conforms to the letter event schema - parse will throw an error if not
$LetterEvent.parse(event);
expect(event.type).toBe("uk.nhs.notify.supplier-api.letter.PRINTED.v1");
expect(event.dataschema).toBe(
`https://notify.nhs.uk/cloudevents/schemas/supplier-api/letter.PRINTED.${event.dataschemaversion}.schema.json`,
);
expect(event.dataschemaversion).toBe("1.0.5");
expect(event.dataschemaversion).toBe("1.0.6");
expect(event.subject).toBe("letter-origin/supplier-api/letter/id1");
expect(event.time).toBe("2025-11-24T15:55:18.000Z");
expect(event.recordedtime).toBe("2025-11-24T15:55:18.000Z");
Expand All @@ -41,5 +42,6 @@ describe("letter-mapper", () => {
event: event.id,
},
});
expect(event.source).toBe(source);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { LetterForEventPub } from "../types";

export default function mapLetterToCloudEvent(
letter: LetterForEventPub,
source: string
): LetterEvent {
const eventId = randomUUID();
const dataschemaversion = eventSchemaPackage.version;
Expand All @@ -15,7 +16,7 @@ export default function mapLetterToCloudEvent(
plane: "data",
dataschema: `https://notify.nhs.uk/cloudevents/schemas/supplier-api/letter.${letter.status}.${dataschemaversion}.schema.json`,
dataschemaversion,
source: "/data-plane/supplier-api/letters",
source: source,
subject: `letter-origin/supplier-api/letter/${letter.id}`,

data: {
Expand Down
Loading