@azteam/express 1.2.116 → 1.2.117

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.117",
4
4
  "description": "",
5
5
  "main": "src/index.js",
6
6
  "repository": {
@@ -0,0 +1,179 @@
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, options = {}) {
9
+ this.pathName = pathName;
10
+ this.repository = repository;
11
+
12
+ this.options = {
13
+ roles: {
14
+ READ: 1,
15
+ CREATE: 2,
16
+ UPDATE: 3,
17
+ DELETE: 4,
18
+ RESTORE: 5,
19
+ }, // system roles
20
+
21
+ rulesCreate: {},
22
+ rulesModify: {},
23
+
24
+ guardResponse: {},
25
+ allowResponse: {},
26
+
27
+ ...options,
28
+ }
29
+ }
30
+
31
+ getPaginate() {
32
+ return {
33
+ path: `/${this.pathName}`,
34
+ method: [
35
+ roleMiddleware([this.options.roles.READ], USER_LEVEL.ADMIN),
36
+ paginateMiddleware(paginateOptions),
37
+ async function(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
+ ],
43
+ }
44
+ }
45
+
46
+ getPaginateTrash() {
47
+ return {
48
+ path: `/trash_${this.pathName}`,
49
+ method: [
50
+ roleMiddleware([this.options.roles.READ], USER_LEVEL.ADMIN),
51
+ paginateMiddleware(paginateOptions),
52
+ async function(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
+ ],
58
+ }
59
+ }
60
+
61
+ getOne() {
62
+ return {
63
+ path: `/${this.pathName}/:id`,
64
+ method: [
65
+ roleMiddleware([this.options.roles.READ], USER_LEVEL.ADMIN),
66
+ validateMiddleware(REQUEST_TYPE.PARAMS, rulesID),
67
+ async function(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
+ ],
73
+ }
74
+
75
+ }
76
+
77
+ getOneTrash() {
78
+ return {
79
+ path: `/trash_${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.findOneTrashById(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
+
94
+ beforePostCreate(data) {
95
+ return data;
96
+ }
97
+
98
+ postCreate() {
99
+ return {
100
+ path: `/${this.pathName}`,
101
+ method: [
102
+ roleMiddleware([this.options.roles.CREATE], USER_LEVEL.ADMIN),
103
+ validateMiddleware(REQUEST_TYPE.BODY, this.options.rulesCreate),
104
+ async function(req, res) {
105
+
106
+ const data = this.beforePostCreate(req.body);
107
+
108
+ const item = await this.repository.createByUser(req.user.id, data);
109
+
110
+ return res.success(item, this.options.guardResponse, this.options.allowResponse);
111
+ },
112
+ ],
113
+ }
114
+ }
115
+
116
+
117
+ beforePutModify(item) {
118
+ return item;
119
+ }
120
+
121
+ putModify() {
122
+ return {
123
+ path: `/${this.pathName}/:id`,
124
+ method: [
125
+ roleMiddleware([this.options.roles.UPDATE], USER_LEVEL.ADMIN),
126
+ validateMiddleware(REQUEST_TYPE.PARAMS, rulesID),
127
+ validateMiddleware(REQUEST_TYPE.BODY, this.options.rulesModify),
128
+ async function(req, res) {
129
+
130
+ let item = await this.options.roles.findOneById(req.params.id);
131
+ if (!item) return res.error(NOT_EXISTS);
132
+
133
+ item = await this.beforePutModify(item);
134
+
135
+ item = await this.options.roles.modifyByUser(req.user.id, item, req.body);
136
+
137
+ return res.success(item, this.options.guardResponse, this.options.allowResponse);
138
+ },
139
+ ],
140
+ }
141
+ }
142
+
143
+ delete() {
144
+ return {
145
+ path: `/${this.pathName}/:id`,
146
+ method: [
147
+ roleMiddleware([this.options.roles.DELETE], USER_LEVEL.ADMIN),
148
+ validateMiddleware(REQUEST_TYPE.PARAMS, rulesID),
149
+ async function(req, res) {
150
+ const item = await this.repository.findOneById(req.params.id);
151
+ if (!item) return res.error(NOT_EXISTS);
152
+
153
+ await this.repository.deleteByUser(req.user.id, item);
154
+ return res.success(true);
155
+ },
156
+ ],
157
+ }
158
+ }
159
+
160
+ postRestore() {
161
+ return {
162
+ path: `/trash_${this.pathName}/:id`,
163
+ method: [
164
+ roleMiddleware([this.options.roles.RESTORE], USER_LEVEL.ADMIN),
165
+ validateMiddleware(REQUEST_TYPE.PARAMS, rulesID),
166
+ async function(req, res) {
167
+ const item = await this.repository.findOneTrashById(req.params.id);
168
+ if (!item) return res.error(NOT_EXISTS);
169
+
170
+ await this.repository.restoreByUser(req.user.id, item);
171
+ return res.success(true);
172
+ },
173
+ ],
174
+ }
175
+
176
+ },
177
+ }
178
+
179
+ 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,9 @@
1
1
  import ApiServer from './ApiServer';
2
2
  import SocketServer from './SocketServer';
3
3
 
4
+ import AdminController from './AdminController';
5
+
6
+
4
7
  export * from './middleware';
5
8
  export * from './constant';
6
9
  export * from './validate';
@@ -8,4 +11,6 @@ export * from './validate';
8
11
  export {
9
12
  ApiServer,
10
13
  SocketServer,
14
+
15
+ AdminController,
11
16
  }