Skip to content

Commit 2d5de86

Browse files
committed
CCM-11343: remove filters from pipes and implement in code
1 parent dbf319e commit 2d5de86

File tree

5 files changed

+121
-37
lines changed

5 files changed

+121
-37
lines changed

infrastructure/terraform/modules/backend-api/pipes_pipe_template_table_events.tf

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -19,43 +19,6 @@ resource "aws_pipes_pipe" "template_table_events" {
1919
arn = module.sqs_template_table_events_pipe_dlq.sqs_queue_arn
2020
}
2121
}
22-
23-
filter_criteria {
24-
filter {
25-
pattern = jsonencode({
26-
"$or" = [
27-
{
28-
# Allow all non-LETTER templates
29-
dynamodb = {
30-
NewImage = {
31-
templateType = {
32-
S = [{ "anything-but" = "LETTER" }]
33-
}
34-
}
35-
}
36-
eventName = ["INSERT", "MODIFY"]
37-
},
38-
{
39-
# Allow LETTER templates only when proofingEnabled and valid status
40-
dynamodb = {
41-
NewImage = {
42-
templateType = {
43-
S = ["LETTER"]
44-
}
45-
templateStatus = {
46-
S = ["PROOF_AVAILABLE", "SUBMITTED", "DELETED"]
47-
}
48-
proofingEnabled = {
49-
BOOL = [true]
50-
}
51-
}
52-
}
53-
eventName = ["INSERT", "MODIFY"]
54-
}
55-
]
56-
})
57-
}
58-
}
5922
}
6023

6124
target_parameters {

lambdas/event-publisher/src/__tests__/domain/event-builder.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,22 @@ import { mockDeep } from 'jest-mock-extended';
22
import { EventBuilder } from '../../domain/event-builder';
33
import { Logger } from 'nhs-notify-web-template-management-utils/logger';
44
import { PublishableEventRecord } from '../../domain/input-schemas';
5+
import { shouldPublish } from '../../domain/should-publish';
6+
7+
jest.mock('../../domain/should-publish');
58

69
beforeAll(() => {
710
jest.useFakeTimers();
811
jest.setSystemTime(new Date('2022-01-01 09:00'));
912
});
1013

14+
beforeEach(() => {
15+
jest.resetAllMocks();
16+
shouldPublishMock.mockReturnValueOnce(true);
17+
});
18+
19+
const shouldPublishMock = jest.mocked(shouldPublish);
20+
1121
const mockLogger = mockDeep<Logger>();
1222

1323
const eventBuilder = new EventBuilder('table-name', 'event-source', mockLogger);
@@ -52,6 +62,9 @@ const publishableEventRecord = (status: string): PublishableEventRecord => ({
5262
letterType: {
5363
S: 'x0',
5464
},
65+
proofingEnabled: {
66+
BOOL: true,
67+
},
5568
files: {
5669
M: {
5770
pdfTemplate: {
@@ -175,6 +188,17 @@ test('builds template deleted event', () => {
175188
);
176189
});
177190

191+
test('should return undefined when not a publishable event', () => {
192+
shouldPublishMock.mockReset();
193+
shouldPublishMock.mockReturnValueOnce(false);
194+
195+
const event = eventBuilder.buildEvent(
196+
publishableEventRecord('PROOF_AVAILABLE')
197+
);
198+
199+
expect(event).toEqual(undefined);
200+
});
201+
178202
test('does not build template event on hard delete', () => {
179203
const hardDeletePublishableEventRecord = {
180204
...publishableEventRecord('SUBMITTED'),
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import {
2+
LetterProperties,
3+
TemplateDto,
4+
TemplateStatus,
5+
TemplateType,
6+
} from 'nhs-notify-backend-client';
7+
import { shouldPublish } from '../../domain/should-publish';
8+
9+
describe('shouldPublish', () => {
10+
test.each(['EMAIL', 'SMS', 'NHS_APP'] satisfies TemplateType[])(
11+
'templateType %p should return true',
12+
(type) => {
13+
expect(shouldPublish({ templateType: type } as TemplateDto)).toEqual(
14+
true
15+
);
16+
}
17+
);
18+
19+
test.each([false, undefined])(
20+
'templateType LETTER should return false when proofingEnabled %p',
21+
(proofingEnabled) => {
22+
expect(
23+
shouldPublish({
24+
templateType: 'LETTER',
25+
templateStatus: 'SUBMITTED',
26+
proofingEnabled,
27+
} as TemplateDto & LetterProperties)
28+
).toEqual(false);
29+
}
30+
);
31+
32+
test.each([
33+
'DELETED',
34+
'PENDING_PROOF_REQUEST',
35+
'PROOF_AVAILABLE',
36+
'SUBMITTED',
37+
'WAITING_FOR_PROOF',
38+
] satisfies TemplateStatus[])(
39+
'templateType LETTER should return true when templateStatus is %p and proofingEnabled is true',
40+
(templateStatus) => {
41+
expect(
42+
shouldPublish({
43+
templateType: 'LETTER',
44+
templateStatus,
45+
proofingEnabled: true,
46+
} as TemplateDto & LetterProperties)
47+
).toEqual(true);
48+
}
49+
);
50+
});

lambdas/event-publisher/src/domain/event-builder.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import {
66
EventMetadata,
77
EventMetadataVersionInformation,
88
} from './base-metadata-schemas';
9+
import { shouldPublish } from './should-publish';
10+
import { $TemplateDtoSchema } from 'nhs-notify-backend-client';
911

1012
export class EventBuilder {
1113
constructor(
@@ -73,6 +75,22 @@ export class EventBuilder {
7375

7476
const dynamoRecord = unmarshall(publishableEventRecord.dynamodb.NewImage);
7577

78+
// This isn't strictly correct. the DTO schema is slightly different from the DB schema.
79+
// I guess this should be a Zod schema of the database schema
80+
// Also doing this may cause this lambda to stop being backwards compatible? I.E. we add a new field
81+
// then older templates being updated without the field will cause errors.
82+
// Or I just rely on the any typing...
83+
const databaseTemplate = $TemplateDtoSchema.parse(dynamoRecord);
84+
85+
if (!shouldPublish(databaseTemplate)) {
86+
this.logger.debug({
87+
description: 'Not publishing event',
88+
publishableEventRecord,
89+
});
90+
91+
return undefined;
92+
}
93+
7694
return $Event.parse({
7795
...this.buildTemplateSavedEventMetadata(
7896
publishableEventRecord.eventID,
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import {
2+
TemplateStatus,
3+
TemplateDto,
4+
LetterProperties,
5+
} from 'nhs-notify-backend-client';
6+
7+
type LetterTemplateDto = TemplateDto & LetterProperties;
8+
9+
const publishableLetterStatuses = new Set<TemplateStatus>([
10+
'DELETED',
11+
'PENDING_PROOF_REQUEST',
12+
'PROOF_AVAILABLE',
13+
'SUBMITTED',
14+
'WAITING_FOR_PROOF',
15+
]);
16+
17+
function shouldPublishLetter(data: LetterTemplateDto): boolean {
18+
return (
19+
publishableLetterStatuses.has(data.templateStatus) && !!data.proofingEnabled
20+
);
21+
}
22+
23+
export function shouldPublish(data: TemplateDto) {
24+
if (data.templateType === 'LETTER') {
25+
return shouldPublishLetter(data);
26+
}
27+
28+
return true;
29+
}

0 commit comments

Comments
 (0)