Sort of working, about to refactor into a separate ts file
This commit is contained in:
parent
e0aec266d6
commit
81d7281e00
@ -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"
|
||||
},
|
||||
|
79
src/index.ts
79
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');
|
||||
|
||||
|
||||
}
|
0
src/processor.ts
Normal file
0
src/processor.ts
Normal file
@ -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);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user