@hedhog/admin 0.11.1 → 0.12.2

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 (54) hide show
  1. package/{hedhog.yaml → bkp.hedhog.yaml} +1012 -814
  2. package/dist/auth/consts/body.js +23 -23
  3. package/package.json +43 -43
  4. package/src/admin.module.ts +37 -37
  5. package/src/auth/auth.controller.ts +72 -72
  6. package/src/auth/auth.module.ts +39 -39
  7. package/src/auth/auth.service.spec.ts +196 -196
  8. package/src/auth/auth.service.ts +234 -234
  9. package/src/auth/consts/body.ts +26 -26
  10. package/src/auth/consts/subject.ts +1 -1
  11. package/src/auth/dto/forget.dto.ts +6 -6
  12. package/src/auth/dto/login.dto.ts +15 -15
  13. package/src/auth/dto/otp.dto.ts +11 -11
  14. package/src/auth/dto/reset.dto.ts +14 -14
  15. package/src/auth/enums/multifactor-type.enum.ts +4 -4
  16. package/src/auth/guards/auth.guard.ts +50 -50
  17. package/src/auth/types/user.type.ts +8 -8
  18. package/src/dto/delete.dto.ts +8 -8
  19. package/src/dto/update-ids.dto.ts +9 -9
  20. package/src/index.ts +20 -20
  21. package/src/menu/dto/create.dto.ts +25 -25
  22. package/src/menu/dto/order.dto.ts +8 -8
  23. package/src/menu/dto/update.dto.ts +19 -19
  24. package/src/menu/menu.controller.ts +105 -105
  25. package/src/menu/menu.module.ts +18 -18
  26. package/src/menu/menu.service.spec.ts +247 -247
  27. package/src/menu/menu.service.ts +263 -263
  28. package/src/role/dto/create.dto.ts +7 -7
  29. package/src/role/dto/update.dto.ts +4 -4
  30. package/src/role/guards/role.guard.ts +123 -123
  31. package/src/role/role.controller.ts +126 -126
  32. package/src/role/role.module.ts +28 -28
  33. package/src/role/role.service.spec.ts +417 -417
  34. package/src/role/role.service.ts +289 -289
  35. package/src/route/dto/create.dto.ts +13 -13
  36. package/src/route/dto/update.dto.ts +15 -15
  37. package/src/route/route.controller.ts +91 -91
  38. package/src/route/route.module.ts +18 -18
  39. package/src/route/route.service.spec.ts +300 -300
  40. package/src/route/route.service.ts +164 -164
  41. package/src/screen/dto/create.dto.ts +11 -11
  42. package/src/screen/dto/update.dto.ts +19 -19
  43. package/src/screen/screen.controller.ts +93 -93
  44. package/src/screen/screen.module.ts +18 -18
  45. package/src/screen/screen.service.spec.ts +298 -298
  46. package/src/screen/screen.service.ts +179 -179
  47. package/src/types/http-method.ts +8 -8
  48. package/src/user/constants/user.constants.ts +1 -1
  49. package/src/user/dto/create.dto.ts +24 -24
  50. package/src/user/dto/update.dto.ts +41 -41
  51. package/src/user/user.controller.ts +75 -75
  52. package/src/user/user.module.ts +18 -18
  53. package/src/user/user.service.spec.ts +294 -294
  54. package/src/user/user.service.ts +129 -129
