bmd-extension 1.6.3

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.
Files changed (226) hide show
  1. package/.vscode/launch.json +14 -0
  2. package/.vscode/settings.json +1 -0
  3. package/CHANGELOG.md +9 -0
  4. package/DEPLOY.md +4 -0
  5. package/README.md +17 -0
  6. package/README_PRIVATE.md +1 -0
  7. package/build.sh +5 -0
  8. package/logo.png +0 -0
  9. package/media/dark/add.svg +40 -0
  10. package/media/dark/block.svg +1 -0
  11. package/media/dark/checklist-1.svg +1 -0
  12. package/media/dark/checklist.svg +1 -0
  13. package/media/dark/control.svg +1 -0
  14. package/media/dark/create.svg +1 -0
  15. package/media/dark/delete-1.svg +1 -0
  16. package/media/dark/delete.svg +1 -0
  17. package/media/dark/deploy-1.svg +2 -0
  18. package/media/dark/deploy.svg +2 -0
  19. package/media/dark/documents.svg +66 -0
  20. package/media/dark/earth.svg +68 -0
  21. package/media/dark/exe.svg +15 -0
  22. package/media/dark/export.svg +1 -0
  23. package/media/dark/function.svg +1 -0
  24. package/media/dark/game-controller.svg +77 -0
  25. package/media/dark/get.svg +1 -0
  26. package/media/dark/id-card.svg +59 -0
  27. package/media/dark/identity.svg +81 -0
  28. package/media/dark/import.svg +1 -0
  29. package/media/dark/information.svg +1 -0
  30. package/media/dark/interface.svg +1 -0
  31. package/media/dark/layout.svg +41 -0
  32. package/media/dark/leaf.svg +54 -0
  33. package/media/dark/list.svg +13 -0
  34. package/media/dark/logo.svg +249 -0
  35. package/media/dark/mockup.svg +1 -0
  36. package/media/dark/needle.svg +1 -0
  37. package/media/dark/pass.svg +46 -0
  38. package/media/dark/plus.svg +1 -0
  39. package/media/dark/post.svg +1 -0
  40. package/media/dark/quit.svg +1 -0
  41. package/media/dark/rocket.svg +28 -0
  42. package/media/dark/service.svg +1 -0
  43. package/media/dark/settings.svg +98 -0
  44. package/media/dark/social.svg +1 -0
  45. package/media/dark/spaceship-1.svg +1 -0
  46. package/media/dark/spaceship-2.svg +1 -0
  47. package/media/dark/spaceship.svg +1 -0
  48. package/media/dark/support.svg +88 -0
  49. package/media/dark/transfer.svg +1 -0
  50. package/media/dark/transform.svg +92 -0
  51. package/media/dark/update.svg +45 -0
  52. package/media/dark/upload-1.svg +1 -0
  53. package/media/dark/upload.svg +41 -0
  54. package/media/dark/vaccine.svg +1 -0
  55. package/media/dark/vr-gaming.svg +1 -0
  56. package/media/light/add.svg +40 -0
  57. package/media/light/block.svg +1 -0
  58. package/media/light/checklist-1.svg +1 -0
  59. package/media/light/checklist.svg +1 -0
  60. package/media/light/control.svg +1 -0
  61. package/media/light/create.svg +1 -0
  62. package/media/light/delete-1.svg +1 -0
  63. package/media/light/delete.svg +1 -0
  64. package/media/light/deploy-1.svg +2 -0
  65. package/media/light/deploy.svg +2 -0
  66. package/media/light/documents.svg +66 -0
  67. package/media/light/earth.svg +68 -0
  68. package/media/light/exe.svg +15 -0
  69. package/media/light/export.svg +1 -0
  70. package/media/light/function.svg +1 -0
  71. package/media/light/game-controller.svg +77 -0
  72. package/media/light/get.svg +1 -0
  73. package/media/light/id-card.svg +59 -0
  74. package/media/light/identity.svg +81 -0
  75. package/media/light/import.svg +1 -0
  76. package/media/light/information.svg +1 -0
  77. package/media/light/interface.svg +1 -0
  78. package/media/light/layout.svg +41 -0
  79. package/media/light/leaf.svg +54 -0
  80. package/media/light/list.svg +13 -0
  81. package/media/light/logo.svg +249 -0
  82. package/media/light/mockup.svg +1 -0
  83. package/media/light/needle.svg +1 -0
  84. package/media/light/pass.svg +46 -0
  85. package/media/light/plus.svg +1 -0
  86. package/media/light/post.svg +1 -0
  87. package/media/light/quit.svg +1 -0
  88. package/media/light/rocket.svg +28 -0
  89. package/media/light/service.svg +1 -0
  90. package/media/light/settings.svg +98 -0
  91. package/media/light/social.svg +1 -0
  92. package/media/light/spaceship-1.svg +1 -0
  93. package/media/light/spaceship-2.svg +1 -0
  94. package/media/light/spaceship.svg +1 -0
  95. package/media/light/support.svg +88 -0
  96. package/media/light/transfer.svg +1 -0
  97. package/media/light/transform.svg +92 -0
  98. package/media/light/update.svg +45 -0
  99. package/media/light/upload-1.svg +1 -0
  100. package/media/light/upload.svg +41 -0
  101. package/media/light/vaccine.svg +1 -0
  102. package/media/light/vr-gaming.svg +1 -0
  103. package/package-lock.json +6430 -0
  104. package/package.json +540 -0
  105. package/snippets-tsr.json +512 -0
  106. package/snippets.json +761 -0
  107. package/src/API.ts +14 -0
  108. package/src/FsProvider.ts +105 -0
  109. package/src/Request.ts +24 -0
  110. package/src/assets/api/api.txt +27 -0
  111. package/src/assets/configuration/AdminConfigurationController.ts.txt +72 -0
  112. package/src/assets/configuration/Configuration.ts.txt +37 -0
  113. package/src/assets/configuration/ConfigurationService.ts.txt +26 -0
  114. package/src/assets/contentDefine/AdminContentDefineController.ts.txt +91 -0
  115. package/src/assets/contentDefine/ContentDefine.ts.txt +45 -0
  116. package/src/assets/contentDefine/ContentDefineService.ts.txt +19 -0
  117. package/src/assets/contentDefine/CustomerContentDefineController.ts.txt +34 -0
  118. package/src/assets/controller/controller.txt +46 -0
  119. package/src/assets/controller/controllerResource.txt +103 -0
  120. package/src/assets/entity/entity.txt +22 -0
  121. package/src/assets/entity-request/entity-request.txt +18 -0
  122. package/src/assets/init/.env.example.txt +37 -0
  123. package/src/assets/init/.env.production.txt +37 -0
  124. package/src/assets/init/.gitignore.txt +8 -0
  125. package/src/assets/init/config.ts.txt +55 -0
  126. package/src/assets/init/deploy.sh.txt +46 -0
  127. package/src/assets/init/package-lock.json.txt +4184 -0
  128. package/src/assets/init/package.json.txt +79 -0
  129. package/src/assets/init/src/Server.ts.txt +167 -0
  130. package/src/assets/init/src/controllers/admin/AuthController.ts.txt +96 -0
  131. package/src/assets/init/src/controllers/admin/CustomerController.ts.txt +107 -0
  132. package/src/assets/init/src/controllers/admin/RoleController.ts.txt +143 -0
  133. package/src/assets/init/src/controllers/admin/StaffController.ts.txt +192 -0
  134. package/src/assets/init/src/controllers/customer/AuthController.ts.txt +170 -0
  135. package/src/assets/init/src/controllers/customer/CustomerController.ts.txt +32 -0
  136. package/src/assets/init/src/core/entity/CoreEntity.ts.txt +70 -0
  137. package/src/assets/init/src/core/services/CoreService.ts.txt +21 -0
  138. package/src/assets/init/src/entity/Customer.ts.txt +68 -0
  139. package/src/assets/init/src/entity/Permission.ts.txt +29 -0
  140. package/src/assets/init/src/entity/Role.ts.txt +36 -0
  141. package/src/assets/init/src/entity/Staff.ts.txt +54 -0
  142. package/src/assets/init/src/entity-request/CustomerInsert.ts.txt +42 -0
  143. package/src/assets/init/src/entity-request/CustomerUpdate.ts.txt +40 -0
  144. package/src/assets/init/src/entity-request/PermissionImport.ts.txt +12 -0
  145. package/src/assets/init/src/entity-request/StaffUpdate.ts.txt +33 -0
  146. package/src/assets/init/src/index.ts.txt +13 -0
  147. package/src/assets/init/src/middleware/auth/Verification.ts.txt +16 -0
  148. package/src/assets/init/src/middleware/auth/VerificationJWT.ts.txt +16 -0
  149. package/src/assets/init/src/middleware/auth/strategy/AuthStrategy.ts.txt +5 -0
  150. package/src/assets/init/src/middleware/auth/strategy/JWT.ts.txt +147 -0
  151. package/src/assets/init/src/middleware/error/handleError.ts.txt +22 -0
  152. package/src/assets/init/src/middleware/error/handleNotFound.ts.txt +7 -0
  153. package/src/assets/init/src/middleware/response/CustomSendResponse.ts.txt +35 -0
  154. package/src/assets/init/src/middleware/response/responseAPI.ts.txt +76 -0
  155. package/src/assets/init/src/middleware/validator/Validator.ts.txt +103 -0
  156. package/src/assets/init/src/services/CustomerService.ts.txt +79 -0
  157. package/src/assets/init/src/services/InitService.ts.txt +11 -0
  158. package/src/assets/init/src/services/MailService.ts.txt +271 -0
  159. package/src/assets/init/src/services/RoleService.ts.txt +66 -0
  160. package/src/assets/init/src/services/StaffService.ts.txt +104 -0
  161. package/src/assets/init/src/ssl/certificate-ca.crt +0 -0
  162. package/src/assets/init/src/ssl/certificate.crt +0 -0
  163. package/src/assets/init/src/ssl/private.key +0 -0
  164. package/src/assets/init/src/types/express.d.ts.txt +97 -0
  165. package/src/assets/init/src/util/expo.ts.txt +53 -0
  166. package/src/assets/init/src/util/helper.ts.txt +321 -0
  167. package/src/assets/init/src/util/language.ts.txt +8 -0
  168. package/src/assets/init/src/util/logger.ts.txt +47 -0
  169. package/src/assets/init/src/util/mailer.ts.txt +32 -0
  170. package/src/assets/init/src/util/password.ts.txt +13 -0
  171. package/src/assets/init/tsconfig.json.txt +26 -0
  172. package/src/assets/service/service.txt +34 -0
  173. package/src/constant.ts +83 -0
  174. package/src/extension.ts +459 -0
  175. package/src/provider/codeAction/controller/addPathParams.ts +65 -0
  176. package/src/provider/codeAction/controller/addTokenParam.ts +43 -0
  177. package/src/provider/codeAction/controller/addValidation.ts +47 -0
  178. package/src/provider/codeAction/controller/codeAction.ts +34 -0
  179. package/src/provider/codeAction/controller/util.ts +48 -0
  180. package/src/provider/codeAction/entity/codeAction.ts +48 -0
  181. package/src/provider/codeAction/entity/handleBuilder.ts +87 -0
  182. package/src/provider/codeAction/entity/handleFunction.ts +487 -0
  183. package/src/provider/codeAction/entity/handleProperty.ts +32 -0
  184. package/src/provider/codeAction/entity/handleRelation.ts +72 -0
  185. package/src/provider/codeAction/entity/helper.ts +132 -0
  186. package/src/provider/codeAction/entity-request/codeAction.ts +178 -0
  187. package/src/provider/codeAction/enum/codeAction.ts +95 -0
  188. package/src/provider/codeAction/service/codeAction.ts +232 -0
  189. package/src/provider/completion/CompletionProvider.ts +108 -0
  190. package/src/provider/errorChecking/checkHeaderToken.ts +60 -0
  191. package/src/provider/errorChecking/checkPathParam.ts +64 -0
  192. package/src/provider/errorChecking/checkRequired.ts +56 -0
  193. package/src/provider/errorChecking/errorChecking.ts +35 -0
  194. package/src/provider/errorChecking/util.ts +56 -0
  195. package/src/provider/treeDataProvider/Dependency.ts +26 -0
  196. package/src/provider/treeDataProvider/TreeProviderCommand.ts +60 -0
  197. package/src/provider/treeDataProvider/TreeProviderProject.ts +65 -0
  198. package/src/provider/treeDataProvider/api/createApi.ts +106 -0
  199. package/src/provider/treeDataProvider/controller/command/createController.ts +99 -0
  200. package/src/provider/treeDataProvider/controller/command/handleMethod.ts +363 -0
  201. package/src/provider/treeDataProvider/controller/treeData.ts +81 -0
  202. package/src/provider/treeDataProvider/deploy/command/handleDeploy.ts +70 -0
  203. package/src/provider/treeDataProvider/deploy/treeData.ts +21 -0
  204. package/src/provider/treeDataProvider/entity/command/addProperty.ts +144 -0
  205. package/src/provider/treeDataProvider/entity/command/addRelation.ts +125 -0
  206. package/src/provider/treeDataProvider/entity/command/createEntity.ts +53 -0
  207. package/src/provider/treeDataProvider/entity/command/createEntityRequest.ts +65 -0
  208. package/src/provider/treeDataProvider/entity/command/exportInterface.ts +130 -0
  209. package/src/provider/treeDataProvider/entity/treeData.ts +49 -0
  210. package/src/provider/treeDataProvider/module/command/configuration.ts +34 -0
  211. package/src/provider/treeDataProvider/module/command/contentDefine.ts +36 -0
  212. package/src/provider/treeDataProvider/module/command/initProject.ts +155 -0
  213. package/src/provider/treeDataProvider/module/treeData.ts +28 -0
  214. package/src/provider/treeDataProvider/project/command/addEnum.ts +0 -0
  215. package/src/provider/treeDataProvider/project/command/addProjectName.ts +23 -0
  216. package/src/provider/treeDataProvider/project/command/getProjectDetails.ts +284 -0
  217. package/src/provider/treeDataProvider/project/treeData.ts +28 -0
  218. package/src/provider/treeDataProvider/service/command/createService.ts +70 -0
  219. package/src/provider/treeDataProvider/service/command/handleConstructor.ts +134 -0
  220. package/src/provider/treeDataProvider/service/treeData.ts +25 -0
  221. package/src/types/project.d.ts +7 -0
  222. package/src/util.ts +145 -0
  223. package/src/utils/Password.ts +19 -0
  224. package/tsconfig.json +18 -0
  225. package/tslint.json +15 -0
  226. package/vsc-extension-quickstart.md +42 -0
