diff --git a/package.json b/package.json index 593d226..f85fb76 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,7 @@ "version": "0.1.0", "scripts": { "build": "tsup", - "start:server": "node external", + "start:server": "node external-service", "invoke": "node app", "test": "vitest" }, diff --git a/src/index.ts b/src/index.ts index 63e6df0..48590ee 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,5 +1,82 @@ import { KinesisStreamEvent } from 'aws-lambda'; +import { KinesisStreamRecord } from 'aws-lambda/trigger/kinesis-stream'; + +/* +Making an assumption here that the eventblock layout is fairly static and that +object fields such as booking_requested will only exist in the booking event, +and other fields may be substituted in for other event types. + +*/ +export interface EventBlock { + id: string; + partitionKey: string; + timestamp: number; + type: string; + booking_requested?: BookingRequested; +} + +export interface BookingRequested { + timestamp: number; + orderId: number; + product_provider: string; +} export const handler = (event: KinesisStreamEvent) => { - console.log(event); + console.log('!!'); + // console.log(event); + + for (const elm of event.Records) { + console.log(elm); + console.log('>-<'); + processEvent(elm); + } }; + +function processEvent(event: KinesisStreamRecord) { + console.log(JSON.stringify(event)); + + let data: EventBlock | null = decodePayload(event.kinesis.data); + + console.log('DecodePayload:: ', JSON.stringify(data)); + + if (data === null) { + return null; + } + + if (data.hasOwnProperty('type')) { + switch (data.type) { + case 'booking_requested': + console.log('booking_requested'); + transformPayload(data); + break; + default: + console.log('Default'); + } + } +} + +function decodePayload(data: any): EventBlock | null { + if (data === null) { + return null; + } + + console.log('-----'); + console.log(data); + console.log(atob(data)); + console.log('-----'); + + try { + const eventBlock: EventBlock = JSON.parse(atob(data)); + + return eventBlock; + } catch (err) { + console.error(err); + return null; + } +} + +function transformPayload(data: EventBlock) { + console.log('Transform and roll out'); + + +} \ No newline at end of file diff --git a/src/processor.ts b/src/processor.ts new file mode 100644 index 0000000..e69de29 diff --git a/test/index.test.ts b/test/index.test.ts index fe9f3be..6c4fb0b 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -2,4 +2,42 @@ describe('myTSTest', () => { it('should pass', () => { expect(1).toBe(1); }); + + describe('decodePayload', () => { + const properDataBase64 = + 'eyJpZCI6ImE0Mzg4MTMyLTE0OTItMTFlYy1hMGIyLWM3OGZmYmQ2OTM0NyIsInBhcnRpdGlvbktleSI6IjJmY2U1M2YxLTFkYWYtNDI0Ny05MjQ1LTBhZDc4ZDBiMGQyZSIsInRpbWVzdGFtcCI6MTYzMTUzODA1OTQ1OSwidHlwZSI6ImJvb2tpbmdfcmVxdWVzdGVkIiwiYm9va2luZ19yZXF1ZXN0ZWQiOnsidGltZXN0YW1wIjoxNjMxNTM4MDU5NDU5LCJvcmRlcklkIjoxMDAxNywicHJvZHVjdF9wcm92aWRlciI6IlN0ZW5hIExpbmUifX0='; + const properDecodedData = { + id: 'a4388132-1492-11ec-a0b2-c78ffbd69347', + partitionKey: '2fce53f1-1daf-4247-9245-0ad78d0b0d2e', + timestamp: 1631538059459, + type: 'booking_requested', + booking_requested: { + timestamp: 1631538059459, + orderId: 10017, + product_provider: 'Stena Line', + }, + }; + + it('should pass', () => { + expect().toEqual(properDecodedData); + }); + }); + + describe('processEvent', () => { + it('should pass', () => { + expect(1).toBe(1); + }); + }); + + describe('transformPayload', () => { + it('should pass', () => { + expect(1).toBe(1); + }); + }); + + describe('handler', () => { + it('should pass', () => { + expect(1).toBe(1); + }); + }); });