|
1 | 1 | import { SQSBatchItemFailure, SQSEvent, SQSHandler } from "aws-lambda"; |
2 | 2 | import { InsertLetter, UpdateLetter } from "@internal/datastore"; |
| 3 | +import { $LetterRequestPreparedEvent } from "@nhsdigital/nhs-notify-event-schemas-letter-rendering-v1"; |
3 | 4 | import { |
4 | | - $LetterRequestPreparedEvent, |
5 | | - LetterRequestPreparedEvent, |
6 | | -} from "@nhsdigital/nhs-notify-event-schemas-letter-rendering-v1"; |
7 | | -import { |
8 | | - $LetterEvent, |
9 | | - LetterEvent, |
| 5 | + $LetterStatusChangeEvent, |
| 6 | + LetterStatusChangeEvent, |
10 | 7 | } from "@nhsdigital/nhs-notify-event-schemas-supplier-api/src/events/letter-events"; |
11 | | -import { |
12 | | - $LetterRequestPreparedEventV2, |
13 | | - LetterRequestPreparedEventV2, |
14 | | -} from "@nhsdigital/nhs-notify-event-schemas-letter-rendering"; |
15 | | -import z from "zod"; |
| 8 | +import { $LetterRequestPreparedEventV2 } from "@nhsdigital/nhs-notify-event-schemas-letter-rendering"; |
16 | 9 | import { MetricsLogger, Unit, metricScope } from "aws-embedded-metrics"; |
17 | 10 | import { Deps } from "../config/deps"; |
18 | | - |
19 | | -type PreparedEvents = LetterRequestPreparedEventV2 | LetterRequestPreparedEvent; |
20 | | - |
21 | | -const SupplierSpecSchema = z.object({ |
22 | | - supplierId: z.string().min(1), |
23 | | - specId: z.string().min(1), |
24 | | - priority: z.int().min(0).max(99).default(10), |
25 | | - billingId: z.string().min(1), |
26 | | -}); |
27 | | - |
28 | | -type SupplierSpec = z.infer<typeof SupplierSpecSchema>; |
29 | | - |
30 | | -const PreparedEventUnionSchema = z.discriminatedUnion("type", [ |
31 | | - $LetterRequestPreparedEventV2, |
32 | | - $LetterRequestPreparedEvent, |
33 | | -]); |
34 | | - |
35 | | -const QueueMessageSchema = z.union([ |
36 | | - $LetterEvent, |
37 | | - z.object({ |
38 | | - letterEvent: PreparedEventUnionSchema, |
39 | | - supplierSpec: SupplierSpecSchema, |
40 | | - }), |
41 | | -]); |
42 | | - |
43 | | -type QueueMessage = z.infer<typeof QueueMessageSchema>; |
44 | | - |
45 | | -type UpsertOperation = { |
46 | | - name: "Insert" | "Update"; |
47 | | - schemas: z.ZodSchema[]; |
48 | | - handler: ( |
49 | | - request: unknown, |
50 | | - supplierSpec: SupplierSpec, |
51 | | - deps: Deps, |
52 | | - ) => Promise<void>; |
53 | | -}; |
| 11 | +import { |
| 12 | + PreparedEvents, |
| 13 | + QueueMessage, |
| 14 | + QueueMessageSchema, |
| 15 | + SupplierSpec, |
| 16 | + UpsertOperation, |
| 17 | +} from "./schemas"; |
54 | 18 |
|
55 | 19 | function getOperationFromType(type: string): UpsertOperation { |
56 | 20 | if ( |
@@ -83,9 +47,9 @@ function getOperationFromType(type: string): UpsertOperation { |
83 | 47 | // if it's not an insert type, it must be an update as we've already parsed the message, but we want to have a separate operation for better logging and metrics |
84 | 48 | return { |
85 | 49 | name: "Update", |
86 | | - schemas: [$LetterEvent], |
| 50 | + schemas: [$LetterStatusChangeEvent], |
87 | 51 | handler: async (request, supplierSpec, deps) => { |
88 | | - const supplierEvent = request as LetterEvent; |
| 52 | + const supplierEvent = request as LetterStatusChangeEvent; |
89 | 53 | const letterToUpdate: UpdateLetter = mapToUpdateLetter(supplierEvent); |
90 | 54 | await deps.letterRepo.updateLetterStatus(letterToUpdate); |
91 | 55 |
|
@@ -129,7 +93,7 @@ function mapToInsertLetter( |
129 | 93 | }; |
130 | 94 | } |
131 | 95 |
|
132 | | -function mapToUpdateLetter(upsertRequest: LetterEvent): UpdateLetter { |
| 96 | +function mapToUpdateLetter(upsertRequest: LetterStatusChangeEvent): UpdateLetter { |
133 | 97 | return { |
134 | 98 | id: upsertRequest.data.domainId, |
135 | 99 | eventId: upsertRequest.id, |
@@ -216,7 +180,7 @@ export default function createUpsertLetterHandler(deps: Deps): SQSHandler { |
216 | 180 |
|
217 | 181 | const queueMessage: QueueMessage = parseQueueMessage(sqsMessage); |
218 | 182 |
|
219 | | - let letterEvent: LetterEvent | PreparedEvents; |
| 183 | + let letterEvent: LetterStatusChangeEvent | PreparedEvents; |
220 | 184 | let supplierSpec: SupplierSpec | undefined; |
221 | 185 |
|
222 | 186 | if ("letterEvent" in queueMessage) { |
|
0 commit comments