@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,298 @@
1
+ import {
2
+ PageOrderDirection,
3
+ PaginationDTO,
4
+ PaginationService,
5
+ } from '@hedhog/pagination';
6
+ import { PrismaService } from '@hedhog/prisma';
7
+ import { BadRequestException } from '@nestjs/common';
8
+ import { Test, TestingModule } from '@nestjs/testing';
9
+ import { DeleteDTO } from '../dto/delete.dto';
10
+ import { UpdateIdsDTO } from '../dto/update-ids.dto';
11
+ import { ScreenService } from './screen.service';
12
+
13
+ describe('ScreenService', () => {
14
+ let service: ScreenService;
15
+ let prismaService: PrismaService;
16
+ let paginationService: PaginationService;
17
+
18
+ beforeEach(async () => {
19
+ const module: TestingModule = await Test.createTestingModule({
20
+ providers: [
21
+ ScreenService,
22
+ {
23
+ provide: PrismaService,
24
+ useValue: {
25
+ screens: {
26
+ create: jest.fn(),
27
+ update: jest.fn(),
28
+ deleteMany: jest.fn(),
29
+ findUnique: jest.fn(),
30
+ },
31
+ route: {
32
+ findMany: jest.fn(),
33
+ },
34
+ role_screen: {
35
+ deleteMany: jest.fn(),
36
+ createMany: jest.fn(),
37
+ },
38
+ route_screens: {
39
+ deleteMany: jest.fn(),
40
+ createMany: jest.fn(),
41
+ },
42
+ createInsensitiveSearch: jest.fn(),
43
+ },
44
+ },
45
+ {
46
+ provide: PaginationService,
47
+ useValue: {
48
+ paginate: jest.fn(),
49
+ },
50
+ },
51
+ ],
52
+ }).compile();
53
+
54
+ service = module.get<ScreenService>(ScreenService);
55
+ prismaService = module.get<PrismaService>(PrismaService);
56
+ paginationService = module.get<PaginationService>(PaginationService);
57
+ });
58
+
59
+ it('should be defined', () => {
60
+ expect(service).toBeDefined();
61
+ });
62
+
63
+ describe('create', () => {
64
+ it('should create a screen', async () => {
65
+ const createDTO = {
66
+ name: 'Test Screen',
67
+ slug: 'test-screen',
68
+ description: 'This is a test screen',
69
+ icon: 'test-icon',
70
+ };
71
+
72
+ jest
73
+ .spyOn(prismaService.screens, 'create')
74
+ .mockResolvedValue(createDTO as any);
75
+
76
+ const result = await service.create(createDTO);
77
+
78
+ expect(prismaService.screens.create).toHaveBeenCalledWith({
79
+ data: createDTO,
80
+ });
81
+ expect(result).toEqual(createDTO);
82
+ });
83
+ });
84
+
85
+ describe('delete', () => {
86
+ it('should throw an error if no ids are provided', async () => {
87
+ const deleteDTO: DeleteDTO = { ids: null };
88
+
89
+ await expect(service.delete(deleteDTO)).rejects.toThrow(
90
+ new BadRequestException(
91
+ `You must select at least one screen to delete.`,
92
+ ),
93
+ );
94
+ });
95
+
96
+ it('should delete screens by ids', async () => {
97
+ const deleteDTO: DeleteDTO = { ids: [1, 2] };
98
+
99
+ jest.spyOn(prismaService.screens, 'deleteMany').mockResolvedValue({
100
+ count: 2,
101
+ });
102
+
103
+ await service.delete(deleteDTO);
104
+
105
+ expect(prismaService.screens.deleteMany).toHaveBeenCalledWith({
106
+ where: { id: { in: deleteDTO.ids } },
107
+ });
108
+ });
109
+ });
110
+
111
+ describe('update', () => {
112
+ it('should update a screen', async () => {
113
+ const updateDTO = {
114
+ id: 1,
115
+ name: 'Updated Screen',
116
+ slug: 'updated-screen',
117
+ icon: 'update-icon',
118
+ created_at: new Date(),
119
+ updated_at: new Date(),
120
+ };
121
+ const updateInput = { id: 1, data: updateDTO };
122
+
123
+ jest.spyOn(prismaService.screens, 'update').mockResolvedValue(updateDTO);
124
+
125
+ const result = await service.update(updateInput);
126
+
127
+ expect(prismaService.screens.update).toHaveBeenCalledWith({
128
+ where: { id: updateInput.id },
129
+ data: updateInput.data,
130
+ });
131
+ expect(result).toEqual(updateDTO);
132
+ });
133
+ });
134
+
135
+ describe('get', () => {
136
+ it('should get a screen by id', async () => {
137
+ const screen = {
138
+ id: 1,
139
+ name: 'Updated Screen',
140
+ slug: 'updated-screen',
141
+ icon: 'update-icon',
142
+ created_at: new Date(),
143
+ updated_at: new Date(),
144
+ };
145
+
146
+ jest.spyOn(prismaService.screens, 'findUnique').mockResolvedValue(screen);
147
+
148
+ const result = await service.get(1);
149
+
150
+ expect(prismaService.screens.findUnique).toHaveBeenCalledWith({
151
+ where: { id: 1 },
152
+ });
153
+ expect(result).toEqual(screen);
154
+ });
155
+ });
156
+
157
+ describe('updateRoles', () => {
158
+ it('should update role for a screen', async () => {
159
+ const screenId = 1;
160
+ const updateIdsDTO: UpdateIdsDTO = { ids: [1, 2, 3] };
161
+ jest.spyOn(prismaService.role_screen, 'deleteMany').mockResolvedValue({
162
+ count: 1,
163
+ });
164
+
165
+ jest.spyOn(prismaService.role_screen, 'createMany').mockResolvedValue({
166
+ count: updateIdsDTO.ids.length,
167
+ });
168
+
169
+ await service.updateRoles(screenId, updateIdsDTO);
170
+
171
+ expect(prismaService.role_screen.deleteMany).toHaveBeenCalledWith({
172
+ where: { screen_id: screenId },
173
+ });
174
+ expect(prismaService.role_screen.createMany).toHaveBeenCalledWith({
175
+ data: updateIdsDTO.ids.map((roleId) => ({
176
+ screen_id: screenId,
177
+ role_id: roleId,
178
+ })),
179
+ skipDuplicates: true,
180
+ });
181
+ });
182
+ });
183
+
184
+ describe('updateRoutes', () => {
185
+ it('should delete existing route_screens and create new ones', async () => {
186
+ const screenId = 1;
187
+ const updateIdsDTO: UpdateIdsDTO = { ids: [1, 2, 3] };
188
+
189
+ prismaService.route.findMany = jest
190
+ .fn()
191
+ .mockResolvedValue([{ id: 1 }, { id: 2 }, { id: 3 }]);
192
+
193
+ prismaService.route_screens.deleteMany = jest.fn();
194
+ prismaService.route_screens.createMany = jest.fn();
195
+
196
+ await service.updateRoutes(screenId, updateIdsDTO);
197
+
198
+ expect(prismaService.route.findMany).toHaveBeenCalledWith({
199
+ where: { id: { in: updateIdsDTO.ids } },
200
+ select: { id: true },
201
+ });
202
+
203
+ expect(prismaService.route_screens.deleteMany).toHaveBeenCalledWith({
204
+ where: { screen_id: screenId },
205
+ });
206
+
207
+ expect(prismaService.route_screens.createMany).toHaveBeenCalledWith({
208
+ data: updateIdsDTO.ids.map((routeId) => ({
209
+ screen_id: screenId,
210
+ route_id: routeId,
211
+ })),
212
+ skipDuplicates: true,
213
+ });
214
+ });
215
+ });
216
+
217
+ describe('listRoles', () => {
218
+ it('should call paginate method with correct parameters', async () => {
219
+ const locale = 'en';
220
+ const screenId = 1;
221
+ const paginationParams: PaginationDTO = {
222
+ page: 1,
223
+ pageSize: 10,
224
+ search: '',
225
+ sortField: '',
226
+ sortOrder: PageOrderDirection.Asc,
227
+ fields: '',
228
+ };
229
+
230
+ paginationService.paginate = jest.fn().mockResolvedValue({
231
+ data: [],
232
+ total: 0,
233
+ });
234
+
235
+ await service.listRoles(locale, screenId, paginationParams);
236
+
237
+ expect(paginationService.paginate).toHaveBeenCalledWith(
238
+ prismaService.role,
239
+ paginationParams,
240
+ {
241
+ include: {
242
+ role_screen: {
243
+ where: {
244
+ screen_id: screenId,
245
+ },
246
+ select: {
247
+ role_id: true,
248
+ screen_id: true,
249
+ },
250
+ },
251
+ },
252
+ },
253
+ );
254
+ });
255
+ });
256
+
257
+ describe('listRoutes', () => {
258
+ it('should paginate the route linked to a screen', async () => {
259
+ const paginationParams: PaginationDTO = {
260
+ page: 1,
261
+ pageSize: 10,
262
+ search: '',
263
+ sortField: '',
264
+ sortOrder: PageOrderDirection.Asc,
265
+ fields: '',
266
+ };
267
+ const screenId = 1;
268
+ const mockPaginationResult = {
269
+ data: [],
270
+ total: 10,
271
+ lastPage: 1,
272
+ page: 1,
273
+ prev: 0,
274
+ next: 2,
275
+ pageSize: 10,
276
+ };
277
+
278
+ jest
279
+ .spyOn(paginationService, 'paginate')
280
+ .mockResolvedValue(mockPaginationResult);
281
+
282
+ await service.listRoutes(screenId, paginationParams);
283
+
284
+ expect(paginationService.paginate).toHaveBeenCalledWith(
285
+ prismaService.route,
286
+ paginationParams,
287
+ {
288
+ include: {
289
+ route_screens: {
290
+ where: { screen_id: screenId },
291
+ select: { route_id: true, screen_id: true },
292
+ },
293
+ },
294
+ },
295
+ );
296
+ });
297
+ });
298
+ });
@@ -0,0 +1,181 @@
1
+ import { PaginationDTO, PaginationService } from '@hedhog/pagination';
2
+ import { PrismaService } from '@hedhog/prisma';
3
+ import {
4
+ BadRequestException,
5
+ Inject,
6
+ Injectable,
7
+ forwardRef,
8
+ } from '@nestjs/common';
9
+ import { DeleteDTO } from '../dto/delete.dto';
10
+ import { UpdateIdsDTO } from '../dto/update-ids.dto';
11
+ import { CreateDTO } from './dto/create.dto';
12
+ import { UpdateDTO } from './dto/update.dto';
13
+
14
+ @Injectable()
15
+ export class ScreenService {
16
+ constructor(
17
+ @Inject(forwardRef(() => PrismaService))
18
+ private readonly prismaService: PrismaService,
19
+ @Inject(forwardRef(() => PaginationService))
20
+ private readonly paginationService: PaginationService,
21
+ ) {}
22
+
23
+ async updateRoles(screenId: number, data: UpdateIdsDTO) {
24
+ await this.prismaService.role_screen.deleteMany({
25
+ where: {
26
+ screen_id: screenId,
27
+ },
28
+ });
29
+
30
+ return this.prismaService.role_screen.createMany({
31
+ data: data.ids.map((roleId) => ({
32
+ screen_id: screenId,
33
+ role_id: roleId,
34
+ })),
35
+ skipDuplicates: true,
36
+ });
37
+ }
38
+ async updateRoutes(screenId: number, { ids }: UpdateIdsDTO) {
39
+ ids = (
40
+ await this.prismaService.route.findMany({
41
+ where: {
42
+ id: {
43
+ in: ids,
44
+ },
45
+ },
46
+ select: {
47
+ id: true,
48
+ },
49
+ })
50
+ ).map((route) => route.id);
51
+
52
+ await this.prismaService.route_screen.deleteMany({
53
+ where: {
54
+ screen_id: screenId,
55
+ },
56
+ });
57
+
58
+ return this.prismaService.route_screen.createMany({
59
+ data: ids.map((routeId) => ({
60
+ screen_id: screenId,
61
+ route_id: routeId,
62
+ })),
63
+ skipDuplicates: true,
64
+ });
65
+ }
66
+ async listRoutes(screenId: number, paginationParams: PaginationDTO) {
67
+ return this.paginationService.paginate(
68
+ this.prismaService.route,
69
+ paginationParams,
70
+ {
71
+ include: {
72
+ route_screens: {
73
+ where: {
74
+ screen_id: screenId,
75
+ },
76
+ select: {
77
+ route_id: true,
78
+ screen_id: true,
79
+ },
80
+ },
81
+ },
82
+ },
83
+ );
84
+ }
85
+
86
+ async listRoles(
87
+ locale: string,
88
+ screenId: number,
89
+ paginationParams: PaginationDTO,
90
+ ) {
91
+ return this.paginationService.paginate(
92
+ this.prismaService.role,
93
+ paginationParams,
94
+ {
95
+ include: {
96
+ role_screen: {
97
+ where: {
98
+ screen_id: screenId,
99
+ },
100
+ select: {
101
+ role_id: true,
102
+ screen_id: true,
103
+ },
104
+ },
105
+ },
106
+ },
107
+ );
108
+ }
109
+
110
+ async getScreens(locale: string, paginationParams: PaginationDTO) {
111
+ const fields = ['slug', 'icon'];
112
+ const OR: any[] = this.prismaService.createInsensitiveSearch(
113
+ fields,
114
+ paginationParams,
115
+ );
116
+
117
+ const result = await this.paginationService.paginate(
118
+ this.prismaService.screen,
119
+ paginationParams,
120
+ {
121
+ where: {
122
+ OR,
123
+ },
124
+ include: {
125
+ screen_locale: {
126
+ where: {
127
+ locale: {
128
+ code: locale,
129
+ },
130
+ },
131
+ select: {
132
+ name: true,
133
+ description: true,
134
+ },
135
+ },
136
+ },
137
+ },
138
+ 'screen_locale',
139
+ );
140
+
141
+ return result;
142
+ }
143
+
144
+ async get(screenId: number) {
145
+ return this.prismaService.screen.findUnique({ where: { id: screenId } });
146
+ }
147
+
148
+ async create({ name, slug, description, icon }: CreateDTO) {
149
+ return this.prismaService.screen.create({
150
+ data: {
151
+ name,
152
+ slug,
153
+ description,
154
+ icon,
155
+ },
156
+ });
157
+ }
158
+
159
+ async update({ id, data }: { id: number; data: UpdateDTO }) {
160
+ return this.prismaService.screen.update({
161
+ where: { id },
162
+ data,
163
+ });
164
+ }
165
+
166
+ async delete({ ids }: DeleteDTO) {
167
+ if (ids == undefined || ids == null) {
168
+ throw new BadRequestException(
169
+ `You must select at least one screen to delete.`,
170
+ );
171
+ }
172
+
173
+ return this.prismaService.screen.deleteMany({
174
+ where: {
175
+ id: {
176
+ in: ids,
177
+ },
178
+ },
179
+ });
180
+ }
181
+ }
@@ -0,0 +1 @@
1
+ export class CreateDTO {}
@@ -0,0 +1,6 @@
1
+ import { IsString } from 'class-validator';
2
+
3
+ export class SettingUserDTO {
4
+ @IsString()
5
+ value: string;
6
+ }
@@ -0,0 +1,17 @@
1
+ import { Type } from 'class-transformer';
2
+ import { IsArray, IsString, ValidateNested } from 'class-validator';
3
+
4
+ class Setting {
5
+ @IsString()
6
+ slug: string;
7
+
8
+ @IsString()
9
+ value: string;
10
+ }
11
+
12
+ export class SettingDTO {
13
+ @IsArray()
14
+ @ValidateNested({ each: true })
15
+ @Type(() => Setting)
16
+ setting: Setting[];
17
+ }
@@ -0,0 +1,3 @@
1
+ import { CreateDTO } from './create.dto';
2
+
3
+ export class UpdateDTO extends CreateDTO {}
@@ -0,0 +1,100 @@
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
+ Put,
13
+ forwardRef,
14
+ } from '@nestjs/common';
15
+ import { User } from '../auth/decorators/user.decorator';
16
+ import { DeleteDTO } from '../dto/delete.dto';
17
+ import { Locale } from '../locale';
18
+ import { Role } from '../role/decorators/role.decorator';
19
+ import { CreateDTO } from './dto/create.dto';
20
+ import { SettingUserDTO } from './dto/setting-user.dto';
21
+ import { SettingDTO } from './dto/setting.dto';
22
+ import { UpdateDTO } from './dto/update.dto';
23
+ import { SettingService } from './setting.service';
24
+
25
+ @Role()
26
+ @Controller('setting')
27
+ export class SettingsController {
28
+ constructor(
29
+ @Inject(forwardRef(() => SettingService))
30
+ private readonly settingService: SettingService,
31
+ ) {}
32
+
33
+ @Get('groups/:slug')
34
+ async getSettingFromGroup(
35
+ @Pagination() paginationParams,
36
+ @Locale() locale,
37
+ @Param('slug') slug: string,
38
+ ) {
39
+ return this.settingService.getSettingFromGroup(
40
+ locale,
41
+ paginationParams,
42
+ slug,
43
+ );
44
+ }
45
+
46
+ @Get('groups')
47
+ async getSettingGroups(@Pagination() paginationParams, @Locale() locale) {
48
+ return this.settingService.getSettingGroups(locale, paginationParams);
49
+ }
50
+
51
+ @Get()
52
+ async getSettings(@Pagination() paginationParams, @Locale() locale) {
53
+ return this.settingService.getSettings(locale, paginationParams);
54
+ }
55
+
56
+ @Get(':settingId')
57
+ async show(@Param('settingId', ParseIntPipe) settingId: number) {
58
+ return this.settingService.get(settingId);
59
+ }
60
+
61
+ @Post()
62
+ create(@Body() data: CreateDTO) {
63
+ return this.settingService.create(data);
64
+ }
65
+
66
+ @Put('user/:slug')
67
+ async updateUserFromSlug(
68
+ @Param('slug') slug: string,
69
+ @Body() { value }: SettingUserDTO,
70
+ @User() { id },
71
+ ) {
72
+ return this.settingService.setSettingUserValue(id, slug, value);
73
+ }
74
+
75
+ @Put(':slug')
76
+ async updateFromSlug(@Param('slug') slug: string, @Body() data: UpdateDTO) {
77
+ return this.settingService.updateFromSlug(slug, data);
78
+ }
79
+
80
+ @Put()
81
+ async setManySettings(@Body() data: SettingDTO) {
82
+ return this.settingService.setManySettings(data);
83
+ }
84
+
85
+ @Patch(':settingId')
86
+ async update(
87
+ @Param('settingId', ParseIntPipe) settingId: number,
88
+ @Body() data: UpdateDTO,
89
+ ) {
90
+ return this.settingService.update({
91
+ id: settingId,
92
+ data,
93
+ });
94
+ }
95
+
96
+ @Delete()
97
+ async delete(@Body() data: DeleteDTO) {
98
+ return this.settingService.delete(data);
99
+ }
100
+ }
@@ -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 { AuthModule } from '../auth/auth.module';
5
+ import { SettingsController } from './setting.controller';
6
+ import { SettingService } from './setting.service';
7
+
8
+ @Module({
9
+ providers: [SettingService],
10
+ exports: [SettingService],
11
+ controllers: [SettingsController],
12
+ imports: [
13
+ forwardRef(() => AuthModule),
14
+ forwardRef(() => PrismaModule),
15
+ forwardRef(() => PaginationModule),
16
+ ],
17
+ })
18
+ export class SettingModule {}