All files / src/devices deviceVerification.ts

97.92% Statements 47/48
85.71% Branches 24/28
100% Functions 11/11
97.67% Lines 42/43

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 879x 9x 9x 9x       9x       17x 17x   17x               102x 102x 102x     17x         11x 11x           11x 8x 8x 8x       3x     17x     4x 4x 4x 2x   2x     17x   4x 4x 4x 2x 2x 2x 2x     2x     17x 2x 2x 1x 1x 1x 1x     1x      
import { Injectable } from '@nestjs/common';
import { EnvironmentService } from '../common_services/config';
import { DeviceDb } from './deviceDb';
import { DynamodbService } from '../common_services/dynamodb/dynamodbService';
import { DeviceStatus } from '../types.d';
 
@Injectable()
export class DeviceVerification {
  deviceDb: DeviceDb;
 
  constructor(
    private readonly dynamodbService: DynamodbService,
    private readonly envService: EnvironmentService,
  ) {
    this.deviceDb = new DeviceDb(
      this.envService.deviceTableName,
      this.envService.modelSerialGsi,
      this.dynamodbService.documentClient,
      this.envService.accessTokenGsi,
    );
  }
 
  isDeviceUuidValid = async (uuid: string): Promise<boolean> => {
    const items = await this.deviceDb.getDeviceByUuid(uuid);
    return items.Items ? items.Items.length > 0 : false;
  };
 
  isDeviceTokenValid = async (
    uuid: string,
    accessToken: string,
    refreshToken?: string,
    idToken?: string,
  ): Promise<boolean> => {
    const items = await this.deviceDb.findDeviceByTokens(
      uuid,
      accessToken,
      refreshToken,
      idToken,
    );
    if (items.Items && items.Items.length === 1) {
      const deviceStatus = items.Items[0].status;
      Eif (deviceStatus !== 'INACTIVE' && deviceStatus !== 'DISABLED') {
        return true;
      }
      return false;
    }
    return false;
  };
 
  isDevicePinValid = async (
    pin: string,
    deviceUuid: string,
  ): Promise<boolean> => {
    const items = await this.deviceDb.findDeviceByPin(pin, deviceUuid);
    if (items.Items && items.Items.length === 1) {
      return true;
    }
    return false;
  };
 
  checkAccessToken = async (accessToken: string): Promise<void> => {
    // TODO: need to update this logic
    const output = await this.deviceDb.getDeviceByAccessToken(accessToken);
    console.log('get entity uuid', output);
    if (output.Items && output.Items.length > 0) {
      const { entityUuid } = output.Items[0];
      Eif (entityUuid) {
        console.info('checkAccessToken passed')
        return;
      }
    }
    throw new Error('Invalid access token.');
  };
 
  checkDeviceStatus = async (accessToken: string): Promise<void> => {
    const output = await this.deviceDb.getDeviceByAccessToken(accessToken);
    if (output.Items && output.Items.length > 0) {
      const { status, entityUuid } = output.Items[0];
      Eif (status === DeviceStatus.ACTIVE && entityUuid) {
        console.info('checkDeviceStatus passed')
        return;
      }
    }
    throw new Error('Device is disabled or inactive.');
  };
}