All files / src/deposits depositService.ts

100% Statements 43/43
100% Branches 13/13
100% Functions 12/12
100% Lines 36/36

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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 998x 8x 8x 8x             8x       8x         8x     8x       38x 38x 38x 38x 38x   38x   38x               38x 2x 2x 1x   1x     38x     3x 3x     3x 3x 3x 3x 2x     2x   1x       1x                               38x 1x 1x           38x 2x      
import { Injectable } from '@nestjs/common';
import gql from 'graphql-tag';
import { DepositDb } from './depositDb';
import { DeviceService, DeviceVerification } from '../devices';
import {
  DepositQueryInput,
  DepositConnection,
  Deposit,
  EntityUuidGsiKeySchema,
} from '../types.d';
import {
  convertDbItemToDeposit,
  convertDbStreamToDeposit,
} from './depositUtils';
import {
  DynamodbService,
  EnvironmentService,
  AppSyncClient,
} from '../common_services';
import { UpdateEventPublisher } from '../common_interfaces';
 
@Injectable()
export class DepositService extends UpdateEventPublisher {
  depositDb: DepositDb;
 
  constructor(
    private readonly dbService: DynamodbService,
    private readonly envService: EnvironmentService,
    private readonly deviceService: DeviceService,
    private readonly deviceVerify: DeviceVerification,
    private readonly appsyncClient: AppSyncClient,
  ) {
    super(appsyncClient, (deviceUuid) => this.deviceService.getDeviceStatus(deviceUuid));
 
    this.depositDb = new DepositDb(
      this.envService.deviceTableName,
      this.dbService,
      envService.depositGsi,
      envService.entityUuidGsi,
    );
  }
 
  getDeposit = async (depositUuid: string): Promise<Deposit> => {
    const output = await this.depositDb.getDeposit(depositUuid);
    if (output.Items && output.Items.length > 0) {
      return convertDbItemToDeposit(output.Items[0]);
    }
    throw new Error(`Deposit ${depositUuid} not found.`);
  };
 
  getDeposits = async (
    event: DepositQueryInput,
    accessToken: string,
  ): Promise<DepositConnection> => {
    const entityUuid = await this.deviceService.getEntityUuidByAccessToken(
      accessToken,
    );
    console.log('get entity uuid:', entityUuid);
    const output = await this.depositDb.getDeposits(event, entityUuid);
    if (output && output.Items && output.Items.length > 0) {
      const deposits = output.Items.map<Deposit>((item) => convertDbItemToDeposit(item));
      const nextToken = output.LastEvaluatedKey
        ? (output.LastEvaluatedKey as EntityUuidGsiKeySchema)
        : undefined;
      return nextToken ? { deposits, nextToken } : { deposits };
    }
    return { deposits: [] };
  };
 
  prepareUpdateEventRequest(item: any, keys: any) {
    return {
      mutation: gql`
        mutation publishDepositUpdateEvent(
          $item: DepositInput!
        ) {
          publishDepositUpdateEvent(deposit: $item) {
            ${keys}
          }
        }
      `,
      variables: {
        item,
      },
    };
  }
 
  onDepositStream = async (event: any): Promise<void> => {
    console.info('deposit stream event:', JSON.stringify(event));
    await this.publishUpdateEventToSubscribers<Deposit>(
      event,
      convertDbStreamToDeposit,
    );
  };
 
  onDepositUpdate = async (accessToken: string) => {
    await this.deviceVerify.checkAccessToken(accessToken);
  };
}