@@ -1,263 +1,263 @@
1
- import { PaginationDTO, PaginationService } from '@hedhog/pagination';
2
- import { PrismaService } from '@hedhog/prisma';
3
- import { itemTranslations } from '@hedhog/core';
4
- import {
5
- BadRequestException,
6
- Inject,
7
- Injectable,
8
- forwardRef,
9
- } from '@nestjs/common';
10
- import { DeleteDTO } from '../dto/delete.dto';
11
- import { UpdateIdsDTO } from '../dto/update-ids.dto';
12
- import { CreateDTO } from './dto/create.dto';
13
- import { OrderDTO } from './dto/order.dto';
14
- import { UpdateDTO } from './dto/update.dto';
15
-
16
- @Injectable()
17
- export class MenuService {
18
- constructor(
19
- @Inject(forwardRef(() => PrismaService))
20
- private readonly prismaService: PrismaService,
21
- @Inject(forwardRef(() => PaginationService))
22
- private readonly paginationService: PaginationService,
23
- ) {}
24
-
25
- async updateScreens(menuId: number, data: UpdateIdsDTO) {
26
- await this.prismaService.menu_screen.deleteMany({
27
- where: {
28
- menu_id: menuId,
29
- },
30
- });
31
-
32
- return this.prismaService.menu_screen.createMany({
33
- data: data.ids.map((screenId) => ({
34
- menu_id: menuId,
35
- screen_id: screenId,
36
- })),
37
- skipDuplicates: true,
38
- });
39
- }
40
- async updateRoles(menuId: number, data: UpdateIdsDTO) {
41
- await this.prismaService.role_menu.deleteMany({
42
- where: {
43
- menu_id: menuId,
44
- },
45
- });
46
-
47
- return this.prismaService.role_menu.createMany({
48
- data: data.ids.map((roleId) => ({
49
- menu_id: menuId,
50
- role_id: roleId,
51
- })),
52
- skipDuplicates: true,
53
- });
54
- }
55
- async listScreens(
56
- locale: string,
57
- menuId: number,
58
- paginationParams: PaginationDTO,
59
- ) {
60
- return this.paginationService.paginate(
61
- this.prismaService.screen,
62
- paginationParams,
63
- {
64
- include: {
65
- screen_locale: {
66
- where: {
67
- locale: {
68
- code: locale,
69
- },
70
- },
71
- select: {
72
- name: true,
73
- },
74
- },
75
- menu_screen: {
76
- where: {
77
- menu_id: menuId,
78
- },
79
- select: {
80
- screen_id: true,
81
- menu_id: true,
82
- },
83
- },
84
- },
85
- },
86
- 'screen_locale',
87
- );
88
- }
89
- async listRoles(
90
- locale: string,
91
- menuId: number,
92
- paginationParams: PaginationDTO,
93
- ) {
94
- return this.paginationService.paginate(
95
- this.prismaService.role,
96
- paginationParams,
97
- {
98
- include: {
99
- role_locale: {
100
- where: {
101
- locale: {
102
- code: locale,
103
- },
104
- },
105
- select: {
106
- name: true,
107
- description: true,
108
- },
109
- },
110
- role_menu: {
111
- where: {
112
- menu_id: menuId,
113
- },
114
- select: {
115
- role_id: true,
116
- menu_id: true,
117
- },
118
- },
119
- },
120
- },
121
- 'role_locale',
122
- );
123
- }
124
-
125
- async getMenus(locale: string, userId: number, menuId = 0) {
126
- if (menuId === 0) {
127
- menuId = null;
128
- }
129
-
130
- let menu = (await this.prismaService.menu.findMany({
131
- where: {
132
- menu_id: menuId,
133
- role_menu: {
134
- some: {
135
- role: {
136
- role_user: {
137
- some: {
138
- user_id: userId,
139
- },
140
- },
141
- },
142
- },
143
- },
144
- },
145
- orderBy: {
146
- order: 'asc',
147
- },
148
- include: {
149
- menu_locale: {
150
- where: {
151
- locale: {
152
- code: locale,
153
- },
154
- },
155
- select: {
156
- name: true,
157
- },
158
- },
159
- },
160
- })) as unknown[] as any[];
161
-
162
- menu = menu.map((m) => itemTranslations('menu_locale', m));
163
-
164
- for (let i = 0; i < menu.length; i++) {
165
- menu[i].menu = await this.getMenus(locale, userId, menu[i].id);
166
- }
167
-
168
- return menu;
169
- }
170
-
171
- async getSystemMenu(locale: string, userId: number) {
172
- return this.getMenus(locale, userId);
173
- }
174
-
175
- async list(locale: string, paginationParams: PaginationDTO) {
176
- const fields = ['url', 'icon'];
177
- const OR = this.prismaService.createInsensitiveSearch(
178
- fields,
179
- paginationParams,
180
- );
181
-
182
- return this.paginationService.paginate(
183
- this.prismaService.menu,
184
- paginationParams,
185
- {
186
- where: {
187
- OR,
188
- },
189
- include: {
190
- menu_locale: {
191
- where: {
192
- locale: {
193
- code: locale,
194
- },
195
- },
196
- select: {
197
- name: true,
198
- },
199
- },
200
- },
201
- },
202
- 'menu_locale',
203
- );
204
- }
205
-
206
- async get(menuId: number) {
207
- return this.prismaService.menu.findUnique({
208
- where: { id: menuId },
209
- });
210
- }
211
-
212
- async create({ slug, url, icon, order, menuId }: CreateDTO) {
213
- return this.prismaService.menu.create({
214
- data: { slug, url, icon, order, menu_id: menuId },
215
- });
216
- }
217
-
218
- async update({ id, data }: { id: number; data: UpdateDTO }) {
219
- return this.prismaService.menu.update({
220
- where: { id },
221
- data,
222
- });
223
- }
224
-
225
- async delete({ ids }: DeleteDTO) {
226
- if (ids == undefined || ids == null) {
227
- throw new BadRequestException(
228
- `You must select at least one menu to delete.`,
229
- );
230
- }
231
-
232
- return this.prismaService.menu.deleteMany({
233
- where: {
234
- id: {
235
- in: ids,
236
- },
237
- },
238
- });
239
- }
240
-
241
- async updateOrder({ ids }: OrderDTO): Promise<void> {
242
- const count = await this.prismaService.menu.count({
243
- where: {
244
- id: {
245
- in: ids,
246
- },
247
- },
248
- });
249
-
250
- if (count !== ids.length) {
251
- throw new BadRequestException('IDs inválidos.');
252
- }
253
-
254
- await Promise.all(
255
- ids.map((id, index) =>
256
- this.prismaService.menu.update({
257
- where: { id },
258
- data: { order: index + 1 },
259
- }),
260
- ),
261
- );
262
- }
263
- }
1
+ import { PaginationDTO, PaginationService } from '@hedhog/pagination';
2
+ import { PrismaService } from '@hedhog/prisma';
3
+ import { itemTranslations } from '@hedhog/core';
4
+ import {
5
+ BadRequestException,
6
+ Inject,
7
+ Injectable,
8
+ forwardRef,
9
+ } from '@nestjs/common';
10
+ import { DeleteDTO } from '../dto/delete.dto';
11
+ import { UpdateIdsDTO } from '../dto/update-ids.dto';
12
+ import { CreateDTO } from './dto/create.dto';
13
+ import { OrderDTO } from './dto/order.dto';
14
+ import { UpdateDTO } from './dto/update.dto';
15
+
16
+ @Injectable()
17
+ export class MenuService {
18
+ constructor(
19
+ @Inject(forwardRef(() => PrismaService))
20
+ private readonly prismaService: PrismaService,
21
+ @Inject(forwardRef(() => PaginationService))
22
+ private readonly paginationService: PaginationService,
23
+ ) {}
24
+
25
+ async updateScreens(menuId: number, data: UpdateIdsDTO) {
26
+ await this.prismaService.menu_screen.deleteMany({
27
+ where: {
28
+ menu_id: menuId,
29
+ },
30
+ });
31
+
32
+ return this.prismaService.menu_screen.createMany({
33
+ data: data.ids.map((screenId) => ({
34
+ menu_id: menuId,
35
+ screen_id: screenId,
36
+ })),
37
+ skipDuplicates: true,
38
+ });
39
+ }
40
+ async updateRoles(menuId: number, data: UpdateIdsDTO) {
41
+ await this.prismaService.role_menu.deleteMany({
42
+ where: {
43
+ menu_id: menuId,
44
+ },
45
+ });
46
+
47
+ return this.prismaService.role_menu.createMany({
48
+ data: data.ids.map((roleId) => ({
49
+ menu_id: menuId,
50
+ role_id: roleId,
51
+ })),
52
+ skipDuplicates: true,
53
+ });
54
+ }
55
+ async listScreens(
56
+ locale: string,
57
+ menuId: number,
58
+ paginationParams: PaginationDTO,
59
+ ) {
60
+ return this.paginationService.paginate(
61
+ this.prismaService.screen,
62
+ paginationParams,
63
+ {
64
+ include: {
65
+ screen_locale: {
66
+ where: {
67
+ locale: {
68
+ code: locale,
69
+ },
70
+ },
71
+ select: {
72
+ name: true,
73
+ },
74
+ },
75
+ menu_screen: {
76
+ where: {
77
+ menu_id: menuId,
78
+ },
79
+ select: {
80
+ screen_id: true,
81
+ menu_id: true,
82
+ },
83
+ },
84
+ },
85
+ },
86
+ 'screen_locale',
87
+ );
88
+ }
89
+ async listRoles(
90
+ locale: string,
91
+ menuId: number,
92
+ paginationParams: PaginationDTO,
93
+ ) {
94
+ return this.paginationService.paginate(
95
+ this.prismaService.role,
96
+ paginationParams,
97
+ {
98
+ include: {
99
+ role_locale: {
100
+ where: {
101
+ locale: {
102
+ code: locale,
103
+ },
104
+ },
105
+ select: {
106
+ name: true,
107
+ description: true,
108
+ },
109
+ },
110
+ role_menu: {
111
+ where: {
112
+ menu_id: menuId,
113
+ },
114
+ select: {
115
+ role_id: true,
116
+ menu_id: true,
117
+ },
118
+ },
119
+ },
120
+ },
121
+ 'role_locale',
122
+ );
123
+ }
124
+
125
+ async getMenus(locale: string, userId: number, menuId = 0) {
126
+ if (menuId === 0) {
127
+ menuId = null;
128
+ }
129
+
130
+ let menu = (await this.prismaService.menu.findMany({
131
+ where: {
132
+ menu_id: menuId,
133
+ role_menu: {
134
+ some: {
135
+ role: {
136
+ role_user: {
137
+ some: {
138
+ user_id: userId,
139
+ },
140
+ },
141
+ },
142
+ },
143
+ },
144
+ },
145
+ orderBy: {
146
+ order: 'asc',
147
+ },
148
+ include: {
149
+ menu_locale: {
150
+ where: {
151
+ locale: {
152
+ code: locale,
153
+ },
154
+ },
155
+ select: {
156
+ name: true,
157
+ },
158
+ },
159
+ },
160
+ })) as unknown[] as any[];
161
+
162
+ menu = menu.map((m) => itemTranslations('menu_locale', m));
163
+
164
+ for (let i = 0; i < menu.length; i++) {
165
+ menu[i].menu = await this.getMenus(locale, userId, menu[i].id);
166
+ }
167
+
168
+ return menu;
169
+ }
170
+
171
+ async getSystemMenu(locale: string, userId: number) {
172
+ return this.getMenus(locale, userId);
173
+ }
174
+
175
+ async list(locale: string, paginationParams: PaginationDTO) {
176
+ const fields = ['url', 'icon'];
177
+ const OR = this.prismaService.createInsensitiveSearch(
178
+ fields,
179
+ paginationParams,
180
+ );
181
+
182
+ return this.paginationService.paginate(
183
+ this.prismaService.menu,
184
+ paginationParams,
185
+ {
186
+ where: {
187
+ OR,
188
+ },
189
+ include: {
190
+ menu_locale: {
191
+ where: {
192
+ locale: {
193
+ code: locale,
194
+ },
195
+ },
196
+ select: {
197
+ name: true,
198
+ },
199
+ },
200
+ },
201
+ },
202
+ 'menu_locale',
203
+ );
204
+ }
205
+
206
+ async get(menuId: number) {
207
+ return this.prismaService.menu.findUnique({
208
+ where: { id: menuId },
209
+ });
210
+ }
211
+
212
+ async create({ slug, url, icon, order, menuId }: CreateDTO) {
213
+ return this.prismaService.menu.create({
214
+ data: { slug, url, icon, order, menu_id: menuId },
215
+ });
216
+ }
217
+
218
+ async update({ id, data }: { id: number; data: UpdateDTO }) {
219
+ return this.prismaService.menu.update({
220
+ where: { id },
221
+ data,
222
+ });
223
+ }
224
+
225
+ async delete({ ids }: DeleteDTO) {
226
+ if (ids == undefined || ids == null) {
227
+ throw new BadRequestException(
228
+ `You must select at least one menu to delete.`,
229
+ );
230
+ }
231
+
232
+ return this.prismaService.menu.deleteMany({
233
+ where: {
234
+ id: {
235
+ in: ids,
236
+ },
237
+ },
238
+ });
239
+ }
240
+
241
+ async updateOrder({ ids }: OrderDTO): Promise<void> {
242
+ const count = await this.prismaService.menu.count({
243
+ where: {
244
+ id: {
245
+ in: ids,
246
+ },
247
+ },
248
+ });
249
+
250
+ if (count !== ids.length) {
251
+ throw new BadRequestException('IDs inválidos.');
252
+ }
253
+
254
+ await Promise.all(
255
+ ids.map((id, index) =>
256
+ this.prismaService.menu.update({
257
+ where: { id },
258
+ data: { order: index + 1 },
259
+ }),
260
+ ),
261
+ );
262
+ }
263
+ }
@@ -1,7 +1,7 @@
1
- import { IsString } from 'class-validator';
2
- import { WithLocaleDTO } from '@hedhog/locale';
3
-
4
- export class CreateDTO extends WithLocaleDTO {
5
- @IsString()
6
- slug: string;
7
- }
1
+ import { IsString } from 'class-validator';
2
+ import { WithLocaleDTO } from '@hedhog/locale';
3
+
4
+ export class CreateDTO extends WithLocaleDTO {
5
+ @IsString()
6
+ slug: string;
7
+ }
@@ -1,4 +1,4 @@
1
- import { PartialType } from '@nestjs/mapped-types';
2
- import { CreateDTO } from './create.dto';
3
-
4
- export class UpdateDTO extends PartialType(CreateDTO) {}
1
+ import { PartialType } from '@nestjs/mapped-types';
2
+ import { CreateDTO } from './create.dto';
3
+
4
+ export class UpdateDTO extends PartialType(CreateDTO) {}