@azteam/express 1.2.174 → 1.2.177

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@azteam/express",
3
- "version": "1.2.174",
3
+ "version": "1.2.177",
4
4
  "main": "src/index.js",
5
5
  "engines": {
6
6
  "node": ">= 12.0.0",
@@ -11,7 +11,7 @@
11
11
  "dependencies": {
12
12
  "@azteam/crypto": "1.0.23",
13
13
  "@azteam/error": "1.0.18",
14
- "@azteam/http-client": "1.0.92",
14
+ "@azteam/http-client": "1.0.93",
15
15
  "@grpc/grpc-js": "1.6.7",
16
16
  "@grpc/proto-loader": "0.6.12",
17
17
  "body-parser": "1.19.0",
@@ -3,88 +3,82 @@ import {NOT_EXISTS} from '@azteam/error';
3
3
  import {REQUEST_TYPE} from './constant';
4
4
  import {rulesID} from './validate';
5
5
  import Controller from './Controller';
6
- import {adminRoleMiddleware, paginateMiddleware, validateMiddleware} from './middleware';
6
+ import {adminRoleMiddleware, paginateMiddleware, validateMiddleware, verifyGoogleAppMiddleware} from './middleware';
7
7
 
8
8
  class AdminController extends Controller {
9
9
  constructor(pathName, repository, options = {}) {
10
- super(pathName, repository, {
11
- roles: {
12
- READ: 1,
13
- CREATE: 2,
14
- UPDATE: 3,
15
- DELETE: 4,
16
- RESTORE: 5,
17
- DESTROY: 6,
18
- }, // system roles
10
+ super(pathName, repository);
19
11
 
20
- paginateOptions: {},
21
- rulesCreate: {},
22
- rulesModify: {},
12
+ this.roles = {
13
+ READ: null,
14
+ CREATE: null,
15
+ UPDATE: null,
16
+ DELETE: null,
17
+ RESTORE: null,
18
+ DESTROY: null,
23
19
 
24
- guardResponse: {},
25
- allowResponse: {},
20
+ ...options.roles,
21
+ };
22
+
23
+ this.paginateOptions = options.paginateOptions || {};
24
+ this.guardResponse = options.guardResponse || {};
25
+ this.allowResponse = options.allowResponse || {};
26
26
 
27
- ...options,
28
- });
27
+ this.rulesCreate = options.rulesCreate || {};
28
+ this.rulesModify = options.rulesModify || {};
29
29
  }
30
30
 
31
+ methodGetPaginatePublic = async (req, res) => {
32
+ const paginateData = await this.repository.find(req.query, req.paginate);
33
+ return res.success(paginateData, this.guardResponse, this.allowResponse);
34
+ };
35
+
31
36
  getPaginatePublic() {
32
37
  return {
38
+ disabled: !this.roles.READ,
33
39
  path: '/',
34
- method: [
35
- adminRoleMiddleware([this.options.roles.READ]),
36
- paginateMiddleware(this.options.paginateOptions),
37
- async (req, res) => {
38
- const paginateData = await this.repository.find(req.query, req.paginate);
39
-
40
- return res.success(paginateData, this.options.guardResponse, this.options.allowResponse);
41
- },
42
- ],
40
+ method: [adminRoleMiddleware([this.roles.READ]), paginateMiddleware(this.paginateOptions), this.methodGetPaginatePublic],
43
41
  };
44
42
  }
45
43
 
44
+ methodGetPaginateTrash = async (req, res) => {
45
+ const paginateData = await this.repository.findTrash(req.query, req.paginate);
46
+ return res.success(paginateData, this.guardResponse, this.allowResponse);
47
+ };
48
+
46
49
  getPaginateTrash() {
47
50
  return {
51
+ disabled: !this.roles.READ,
48
52
  path: '/',
49
- method: [
50
- adminRoleMiddleware([this.options.roles.READ]),
51
- paginateMiddleware(this.options.paginateOptions),
52
- async (req, res) => {
53
- const paginateData = await this.repository.findTrash(req.query, req.paginate);
54
-
55
- return res.success(paginateData, this.options.guardResponse, this.options.allowResponse);
56
- },
57
- ],
53
+ method: [adminRoleMiddleware([this.roles.READ]), paginateMiddleware(this.paginateOptions), this.methodGetPaginateTrash],
58
54
  };
59
55
  }
60
56
 
57
+ methodGetOnePublic = async (req, res) => {
58
+ const item = await this.repository.findOneById(req.params.id);
59
+ if (!item) return res.error(NOT_EXISTS);
60
+ return res.success(item, this.guardResponse, this.allowResponse);
61
+ };
62
+
61
63
  getOnePublic() {
62
64
  return {
65
+ disabled: !this.roles.READ,
63
66
  path: '/:id',
64
- method: [
65
- adminRoleMiddleware([this.options.roles.READ]),
66
- validateMiddleware(REQUEST_TYPE.PARAMS, rulesID),
67
- async (req, res) => {
68
- const item = await this.repository.findOneById(req.params.id);
69
- if (!item) return res.error(NOT_EXISTS);
70
- return res.success(item, this.options.guardResponse, this.options.allowResponse);
71
- },
72
- ],
67
+ method: [adminRoleMiddleware([this.roles.READ]), validateMiddleware(REQUEST_TYPE.PARAMS, rulesID), this.methodGetOnePublic],
73
68
  };
74
69
  }
75
70
 
71
+ methodGetOneTrash = async (req, res) => {
72
+ const item = await this.repository.findOneTrashById(req.params.id);
73
+ if (!item) return res.error(NOT_EXISTS);
74
+ return res.success(item, this.guardResponse, this.allowResponse);
75
+ };
76
+
76
77
  getOneTrash() {
77
78
  return {
79
+ disabled: !this.roles.READ,
78
80
  path: '/:id',
79
- method: [
80
- adminRoleMiddleware([this.options.roles.READ]),
81
- validateMiddleware(REQUEST_TYPE.PARAMS, rulesID),
82
- async (req, res) => {
83
- const item = await this.repository.findOneTrashById(req.params.id);
84
- if (!item) return res.error(NOT_EXISTS);
85
- return res.success(item, this.options.guardResponse, this.options.allowResponse);
86
- },
87
- ],
81
+ method: [adminRoleMiddleware([this.roles.READ]), validateMiddleware(REQUEST_TYPE.PARAMS, rulesID), this.methodGetOneTrash],
88
82
  };
89
83
  }
90
84
 
@@ -96,22 +90,21 @@ class AdminController extends Controller {
96
90
  return item;
97
91
  }
98
92
 
99
- postCreatePublic() {
100
- return {
101
- path: '/',
102
- method: [
103
- adminRoleMiddleware([this.options.roles.CREATE]),
104
- validateMiddleware(REQUEST_TYPE.BODY, this.options.rulesCreate),
105
- async (req, res) => {
106
- const data = await this.beforeCreate(req.body, req.user);
93
+ methodPostCreatePublic = async (req, res) => {
94
+ const data = await this.beforeCreate(req.body, req.user);
107
95
 
108
- let item = await this.repository.createByUser(req.user.id, data);
96
+ let item = await this.repository.createByUser(req.user.id, data);
109
97
 
110
- item = await this.afterCreate(item, req.user);
98
+ item = await this.afterCreate(item, req.user);
111
99
 
112
- return res.success(item, this.options.guardResponse, this.options.allowResponse);
113
- },
114
- ],
100
+ return res.success(item, this.guardResponse, this.allowResponse);
101
+ };
102
+
103
+ postCreatePublic() {
104
+ return {
105
+ disabled: !this.roles.CREATE,
106
+ path: '/',
107
+ method: [adminRoleMiddleware([this.roles.CREATE]), validateMiddleware(REQUEST_TYPE.BODY, this.rulesCreate), this.methodPostCreatePublic],
115
108
  };
116
109
  }
117
110
 
@@ -123,77 +116,99 @@ class AdminController extends Controller {
123
116
  return item;
124
117
  }
125
118
 
119
+ methodPutModifyPublic = async (req, res) => {
120
+ let item = await this.repository.findOneById(req.params.id);
121
+ if (!item) return res.error(NOT_EXISTS);
122
+
123
+ const data = await this.beforeModify(req.body, req.user);
124
+
125
+ item = await this.repository.modifyByUser(req.user.id, item, data);
126
+
127
+ item = await this.afterModify(item, req.user);
128
+
129
+ return res.success(item, this.guardResponse, this.allowResponse);
130
+ };
131
+
126
132
  putModifyPublic() {
127
133
  return {
134
+ disabled: !this.roles.UPDATE,
128
135
  path: '/:id',
129
136
  method: [
130
- adminRoleMiddleware([this.options.roles.UPDATE]),
137
+ adminRoleMiddleware([this.roles.UPDATE]),
131
138
  validateMiddleware(REQUEST_TYPE.PARAMS, rulesID),
132
- validateMiddleware(REQUEST_TYPE.BODY, this.options.rulesModify),
133
- async (req, res) => {
134
- let item = await this.repository.findOneById(req.params.id);
135
- if (!item) return res.error(NOT_EXISTS);
136
-
137
- const data = await this.beforeModify(req.body, req.user);
138
-
139
- item = await this.repository.modifyByUser(req.user.id, item, data);
140
-
141
- item = await this.afterModify(item, req.user);
142
-
143
- return res.success(item, this.options.guardResponse, this.options.allowResponse);
144
- },
139
+ validateMiddleware(REQUEST_TYPE.BODY, this.rulesModify),
140
+ this.methodPutModifyPublic,
145
141
  ],
146
142
  };
147
143
  }
148
144
 
145
+ methodDeletePublic = async (req, res) => {
146
+ const item = await this.repository.findOneById(req.params.id);
147
+ if (!item) return res.error(NOT_EXISTS);
148
+
149
+ await this.repository.deleteByUser(req.user.id, item);
150
+ return res.success(true);
151
+ };
152
+
149
153
  deletePublic() {
150
154
  return {
155
+ disabled: !this.roles.DELETE,
151
156
  path: '/:id',
152
- method: [
153
- adminRoleMiddleware([this.options.roles.DELETE]),
154
- validateMiddleware(REQUEST_TYPE.PARAMS, rulesID),
155
- async (req, res) => {
156
- const item = await this.repository.findOneById(req.params.id);
157
- if (!item) return res.error(NOT_EXISTS);
158
-
159
- await this.repository.deleteByUser(req.user.id, item);
160
- return res.success(true);
161
- },
162
- ],
157
+ method: [adminRoleMiddleware([this.roles.DELETE]), validateMiddleware(REQUEST_TYPE.PARAMS, rulesID), this.methodDeletePublic],
163
158
  };
164
159
  }
165
160
 
161
+ methodPostRestoreTrash = async (req, res) => {
162
+ const item = await this.repository.findOneTrashById(req.params.id);
163
+ if (!item) return res.error(NOT_EXISTS);
164
+
165
+ await this.repository.restoreByUser(req.user.id, item);
166
+ return res.success(true);
167
+ };
168
+
166
169
  postRestoreTrash() {
167
170
  return {
171
+ disabled: !this.roles.RESTORE,
168
172
  path: '/:id',
169
- method: [
170
- adminRoleMiddleware([this.options.roles.RESTORE]),
171
- validateMiddleware(REQUEST_TYPE.PARAMS, rulesID),
172
- async (req, res) => {
173
- const item = await this.repository.findOneTrashById(req.params.id);
174
- if (!item) return res.error(NOT_EXISTS);
175
-
176
- await this.repository.restoreByUser(req.user.id, item);
177
- return res.success(true);
178
- },
179
- ],
173
+ method: [adminRoleMiddleware([this.roles.RESTORE]), validateMiddleware(REQUEST_TYPE.PARAMS, rulesID), this.methodPostRestoreTrash],
180
174
  };
181
175
  }
182
176
 
177
+ methodDeleteDestroyTrash = async (req, res) => {
178
+ const item = await this.repository.findOneTrashById(req.params.id);
179
+ if (!item) return res.error(NOT_EXISTS);
180
+
181
+ await this.repository.destroy(item);
182
+ return res.success(true);
183
+ };
184
+
183
185
  deleteDestroyTrash() {
184
186
  return {
187
+ disabled: !this.roles.DESTROY,
185
188
  path: '/:id',
186
- method: [
187
- adminRoleMiddleware([this.options.roles.DESTROY]),
188
- validateMiddleware(REQUEST_TYPE.PARAMS, rulesID),
189
- async (req, res) => {
190
- const item = await this.repository.findOneTrashById(req.params.id);
191
- if (!item) return res.error(NOT_EXISTS);
189
+ method: [adminRoleMiddleware([this.roles.DESTROY]), validateMiddleware(REQUEST_TYPE.PARAMS, rulesID), this.methodDeleteDestroyTrash],
190
+ };
191
+ }
192
192
 
193
- await this.repository.destroy(item);
194
- return res.success(true);
195
- },
196
- ],
193
+ methodDPostImportSheet = async (req, res) => {
194
+ return res.success(true);
195
+ };
196
+ postImportSheet() {
197
+ return {
198
+ disabled: !this.roles.CREATE,
199
+ path: '/',
200
+ method: [adminRoleMiddleware([this.roles.CREATE]), verifyGoogleAppMiddleware(), this.methodDPostImportSheet],
201
+ };
202
+ }
203
+
204
+ methodPostExportSheet = async (req, res) => {
205
+ return res.success(true);
206
+ };
207
+ postExportSheet() {
208
+ return {
209
+ disabled: !this.roles.READ,
210
+ path: '/',
211
+ method: [adminRoleMiddleware([this.roles.READ]), verifyGoogleAppMiddleware(), this.methodPostExportSheet],
197
212
  };
198
213
  }
199
214
  }
package/src/Controller.js CHANGED
@@ -1,11 +1,7 @@
1
1
  class Controller {
2
- constructor(pathName = '', repository = null, options = {}) {
2
+ constructor(pathName = '', repository = null) {
3
3
  this.pathName = pathName;
4
4
  this.repository = repository;
5
-
6
- this.options = {
7
- ...options,
8
- };
9
5
  }
10
6
 
11
7
  publicRouter() {
package/src/Server.js CHANGED
@@ -254,30 +254,32 @@ class Server {
254
254
 
255
255
  const msg = [];
256
256
  _.map(this.controllers, (data) => {
257
- const controller = data.controller;
257
+ const {controller} = data;
258
258
  const controllerName = data.name;
259
259
  const controllerVersion = data.version;
260
260
 
261
261
  const listPublicRouter = controller.publicRouter();
262
262
 
263
263
  _.map(listPublicRouter, (method) => {
264
- const {name, type} = method;
265
-
266
- const router = controller[name]();
267
-
268
- router.path = `/${method.path}/${router.path}`;
269
- router.path = controller.pathName ? `/${controller.pathName}${router.path}` : router.path;
270
- router.path = controllerVersion.startsWith('v') ? `/${controllerVersion}${router.path}` : router.path;
271
- router.path = path.normalize(router.path);
272
-
273
- msg.push({
274
- controller: controllerName,
275
- version: controllerVersion,
276
- type,
277
- method: name,
278
- path: router.path,
279
- });
280
- app[type](router.path, ...router.method);
264
+ const {name, type, disabled} = method;
265
+
266
+ if (!disabled) {
267
+ const router = controller[name]();
268
+
269
+ router.path = `/${method.path}/${router.path}`;
270
+ router.path = controller.pathName ? `/${controller.pathName}${router.path}` : router.path;
271
+ router.path = controllerVersion.startsWith('v') ? `/${controllerVersion}${router.path}` : router.path;
272
+ router.path = path.normalize(router.path);
273
+
274
+ msg.push({
275
+ controller: controllerName,
276
+ version: controllerVersion,
277
+ type,
278
+ method: name,
279
+ path: router.path,
280
+ });
281
+ app[type](router.path, ...router.method);
282
+ }
281
283
  });
282
284
  });
283
285