@hedhog/admin 0.0.104 → 0.0.105

Sign up to get free protection for your applications and to get access to all the features.
Files changed (186) hide show
  1. package/README.md +141 -141
  2. package/dist/admin.module.js +7 -7
  3. package/dist/admin.module.js.map +1 -1
  4. package/dist/auth/auth.service.d.ts +1 -1
  5. package/dist/auth/auth.service.d.ts.map +1 -1
  6. package/dist/auth/auth.service.js +8 -8
  7. package/dist/auth/auth.service.js.map +1 -1
  8. package/dist/auth/auth.service.spec.js +16 -16
  9. package/dist/auth/auth.service.spec.js.map +1 -1
  10. package/dist/dto/with-locale.dto.d.ts +4 -0
  11. package/dist/dto/with-locale.dto.d.ts.map +1 -0
  12. package/dist/dto/{with-locales.dto.js → with-locale.dto.js} +5 -5
  13. package/dist/dto/with-locale.dto.js.map +1 -0
  14. package/dist/index.d.ts +2 -2
  15. package/dist/index.d.ts.map +1 -1
  16. package/dist/index.js +2 -2
  17. package/dist/index.js.map +1 -1
  18. package/dist/locale/locale.controller.js +1 -1
  19. package/dist/locale/locale.controller.js.map +1 -1
  20. package/dist/locale/locale.middleware.d.ts +1 -1
  21. package/dist/locale/locale.middleware.d.ts.map +1 -1
  22. package/dist/locale/locale.middleware.js +2 -2
  23. package/dist/locale/locale.middleware.js.map +1 -1
  24. package/dist/locale/locale.service.d.ts +6 -6
  25. package/dist/locale/locale.service.d.ts.map +1 -1
  26. package/dist/locale/locale.service.js +40 -40
  27. package/dist/locale/locale.service.js.map +1 -1
  28. package/dist/locale/locale.service.spec.js +14 -14
  29. package/dist/locale/locale.service.spec.js.map +1 -1
  30. package/dist/menu/menu.controller.js +3 -3
  31. package/dist/menu/menu.controller.js.map +1 -1
  32. package/dist/menu/menu.service.d.ts +3 -3
  33. package/dist/menu/menu.service.d.ts.map +1 -1
  34. package/dist/menu/menu.service.js +35 -35
  35. package/dist/menu/menu.service.js.map +1 -1
  36. package/dist/menu/menu.service.spec.js +33 -37
  37. package/dist/menu/menu.service.spec.js.map +1 -1
  38. package/dist/role/dto/create.dto.d.ts +2 -2
  39. package/dist/role/dto/create.dto.d.ts.map +1 -1
  40. package/dist/role/dto/create.dto.js +2 -2
  41. package/dist/role/dto/create.dto.js.map +1 -1
  42. package/dist/role/guards/role.guard.js +4 -4
  43. package/dist/role/guards/role.guard.js.map +1 -1
  44. package/dist/role/role.controller.js +7 -7
  45. package/dist/role/role.controller.js.map +1 -1
  46. package/dist/role/role.service.d.ts +2 -2
  47. package/dist/role/role.service.d.ts.map +1 -1
  48. package/dist/role/role.service.js +35 -35
  49. package/dist/role/role.service.js.map +1 -1
  50. package/dist/role/role.service.spec.d.ts +0 -1
  51. package/dist/role/role.service.spec.js +400 -340
  52. package/dist/role/role.service.spec.js.map +1 -1
  53. package/dist/route/route.controller.js +3 -3
  54. package/dist/route/route.controller.js.map +1 -1
  55. package/dist/route/route.service.d.ts +2 -2
  56. package/dist/route/route.service.d.ts.map +1 -1
  57. package/dist/route/route.service.js +18 -18
  58. package/dist/route/route.service.js.map +1 -1
  59. package/dist/route/route.service.spec.js +27 -27
  60. package/dist/route/route.service.spec.js.map +1 -1
  61. package/dist/screen/screen.controller.js +4 -4
  62. package/dist/screen/screen.controller.js.map +1 -1
  63. package/dist/screen/screen.service.d.ts +2 -2
  64. package/dist/screen/screen.service.d.ts.map +1 -1
  65. package/dist/screen/screen.service.js +16 -16
  66. package/dist/screen/screen.service.js.map +1 -1
  67. package/dist/screen/screen.service.spec.js +16 -16
  68. package/dist/screen/screen.service.spec.js.map +1 -1
  69. package/dist/setting/dto/setting.dto.d.ts +9 -0
  70. package/dist/setting/dto/setting.dto.d.ts.map +1 -0
  71. package/dist/setting/dto/{settings.dto.js → setting.dto.js} +6 -6
  72. package/dist/setting/dto/setting.dto.js.map +1 -0
  73. package/dist/setting/{settings.controller.d.ts → setting.controller.d.ts} +6 -6
  74. package/dist/setting/setting.controller.d.ts.map +1 -0
  75. package/dist/setting/{settings.controller.js → setting.controller.js} +20 -20
  76. package/dist/setting/setting.controller.js.map +1 -0
  77. package/dist/setting/setting.module.d.ts +3 -0
  78. package/dist/setting/setting.module.d.ts.map +1 -0
  79. package/dist/setting/{settings.module.js → setting.module.js} +6 -6
  80. package/dist/setting/setting.module.js.map +1 -0
  81. package/dist/setting/{settings.service.d.ts → setting.service.d.ts} +4 -4
  82. package/dist/setting/setting.service.d.ts.map +1 -0
  83. package/dist/setting/{settings.service.js → setting.service.js} +42 -42
  84. package/dist/setting/setting.service.js.map +1 -0
  85. package/dist/setting/setting.service.spec.d.ts +2 -0
  86. package/dist/setting/setting.service.spec.d.ts.map +1 -0
  87. package/dist/setting/{settings.service.spec.js → setting.service.spec.js} +17 -17
  88. package/dist/setting/setting.service.spec.js.map +1 -0
  89. package/dist/user/user.controller.js +3 -3
  90. package/dist/user/user.controller.js.map +1 -1
  91. package/dist/user/user.service.d.ts +2 -2
  92. package/dist/user/user.service.d.ts.map +1 -1
  93. package/dist/user/user.service.js +9 -9
  94. package/dist/user/user.service.js.map +1 -1
  95. package/dist/user/user.service.spec.js +24 -24
  96. package/dist/user/user.service.spec.js.map +1 -1
  97. package/package.json +2 -2
  98. package/src/admin.module.ts +36 -0
  99. package/src/auth/auth.controller.ts +48 -0
  100. package/src/auth/auth.module.ts +39 -0
  101. package/src/auth/auth.service.spec.ts +196 -0
  102. package/src/auth/auth.service.ts +175 -0
  103. package/src/auth/decorators/public.decorator.ts +4 -0
  104. package/src/auth/decorators/user.decorator.ts +17 -0
  105. package/src/auth/dto/forget.dto.ts +6 -0
  106. package/src/auth/dto/login.dto.ts +15 -0
  107. package/src/auth/dto/otp.dto.ts +11 -0
  108. package/src/auth/enums/multifactor-type.enum.ts +4 -0
  109. package/src/auth/guards/auth.guard.ts +50 -0
  110. package/src/auth/types/user.type.ts +8 -0
  111. package/src/dto/delete.dto.ts +8 -0
  112. package/src/dto/update-ids.dto.ts +9 -0
  113. package/src/dto/with-locale.dto.ts +8 -0
  114. package/src/index.ts +34 -0
  115. package/src/locale/dto/create.dto.ts +12 -0
  116. package/src/locale/dto/delete.dto.ts +8 -0
  117. package/src/locale/dto/set-enabled.dto.ts +9 -0
  118. package/src/locale/dto/update.dto.ts +15 -0
  119. package/src/locale/index.ts +4 -0
  120. package/src/locale/locale.controller.ts +79 -0
  121. package/src/locale/locale.decorator.ts +8 -0
  122. package/src/locale/locale.middleware.ts +34 -0
  123. package/src/locale/locale.module.ts +23 -0
  124. package/src/locale/locale.service.spec.ts +193 -0
  125. package/src/locale/locale.service.ts +366 -0
  126. package/src/menu/dto/create.dto.ts +25 -0
  127. package/src/menu/dto/order.dto.ts +8 -0
  128. package/src/menu/dto/update.dto.ts +19 -0
  129. package/src/menu/menu.controller.ts +106 -0
  130. package/src/menu/menu.module.ts +18 -0
  131. package/src/menu/menu.service.spec.ts +247 -0
  132. package/src/menu/menu.service.ts +263 -0
  133. package/src/role/decorators/role.decorator.ts +4 -0
  134. package/src/role/dto/create.dto.ts +7 -0
  135. package/src/role/dto/update.dto.ts +4 -0
  136. package/src/role/guards/role.guard.ts +122 -0
  137. package/src/role/role.controller.ts +126 -0
  138. package/src/role/role.module.ts +28 -0
  139. package/src/role/role.service.spec.ts +417 -0
  140. package/src/role/role.service.ts +302 -0
  141. package/src/route/dto/create.dto.ts +13 -0
  142. package/src/route/dto/update.dto.ts +15 -0
  143. package/src/route/route.controller.ts +91 -0
  144. package/src/route/route.module.ts +18 -0
  145. package/src/route/route.service.spec.ts +299 -0
  146. package/src/route/route.service.ts +164 -0
  147. package/src/screen/dto/create.dto.ts +19 -0
  148. package/src/screen/dto/update.dto.ts +19 -0
  149. package/src/screen/screen.controller.ts +93 -0
  150. package/src/screen/screen.module.ts +18 -0
  151. package/src/screen/screen.service.spec.ts +298 -0
  152. package/src/screen/screen.service.ts +181 -0
  153. package/src/setting/dto/create.dto.ts +1 -0
  154. package/src/setting/dto/setting-user.dto.ts +6 -0
  155. package/src/setting/dto/setting.dto.ts +17 -0
  156. package/src/setting/dto/update.dto.ts +3 -0
  157. package/src/setting/setting.controller.ts +100 -0
  158. package/src/setting/setting.module.ts +18 -0
  159. package/src/setting/setting.service.spec.ts +183 -0
  160. package/src/setting/setting.service.ts +346 -0
  161. package/src/types/http-method.ts +8 -0
  162. package/src/user/constants/user.constants.ts +1 -0
  163. package/src/user/dto/create.dto.ts +24 -0
  164. package/src/user/dto/update.dto.ts +41 -0
  165. package/src/user/user.controller.ts +75 -0
  166. package/src/user/user.module.ts +18 -0
  167. package/src/user/user.service.spec.ts +294 -0
  168. package/src/user/user.service.ts +129 -0
  169. package/tsconfig.lib.json +9 -0
  170. package/tsconfig.production.json +20 -0
  171. package/dist/dto/with-locales.dto.d.ts +0 -4
  172. package/dist/dto/with-locales.dto.d.ts.map +0 -1
  173. package/dist/dto/with-locales.dto.js.map +0 -1
  174. package/dist/setting/dto/settings.dto.d.ts +0 -9
  175. package/dist/setting/dto/settings.dto.d.ts.map +0 -1
  176. package/dist/setting/dto/settings.dto.js.map +0 -1
  177. package/dist/setting/settings.controller.d.ts.map +0 -1
  178. package/dist/setting/settings.controller.js.map +0 -1
  179. package/dist/setting/settings.module.d.ts +0 -3
  180. package/dist/setting/settings.module.d.ts.map +0 -1
  181. package/dist/setting/settings.module.js.map +0 -1
  182. package/dist/setting/settings.service.d.ts.map +0 -1
  183. package/dist/setting/settings.service.js.map +0 -1
  184. package/dist/setting/settings.service.spec.d.ts +0 -2
  185. package/dist/setting/settings.service.spec.d.ts.map +0 -1
  186. package/dist/setting/settings.service.spec.js.map +0 -1
