@azteam/express 1.2.116 → 1.2.119

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.116",
3
+ "version": "1.2.119",
4
4
  "description": "",
5
5
  "main": "src/index.js",
6
6
  "repository": {
@@ -0,0 +1,194 @@
1
+ import { NOT_EXISTS, PERMISSION } from '@azteam/error';
2
+
3
+ import { HTTP_METHOD, paginateMiddleware, REQUEST_TYPE, roleMiddleware, validateMiddleware, rulesID } from './ApiServer';
4
+ import Controller from './Controller';
5
+
6
+ import { USER_LEVEL } from './constant';
7
+
8
+
9
+ class AdminController extends Controller {
10
+ constructor(pathName, repository, options = {}) {
11
+ this.options = {
12
+ roles: {
13
+ READ: 1,
14
+ CREATE: 2,
15
+ UPDATE: 3,
16
+ DELETE: 4,
17
+ RESTORE: 5,
18
+ }, // system roles
19
+
20
+ rulesCreate: {},
21
+ rulesModify: {},
22
+
23
+ guardResponse: {},
24
+ allowResponse: {},
25
+
26
+ ...options,
27
+ }
28
+
29
+ super(pathName, repository);
30
+ }
31
+
32
+
33
+ usePublic(controllers = []){
34
+ return super([
35
+ getPaginate,
36
+ getPaginateTrash,
37
+ getOne,
38
+ getOneTrash,
39
+ postCreate,
40
+ putModify,
41
+ deleteOne,
42
+ postRestore,
43
+ ]);
44
+ }
45
+
46
+
47
+ getPaginate() {
48
+ return {
49
+ path: `/${this.pathName}`,
50
+ method: [
51
+ roleMiddleware([this.options.roles.READ], USER_LEVEL.ADMIN),
52
+ paginateMiddleware(paginateOptions),
53
+ async function(req, res) {
54
+ const paginateData = await this.repository.find(req.query, req.paginate);
55
+
56
+ return res.success(paginateData, this.options.guardResponse, this.options.allowResponse);
57
+ },
58
+ ],
59
+ }
60
+ }
61
+
62
+ getPaginateTrash() {
63
+ return {
64
+ path: `/trash_${this.pathName}`,
65
+ method: [
66
+ roleMiddleware([this.options.roles.READ], USER_LEVEL.ADMIN),
67
+ paginateMiddleware(paginateOptions),
68
+ async function(req, res) {
69
+ const paginateData = await this.repository.findTrash(req.query, req.paginate);
70
+
71
+ return res.success(paginateData, this.options.guardResponse, this.options.allowResponse);
72
+ },
73
+ ],
74
+ }
75
+ }
76
+
77
+ getOne() {
78
+ return {
79
+ path: `/${this.pathName}/:id`,
80
+ method: [
81
+ roleMiddleware([this.options.roles.READ], USER_LEVEL.ADMIN),
82
+ validateMiddleware(REQUEST_TYPE.PARAMS, rulesID),
83
+ async function(req, res) {
84
+ const item = await this.repository.findOneById(req.params.id);
85
+ if (!item) return res.error(NOT_EXISTS);
86
+ return res.success(item, this.options.guardResponse, this.options.allowResponse);
87
+ },
88
+ ],
89
+ }
90
+
91
+ }
92
+
93
+ getOneTrash() {
94
+ return {
95
+ path: `/trash_${this.pathName}/:id`,
96
+ method: [
97
+ roleMiddleware([this.options.roles.READ], USER_LEVEL.ADMIN),
98
+ validateMiddleware(REQUEST_TYPE.PARAMS, rulesID),
99
+ async function(req, res) {
100
+ const item = await this.repository.findOneTrashById(req.params.id);
101
+ if (!item) return res.error(NOT_EXISTS);
102
+ return res.success(item, this.options.guardResponse, this.options.allowResponse);
103
+ },
104
+ ],
105
+ }
106
+
107
+ }
108
+
109
+
110
+ beforePostCreate(data) {
111
+ return data;
112
+ }
113
+
114
+ postCreate() {
115
+ return {
116
+ path: `/${this.pathName}`,
117
+ method: [
118
+ roleMiddleware([this.options.roles.CREATE], USER_LEVEL.ADMIN),
119
+ validateMiddleware(REQUEST_TYPE.BODY, this.options.rulesCreate),
120
+ async function(req, res) {
121
+
122
+ const data = this.beforePostCreate(req.body);
123
+
124
+ const item = await this.repository.createByUser(req.user.id, data);
125
+
126
+ return res.success(item, this.options.guardResponse, this.options.allowResponse);
127
+ },
128
+ ],
129
+ }
130
+ }
131
+
132
+
133
+ beforePutModify(item) {
134
+ return item;
135
+ }
136
+
137
+ putModify() {
138
+ return {
139
+ path: `/${this.pathName}/:id`,
140
+ method: [
141
+ roleMiddleware([this.options.roles.UPDATE], USER_LEVEL.ADMIN),
142
+ validateMiddleware(REQUEST_TYPE.PARAMS, rulesID),
143
+ validateMiddleware(REQUEST_TYPE.BODY, this.options.rulesModify),
144
+ async function(req, res) {
145
+
146
+ let item = await this.options.roles.findOneById(req.params.id);
147
+ if (!item) return res.error(NOT_EXISTS);
148
+
149
+ item = await this.beforePutModify(item);
150
+
151
+ item = await this.options.roles.modifyByUser(req.user.id, item, req.body);
152
+
153
+ return res.success(item, this.options.guardResponse, this.options.allowResponse);
154
+ },
155
+ ],
156
+ }
157
+ }
158
+
159
+ deleteOne() {
160
+ return {
161
+ path: `/${this.pathName}/:id`,
162
+ method: [
163
+ roleMiddleware([this.options.roles.DELETE], USER_LEVEL.ADMIN),
164
+ validateMiddleware(REQUEST_TYPE.PARAMS, rulesID),
165
+ async function(req, res) {
166
+ const item = await this.repository.findOneById(req.params.id);
167
+ if (!item) return res.error(NOT_EXISTS);
168
+
169
+ await this.repository.deleteByUser(req.user.id, item);
170
+ return res.success(true);
171
+ },
172
+ ],
173
+ }
174
+ }
175
+
176
+ postRestore() {
177
+ return {
178
+ path: `/trash_${this.pathName}/:id`,
179
+ method: [
180
+ roleMiddleware([this.options.roles.RESTORE], USER_LEVEL.ADMIN),
181
+ validateMiddleware(REQUEST_TYPE.PARAMS, rulesID),
182
+ async function(req, res) {
183
+ const item = await this.repository.findOneTrashById(req.params.id);
184
+ if (!item) return res.error(NOT_EXISTS);
185
+
186
+ await this.repository.restoreByUser(req.user.id, item);
187
+ return res.success(true);
188
+ },
189
+ ],
190
+ }
191
+ },
192
+ }
193
+
194
+ export default AdminController;
@@ -0,0 +1,21 @@
1
+ import { NOT_EXISTS, PERMISSION } from '@azteam/error';
2
+
3
+ import { HTTP_METHOD, paginateMiddleware, REQUEST_TYPE, roleMiddleware, validateMiddleware, rulesID } from './ApiServer';
4
+ import { USER_LEVEL } from './constant';
5
+
6
+
7
+ class Controller {
8
+ constructor(pathName, repository) {
9
+ this.pathName = pathName;
10
+ this.repository = repository;
11
+ }
12
+
13
+
14
+ usePublic(controllers = []) {
15
+ return [
16
+ ...controllers
17
+ ]
18
+ }
19
+ }
20
+
21
+ export default Controller;
package/src/constant.js CHANGED
@@ -11,3 +11,10 @@ export const REQUEST_TYPE = {
11
11
  BODY: 'body',
12
12
  QUERY: 'query',
13
13
  }
14
+
15
+ export const USER_LEVEL = {
16
+ GUESS: 0,
17
+ USER: 1,
18
+ ADMIN: 50,
19
+ SYSTEM: 100,
20
+ };
package/src/index.js CHANGED
@@ -1,6 +1,10 @@
1
1
  import ApiServer from './ApiServer';
2
2
  import SocketServer from './SocketServer';
3
3
 
4
+ import Controller from './Controller';
5
+ import AdminController from './AdminController';
6
+
7
+
4
8
  export * from './middleware';
5
9
  export * from './constant';
6
10
  export * from './validate';
@@ -8,4 +12,7 @@ export * from './validate';
8
12
  export {
9
13
  ApiServer,
10
14
  SocketServer,
11
- }
15
+
16
+ Controller,
17
+ AdminController,
18
+ }