@hedhog/admin 0.0.103 → 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,126 @@
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 { RoleService } from './role.service';
21
+
22
+ @Role()
23
+ @Controller('role')
24
+ export class RoleController {
25
+ constructor(
26
+ @Inject(forwardRef(() => RoleService))
27
+ private readonly roleService: RoleService,
28
+ ) {}
29
+
30
+ @Get()
31
+ async getRoles(@Pagination() paginationParams, @Locale() locale) {
32
+ return this.roleService.getRoles(locale, paginationParams);
33
+ }
34
+
35
+ @Get(':roleId/user')
36
+ async listUsers(
37
+ @Pagination() paginationParams,
38
+ @Param('roleId', ParseIntPipe) roleId: number,
39
+ ) {
40
+ return this.roleService.listUsers(roleId, paginationParams);
41
+ }
42
+
43
+ @Get(':roleId/menu')
44
+ async listMenus(
45
+ @Pagination() paginationParams,
46
+ @Param('roleId', ParseIntPipe) roleId: number,
47
+ @Locale() locale,
48
+ ) {
49
+ return this.roleService.listMenus(locale, roleId, paginationParams);
50
+ }
51
+
52
+ @Get(':roleId/route')
53
+ async listRoutes(
54
+ @Pagination() paginationParams,
55
+ @Param('roleId', ParseIntPipe) roleId: number,
56
+ ) {
57
+ return this.roleService.listRoutes(roleId, paginationParams);
58
+ }
59
+
60
+ @Get(':roleId/screens')
61
+ async listScreens(
62
+ @Pagination() paginationParams,
63
+ @Param('roleId', ParseIntPipe) roleId: number,
64
+ @Locale() locale,
65
+ ) {
66
+ return this.roleService.listScreens(locale, roleId, paginationParams);
67
+ }
68
+
69
+ @Patch(':roleId/user')
70
+ async updateUsers(
71
+ @Param('roleId', ParseIntPipe) roleId: number,
72
+ @Body() data: UpdateIdsDTO,
73
+ ) {
74
+ return this.roleService.updateUsers(roleId, data);
75
+ }
76
+
77
+ @Patch(':roleId/menu')
78
+ async updateMenus(
79
+ @Param('roleId', ParseIntPipe) roleId: number,
80
+ @Body() data: UpdateIdsDTO,
81
+ ) {
82
+ return this.roleService.updateMenus(roleId, data);
83
+ }
84
+
85
+ @Patch(':roleId/route')
86
+ async updateRoutes(
87
+ @Param('roleId', ParseIntPipe) roleId: number,
88
+ @Body() data: UpdateIdsDTO,
89
+ ) {
90
+ return this.roleService.updateRoutes(roleId, data);
91
+ }
92
+
93
+ @Patch(':roleId/screens')
94
+ async updateScreens(
95
+ @Param('roleId', ParseIntPipe) roleId: number,
96
+ @Body() data: UpdateIdsDTO,
97
+ ) {
98
+ return this.roleService.updateScreens(roleId, data);
99
+ }
100
+
101
+ @Get(':roleId')
102
+ async show(@Param('roleId', ParseIntPipe) roleId: number, @Locale() locale) {
103
+ return this.roleService.get(locale, roleId);
104
+ }
105
+
106
+ @Post()
107
+ create(@Body() data: CreateDTO) {
108
+ return this.roleService.create(data);
109
+ }
110
+
111
+ @Patch(':roleId')
112
+ async update(
113
+ @Param('roleId', ParseIntPipe) roleId: number,
114
+ @Body() data: UpdateDTO,
115
+ ) {
116
+ return this.roleService.update({
117
+ id: roleId,
118
+ data,
119
+ });
120
+ }
121
+
122
+ @Delete()
123
+ async delete(@Body() data: DeleteDTO) {
124
+ return this.roleService.delete(data);
125
+ }
126
+ }
@@ -0,0 +1,28 @@
1
+ import { PaginationModule } from '@hedhog/pagination';
2
+ import { PrismaModule } from '@hedhog/prisma';
3
+ import { forwardRef, Module } from '@nestjs/common';
4
+ import { APP_GUARD } from '@nestjs/core';
5
+ import { AuthModule } from '../auth/auth.module';
6
+ import { LocaleModule } from '../locale';
7
+ import { RoleGuard } from './guards/role.guard';
8
+ import { RoleController } from './role.controller';
9
+ import { RoleService } from './role.service';
10
+
11
+ @Module({
12
+ imports: [
13
+ forwardRef(() => AuthModule),
14
+ forwardRef(() => PrismaModule),
15
+ forwardRef(() => PaginationModule),
16
+ forwardRef(() => LocaleModule),
17
+ ],
18
+ controllers: [RoleController],
19
+ providers: [
20
+ RoleService,
21
+ {
22
+ provide: APP_GUARD,
23
+ useClass: RoleGuard,
24
+ },
25
+ ],
26
+ exports: [RoleService],
27
+ })
28
+ export class RoleModule {}
@@ -0,0 +1,417 @@
1
+ /*
2
+ describe('RoleService', () => {
3
+ let roleService: RoleService;
4
+ let prismaService: PrismaService;
5
+ let paginationService: PaginationService;
6
+
7
+ beforeEach(async () => {
8
+ const module: TestingModule = await Test.createTestingModule({
9
+ providers: [
10
+ RoleService,
11
+ {
12
+ provide: PrismaService,
13
+ useValue: {
14
+ role {
15
+ create: jest.fn(),
16
+ update: jest.fn(),
17
+ findUnique: jest.fn(),
18
+ deleteMany: jest.fn(),
19
+ findMany: jest.fn(),
20
+ },
21
+ role_user: {
22
+ deleteMany: jest.fn(),
23
+ createMany: jest.fn(),
24
+ },
25
+ role_menu: {
26
+ deleteMany: jest.fn(),
27
+ createMany: jest.fn(),
28
+ },
29
+ role_screen: {
30
+ deleteMany: jest.fn(),
31
+ createMany: jest.fn(),
32
+ },
33
+ role_route: {
34
+ deleteMany: jest.fn(),
35
+ createMany: jest.fn(),
36
+ },
37
+ },
38
+ },
39
+ {
40
+ provide: PaginationService,
41
+ useValue: {
42
+ paginate: jest.fn(),
43
+ },
44
+ },
45
+ ],
46
+ }).compile();
47
+
48
+ roleService = module.get<RoleService>(RoleService);
49
+ prismaService = module.get<PrismaService>(PrismaService);
50
+ paginationService = module.get<PaginationService>(PaginationService);
51
+ });
52
+ /*
53
+ describe('create', () => {
54
+ it('should create a new role', async () => {
55
+ const dto: CreateDTO = {
56
+ name: 'Admin',
57
+ description: 'Administrator role',
58
+ };
59
+ const result = {
60
+ id: 1,
61
+ ...dto,
62
+ created_at: new Date(),
63
+ updated_at: new Date(),
64
+ };
65
+
66
+ jest.spyOn(prismaService.role, 'create').mockResolvedValue(result);
67
+
68
+ expect(await roleService.create(dto)).toEqual(result);
69
+ });
70
+ });
71
+ */
72
+ // describe('update', () => {
73
+ // it('should update an existing role', async () => {
74
+ // const dto: UpdateDTO = {
75
+ // name: 'Admin',
76
+ // description: 'Updated description',
77
+ // };
78
+ // const result = {
79
+ // id: 1,
80
+ // ...dto,
81
+ // created_at: new Date(),
82
+ // updated_at: new Date(),
83
+ // };
84
+
85
+ // jest.spyOn(prismaService.role, 'update').mockResolvedValue(result);
86
+
87
+ // expect(await roleService.update({ id: 1, data: dto })).toEqual(result);
88
+ // });
89
+ // });
90
+ /*
91
+ describe('delete', () => {
92
+ it('should delete role', async () => {
93
+ const dto: DeleteDTO = { ids: [1, 2] };
94
+ jest
95
+ .spyOn(prismaService.role, 'deleteMany')
96
+ .mockResolvedValue({ count: 2 });
97
+
98
+ await roleService.delete(dto);
99
+ expect(prismaService.role.deleteMany).toHaveBeenCalledWith({
100
+ where: {
101
+ id: { in: dto.ids },
102
+ },
103
+ });
104
+ });
105
+
106
+ it('should throw BadRequestException if no ids are provided', async () => {
107
+ const dto: DeleteDTO = { ids: null };
108
+ await expect(roleService.delete(dto)).rejects.toThrow(
109
+ BadRequestException,
110
+ );
111
+ });
112
+ });
113
+
114
+ describe('updateUsers', () => {
115
+ it('should update user for a role', async () => {
116
+ const roleId = 1;
117
+ const data: UpdateIdsDTO = { ids: [1, 2] };
118
+
119
+ jest
120
+ .spyOn(prismaService.role_user, 'deleteMany')
121
+ .mockResolvedValue({ count: 2 });
122
+ jest
123
+ .spyOn(prismaService.role_user, 'createMany')
124
+ .mockResolvedValue({ count: 2 });
125
+
126
+ await roleService.updateUsers(roleId, data);
127
+
128
+ expect(prismaService.role_user.deleteMany).toHaveBeenCalledWith({
129
+ where: { role_id: roleId },
130
+ });
131
+
132
+ expect(prismaService.role_user.createMany).toHaveBeenCalledWith({
133
+ data: data.ids.map((userId) => ({
134
+ role_id: roleId,
135
+ user_id: userId,
136
+ })),
137
+ skipDuplicates: true,
138
+ });
139
+ });
140
+ });
141
+
142
+ describe('updateScreens', () => {
143
+ it('should update screens for a role', async () => {
144
+ const roleId = 1;
145
+ const data: UpdateIdsDTO = { ids: [1, 2] };
146
+
147
+ jest
148
+ .spyOn(prismaService.role_screen, 'deleteMany')
149
+ .mockResolvedValue({ count: 2 });
150
+ jest
151
+ .spyOn(prismaService.role_screen, 'createMany')
152
+ .mockResolvedValue({ count: 2 });
153
+
154
+ await roleService.updateScreens(roleId, data);
155
+
156
+ expect(prismaService.role_screen.deleteMany).toHaveBeenCalledWith({
157
+ where: { role_id: roleId },
158
+ });
159
+
160
+ expect(prismaService.role_screen.createMany).toHaveBeenCalledWith({
161
+ data: data.ids.map((screenId) => ({
162
+ role_id: roleId,
163
+ screen_id: screenId,
164
+ })),
165
+ skipDuplicates: true,
166
+ });
167
+ });
168
+ });
169
+
170
+ describe('updateRoutes', () => {
171
+ it('should update route for a role', async () => {
172
+ const roleId = 1;
173
+ const data: UpdateIdsDTO = { ids: [1, 2] };
174
+
175
+ jest
176
+ .spyOn(prismaService.role_route, 'deleteMany')
177
+ .mockResolvedValue({ count: 2 });
178
+ jest
179
+ .spyOn(prismaService.role_route, 'createMany')
180
+ .mockResolvedValue({ count: 2 });
181
+
182
+ await roleService.updateRoutes(roleId, data);
183
+
184
+ expect(prismaService.role_route.deleteMany).toHaveBeenCalledWith({
185
+ where: { role_id: roleId },
186
+ });
187
+
188
+ expect(prismaService.role_route.createMany).toHaveBeenCalledWith({
189
+ data: data.ids.map((routeId) => ({
190
+ role_id: roleId,
191
+ route_id: routeId,
192
+ })),
193
+ skipDuplicates: true,
194
+ });
195
+ });
196
+ });
197
+
198
+ describe('updateMenus', () => {
199
+ it('should update menu for a role', async () => {
200
+ const roleId = 1;
201
+ const data: UpdateIdsDTO = { ids: [1, 2] };
202
+
203
+ jest
204
+ .spyOn(prismaService.role_menu, 'deleteMany')
205
+ .mockResolvedValue({ count: 2 });
206
+ jest
207
+ .spyOn(prismaService.role_menu, 'createMany')
208
+ .mockResolvedValue({ count: 2 });
209
+
210
+ await roleService.updateMenus(roleId, data);
211
+
212
+ expect(prismaService.role_menu.deleteMany).toHaveBeenCalledWith({
213
+ where: { role_id: roleId },
214
+ });
215
+
216
+ expect(prismaService.role_menu.createMany).toHaveBeenCalledWith({
217
+ data: data.ids.map((menuId) => ({
218
+ role_id: roleId,
219
+ menu_id: menuId,
220
+ })),
221
+ skipDuplicates: true,
222
+ });
223
+ });
224
+ });
225
+
226
+ describe('listUsers', () => {
227
+ it('should list user associated with a role', async () => {
228
+ const roleId = 1;
229
+ const paginationParams: PaginationDTO = {
230
+ page: 1,
231
+ pageSize: 10,
232
+ search: '',
233
+ sortField: '',
234
+ sortOrder: PageOrderDirection.Asc,
235
+ fields: '',
236
+ };
237
+ const mockPaginationResult = {
238
+ data: [],
239
+ total: 10,
240
+ lastPage: 1,
241
+ page: 1,
242
+ prev: 0,
243
+ next: 2,
244
+ pageSize: 10,
245
+ };
246
+
247
+ jest
248
+ .spyOn(paginationService, 'paginate')
249
+ .mockResolvedValue(mockPaginationResult);
250
+
251
+ await roleService.listUsers(roleId, paginationParams);
252
+
253
+ expect(paginationService.paginate).toHaveBeenCalledWith(
254
+ prismaService.user,
255
+ paginationParams,
256
+ {
257
+ include: {
258
+ role_user: {
259
+ where: { role_id: roleId },
260
+ select: { user_id: true, role_id: true },
261
+ },
262
+ },
263
+ },
264
+ );
265
+ });
266
+ });
267
+
268
+ describe('listMenus', () => {
269
+ it('should list menu associated with a role', async () => {
270
+ const locale = 'en';
271
+ const roleId = 1;
272
+ const paginationParams: PaginationDTO = {
273
+ page: 1,
274
+ pageSize: 10,
275
+ search: '',
276
+ sortField: '',
277
+ sortOrder: PageOrderDirection.Asc,
278
+ fields: '',
279
+ };
280
+ const mockPaginationResult = {
281
+ data: [],
282
+ total: 10,
283
+ lastPage: 1,
284
+ page: 1,
285
+ prev: 0,
286
+ next: 2,
287
+ pageSize: 10,
288
+ };
289
+
290
+ jest
291
+ .spyOn(paginationService, 'paginate')
292
+ .mockResolvedValue(mockPaginationResult);
293
+
294
+ await roleService.listMenus(locale, roleId, paginationParams);
295
+
296
+ expect(paginationService.paginate).toHaveBeenCalledWith(
297
+ prismaService.menu,
298
+ paginationParams,
299
+ {
300
+ include: {
301
+ menu_locale: {
302
+ where: { locale: { code: locale } },
303
+ select: { name: true },
304
+ },
305
+ role_menu: {
306
+ where: { role_id: roleId },
307
+ select: { menu_id: true, role_id: true },
308
+ },
309
+ },
310
+ },
311
+ 'menu_locale',
312
+ );
313
+ });
314
+ });
315
+
316
+ describe('listRoutes', () => {
317
+ it('should list route associated with a role', async () => {
318
+ const roleId = 1;
319
+ const paginationParams: PaginationDTO = {
320
+ page: 1,
321
+ pageSize: 10,
322
+ search: '',
323
+ sortField: '',
324
+ sortOrder: PageOrderDirection.Asc,
325
+ fields: '',
326
+ };
327
+ const mockPaginationResult = {
328
+ data: [],
329
+ total: 10,
330
+ lastPage: 1,
331
+ page: 1,
332
+ prev: 0,
333
+ next: 2,
334
+ pageSize: 10,
335
+ };
336
+
337
+ jest
338
+ .spyOn(paginationService, 'paginate')
339
+ .mockResolvedValue(mockPaginationResult);
340
+
341
+ await roleService.listRoutes(roleId, paginationParams);
342
+
343
+ expect(paginationService.paginate).toHaveBeenCalledWith(
344
+ prismaService.route,
345
+ paginationParams,
346
+ {
347
+ include: {
348
+ role_route: {
349
+ where: { role_id: roleId },
350
+ select: { route_id: true, role_id: true },
351
+ },
352
+ },
353
+ },
354
+ );
355
+ });
356
+ });
357
+
358
+ describe('listScreens', () => {
359
+ it('should list screens associated with a role', async () => {
360
+ const locale = 'en';
361
+ const roleId = 1;
362
+ const paginationParams: PaginationDTO = {
363
+ page: 1,
364
+ pageSize: 10,
365
+ search: '',
366
+ sortField: '',
367
+ sortOrder: PageOrderDirection.Asc,
368
+ fields: '',
369
+ };
370
+ const mockPaginationResult = {
371
+ data: [],
372
+ total: 10,
373
+ lastPage: 1,
374
+ page: 1,
375
+ prev: 0,
376
+ next: 2,
377
+ pageSize: 10,
378
+ };
379
+
380
+ jest
381
+ .spyOn(paginationService, 'paginate')
382
+ .mockResolvedValue(mockPaginationResult);
383
+
384
+ await roleService.listScreens(locale, roleId, paginationParams);
385
+
386
+ expect(paginationService.paginate).toHaveBeenCalledWith(
387
+ prismaService.screens,
388
+ paginationParams,
389
+ {
390
+ include: {
391
+ screen_locale: {
392
+ where: { locale: { code: locale } },
393
+ select: { name: true },
394
+ },
395
+ role_screen: {
396
+ where: { role_id: roleId },
397
+ select: { screen_id: true, role_id: true },
398
+ },
399
+ },
400
+ },
401
+ 'screen_locale',
402
+ );
403
+ });
404
+ });
405
+
406
+ describe('get', () => {
407
+ it('should get a specific role by ID', async () => {
408
+ const roleId = 1;
409
+ const result = { id: roleId, name: 'Admin', description: 'Admin role' };
410
+
411
+ jest.spyOn(prismaService.role, 'findUnique').mockResolvedValue(result);
412
+
413
+ expect(await roleService.get('en', roleId)).toEqual(result);
414
+ });
415
+ });
416
+ });
417
+ */