|
1 | | -import { SQSRecord } from 'aws-lambda'; |
| 1 | +import { SQSRecord} from 'aws-lambda'; |
2 | 2 | import { Logger } from 'utils'; |
3 | | -import { NudgeCommand } from 'domain/nudge-command'; |
4 | | -import { $NudgeCommand } from 'app/nudge-command-validator'; |
| 3 | +import { PDMResourceAvailable } from 'digital-letters-events'; |
| 4 | +import { InvalidPdmResourceAvailableEvent } from 'domain/invalid-pdm-resource-available-event'; |
| 5 | +import { messageDownloadedValidator } from 'digital-letters-events/PDMResourceAvailable.js'; |
5 | 6 |
|
6 | 7 | export const parseSqsRecord = ( |
7 | 8 | sqsRecord: SQSRecord, |
8 | 9 | logger: Logger, |
9 | | -): NudgeCommand => { |
| 10 | +): PDMResourceAvailable => { |
10 | 11 | logger.info('Parsing SQS Record', { |
11 | 12 | messageId: sqsRecord.messageId, |
12 | 13 | }); |
| 14 | + const sqsEventBody = JSON.parse(sqsRecord.body); |
| 15 | + const sqsEventDetail = sqsEventBody.detail; |
| 16 | + const isEventValid = messageDownloadedValidator(sqsEventDetail); |
| 17 | + if(!isEventValid) { |
| 18 | + logger.error({ |
| 19 | + err: messageDownloadedValidator.errors, |
| 20 | + description: 'The SQS message does not contain a valid PDMResourceAvailable event', |
| 21 | + messageId: sqsRecord.messageId, |
| 22 | + }); |
| 23 | + throw new InvalidPdmResourceAvailableEvent(sqsRecord.messageId); |
| 24 | + } |
13 | 25 |
|
14 | | - const jsonParsed = JSON.parse(sqsRecord.body) as NudgeCommand; |
15 | | - const zodParsed = $NudgeCommand.parse(jsonParsed); |
16 | | - |
17 | | - logger.info('Parsed SQS Record as Nudge Command Event', { |
18 | | - messageId: sqsRecord.messageId, |
19 | | - sourceEventId: zodParsed.sourceEventId, |
| 26 | + logger.info('Parsed valid PDMResourceAvailable Event', { |
| 27 | + messageId: sqsRecord.messageId |
20 | 28 | }); |
21 | 29 |
|
22 | | - return zodParsed; |
| 30 | + return sqsEventDetail as PDMResourceAvailable; |
23 | 31 | }; |
0 commit comments