All files / src/devices deviceMetrics.ts

95.45% Statements 21/22
50% Branches 1/2
100% Functions 5/5
95% Lines 19/20

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  16x     16x 16x 16x     16x   125x 125x 125x     125x     1x 1x                     1x     125x   1x 1x 1x     1x                   1x 1x      
import { DocumentClient } from 'aws-sdk/clients/dynamodb';
import { Injectable } from '@nestjs/common';
import { DeviceMetricsInput, DeviceMetricsOutput } from '../types.d';
import { DeviceType } from './deviceTypes.d';
import { DeviceVerification } from './deviceVerification';
import { EnvironmentService } from '../common_services/config';
import { DynamodbService } from '../common_services/dynamodb/dynamodbService';
 
@Injectable()
export class DeviceMetrics {
  constructor(
    private readonly dynamodbService: DynamodbService,
    private readonly envService: EnvironmentService,
    private readonly deviceVerification: DeviceVerification,
  ) {}
 
  getDeviceMetrics = async (
    id: string,
    type: string,
  ): Promise<DocumentClient.QueryOutput> => {
    const params = {
      TableName: this.envService.deviceTableName,
      ExpressionAttributeValues: {
        ':id': id,
        ':type': type,
      },
      ExpressionAttributeNames: {
        '#type': 'type',
      },
      KeyConditionExpression: 'id = :id AND #type = :type',
    };
    return this.dynamodbService.documentClient.query(params).promise();
  };
 
  createDeviceMetrics = async (
    input: DeviceMetricsInput,
  ): Promise<DeviceMetricsOutput> => {
    const { deviceUuid, activity, metric, timestamp } = input;
    Iif (!(await this.deviceVerification.isDeviceUuidValid(deviceUuid))) {
      throw new Error('Device uuid doesnt exist.');
    }
    const params: DocumentClient.PutItemInput = {
      TableName: this.envService.deviceTableName,
      Item: {
        id: deviceUuid,
        activity,
        metric,
        timestamp,
        type: `${DeviceType.metrics}.${timestamp}`,
      },
    };
    await this.dynamodbService.documentClient.put(params).promise();
    return { id: deviceUuid };
  };
}