All files / src/lambda deviceTableStreamHandler.ts

100% Statements 24/24
100% Branches 6/6
100% Functions 3/3
100% Lines 24/24

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 493x           3x 3x 3x 3x   3x   3x   2x 2x   2x 7x 7x 5x   7x     2x 5x 5x   1x 1x     1x 1x     1x 1x     2x            
import {
  withMiddlewares,
  bootstrapNestJSMiddleware,
  types,
} from '@npco/component-core';
import { Context, Handler } from 'aws-lambda';
import { DepositService } from '../deposits/depositService';
import { TransactionService } from '../transactions/transactionService';
import { logMiddleware } from '../middlewares';
import { AppModule } from '../appModule';
import { JsonObject } from '../types.d';
import { DeviceService } from '../devices';
 
export const onDeviceTableStreamHandler: Handler = withMiddlewares(
  (event: any, context: Context) => {
    const { app } = context as types.AppContext;
    const stream: JsonObject<any> = {};
 
    event.Records.forEach((record: any) => {
      const eventType = record.dynamodb.Keys.type.S.split('.')[0];
      if (typeof stream[eventType] === 'undefined') {
        stream[eventType] = [];
      }
      stream[eventType].push(record);
    });
 
    Object.keys(stream).forEach((eventType) => {
      const records = stream[eventType];
      switch (eventType) {
        case 'deposit': {
          app.get(DepositService).onDepositStream(records);
          break;
        }
        case 'transaction': {
          app.get(TransactionService).onTransactionStream(records);
          break;
        }
        case 'settings': {
          app.get(DeviceService).onDeviceSettingsStream(records);
          break;
        }
        default:
          break;
      }
    });
  },
  [logMiddleware, bootstrapNestJSMiddleware(AppModule)],
);