async _registrationUpdate()

in source/services/jitr/lib/jitrHelper.js [118:191]


  async _registrationUpdate(thing) {
    try {
      let _keyConditionExpression = 'deviceId = :did';
      let _expressionAttributeValues = {
        ':did': thing,
      };

      let params = {
        TableName: process.env.REGISTRATION_TBL,
        IndexName: 'deviceId-index',
        KeyConditionExpression: _keyConditionExpression,
        ExpressionAttributeValues: _expressionAttributeValues,
      };

      let docClient = new AWS.DynamoDB.DocumentClient(this.dynamoConfig);
      let data = await docClient.query(params).promise();
      let devices = data.Items.filter(device => device.status === 'pending');
      if (devices.length === 0) {
        Logger.error(
          Logger.levels.INFO,
          `[DeviceNotFoundFailure] Device ${thing} has not registered.`
        );

        return Promise.reject({
          code: 400,
          error: 'DeviceNotFoundFailure',
          message: `Device ${thing} has not registered.`
        });
      }

      let device = devices[0];
      params = {
        TableName: process.env.REGISTRATION_TBL,
        Key: {
          deviceId: thing,
          userId: device.userId,
        },
        ExpressionAttributeNames: {
          '#A': 'activatedAt',
          '#U': 'updatedAt',
          '#S': 'status',
        },
        ExpressionAttributeValues: {
          ':a': moment().utc().format(),
          ':u': moment().utc().format(),
          ':s': 'complete',
        },
        UpdateExpression: 'SET #A = :a, #U = :u, #S = :s',
      };

      await docClient.update(params).promise();
      return Promise.resolve({
        code: 200,
        error: 'RegistrationUpdateSuccess',
        message: `Success in updating registration for device ${thing}.`,
      });
    } catch (err) {
      if (err.error === 'DeviceNotFoundFailure') {
        return Promise.reject(err);
      }

      Logger.error(Logger.levels.INFO, err);
      Logger.error(
        Logger.levels.INFO,
        `[RegistrationUpdateFailure] Error occurred while updating registration for device ${thing}.`
      );

      return Promise.reject({
        code: 500,
        error: 'RegistrationUpdateFailure',
        message: `Error occurred while updating registration for device ${thing}.`,
      });
    }
  }