|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const _ = require('lodash'); |
| 4 | +const error = require('../lib/error'); |
| 5 | +const accessListModel = require('../models/access_list'); |
| 6 | + |
| 7 | +function omissions () { |
| 8 | + return ['is_deleted']; |
| 9 | +} |
| 10 | + |
| 11 | +const internalAccessList = { |
| 12 | + |
| 13 | + /** |
| 14 | + * @param {Access} access |
| 15 | + * @param {Object} data |
| 16 | + * @returns {Promise} |
| 17 | + */ |
| 18 | + create: (access, data) => { |
| 19 | + return access.can('access_lists:create', data) |
| 20 | + .then(access_data => { |
| 21 | + // TODO |
| 22 | + return {}; |
| 23 | + }); |
| 24 | + }, |
| 25 | + |
| 26 | + /** |
| 27 | + * @param {Access} access |
| 28 | + * @param {Object} data |
| 29 | + * @param {Integer} data.id |
| 30 | + * @param {String} [data.email] |
| 31 | + * @param {String} [data.name] |
| 32 | + * @return {Promise} |
| 33 | + */ |
| 34 | + update: (access, data) => { |
| 35 | + return access.can('access_lists:update', data.id) |
| 36 | + .then(access_data => { |
| 37 | + // TODO |
| 38 | + return {}; |
| 39 | + }); |
| 40 | + }, |
| 41 | + |
| 42 | + /** |
| 43 | + * @param {Access} access |
| 44 | + * @param {Object} data |
| 45 | + * @param {Integer} data.id |
| 46 | + * @param {Array} [data.expand] |
| 47 | + * @param {Array} [data.omit] |
| 48 | + * @return {Promise} |
| 49 | + */ |
| 50 | + get: (access, data) => { |
| 51 | + if (typeof data === 'undefined') { |
| 52 | + data = {}; |
| 53 | + } |
| 54 | + |
| 55 | + if (typeof data.id === 'undefined' || !data.id) { |
| 56 | + data.id = access.token.get('attrs').id; |
| 57 | + } |
| 58 | + |
| 59 | + return access.can('access_lists:get', data.id) |
| 60 | + .then(access_data => { |
| 61 | + let query = accessListModel |
| 62 | + .query() |
| 63 | + .where('is_deleted', 0) |
| 64 | + .andWhere('id', data.id) |
| 65 | + .allowEager('[owner]') |
| 66 | + .first(); |
| 67 | + |
| 68 | + if (access_data.permission_visibility !== 'all') { |
| 69 | + query.andWhere('owner_user_id', access.token.get('attrs').id); |
| 70 | + } |
| 71 | + |
| 72 | + // Custom omissions |
| 73 | + if (typeof data.omit !== 'undefined' && data.omit !== null) { |
| 74 | + query.omit(data.omit); |
| 75 | + } |
| 76 | + |
| 77 | + if (typeof data.expand !== 'undefined' && data.expand !== null) { |
| 78 | + query.eager('[' + data.expand.join(', ') + ']'); |
| 79 | + } |
| 80 | + |
| 81 | + return query; |
| 82 | + }) |
| 83 | + .then(row => { |
| 84 | + if (row) { |
| 85 | + return _.omit(row, omissions()); |
| 86 | + } else { |
| 87 | + throw new error.ItemNotFoundError(data.id); |
| 88 | + } |
| 89 | + }); |
| 90 | + }, |
| 91 | + |
| 92 | + /** |
| 93 | + * @param {Access} access |
| 94 | + * @param {Object} data |
| 95 | + * @param {Integer} data.id |
| 96 | + * @param {String} [data.reason] |
| 97 | + * @returns {Promise} |
| 98 | + */ |
| 99 | + delete: (access, data) => { |
| 100 | + return access.can('access_lists:delete', data.id) |
| 101 | + .then(() => { |
| 102 | + return internalAccessList.get(access, {id: data.id}); |
| 103 | + }) |
| 104 | + .then(row => { |
| 105 | + if (!row) { |
| 106 | + throw new error.ItemNotFoundError(data.id); |
| 107 | + } |
| 108 | + |
| 109 | + return accessListModel |
| 110 | + .query() |
| 111 | + .where('id', row.id) |
| 112 | + .patch({ |
| 113 | + is_deleted: 1 |
| 114 | + }); |
| 115 | + }) |
| 116 | + .then(() => { |
| 117 | + return true; |
| 118 | + }); |
| 119 | + }, |
| 120 | + |
| 121 | + /** |
| 122 | + * All Lists |
| 123 | + * |
| 124 | + * @param {Access} access |
| 125 | + * @param {Array} [expand] |
| 126 | + * @param {String} [search_query] |
| 127 | + * @returns {Promise} |
| 128 | + */ |
| 129 | + getAll: (access, expand, search_query) => { |
| 130 | + return access.can('access_lists:list') |
| 131 | + .then(access_data => { |
| 132 | + let query = accessListModel |
| 133 | + .query() |
| 134 | + .where('is_deleted', 0) |
| 135 | + .groupBy('id') |
| 136 | + .omit(['is_deleted']) |
| 137 | + .allowEager('[owner]') |
| 138 | + .orderBy('name', 'ASC'); |
| 139 | + |
| 140 | + if (access_data.permission_visibility !== 'all') { |
| 141 | + query.andWhere('owner_user_id', access.token.get('attrs').id); |
| 142 | + } |
| 143 | + |
| 144 | + // Query is used for searching |
| 145 | + if (typeof search_query === 'string') { |
| 146 | + query.where(function () { |
| 147 | + this.where('name', 'like', '%' + search_query + '%'); |
| 148 | + }); |
| 149 | + } |
| 150 | + |
| 151 | + if (typeof expand !== 'undefined' && expand !== null) { |
| 152 | + query.eager('[' + expand.join(', ') + ']'); |
| 153 | + } |
| 154 | + |
| 155 | + return query; |
| 156 | + }); |
| 157 | + }, |
| 158 | + |
| 159 | + /** |
| 160 | + * Report use |
| 161 | + * |
| 162 | + * @param {Integer} user_id |
| 163 | + * @param {String} visibility |
| 164 | + * @returns {Promise} |
| 165 | + */ |
| 166 | + getCount: (user_id, visibility) => { |
| 167 | + let query = accessListModel |
| 168 | + .query() |
| 169 | + .count('id as count') |
| 170 | + .where('is_deleted', 0); |
| 171 | + |
| 172 | + if (visibility !== 'all') { |
| 173 | + query.andWhere('owner_user_id', user_id); |
| 174 | + } |
| 175 | + |
| 176 | + return query.first() |
| 177 | + .then(row => { |
| 178 | + return parseInt(row.count, 10); |
| 179 | + }); |
| 180 | + } |
| 181 | +}; |
| 182 | + |
| 183 | +module.exports = internalAccessList; |
0 commit comments