Skip to content

Commit 028305e

Browse files
committed
CCM-11343: create a schema which is a subset of db template
1 parent 2d5de86 commit 028305e

File tree

4 files changed

+28
-21
lines changed

4 files changed

+28
-21
lines changed

lambdas/backend-client/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ export * from './types/result';
44
export * from './schemas/template-schema';
55
export * from './schemas/client';
66
export * from './schemas/union-lists';
7+
export * from './schemas/schema-for';
78
export * from './client-configuration-api-client';

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

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
import { Logger } from 'nhs-notify-web-template-management-utils/logger';
2-
import { PublishableEventRecord } from './input-schemas';
2+
import { $DynamoDBTemplate, PublishableEventRecord } from './input-schemas';
33
import { Event, $Event } from './output-schemas';
44
import { unmarshall } from '@aws-sdk/util-dynamodb';
55
import {
66
EventMetadata,
77
EventMetadataVersionInformation,
88
} from './base-metadata-schemas';
99
import { shouldPublish } from './should-publish';
10-
import { $TemplateDtoSchema } from 'nhs-notify-backend-client';
1110

1211
export class EventBuilder {
1312
constructor(
@@ -75,12 +74,7 @@ export class EventBuilder {
7574

7675
const dynamoRecord = unmarshall(publishableEventRecord.dynamodb.NewImage);
7776

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);
77+
const databaseTemplate = $DynamoDBTemplate.parse(dynamoRecord);
8478

8579
if (!shouldPublish(databaseTemplate)) {
8680
this.logger.debug({
@@ -94,8 +88,8 @@ export class EventBuilder {
9488
return $Event.parse({
9589
...this.buildTemplateSavedEventMetadata(
9690
publishableEventRecord.eventID,
97-
dynamoRecord.templateStatus,
98-
dynamoRecord.id
91+
databaseTemplate.templateStatus,
92+
databaseTemplate.id
9993
),
10094
data: dynamoRecord,
10195
});

lambdas/event-publisher/src/domain/input-schemas.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
import { z } from 'zod';
2-
import { AttributeValue } from '@aws-sdk/client-dynamodb';
2+
import type { AttributeValue } from '@aws-sdk/client-dynamodb';
3+
import {
4+
schemaFor,
5+
TEMPLATE_STATUS_LIST,
6+
TEMPLATE_TYPE_LIST,
7+
} from 'nhs-notify-backend-client';
8+
import type { DatabaseTemplate } from 'nhs-notify-web-template-management-utils';
39

410
const $AttributeValue: z.ZodType<AttributeValue> = z.lazy(() =>
511
z.union([
@@ -25,6 +31,18 @@ export const $DynamoDBStreamRecord = z.object({
2531
tableName: z.string(),
2632
});
2733

34+
// We need a subset of the database fields so we can apply filtering rules
35+
export const $DynamoDBTemplate = schemaFor<Partial<DatabaseTemplate>>()(
36+
z.object({
37+
id: z.string(),
38+
templateType: z.enum(TEMPLATE_TYPE_LIST),
39+
templateStatus: z.enum(TEMPLATE_STATUS_LIST),
40+
proofingEnabled: z.boolean().optional(),
41+
})
42+
);
43+
44+
export type DynamoDBTemplate = z.infer<typeof $DynamoDBTemplate>;
45+
2846
// the lambda doesn't necessarily have to only accept inputs from a dynamodb stream via an
2947
// eventbridge pipe, but that's all it is doing at the moment
3048
export const $PublishableEventRecord = $DynamoDBStreamRecord;

lambdas/event-publisher/src/domain/should-publish.ts

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,20 @@
1-
import {
2-
TemplateStatus,
3-
TemplateDto,
4-
LetterProperties,
5-
} from 'nhs-notify-backend-client';
1+
import type { DynamoDBTemplate } from './input-schemas';
62

7-
type LetterTemplateDto = TemplateDto & LetterProperties;
8-
9-
const publishableLetterStatuses = new Set<TemplateStatus>([
3+
const publishableLetterStatuses = new Set<DynamoDBTemplate['templateStatus']>([
104
'DELETED',
115
'PENDING_PROOF_REQUEST',
126
'PROOF_AVAILABLE',
137
'SUBMITTED',
148
'WAITING_FOR_PROOF',
159
]);
1610

17-
function shouldPublishLetter(data: LetterTemplateDto): boolean {
11+
function shouldPublishLetter(data: DynamoDBTemplate): boolean {
1812
return (
1913
publishableLetterStatuses.has(data.templateStatus) && !!data.proofingEnabled
2014
);
2115
}
2216

23-
export function shouldPublish(data: TemplateDto) {
17+
export function shouldPublish(data: DynamoDBTemplate) {
2418
if (data.templateType === 'LETTER') {
2519
return shouldPublishLetter(data);
2620
}

0 commit comments

Comments
 (0)