All files / src/transactions transactionDb.ts

100% Statements 38/38
86.67% Branches 13/15
100% Functions 7/7
100% Lines 38/38

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 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139                13x 82x     82x 82x 82x 82x 82x 82x     82x     102x               102x     82x         1x 1x                 1x     82x             125x 125x 125x 125x 125x 125x 125x   125x                   125x             125x 125x       125x           125x         94x 94x                 31x     82x                 31x   31x 17x   14x   31x                  
import { DocumentClient } from 'aws-sdk/clients/dynamodb';
import { TransactionQueryInput, Transaction } from '../types.d';
import { DynamodbService } from '../common_services';
 
/**
 * Transaction db
 *  used to access transaction database table
 */
export class TransactionDb {
  type = 'transaction';
 
  constructor(
    private readonly tableName: string,
    private readonly dynamodbService: DynamodbService,
    private readonly transactionGsi: string,
    private readonly accessTokenGsi: string,
    private readonly entityUuidGsi: string,
    private readonly siteUuidGsi: string,
  ) {}
 
  getTransaction = (
    transactionUuid: string,
  ): Promise<DocumentClient.QueryOutput> => {
    const params: DocumentClient.QueryInput = {
      TableName: this.tableName,
      IndexName: this.transactionGsi,
      KeyConditionExpression: 'transactionUuid = :transactionUuid',
      ExpressionAttributeValues: {
        ':transactionUuid': transactionUuid,
      },
    };
    return this.dynamodbService.documentClient.query(params).promise();
  };
 
  saveTransaction = async (
    id: string,
    transactionUuid: string,
    type: string,
    trxn: Transaction,
  ): Promise<DocumentClient.PutItemOutput> => {
    const params: DocumentClient.PutItemInput = {
      TableName: this.tableName,
      Item: {
        ...trxn,
        id,
        transactionUuid,
        type,
      },
    };
    return this.dynamodbService.documentClient.put(params).promise();
  };
 
  getTransactionsMultiQueries = async (
    limit: number,
    timestampFilter: string,
    entityUuid: string,
    nextToken?: DocumentClient.Key,
    filter?: string,
    prev?: DocumentClient.QueryOutput,
  ): Promise<DocumentClient.QueryOutput> => {
    let values: { [key: string]: string } = {};
    const attributeNames = { '#type': 'type' };
    let keyExpression = '';
    Eif (entityUuid) {
      values = { ':entityUuid': entityUuid };
      keyExpression = 'entityUuid = :entityUuid';
    }
    const params: DocumentClient.QueryInput = {
      TableName: this.tableName,
      IndexName: this.entityUuidGsi,
      ExpressionAttributeValues: values,
      KeyConditionExpression: keyExpression,
      ExpressionAttributeNames: attributeNames,
      Limit: limit,
      ScanIndexForward: false,
    };
 
    const queryOutput = await this.dynamodbService.getQueryOutput(
      params,
      this.type,
      timestampFilter,
      filter,
      nextToken,
    );
    let combinedItems = prev && prev.Items ? prev.Items : [];
    combinedItems = queryOutput.Items
      ? [...combinedItems, ...queryOutput.Items]
      : combinedItems;
 
    const outputItems = {
      Items: combinedItems,
      Count: combinedItems.length,
      LastEvaluatedKey: queryOutput.LastEvaluatedKey,
    };
 
    if (
      typeof queryOutput.Items !== 'undefined' &&
      queryOutput.Items.length < limit &&
      queryOutput.LastEvaluatedKey
    ) {
      console.log(`try another query ${combinedItems.length} ${limit}`);
      return this.getTransactionsMultiQueries(
        limit - queryOutput.Items.length,
        timestampFilter,
        entityUuid,
        queryOutput.LastEvaluatedKey,
        filter,
        outputItems,
      );
    }
    return outputItems;
  };
 
  getTransactions = (
    event: TransactionQueryInput,
    entityUuid: string,
  ): Promise<DocumentClient.QueryOutput> => {
    const {
      limit,
      nextToken,
      filter,
      timestampFilter,
    } = event;
    let tFilter: string;
    if (!timestampFilter) {
      tFilter = this.dynamodbService.utils.createLastYearQuery();
    } else {
      tFilter = timestampFilter;
    }
    return this.getTransactionsMultiQueries(
      limit,
      tFilter,
      entityUuid,
      nextToken,
      filter,
    );
  };
}