@@ -0,0 +1,299 @@
1
+ import {
2
+ PageOrderDirection,
3
+ PaginationDTO,
4
+ PaginationService,
5
+ } from '@hedhog/pagination';
6
+ import { PrismaService } from '@hedhog/prisma';
7
+ import { Test, TestingModule } from '@nestjs/testing';
8
+ import { DeleteDTO } from '../dto/delete.dto';
9
+ import { UpdateIdsDTO } from '../dto/update-ids.dto';
10
+ import { CreateDTO } from './dto/create.dto';
11
+ import { UpdateDTO } from './dto/update.dto';
12
+ import { RouteService } from './route.service';
13
+
14
+ describe('RouteService', () => {
15
+ let service: RouteService;
16
+ let prismaService: PrismaService;
17
+ let paginationService: PaginationService;
18
+
19
+ const mockPrismaService = {
20
+ route: {
21
+ findUnique: jest.fn(),
22
+ create: jest.fn(),
23
+ update: jest.fn(),
24
+ deleteMany: jest.fn(),
25
+ },
26
+ role_route: {
27
+ deleteMany: jest.fn(),
28
+ createMany: jest.fn(),
29
+ },
30
+ route_screens: {
31
+ deleteMany: jest.fn(),
32
+ createMany: jest.fn(),
33
+ },
34
+ createInsensitiveSearch: jest.fn(),
35
+ };
36
+
37
+ const mockPaginationService = {
38
+ paginate: jest.fn(),
39
+ };
40
+
41
+ beforeEach(async () => {
42
+ const module: TestingModule = await Test.createTestingModule({
43
+ providers: [
44
+ RouteService,
45
+ { provide: PrismaService, useValue: mockPrismaService },
46
+ { provide: PaginationService, useValue: mockPaginationService },
47
+ ],
48
+ }).compile();
49
+
50
+ service = module.get<RouteService>(RouteService);
51
+ prismaService = module.get<PrismaService>(PrismaService);
52
+ paginationService = module.get<PaginationService>(PaginationService);
53
+ });
54
+
55
+ it('should be defined', () => {
56
+ expect(service).toBeDefined();
57
+ });
58
+
59
+ describe('getRoutes', () => {
60
+ it('should paginate route', async () => {
61
+ const paginationParams: PaginationDTO = {
62
+ page: 1,
63
+ pageSize: 10,
64
+ search: '',
65
+ sortField: '',
66
+ sortOrder: PageOrderDirection.Asc,
67
+ fields: '',
68
+ };
69
+ const result = { data: [], total: 0 };
70
+
71
+ mockPrismaService.createInsensitiveSearch.mockReturnValue([]);
72
+ mockPaginationService.paginate.mockResolvedValue(result);
73
+
74
+ const res = await service.getRoutes(paginationParams);
75
+
76
+ expect(mockPrismaService.createInsensitiveSearch).toHaveBeenCalledWith(
77
+ ['url', 'method'],
78
+ paginationParams,
79
+ );
80
+ expect(mockPaginationService.paginate).toHaveBeenCalledWith(
81
+ mockPrismaService.route,
82
+ paginationParams,
83
+ { where: { OR: [] } },
84
+ );
85
+ expect(res).toEqual(result);
86
+ });
87
+ });
88
+
89
+ describe('getRouteById', () => {
90
+ it('should return a route by ID', async () => {
91
+ const routeId = 1;
92
+ const route = { id: routeId, url: '/test', method: 'GET' };
93
+
94
+ mockPrismaService.route.findUnique.mockResolvedValue(route);
95
+
96
+ const res = await service.getRouteById(routeId);
97
+
98
+ expect(mockPrismaService.route.findUnique).toHaveBeenCalledWith({
99
+ where: { id: routeId },
100
+ });
101
+ expect(res).toEqual(route);
102
+ });
103
+ });
104
+
105
+ describe('create', () => {
106
+ it('should create a new route', async () => {
107
+ const createDto: CreateDTO = { url: '/test', method: 'GET' };
108
+ const route = { id: 1, ...createDto };
109
+
110
+ mockPrismaService.route.create.mockResolvedValue(route);
111
+
112
+ const res = await service.create(createDto);
113
+
114
+ expect(mockPrismaService.route.create).toHaveBeenCalledWith({
115
+ data: createDto,
116
+ });
117
+ expect(res).toEqual(route);
118
+ });
119
+ });
120
+
121
+ describe('update', () => {
122
+ it('should update a route', async () => {
123
+ const updateDto: UpdateDTO = { url: '/updated', method: 'POST' };
124
+ const routeId = 1;
125
+ const updatedRoute = { id: routeId, ...updateDto };
126
+
127
+ mockPrismaService.route.update.mockResolvedValue(updatedRoute);
128
+
129
+ const res = await service.update({ id: routeId, data: updateDto });
130
+
131
+ expect(mockPrismaService.route.update).toHaveBeenCalledWith({
132
+ where: { id: routeId },
133
+ data: updateDto,
134
+ });
135
+ expect(res).toEqual(updatedRoute);
136
+ });
137
+ });
138
+
139
+ describe('delete', () => {
140
+ it('should delete route by ids', async () => {
141
+ const deleteDto: DeleteDTO = { ids: [1, 2] };
142
+ const result = { count: 2 };
143
+
144
+ mockPrismaService.route.deleteMany.mockResolvedValue(result);
145
+
146
+ const res = await service.delete(deleteDto);
147
+
148
+ expect(mockPrismaService.route.deleteMany).toHaveBeenCalledWith({
149
+ where: { id: { in: deleteDto.ids } },
150
+ });
151
+ expect(res).toEqual(result);
152
+ });
153
+ });
154
+
155
+ describe('updateRoles', () => {
156
+ it('should update role for a route', async () => {
157
+ const routeId = 1;
158
+ const updateIdsDto: UpdateIdsDTO = { ids: [1, 2] };
159
+ const result = { count: 2 };
160
+
161
+ mockPrismaService.role_route.deleteMany.mockResolvedValue({});
162
+ mockPrismaService.role_route.createMany.mockResolvedValue(result);
163
+
164
+ const res = await service.updateRoles(routeId, updateIdsDto);
165
+
166
+ expect(mockPrismaService.role_route.deleteMany).toHaveBeenCalledWith({
167
+ where: { route_id: routeId },
168
+ });
169
+ expect(mockPrismaService.role_route.createMany).toHaveBeenCalledWith({
170
+ data: updateIdsDto.ids.map((roleId) => ({
171
+ role_id: roleId,
172
+ route_id: routeId,
173
+ })),
174
+ skipDuplicates: true,
175
+ });
176
+ expect(res).toEqual(result);
177
+ });
178
+ });
179
+
180
+ describe('updateScreens', () => {
181
+ it('should update screens for a route', async () => {
182
+ const routeId = 1;
183
+ const updateIdsDto: UpdateIdsDTO = { ids: [1, 2] };
184
+ const result = { count: 2 };
185
+
186
+ mockPrismaService.route_screens.deleteMany.mockResolvedValue({});
187
+ mockPrismaService.route_screens.createMany.mockResolvedValue(result);
188
+
189
+ const res = await service.updateScreens(routeId, updateIdsDto);
190
+
191
+ expect(mockPrismaService.route_screens.deleteMany).toHaveBeenCalledWith({
192
+ where: { route_id: routeId },
193
+ });
194
+ expect(mockPrismaService.route_screens.createMany).toHaveBeenCalledWith({
195
+ data: updateIdsDto.ids.map((screenId) => ({
196
+ screen_id: screenId,
197
+ route_id: routeId,
198
+ })),
199
+ skipDuplicates: true,
200
+ });
201
+ expect(res).toEqual(result);
202
+ });
203
+ });
204
+
205
+ describe('listRoles', () => {
206
+ it('should call paginate method with correct parameters', async () => {
207
+ const locale = 'en';
208
+ const routeId = 1;
209
+ const paginationParams: PaginationDTO = {
210
+ page: 1,
211
+ pageSize: 10,
212
+ search: '',
213
+ sortField: '',
214
+ sortOrder: PageOrderDirection.Asc,
215
+ fields: '',
216
+ };
217
+
218
+ paginationService.paginate = jest.fn().mockResolvedValue({
219
+ data: [],
220
+ total: 0,
221
+ });
222
+
223
+ await service.listRoles(locale, routeId, paginationParams);
224
+
225
+ expect(paginationService.paginate).toHaveBeenCalledWith(
226
+ prismaService.role,
227
+ paginationParams,
228
+ {
229
+ include: {
230
+ role_locale: {
231
+ where: {
232
+ locale: { code: locale },
233
+ },
234
+ select: {
235
+ name: true,
236
+ description: true,
237
+ },
238
+ },
239
+ role_route: {
240
+ where: { route_id: routeId },
241
+ select: {
242
+ route_id: true,
243
+ role_id: true,
244
+ },
245
+ },
246
+ },
247
+ },
248
+ 'role_locale',
249
+ );
250
+ });
251
+ });
252
+
253
+ describe('listScreens', () => {
254
+ it('should call paginate method with correct parameters', async () => {
255
+ const locale = 'en';
256
+ const routeId = 1;
257
+ const paginationParams: PaginationDTO = {
258
+ page: 1,
259
+ pageSize: 10,
260
+ search: '',
261
+ sortField: '',
262
+ sortOrder: PageOrderDirection.Asc,
263
+ fields: '',
264
+ };
265
+
266
+ paginationService.paginate = jest.fn().mockResolvedValue({
267
+ data: [],
268
+ total: 0,
269
+ });
270
+
271
+ await service.listScreens(locale, routeId, paginationParams);
272
+
273
+ expect(paginationService.paginate).toHaveBeenCalledWith(
274
+ prismaService.screens,
275
+ paginationParams,
276
+ {
277
+ include: {
278
+ screen_locale: {
279
+ where: {
280
+ locale: { code: locale },
281
+ },
282
+ select: {
283
+ name: true,
284
+ },
285
+ },
286
+ route_screens: {
287
+ where: { route_id: routeId },
288
+ select: {
289
+ route_id: true,
290
+ screen_id: true,
291
+ },
292
+ },
293
+ },
294
+ },
295
+ 'screen_locale',
296
+ );
297
+ });
298
+ });
299
+ });
@@ -0,0 +1,164 @@
1
+ import { PaginationDTO, PaginationService } from '@hedhog/pagination';
2
+ import { PrismaService } from '@hedhog/prisma';
3
+ import { forwardRef, Inject, Injectable } from '@nestjs/common';
4
+ import { DeleteDTO } from '../dto/delete.dto';
5
+ import { UpdateIdsDTO } from '../dto/update-ids.dto';
6
+ import { CreateDTO } from './dto/create.dto';
7
+ import { UpdateDTO } from './dto/update.dto';
8
+
9
+ @Injectable()
10
+ export class RouteService {
11
+ constructor(
12
+ @Inject(forwardRef(() => PrismaService))
13
+ private readonly prismaService: PrismaService,
14
+ @Inject(forwardRef(() => PaginationService))
15
+ private readonly paginationService: PaginationService,
16
+ ) {}
17
+
18
+ async getRoutes(paginationParams: PaginationDTO) {
19
+ const fields = ['url', 'method'];
20
+
21
+ const OR: any[] = this.prismaService.createInsensitiveSearch(
22
+ fields,
23
+ paginationParams,
24
+ );
25
+
26
+ return this.paginationService.paginate(
27
+ this.prismaService.route,
28
+ paginationParams,
29
+ {
30
+ where: {
31
+ OR,
32
+ },
33
+ },
34
+ );
35
+ }
36
+
37
+ async getRouteById(routeId: number) {
38
+ return this.prismaService.route.findUnique({ where: { id: routeId } });
39
+ }
40
+
41
+ async create({ url, method }: CreateDTO) {
42
+ return this.prismaService.route.create({ data: { url, method } });
43
+ }
44
+
45
+ async update({ id, data }: { id: number; data: UpdateDTO }) {
46
+ return this.prismaService.route.update({
47
+ where: { id },
48
+ data,
49
+ });
50
+ }
51
+
52
+ async delete({ ids }: DeleteDTO) {
53
+ return this.prismaService.route.deleteMany({
54
+ where: {
55
+ id: {
56
+ in: ids,
57
+ },
58
+ },
59
+ });
60
+ }
61
+
62
+ async listRoles(
63
+ locale: string,
64
+ routeId: number,
65
+ paginationParams: PaginationDTO,
66
+ ) {
67
+ return this.paginationService.paginate(
68
+ this.prismaService.role,
69
+ paginationParams,
70
+ {
71
+ include: {
72
+ role_locale: {
73
+ where: {
74
+ locale: {
75
+ code: locale,
76
+ },
77
+ },
78
+ select: {
79
+ name: true,
80
+ description: true,
81
+ },
82
+ },
83
+ role_route: {
84
+ where: {
85
+ route_id: routeId,
86
+ },
87
+ select: {
88
+ route_id: true,
89
+ role_id: true,
90
+ },
91
+ },
92
+ },
93
+ },
94
+ 'role_locale',
95
+ );
96
+ }
97
+
98
+ async updateRoles(routeId: number, data: UpdateIdsDTO) {
99
+ await this.prismaService.role_route.deleteMany({
100
+ where: {
101
+ route_id: routeId,
102
+ },
103
+ });
104
+
105
+ return this.prismaService.role_route.createMany({
106
+ data: data.ids.map((roleId) => ({
107
+ role_id: roleId,
108
+ route_id: routeId,
109
+ })),
110
+ skipDuplicates: true,
111
+ });
112
+ }
113
+
114
+ async listScreens(
115
+ locale: string,
116
+ routeId: number,
117
+ paginationParams: PaginationDTO,
118
+ ) {
119
+ return this.paginationService.paginate(
120
+ this.prismaService.screen,
121
+ paginationParams,
122
+ {
123
+ include: {
124
+ screen_locale: {
125
+ where: {
126
+ locale: {
127
+ code: locale,
128
+ },
129
+ },
130
+ select: {
131
+ name: true,
132
+ },
133
+ },
134
+ route_screens: {
135
+ where: {
136
+ route_id: routeId,
137
+ },
138
+ select: {
139
+ route_id: true,
140
+ screen_id: true,
141
+ },
142
+ },
143
+ },
144
+ },
145
+ 'screen_locale',
146
+ );
147
+ }
148
+
149
+ async updateScreens(routeId: number, data: UpdateIdsDTO) {
150
+ await this.prismaService.route_screen.deleteMany({
151
+ where: {
152
+ route_id: routeId,
153
+ },
154
+ });
155
+
156
+ return this.prismaService.route_screen.createMany({
157
+ data: data.ids.map((screenId) => ({
158
+ screen_id: screenId,
159
+ route_id: routeId,
160
+ })),
161
+ skipDuplicates: true,
162
+ });
163
+ }
164
+ }
@@ -0,0 +1,19 @@
1
+ import { IsNotEmpty, IsString } from 'class-validator';
2
+
3
+ export class CreateDTO {
4
+ @IsString({ message: 'O nome deve ser uma string' })
5
+ @IsNotEmpty({ message: 'O nome é obrigatório.' })
6
+ name: string;
7
+
8
+ @IsString({ message: 'O slug deve ser uma string' })
9
+ @IsNotEmpty({ message: 'O slug é obrigatório.' })
10
+ slug: string;
11
+
12
+ @IsString({ message: 'A descrição deve ser uma string' })
13
+ @IsNotEmpty({ message: 'A descrição é obrigatória.' })
14
+ description: string;
15
+
16
+ @IsString({ message: 'O ícone deve ser uma string' })
17
+ @IsNotEmpty({ message: 'A descrição é obrigatória.' })
18
+ icon?: string;
19
+ }
@@ -0,0 +1,19 @@
1
+ import { IsOptional, IsString } from 'class-validator';
2
+
3
+ export class UpdateDTO {
4
+ @IsOptional()
5
+ @IsString({ message: 'O nome deve ser uma string' })
6
+ name?: string;
7
+
8
+ @IsOptional()
9
+ @IsString({ message: 'O slug deve ser uma string' })
10
+ slug?: string;
11
+
12
+ @IsOptional()
13
+ @IsString({ message: 'A descrição deve ser uma string' })
14
+ description?: string;
15
+
16
+ @IsOptional()
17
+ @IsString({ message: 'O ícone deve ser uma string' })
18
+ icon?: string;
19
+ }
@@ -0,0 +1,93 @@
1
+ import { Pagination } from '@hedhog/pagination';
2
+ import {
3
+ Body,
4
+ Controller,
5
+ Delete,
6
+ Get,
7
+ Inject,
8
+ Param,
9
+ ParseIntPipe,
10
+ Patch,
11
+ Post,
12
+ forwardRef,
13
+ } from '@nestjs/common';
14
+ import { DeleteDTO } from '../dto/delete.dto';
15
+ import { UpdateIdsDTO } from '../dto/update-ids.dto';
16
+ import { Locale } from '../locale';
17
+ import { Role } from '../role/decorators/role.decorator';
18
+ import { CreateDTO } from './dto/create.dto';
19
+ import { UpdateDTO } from './dto/update.dto';
20
+ import { ScreenService } from './screen.service';
21
+
22
+ @Role()
23
+ @Controller('screens')
24
+ export class ScreenController {
25
+ constructor(
26
+ @Inject(forwardRef(() => ScreenService))
27
+ private readonly screenService: ScreenService,
28
+ ) {}
29
+
30
+ @Get()
31
+ async getScreens(@Pagination() paginationParams, @Locale() locale) {
32
+ return this.screenService.getScreens(locale, paginationParams);
33
+ }
34
+
35
+ @Get(':screenId/role')
36
+ async listRoles(
37
+ @Param('screenId', ParseIntPipe) screenId: number,
38
+ @Pagination() paginationParams,
39
+ @Locale() locale,
40
+ ) {
41
+ return this.screenService.listRoles(locale, screenId, paginationParams);
42
+ }
43
+
44
+ @Get(':screenId/route')
45
+ async listRoutes(
46
+ @Param('screenId', ParseIntPipe) screenId: number,
47
+ @Pagination() paginationParams,
48
+ ) {
49
+ return this.screenService.listRoutes(screenId, paginationParams);
50
+ }
51
+
52
+ @Patch(':screenId/role')
53
+ async updateRoles(
54
+ @Param('screenId', ParseIntPipe) screenId: number,
55
+ @Body() data: UpdateIdsDTO,
56
+ ) {
57
+ return this.screenService.updateRoles(screenId, data);
58
+ }
59
+
60
+ @Patch(':screenId/route')
61
+ async updateRoutes(
62
+ @Param('screenId', ParseIntPipe) screenId: number,
63
+ @Body() data: UpdateIdsDTO,
64
+ ) {
65
+ return this.screenService.updateRoutes(screenId, data);
66
+ }
67
+
68
+ @Get(':screenId')
69
+ async show(@Param('screenId', ParseIntPipe) screenId: number) {
70
+ return this.screenService.get(screenId);
71
+ }
72
+
73
+ @Post()
74
+ create(@Body() data: CreateDTO) {
75
+ return this.screenService.create(data);
76
+ }
77
+
78
+ @Patch(':screenId')
79
+ async update(
80
+ @Param('screenId', ParseIntPipe) screenId: number,
81
+ @Body() data: UpdateDTO,
82
+ ) {
83
+ return this.screenService.update({
84
+ id: screenId,
85
+ data,
86
+ });
87
+ }
88
+
89
+ @Delete()
90
+ async delete(@Body() data: DeleteDTO) {
91
+ return this.screenService.delete(data);
92
+ }
93
+ }
@@ -0,0 +1,18 @@
1
+ import { PaginationModule } from '@hedhog/pagination';
2
+ import { PrismaModule } from '@hedhog/prisma';
3
+ import { Module, forwardRef } from '@nestjs/common';
4
+ import { ScreenController } from './screen.controller';
5
+ import { ScreenService } from './screen.service';
6
+ import { AuthModule } from '../auth/auth.module';
7
+
8
+ @Module({
9
+ providers: [ScreenService],
10
+ exports: [ScreenService],
11
+ controllers: [ScreenController],
12
+ imports: [
13
+ forwardRef(() => AuthModule),
14
+ forwardRef(() => PrismaModule),
15
+ forwardRef(() => PaginationModule),
16
+ ],
17
+ })
18
+ export class ScreenModule {}