@azteam/express 1.2.282 → 1.2.284

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -34,7 +34,7 @@ function _assertThisInitialized(self) { if (self === void 0) { throw new Referen
34
34
  function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
35
35
  function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
36
36
  function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
37
- var ALLOW_FIELDS = ['created_id', 'modified_id', 'restored_id', 'deleted_id', 'resource'];
37
+ var ALLOW_FIELDS = ['created_at', 'created_id', 'modified_at', 'modified_id', 'deleted_at', 'deleted_id', 'restored_id', 'resource', 'priority', 'is_processing'];
38
38
  var AdminController = /*#__PURE__*/function (_Controller) {
39
39
  _inherits(AdminController, _Controller);
40
40
  var _super = _createSuper(AdminController);
package/lib/Server.js CHANGED
@@ -229,7 +229,7 @@ var Server = /*#__PURE__*/function () {
229
229
  var responseGuard = guard;
230
230
  var responseAllows = allow;
231
231
  if (_lodash["default"].isArray(guard)) {
232
- responseGuard = [].concat(_toConsumableArray(guard), ['__v', '_id', 'deleted_at', 'updated_at', 'created_id', 'modified_id', 'resource', 'is_processing', 'priority']);
232
+ responseGuard = [].concat(_toConsumableArray(guard), ['__v', '_id', 'created_at', 'created_id', 'modified_at', 'modified_id', 'deleted_at', 'deleted_id', 'restored_id', 'resource', 'is_processing', 'priority']);
233
233
  }
234
234
  if (resType === RES_TYPE.DOCS) {
235
235
  guardData.docs = _lodash["default"].map(data.docs, function (item) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@azteam/express",
3
- "version": "1.2.282",
3
+ "version": "1.2.284",
4
4
  "main": "./lib/index.js",
5
5
  "module": "./src/index.js",
6
6
  "scripts": {
@@ -0,0 +1,322 @@
1
+ import _ from 'lodash';
2
+ import {NOT_EXISTS} from '@azteam/error';
3
+
4
+ import {REQUEST_TYPE} from './constant';
5
+ import {rulesId, schemaBoolean, schemaEnum, schemaNumber} from './validate';
6
+ import {adminRoleMiddleware, paginateMiddleware, validateMiddleware} from './middleware';
7
+ import Controller from './Controller';
8
+
9
+ const ALLOW_FIELDS = [
10
+ 'created_at',
11
+ 'created_id',
12
+ 'modified_at',
13
+ 'modified_id',
14
+ 'deleted_at',
15
+ 'deleted_id',
16
+ 'restored_id',
17
+ 'resource',
18
+ 'priority',
19
+ 'is_processing',
20
+ ];
21
+
22
+ class AdminController extends Controller {
23
+ constructor(pathName, repository, options = {}) {
24
+ super(pathName, repository);
25
+
26
+ this.roles = {
27
+ EXEC: null,
28
+ READ: null,
29
+ CREATE: null,
30
+ UPDATE: null,
31
+ DELETE: null,
32
+ RESTORE: null,
33
+ DESTROY: null,
34
+ IMPORT: null,
35
+ EXPORT: null,
36
+
37
+ ...options.roles,
38
+ };
39
+
40
+ this.paginateOptions = options.paginateOptions || {};
41
+ this.guardResponse = options.guardResponse || [];
42
+ this.allowResponse = options.allowResponse ? [...options.allowResponse, ...ALLOW_FIELDS] : ALLOW_FIELDS;
43
+
44
+ this.rulesCreate = {
45
+ ...options.rulesCreate,
46
+ priority: schemaNumber(true),
47
+ status: schemaEnum([0, 1, 2], true),
48
+ };
49
+
50
+ this.rulesModify = {
51
+ isForceModify: schemaBoolean(true),
52
+ ..._.mapValues(this.rulesCreate, function (obj) {
53
+ return {...obj, optional: true};
54
+ }),
55
+ };
56
+
57
+ if (this.paginateOptions) {
58
+ this.paginateOptions.allowSearchFields = [
59
+ ...this.paginateOptions.allowSearchFields,
60
+ 'status',
61
+ 'created_at_start',
62
+ 'created_at_end',
63
+ 'modified_at_start',
64
+ 'modified_at_end',
65
+ ];
66
+ this.paginateOptions.allowSortFields = [...this.paginateOptions.allowSortFields, 'created_at', 'modified_at', 'status'];
67
+ }
68
+ }
69
+
70
+ methodGetPaginatePublic = async (req, res) => {
71
+ const paginateData = await this.repository.find(req.query, req.paginate);
72
+ return res.success(paginateData, this.guardResponse, this.allowResponse);
73
+ };
74
+
75
+ getPaginatePublic() {
76
+ return {
77
+ disabled: !this.roles.READ,
78
+ path: '/',
79
+ method: [adminRoleMiddleware([this.roles.READ]), paginateMiddleware(this.paginateOptions), this.methodGetPaginatePublic],
80
+ };
81
+ }
82
+
83
+ methodGetPaginateTrash = async (req, res) => {
84
+ const paginateData = await this.repository.findTrash(req.query, req.paginate);
85
+ return res.success(paginateData, this.guardResponse, this.allowResponse);
86
+ };
87
+
88
+ getPaginateTrash() {
89
+ return {
90
+ disabled: !this.roles.READ,
91
+ path: '/',
92
+ method: [adminRoleMiddleware([this.roles.READ]), paginateMiddleware(this.paginateOptions), this.methodGetPaginateTrash],
93
+ };
94
+ }
95
+
96
+ methodGetOnePublic = async (req, res) => {
97
+ const item = await this.repository.findOneById(req.params.id);
98
+ if (!item) return res.error(NOT_EXISTS);
99
+ return res.success(item, this.guardResponse, this.allowResponse);
100
+ };
101
+
102
+ getOnePublic() {
103
+ return {
104
+ disabled: !this.roles.READ,
105
+ path: '/:id',
106
+ method: [adminRoleMiddleware([this.roles.READ]), validateMiddleware(REQUEST_TYPE.PARAMS, rulesId), this.methodGetOnePublic],
107
+ };
108
+ }
109
+
110
+ methodGetOneTrash = async (req, res) => {
111
+ const item = await this.repository.findOneTrashById(req.params.id);
112
+ if (!item) return res.error(NOT_EXISTS);
113
+ return res.success(item, this.guardResponse, this.allowResponse);
114
+ };
115
+
116
+ getOneTrash() {
117
+ return {
118
+ disabled: !this.roles.READ,
119
+ path: '/:id',
120
+ method: [adminRoleMiddleware([this.roles.READ]), validateMiddleware(REQUEST_TYPE.PARAMS, rulesId), this.methodGetOneTrash],
121
+ };
122
+ }
123
+
124
+ async beforeCreate(data) {
125
+ return data;
126
+ }
127
+
128
+ async afterCreate(item) {
129
+ return item;
130
+ }
131
+
132
+ methodPostCreatePublic = async (req, res) => {
133
+ const data = await this.beforeCreate(req.body);
134
+
135
+ let item = await this.repository.createByUser(req.user.id, data);
136
+
137
+ item = await this.afterCreate(item);
138
+
139
+ return res.success(item, this.guardResponse, this.allowResponse);
140
+ };
141
+
142
+ postCreatePublic() {
143
+ return {
144
+ disabled: !this.roles.CREATE,
145
+ path: '/',
146
+ method: [adminRoleMiddleware([this.roles.CREATE]), validateMiddleware(REQUEST_TYPE.BODY, this.rulesCreate), this.methodPostCreatePublic],
147
+ };
148
+ }
149
+
150
+ async beforeModify(data) {
151
+ return data;
152
+ }
153
+
154
+ async afterModify(item) {
155
+ return item;
156
+ }
157
+
158
+ methodPutModifyPublic = async (req, res) => {
159
+ let item = await this.repository.findOneById(req.params.id);
160
+ if (!item) return res.error(NOT_EXISTS);
161
+
162
+ const data = await this.beforeModify(req.body);
163
+
164
+ item.loadData(data);
165
+ item.modified_id = req.user.id;
166
+ await item.save();
167
+
168
+ item = await this.afterModify(item);
169
+
170
+ return res.success(item, this.guardResponse, this.allowResponse);
171
+ };
172
+
173
+ putModifyPublic() {
174
+ return {
175
+ disabled: !this.roles.UPDATE,
176
+ path: '/:id',
177
+ method: [
178
+ adminRoleMiddleware([this.roles.UPDATE]),
179
+ validateMiddleware(REQUEST_TYPE.PARAMS, rulesId),
180
+ validateMiddleware(REQUEST_TYPE.BODY, this.rulesModify),
181
+ this.methodPutModifyPublic,
182
+ ],
183
+ };
184
+ }
185
+
186
+ putModifyStatusAvailablePublic() {
187
+ return {
188
+ disabled: !this.roles.UPDATE,
189
+ path: '/available/:id',
190
+ method: [
191
+ adminRoleMiddleware([this.roles.UPDATE]),
192
+ validateMiddleware(REQUEST_TYPE.PARAMS, rulesId),
193
+ async (req, res) => {
194
+ let item = await this.repository.findOneById(req.params.id);
195
+ if (!item) return res.error(NOT_EXISTS);
196
+
197
+ await item.modifyStatusAvailable(req.user.id);
198
+
199
+ item = await this.afterModify(item);
200
+
201
+ return res.success(item, this.guardResponse, this.allowResponse);
202
+ },
203
+ ],
204
+ };
205
+ }
206
+
207
+ putModifyStatusUnavailablePublic() {
208
+ return {
209
+ disabled: !this.roles.UPDATE,
210
+ path: '/unavailable/:id',
211
+ method: [
212
+ adminRoleMiddleware([this.roles.UPDATE]),
213
+ validateMiddleware(REQUEST_TYPE.PARAMS, rulesId),
214
+ async (req, res) => {
215
+ let item = await this.repository.findOneById(req.params.id);
216
+ if (!item) return res.error(NOT_EXISTS);
217
+
218
+ await item.modifyStatusUnavailable(req.body.message, req.user.id);
219
+
220
+ item = await this.afterModify(item);
221
+
222
+ return res.success(item, this.guardResponse, this.allowResponse);
223
+ },
224
+ ],
225
+ };
226
+ }
227
+
228
+ putModifyStatusWaitingPublic() {
229
+ return {
230
+ disabled: !this.roles.UPDATE,
231
+ path: '/waiting/:id',
232
+ method: [
233
+ adminRoleMiddleware([this.roles.UPDATE]),
234
+ validateMiddleware(REQUEST_TYPE.PARAMS, rulesId),
235
+ async (req, res) => {
236
+ let item = await this.repository.findOneById(req.params.id);
237
+ if (!item) return res.error(NOT_EXISTS);
238
+
239
+ await item.modifyStatusWaiting(req.user.id);
240
+
241
+ item = await this.afterModify(item);
242
+
243
+ return res.success(item, this.guardResponse, this.allowResponse);
244
+ },
245
+ ],
246
+ };
247
+ }
248
+
249
+ async afterDelete(item) {
250
+ return item;
251
+ }
252
+
253
+ methodDeletePublic = async (req, res) => {
254
+ const item = await this.repository.findOneById(req.params.id);
255
+ if (!item) return res.error(NOT_EXISTS);
256
+
257
+ await item.delete(req.user.id);
258
+
259
+ await this.afterDelete(item);
260
+
261
+ return res.success(true);
262
+ };
263
+
264
+ deletePublic() {
265
+ return {
266
+ disabled: !this.roles.DELETE,
267
+ path: '/:id',
268
+ method: [adminRoleMiddleware([this.roles.DELETE]), validateMiddleware(REQUEST_TYPE.PARAMS, rulesId), this.methodDeletePublic],
269
+ };
270
+ }
271
+
272
+ methodPostRestoreTrash = async (req, res) => {
273
+ const item = await this.repository.findOneTrashById(req.params.id);
274
+ if (!item) return res.error(NOT_EXISTS);
275
+
276
+ await item.restore(req.user.id);
277
+
278
+ return res.success(true);
279
+ };
280
+
281
+ postRestoreTrash() {
282
+ return {
283
+ disabled: !this.roles.RESTORE,
284
+ path: '/:id',
285
+ method: [adminRoleMiddleware([this.roles.RESTORE]), validateMiddleware(REQUEST_TYPE.PARAMS, rulesId), this.methodPostRestoreTrash],
286
+ };
287
+ }
288
+
289
+ async afterDestroy(item) {
290
+ return item;
291
+ }
292
+
293
+ methodDeleteDestroyTrash = async (req, res) => {
294
+ const item = await this.repository.findOne(
295
+ {
296
+ _id: req.params.id,
297
+ },
298
+ {
299
+ force: true,
300
+ }
301
+ );
302
+ if (!item) return res.error(NOT_EXISTS);
303
+
304
+ const oldItem = _.cloneDeep(item);
305
+
306
+ await this.repository.destroy(item.id);
307
+
308
+ await this.afterDestroy(oldItem);
309
+
310
+ return res.success(true);
311
+ };
312
+
313
+ deleteDestroyTrash() {
314
+ return {
315
+ disabled: !this.roles.DESTROY,
316
+ path: '/:id',
317
+ method: [adminRoleMiddleware([this.roles.DESTROY]), validateMiddleware(REQUEST_TYPE.PARAMS, rulesId), this.methodDeleteDestroyTrash],
318
+ };
319
+ }
320
+ }
321
+
322
+ export default AdminController;
@@ -0,0 +1,38 @@
1
+ class Controller {
2
+ constructor(pathName = '', repository = null) {
3
+ this.pathName = pathName;
4
+ this.repository = repository;
5
+ }
6
+
7
+ publicRouter() {
8
+ let child = this;
9
+ const result = [];
10
+
11
+ while (Object.getPrototypeOf(child.__proto__)) {
12
+ const data = Object.getOwnPropertyNames(Object.getPrototypeOf(child));
13
+
14
+ data.map(function (methodName) {
15
+ const matches = methodName.match(/^(get|post|put|patch|delete)/);
16
+ if (matches) {
17
+ let path = '/';
18
+ if (methodName.endsWith('Public')) {
19
+ path += 'public';
20
+ } else if (methodName.endsWith('Trash')) {
21
+ path += 'trash';
22
+ }
23
+
24
+ result.push({
25
+ type: matches[0],
26
+ name: methodName,
27
+ path,
28
+ });
29
+ }
30
+ return true;
31
+ });
32
+ child = child.__proto__;
33
+ }
34
+ return result;
35
+ }
36
+ }
37
+
38
+ export default Controller;