api/responses/ok.js
/**
   * Private method that fetches the result count for the current query.
   */
Private method that fetches the result count for the current query.
Private method that fetches the result count for the current query .
function fetchResultCount(req, query, modelName) {
    var models = sails.models;
    if (_.has(models, modelName)) {
      var model = models[modelName];
      var promise;
      var filterQuery = { where: {} };
      if (query.where) {
        filterQuery.where = JSON.parse(query.where);
      }
      if (options && _.has(options, 'filteredTotal')) {
        promise = options.filteredTotal;
      }
      else {
        if (_.has(model.attributes, 'expiredAt')) {
          filterQuery.where.expiredAt = null;
        }
        var queryFilter = null;
        if (_.has(req.headers, 'x-uhn-deep-query')) {
          queryFilter = JSON.parse(req.headers['x-uhn-deep-query']);
          promise = model.find(filterQuery)
            .populate(queryFilter.collection, queryFilter.where)
            .then(function (totalItems) {
              return _.filter(totalItems, function (item) {
                return item[queryFilter.collection].length > 0;
              });
            });
        }
        if (_.has(req, 'criteria') && req.criteria.length > 0 && !_.isEmpty(_.first(req.criteria).where)) {
          if (queryFilter && _.has(req.headers, 'x-uhn-deep-query')) {
            promise = promise.then(function (totalItems) {
              return PermissionService.filterByCriteria(req.criteria, totalItems).length;
            });
          }
          else {
            var criteriaList = _.pluck(req.criteria, 'where');
            if (!filterQuery.where.or) {
              filterQuery.where.or = criteriaList;
            } else {
              filterQuery.where.or.concat(criteriaList);
            }
            promise = model.count().where(filterQuery);
          }
        }
        else {
          if (!queryFilter) {
            promise = model.count(filterQuery);
          }
          else {
            promise = promise.then(function (totalItems) {
              return totalItems.length;
            });
          }
        }
      }
      return promise;
    }
    return Promise.resolve(0);
  }
function fetchResultCount ( req , query , modelName ) { var models = sails . models ; if ( _ . has ( models , modelName ) ) { var model = models [ modelName ] ; var promise ; var filterQuery = { where : { } } ; if ( query . where ) { filterQuery . where = JSON . parse ( query . where ) ; } if ( options && _ . has ( options , 'filteredTotal' ) ) { promise = options . filteredTotal ; } else { if ( _ . has ( model . attributes , 'expiredAt' ) ) { filterQuery . where . expiredAt = null ; } var queryFilter = null ; if ( _ . has ( req . headers , 'x-uhn-deep-query' ) ) { queryFilter = JSON . parse ( req . headers [ 'x-uhn-deep-query' ] ) ; promise = model . find ( filterQuery ) . populate ( queryFilter . collection , queryFilter . where ) . then ( function ( totalItems ) { return _ . filter ( totalItems , function ( item ) { return item [ queryFilter . collection ] . length > 0 ; } ) ; } ) ; } if ( _ . has ( req , 'criteria' ) && req . criteria . length > 0 && ! _ . isEmpty ( _ . first ( req . criteria ) . where ) ) { if ( queryFilter && _ . has ( req . headers , 'x-uhn-deep-query' ) ) { promise = promise . then ( function ( totalItems ) { return PermissionService . filterByCriteria ( req . criteria , totalItems ) . length ; } ) ; } else { var criteriaList = _ . pluck ( req . criteria , 'where' ) ; if ( ! filterQuery . where . or ) { filterQuery . where . or = criteriaList ; } else { filterQuery . where . or . concat ( criteriaList ) ; } promise = model . count ( ) . where ( filterQuery ) ; } } else { if ( ! queryFilter ) { promise = model . count ( filterQuery ) ; } else { promise = promise . then ( function ( totalItems ) { return totalItems . length ; } ) ; } } } return promise ; } return Promise . resolve ( 0 ) ; }
Private method that fetches the result count for the current query.
Private method that fetches the result count for the current query .
// if provided a query, include in where clause // when options.filteredTotal is passed to res.ok, use the filtered total instead // otherwise, we can just return the model count total /**
         * if header was sent from frontend (a query from getQueryLinks), apply populate here
         * with appropriate where clause for populate to get correct filtered count.
         *
         * Header is set in HateoasController.js in dados-client and
         * queryLinks are defined within the respective Model file in Sails.
         */ // for non-admins, apply criteria here to get restricted count of permitted records // if header was included with populate query, filter matchingRecords by given successful matched criteria // otherwise no header set and just filter criteria from find // and merge filterQuery with any applicable criteria // otherwise no criteria set // if no header set for populate filter, just do direct count // header set and no criteria, so just return direct count