async list()

in addons/addon-environment-sc-api/packages/environment-type-mgmt-services/lib/environment-type/env-type-service.js [91:142]


  async list(requestContext, { fields = [], filter = { status: [envTypeStatusEnum.approved] } } = {}) {
    // ensure that the caller has permissions to list environment types
    // Perform default condition checks to make sure the user is active
    await this.assertAuthorized(requestContext, { action: 'list', conditions: [allowIfActive] });

    const filterStatuses = filter.status || [envTypeStatusEnum.approved];

    // Validate filter
    const invalidFilterStatuses = _.filter(filterStatuses, s => !envTypeStatusEnum.isValidStatus(s));
    if (!_.isEmpty(invalidFilterStatuses)) {
      throw this.boom.badRequest(
        `Invalid status specified for filter. Valid values for status are ${_.join(
          envTypeStatusEnum.getValidStatuses(),
        )}`,
        true,
      );
    }

    const includeAll = _.includes(filterStatuses, '*');
    const includeNotApproved = _.includes(filterStatuses, envTypeStatusEnum.notApproved) || includeAll;
    const includeApproved = _.includes(filterStatuses, envTypeStatusEnum.approved) || includeAll;

    // Remember doing a scanning is not a good idea if you have millions of records
    const envTypes = await this._scanner()
      .limit(3000)
      .projection(fields)
      .scan();

    // Check if the caller has permissions to list not-approved environment types
    // Perform default condition checks to make sure the user is active and is admin
    const allowedToListNotApproved = await this.isAuthorized(requestContext, {
      action: 'list-not-approved',
      conditions: [allowIfActive, allowIfAdmin],
    });
    if (includeAll && allowedToListNotApproved) {
      // if asked to return all env types then no need to do any further filtering
      // return all env types
      return envTypes;
    }

    // Code reached here, means we may need to filter
    let result = [];
    if (includeNotApproved && allowedToListNotApproved) {
      const notApprovedEnvTypes = _.filter(envTypes, envType => envTypeStatusEnum.isNotApproved(envType.status));
      result = [...result, ...notApprovedEnvTypes];
    }
    if (includeApproved) {
      const approvedEnvTypes = _.filter(envTypes, envType => envTypeStatusEnum.isApproved(envType.status));
      result = [...result, ...approvedEnvTypes];
    }
    return result;
  }