Sort of working, about to refactor into a separate ts file

This commit is contained in:
Martin Donnelly 2024-08-06 14:53:50 +01:00
parent e0aec266d6
commit 81d7281e00
4 changed files with 117 additions and 2 deletions

View File

@ -3,7 +3,7 @@
"version": "0.1.0", "version": "0.1.0",
"scripts": { "scripts": {
"build": "tsup", "build": "tsup",
"start:server": "node external", "start:server": "node external-service",
"invoke": "node app", "invoke": "node app",
"test": "vitest" "test": "vitest"
}, },

View File

@ -1,5 +1,82 @@
import { KinesisStreamEvent } from 'aws-lambda'; 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) => { 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');
}

0
src/processor.ts Normal file
View File

View File

@ -2,4 +2,42 @@ describe('myTSTest', () => {
it('should pass', () => { it('should pass', () => {
expect(1).toBe(1); 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);
});
});
}); });