@@ -0,0 +1,192 @@
1
+ import { RoleService } from './../../services/RoleService';
2
+ // IMPORT LIBRARY
3
+ import { Controller, Post, UseAuth, Req, Request, Res, Response, HeaderParams, BodyParams, Get, PathParams, QueryParams } from '@tsed/common';
4
+ import { Docs } from '@tsed/swagger';
5
+ import Joi from '@hapi/joi';
6
+ import { Raw } from 'typeorm';
7
+
8
+ // IMPORT CUSTOM
9
+ import { VerificationJWT } from '../../middleware/auth/VerificationJWT';
10
+ import { Validator } from '../../middleware/validator/Validator';
11
+ import { Staff } from '../../entity/Staff';
12
+ import { MultipartFile } from '@tsed/multipartfiles';
13
+ import config from '../../../config';
14
+ import { StaffService } from '../../services/StaffService';
15
+ import JWT, { AuthType } from '../../middleware/auth/strategy/JWT';
16
+
17
+ import { Role } from '../../entity/Role';
18
+ import { StaffUpdate } from '../../entity-request/StaffUpdate';
19
+ import { Password } from '../../util/password';
20
+ import CONFIG from '../../../config';
21
+
22
+ @Controller("/admin/staff")
23
+ @Docs("docs_admin")
24
+ export class StaffController {
25
+ constructor(
26
+ private staffService: StaffService,
27
+ private roleService: RoleService
28
+ ) { }
29
+
30
+
31
+ // =====================GET LIST=====================
32
+ @Get('')
33
+ @UseAuth(VerificationJWT)
34
+ async findAll(
35
+ @QueryParams('page') page: number,
36
+ @QueryParams('limit') limit: number,
37
+ @QueryParams('search') search: string = '',
38
+ @HeaderParams("token") token: string,
39
+ @Req() req: Request,
40
+ @Res() res: Response
41
+ ) {
42
+ let where = `staff.name LIKE :search`
43
+
44
+ const [staffs, total] = await Staff.createQueryBuilder('staff')
45
+ .where(where, {search: `%${search}%`})
46
+ .skip((page - 1) * limit)
47
+ .take(limit)
48
+ .orderBy('staff.id', 'DESC')
49
+ .getManyAndCount()
50
+
51
+ return res.sendOK({ staffs, total });
52
+ }
53
+
54
+
55
+ // =====================CREATE=====================
56
+ @Post('')
57
+ @UseAuth(VerificationJWT)
58
+ @Validator({
59
+ staff: Joi.required(),
60
+ roleId: Joi.number().required()
61
+ })
62
+ async create(
63
+ @BodyParams('staff') staff: Staff,
64
+ @BodyParams('roleId') roleId: number,
65
+ @HeaderParams("token") token: string,
66
+ @Req() req: Request,
67
+ @Res() res: Response,
68
+ ) {
69
+ await this.staffService.checkDuplicate(staff);
70
+
71
+ staff.password = await Password.hash(staff.password);
72
+ staff.role = new Role();
73
+ staff.role.id = roleId;
74
+ delete staff.id;
75
+ await staff.save();
76
+
77
+ return res.sendOK(staff)
78
+ }
79
+
80
+
81
+ // =====================UPDATE ADMIN INFO=====================
82
+ @Post('/:staffId/update')
83
+ @UseAuth(VerificationJWT)
84
+ @Validator({
85
+ staff: Joi.required(),
86
+ staffId: Joi.number().required()
87
+ })
88
+ async update(
89
+ @BodyParams('staff') staff: StaffUpdate,
90
+ @BodyParams("roleId") roleId: number,
91
+ @PathParams('staffId') staffId: number,
92
+ @HeaderParams("token") token: string,
93
+ @Req() req: Request,
94
+ @Res() res: Response,
95
+ ) {
96
+ await Staff.findOneOrThrowId(staffId);
97
+ const role = await Role.findOneOrThrowId(roleId)
98
+
99
+ const newStaff = staff.toStaff();
100
+ newStaff.id = staffId;
101
+ newStaff.role = role
102
+ await newStaff.save();
103
+
104
+ return res.sendOK(staff)
105
+ }
106
+
107
+
108
+ // =====================RESET PASSWORD=====================
109
+ @Post('/:staffId/password/reset')
110
+ @UseAuth(VerificationJWT)
111
+ @Validator({
112
+ newPassword: Joi.string().required(),
113
+ staffId: Joi.number().required()
114
+ })
115
+ async resetPassword(
116
+ @BodyParams('newPassword') newPassword: string,
117
+ @PathParams('staffId') staffId: number,
118
+ @HeaderParams("token") token: string,
119
+ @Req() req: Request,
120
+ @Res() res: Response,
121
+ ) {
122
+ const staff = await Staff.findOneOrThrowId(staffId);
123
+ staff.password = await Password.hash(newPassword);
124
+ await staff.save();
125
+
126
+ return res.sendOK(staff)
127
+ }
128
+
129
+
130
+ // =====================UPDATE ADMIN ROLE=====================
131
+ @Post('/:staffId/update/role')
132
+ @UseAuth(VerificationJWT)
133
+ @Validator({
134
+ roleId: Joi.number().required(),
135
+ staffId: Joi.number().required()
136
+ })
137
+ async updateRoleAdmin(
138
+ @BodyParams('roleId') roleId: number,
139
+ @PathParams('staffId') staffId: number,
140
+ @HeaderParams("token") token: string,
141
+ @Req() req: Request,
142
+ @Res() res: Response,
143
+ ) {
144
+ const staff = await Staff.findOneOrThrowId(staffId);
145
+ const role = await Role.findOneOrThrowId(roleId);
146
+
147
+ staff.id = staffId;
148
+ staff.role = role;
149
+ await staff.save();
150
+
151
+ return res.sendOK(staff)
152
+ }
153
+
154
+
155
+ // =====================INIT=====================
156
+ @Post('/init')
157
+ @Validator({
158
+ bmdPassword: Joi.required()
159
+ })
160
+ async init(
161
+ @Res() res: Response,
162
+ @Req() req: Request,
163
+ @BodyParams('bmdPassword') bmdPassword: string,
164
+ ) {
165
+ if (bmdPassword == 'bmd123456789') {
166
+ const roleAdmin = await this.roleService.initRole('Admin', 'Quản trị toàn hệ thống')
167
+ await this.roleService.initRole('User', 'Quản lí một vài tính năng')
168
+
169
+ this.staffService.initStaff(roleAdmin, 'Admin', 'admin', 'bmd1234567890')
170
+ this.staffService.initStaff(roleAdmin, 'Developer', 'develop', 'bmd1234567890')
171
+
172
+ return res.sendOK({}, 'Init success')
173
+ } else {
174
+ return res.sendClientError('Wrong password')
175
+ }
176
+ }
177
+
178
+
179
+ // =====================UPLOAD IMAGE=====================
180
+ @Post('/upload')
181
+ @UseAuth(VerificationJWT)
182
+ uploadFile(
183
+ @HeaderParams("token") token: string,
184
+ @Req() req: Request,
185
+ @Res() res: Response,
186
+ @MultipartFile('file') file: Express.Multer.File,
187
+ ) {
188
+ file.path = file.path.replace(CONFIG.UPLOAD_DIR, '');
189
+ return res.sendOK(file)
190
+ }
191
+
192
+ } // END FILE
@@ -0,0 +1,170 @@
1
+ // IMPORT LIBRARY
2
+ import { Controller, UseAuth, Req, Get, Res, Response, HeaderParams, PathParams, Post, BodyParams } from '@tsed/common';
3
+ import Joi from '@hapi/joi';
4
+ import { Docs } from '@tsed/swagger';
5
+ import { Request } from 'express';
6
+
7
+
8
+ // IMPORT CUSTOM
9
+ import { Validator } from '../../middleware/validator/Validator';
10
+ import { VerificationJWT } from '../../middleware/auth/VerificationJWT';
11
+ import JWT, { AuthType } from '../../middleware/auth/strategy/JWT';
12
+ import { CustomerService } from '../../services/CustomerService';
13
+ import { CustomerUpdate } from '../../entity-request/CustomerUpdate';
14
+ import { Password } from '../../util/password';
15
+ import { Customer } from '../../entity/Customer';
16
+ import { getCurrentTimeInt, randomString } from '../../util/helper';
17
+ import { MailService } from '../../services/MailService';
18
+
19
+
20
+ @Controller("/customer/auth")
21
+ @Docs("docs_customer")
22
+ export class AuthController {
23
+ constructor(
24
+ private mailService: MailService,
25
+ private customerService: CustomerService,
26
+ ) { }
27
+
28
+
29
+ // =====================LOGIN=====================
30
+ @Post('/login')
31
+ @Validator({
32
+ phone: Joi.string().required(),
33
+ password: Joi.string().required()
34
+ })
35
+ async login(
36
+ @HeaderParams("version") version: string,
37
+ @BodyParams('phone') phone: string,
38
+ @BodyParams('password') password: string,
39
+ @BodyParams('expoToken') expoToken: string,
40
+ @Res() res: Response
41
+ ) {
42
+ const customer = await this.customerService.login(phone, password);
43
+ customer.expoToken = expoToken
44
+ await customer.save()
45
+ const token = JWT.sign({ id: customer.id, type: AuthType.Customer });
46
+
47
+ return res.sendOK({ token })
48
+ }
49
+
50
+
51
+ // =====================PROFILE=====================
52
+ @Get('/profile')
53
+ @UseAuth(VerificationJWT)
54
+ async getInfo(
55
+ @HeaderParams("version") version: string,
56
+ @HeaderParams("token") token: string,
57
+ @Req() req: Request,
58
+ @Res() res: Response,
59
+ ) {
60
+ return res.sendOK(req.customer)
61
+ }
62
+
63
+
64
+ // =====================UPDATE PROFILE=====================
65
+ @Post('/profile')
66
+ @UseAuth(VerificationJWT)
67
+ async updateInfo(
68
+ @HeaderParams("token") token: string,
69
+ @HeaderParams("version") version: string,
70
+ @Req() req: Request,
71
+ @Res() res: Response,
72
+ @BodyParams("customer") customerUpdate: CustomerUpdate,
73
+ ) {
74
+ const customer = customerUpdate.toCustomer()
75
+ customer.id = req.customer.id
76
+
77
+ await customer.save()
78
+
79
+ return res.sendOK(customer)
80
+ }
81
+
82
+
83
+ // =====================UPDATE PASSWORD=====================
84
+ @Post('/password/update')
85
+ @UseAuth(VerificationJWT)
86
+ @Validator({
87
+ oldPassword: Joi.string().required(),
88
+ newPassword: Joi.string().required()
89
+ })
90
+ async changePassword(
91
+ @HeaderParams("version") version: string,
92
+ @HeaderParams("token") token: string,
93
+ @Req() req: Request,
94
+ @Res() res: Response,
95
+ @BodyParams('oldPassword') oldPassword: string,
96
+ @BodyParams('newPassword') newPassword: string,
97
+ ) {
98
+ const { customer } = req;
99
+
100
+ await this.customerService.validatePassword(customer, oldPassword)
101
+
102
+ if (oldPassword == newPassword) {
103
+ return res.sendClientError('Mật khẩu mới không được trùng mật khẩu cũ')
104
+ }
105
+
106
+ // Update password
107
+ customer.password = await Password.hash(newPassword);
108
+ await customer.save();
109
+
110
+ return res.sendOK(customer, 'Cập nhật mật khẩu thành công');
111
+ }
112
+
113
+
114
+ // =====================FORGOT=====================
115
+ @Post('/password/forgot')
116
+ @Validator({
117
+ email: Joi.required(),
118
+ })
119
+ async forgot(
120
+ @HeaderParams("version") version: string,
121
+ @BodyParams("email") email: string,
122
+ @Req() req: Request,
123
+ @Res() res: Response,
124
+ ) {
125
+ const customer = await Customer.findOne({ where: { email } })
126
+ if (!customer) {
127
+ return res.sendClientError('Email không tồn tại')
128
+ }
129
+
130
+ const token = JWT.sign({ id: customer.id, type: AuthType.Customer, ia: getCurrentTimeInt() })
131
+ this.mailService.sendMailLinkReset(token, customer)
132
+
133
+ return res.sendOK({}, 'Vui lòng kiểm tra email và truy cập vào đường link xác nhận.')
134
+ }
135
+
136
+
137
+ // =====================CONFIRM FORGOT=====================
138
+ @Post('/password/forgot/confirm')
139
+ @Validator({
140
+ token: Joi.required(),
141
+ })
142
+ async reForgot(
143
+ @HeaderParams("version") version: string,
144
+ @BodyParams("token") token: string,
145
+ @Req() req: Request,
146
+ @Res() res: Response,
147
+ ) {
148
+ const customerId = new JWT().getAuthId(token, AuthType.Customer)
149
+
150
+ const customer = await Customer.findOne({ where: { id: customerId } })
151
+ if (!customer) {
152
+ return res.sendClientError("Tài khoản không tồn tại")
153
+ }
154
+
155
+ const ia = JWT.getIa(token)
156
+ if (ia < customer.updatedAt) {
157
+ return res.sendClientError("Yêu cầu đã hết hạn. Vui lòng gửi yêu cầu khác.")
158
+ }
159
+
160
+ const newPassword = randomString(6)
161
+ customer.password = await Password.hash(newPassword)
162
+ await customer.save()
163
+
164
+ this.mailService.sendMailReset(newPassword, customer)
165
+
166
+ return res.sendOK(customer)
167
+ }
168
+
169
+
170
+ } // END FILE
@@ -0,0 +1,32 @@
1
+ // IMPORT LIBRARY
2
+ import { Request } from 'express';
3
+ import { Controller, UseAuth, Req, Get, Res, Response, HeaderParams, Post, BodyParams } from '@tsed/common';
4
+ import Joi from '@hapi/joi';
5
+ import { Docs } from '@tsed/swagger';
6
+ import { MultipartFile } from '@tsed/multipartfiles';
7
+
8
+ // IMPORT CUSTOM
9
+ import { VerificationJWT } from '../../middleware/auth/VerificationJWT';
10
+ import CONFIG from '../../../config';
11
+
12
+ @Controller("/customer/customer")
13
+ @Docs("docs_customer")
14
+ export class CustomerController {
15
+
16
+ constructor() { }
17
+
18
+
19
+ // =====================UPLOAD IMAGE=====================
20
+ @Post('/upload')
21
+ @UseAuth(VerificationJWT)
22
+ uploadFile(
23
+ @HeaderParams("version") version: string,
24
+ @HeaderParams('token') token: string,
25
+ @Res() res: Response,
26
+ @MultipartFile('file') file: Express.Multer.File,
27
+ ) {
28
+ file.path = file.path.replace(CONFIG.UPLOAD_DIR, '');
29
+ return res.sendOK(file)
30
+ }
31
+
32
+ } // END FILE
@@ -0,0 +1,70 @@
1
+ import { Column, BaseEntity, ObjectType, ObjectID, FindOneOptions, FindConditions, Repository, Connection, PrimaryGeneratedColumn } from "typeorm";
2
+ import { BadRequest } from "ts-httpexceptions";
3
+
4
+ import { getCurrentTimeInt } from "../../util/helper"
5
+
6
+ export default class CoreEntity extends BaseEntity {
7
+ public static connection: Connection;
8
+
9
+ constructor() {
10
+ super()
11
+ }
12
+
13
+
14
+ // PROPERTIES
15
+
16
+ @PrimaryGeneratedColumn()
17
+ id: number;
18
+
19
+ @Column()
20
+ createdAt: number;
21
+
22
+ @Column()
23
+ updatedAt: number;
24
+
25
+
26
+ // METHODS
27
+
28
+ static getRepository<T extends BaseEntity>(this: ObjectType<T>): Repository<T> {
29
+ const connection: Connection = (this as any).usedConnection || (this as any).connection;
30
+ return connection.getRepository<T>(this);
31
+ }
32
+
33
+
34
+ save(): Promise<this> {
35
+ if (!this.hasId()) {
36
+ this.createdAt = getCurrentTimeInt()
37
+ }
38
+ this.updatedAt = getCurrentTimeInt()
39
+ return super.save()
40
+ }
41
+
42
+
43
+ static async findOneOrThrowId<T extends BaseEntity>(
44
+ this: ObjectType<T>, id?: string | number | Date | ObjectID,
45
+ options?: FindOneOptions<T>,
46
+ replaceName?: string
47
+ ): Promise<T> {
48
+ try {
49
+ return await super.findOneOrFail<T>(id, options)
50
+ } catch (error) {
51
+ console.log(error);
52
+ throw new BadRequest(`${replaceName ? replaceName : this.name} không tồn tại.`)
53
+ }
54
+ }
55
+
56
+
57
+ static async findOneOrThrowOption<T extends BaseEntity>(
58
+ this: ObjectType<T>,
59
+ options?: FindOneOptions<T>,
60
+ replaceName?: string
61
+ ): Promise<T> {
62
+ try {
63
+ return await super.findOneOrFail<T>(options)
64
+ } catch (error) {
65
+ console.log(error);
66
+ throw new BadRequest(`${replaceName ? replaceName : this.name} không tồn tại.`)
67
+ }
68
+ }
69
+
70
+ } // END FILE
@@ -0,0 +1,21 @@
1
+ import { Service, AfterRoutesInit } from "@tsed/common";
2
+ import { TypeORMService } from "@tsed/typeorm";
3
+ import { Connection, EntityManager } from "typeorm";
4
+ import CoreEntity from "../entity/CoreEntity";
5
+
6
+ @Service()
7
+ export class CoreService implements AfterRoutesInit {
8
+ public connection: Connection;
9
+ public manager: EntityManager
10
+
11
+ constructor(
12
+ public typeORMService: TypeORMService,
13
+ ) { }
14
+
15
+ $afterRoutesInit() {
16
+ this.connection = <any>this.typeORMService.get()
17
+ this.manager = this.connection.manager
18
+ CoreEntity.connection = this.connection
19
+ }
20
+
21
+ } // END FILE
@@ -0,0 +1,68 @@
1
+ // IMPORT LIBRARY
2
+ import { Entity, PrimaryGeneratedColumn, Column, OneToMany, ManyToOne } from "typeorm";
3
+ import { Property } from "@tsed/common";
4
+
5
+ // IMPORT CUSTOM
6
+ import { addPrefix } from "../util/helper"
7
+ import CoreEntity from '../core/entity/CoreEntity';
8
+
9
+ export enum GenderType {
10
+ Male = 'MALE',
11
+ Female = 'FEMALE'
12
+ }
13
+
14
+ @Entity(addPrefix("customer"))
15
+ export class Customer extends CoreEntity {
16
+ constructor() {
17
+ super()
18
+ }
19
+
20
+ // PROPERTIES
21
+
22
+ @Column()
23
+ @Property()
24
+ phone: string
25
+
26
+ @Column()
27
+ @Property()
28
+ name: string;
29
+
30
+ @Column({ default: '' })
31
+ @Property()
32
+ address: string
33
+
34
+ @Column()
35
+ @Property()
36
+ password: string
37
+
38
+ @Column()
39
+ @Property()
40
+ email: string
41
+
42
+ @Column({ default: GenderType.Male })
43
+ @Property()
44
+ gender: string
45
+
46
+ @Column({ nullable: true })
47
+ @Property()
48
+ avatar: string;
49
+
50
+ @Column({ nullable: true })
51
+ @Property()
52
+ expoToken: string;
53
+
54
+ @Column({ default: false })
55
+ @Property()
56
+ isBlock: boolean
57
+
58
+ @Column({ default: false, select: false })
59
+ @Property()
60
+ isDeleted: boolean
61
+
62
+ // RELATIONS
63
+
64
+
65
+ // METHODS
66
+
67
+
68
+ } // END FILE
@@ -0,0 +1,29 @@
1
+ import { Entity, PrimaryGeneratedColumn, Column, OneToMany, ManyToOne, ManyToMany, JoinTable } from "typeorm";
2
+ import { Property } from "@tsed/common";
3
+
4
+ import { addPrefix } from "../util/helper"
5
+ import CoreEntity from '../core/entity/CoreEntity';
6
+ import { Role } from "./Role";
7
+
8
+ @Entity(addPrefix("permission"))
9
+ export class Permission extends CoreEntity {
10
+ constructor() {
11
+ super()
12
+ }
13
+
14
+ // PROPERTIES
15
+
16
+ @Column()
17
+ @Property()
18
+ path: string;
19
+
20
+
21
+ // RELATIONS
22
+
23
+ @ManyToMany(type => Role, role => role.permissions)
24
+ @JoinTable()
25
+ roles: Role[]
26
+
27
+ // METHODS
28
+
29
+ } // END FILE
@@ -0,0 +1,36 @@
1
+ import { Entity, PrimaryGeneratedColumn, Column, OneToMany, ManyToOne, ManyToMany } from "typeorm";
2
+ import { Property } from "@tsed/common";
3
+
4
+ import { addPrefix } from "../util/helper"
5
+ import CoreEntity from '../core/entity/CoreEntity';
6
+ import { Permission } from "./Permission";
7
+ import { Staff } from "./Staff";
8
+
9
+ @Entity(addPrefix("role"))
10
+ export class Role extends CoreEntity {
11
+ constructor() {
12
+ super()
13
+ }
14
+
15
+ // PROPERTIES
16
+
17
+ @Column()
18
+ @Property()
19
+ name: string;
20
+
21
+ @Column()
22
+ @Property()
23
+ description: string
24
+
25
+
26
+ // RELATIONS
27
+
28
+ @OneToMany(() => Staff, admin => admin.role)
29
+ staff: Staff[]
30
+
31
+ @ManyToMany(() => Permission, permission => permission.roles)
32
+ permissions: Permission[]
33
+
34
+ // METHODS
35
+
36
+ } // END FILE
@@ -0,0 +1,54 @@
1
+ // IMPORT LIBRARY
2
+ import { Entity, PrimaryGeneratedColumn, Column, OneToMany, ManyToOne, ManyToMany } from "typeorm";
3
+ import { Property } from "@tsed/common";
4
+
5
+ // IMPORT CUSTOM
6
+ import { addPrefix } from "../util/helper"
7
+ import CoreEntity from '../core/entity/CoreEntity';
8
+ import { Role } from "./Role";
9
+
10
+ @Entity(addPrefix("staff"))
11
+ export class Staff extends CoreEntity {
12
+ constructor() {
13
+ super()
14
+ }
15
+
16
+ // PROPERTIES
17
+
18
+ @Column()
19
+ @Property()
20
+ username: string;
21
+
22
+ @Column({ select: false })
23
+ password: string;
24
+
25
+ @Column({ default: "" })
26
+ @Property()
27
+ name: string;
28
+
29
+ @Column({ default: "" })
30
+ @Property()
31
+ avatar: string;
32
+
33
+ @Column({ default: "" })
34
+ @Property()
35
+ phone: string
36
+
37
+ @Column({ default: "" })
38
+ @Property()
39
+ email: string
40
+
41
+ @Column({ default: false })
42
+ @Property()
43
+ isBlock: boolean
44
+
45
+ // RELATIONS
46
+
47
+ @ManyToOne(type => Role, role => role.staff)
48
+ role: Role;
49
+
50
+
51
+ // METHODS
52
+
53
+
54
+ } // END FILE