'use strict'; const _ = require('lodash'); const error = require('../lib/error'); const redirectionHostModel = require('../models/redirection_host'); function omissions () { return ['is_deleted']; } const internalProxyHost = { /** * All Hosts * * @param {Access} access * @param {Array} [expand] * @param {String} [search_query] * @returns {Promise} */ getAll: (access, expand, search_query) => { return access.can('redirection_hosts:list') .then(access_data => { let query = redirectionHostModel .query() .where('is_deleted', 0) .groupBy('id') .omit(['is_deleted']) .orderBy('domain_name', 'ASC'); if (access_data.permission_visibility !== 'all') { query.andWhere('owner_user_id', access.token.get('attrs').id); } // Query is used for searching if (typeof search_query === 'string') { query.where(function () { this.where('domain_name', 'like', '%' + search_query + '%'); }); } if (typeof expand !== 'undefined' && expand !== null) { query.eager('[' + expand.join(', ') + ']'); } return query; }); }, /** * Report use * * @param {Integer} user_id * @param {String} visibility * @returns {Promise} */ getCount: (user_id, visibility) => { let query = redirectionHostModel .query() .count('id as count') .where('is_deleted', 0); if (visibility !== 'all') { query.andWhere('owner_user_id', user_id); } return query.first() .then(row => { return parseInt(row.count, 10); }); } }; module.exports = internalProxyHost;