async _getCommandsPage()

in source/services/api/command/lib/command.js [87:153]


  async _getCommandsPage(deviceId, lastevalkey, commandStatus, length) {
    let _keyConditionExpression = 'deviceId = :did';
    let _expressionAttributeValues = {
      ':did': deviceId,
    };
    let _expressionAttributeNames = '';

    /**
     * DynamoDB FilterExpression
     * If there are query parameters, add the value to the filter expression.
     */
    let _filterExpression = '';

    // Filters with command status
    if (commandStatus !== undefined
      && commandStatus.trim() !== '') {
      _filterExpression = '#status = :commandStatus';
      _expressionAttributeValues[':commandStatus'] = commandStatus.trim();
      // params.ExpressionAttributeNames = {
      _expressionAttributeNames = {
        '#status': 'status'
      };
    }

    let params = this.commonUtils.generateDynamoDBQueryParams(
      process.env.COMMANDS_TBL,
      _keyConditionExpression,
      _expressionAttributeValues,
      _expressionAttributeNames,
      _filterExpression,
      'deviceId-updatedAt-index',
      false,
      lastevalkey
    );

    let commands = [];
    let docClient = new AWS.DynamoDB.DocumentClient(this.dynamoConfig);
    try {
      let result = await docClient.query(params).promise();
      commands = result.Items;
      length += commands.length;

      // In case the result is less than 20 in total due to FilterExpression, call the method again with LastEvaluatedKey.
      if (length < 20
        && result.LastEvaluatedKey) {
        lastevalkey = result.LastEvaluatedKey;
        try {
          let data = await this._getCommandsPage(deviceId, lastevalkey, commandStatus, length);
          commands = [...commands, ...data.Items];
        result.LastEvaluatedKey = data.LastEvaluatedKey;
        } catch (err) {
          return Promise.reject(err);
        }
      }

      result.Items = commands;
      result.commandStatus = commandStatus;
      return Promise.resolve(result);
    } catch (err) {
      Logger.error(Logger.levels.INFO, err);
      return Promise.reject({
        code: 500,
        error: 'CommandQueryFailure',
        message: `Error occurred while attempting to retrieve commands for device "${deviceId}".`,
      });
    }
  }