@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.
- package/README.md +141 -141
- package/dist/admin.module.js +7 -7
- package/dist/admin.module.js.map +1 -1
- package/dist/auth/auth.service.d.ts +1 -1
- package/dist/auth/auth.service.d.ts.map +1 -1
- package/dist/auth/auth.service.js +8 -8
- package/dist/auth/auth.service.js.map +1 -1
- package/dist/auth/auth.service.spec.js +16 -16
- package/dist/auth/auth.service.spec.js.map +1 -1
- package/dist/dto/with-locale.dto.d.ts +4 -0
- package/dist/dto/with-locale.dto.d.ts.map +1 -0
- package/dist/dto/{with-locales.dto.js → with-locale.dto.js} +5 -5
- package/dist/dto/with-locale.dto.js.map +1 -0
- package/dist/index.d.ts +2 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -2
- package/dist/index.js.map +1 -1
- package/dist/locale/locale.controller.js +1 -1
- package/dist/locale/locale.controller.js.map +1 -1
- package/dist/locale/locale.middleware.d.ts +1 -1
- package/dist/locale/locale.middleware.d.ts.map +1 -1
- package/dist/locale/locale.middleware.js +2 -2
- package/dist/locale/locale.middleware.js.map +1 -1
- package/dist/locale/locale.service.d.ts +6 -6
- package/dist/locale/locale.service.d.ts.map +1 -1
- package/dist/locale/locale.service.js +40 -40
- package/dist/locale/locale.service.js.map +1 -1
- package/dist/locale/locale.service.spec.js +14 -14
- package/dist/locale/locale.service.spec.js.map +1 -1
- package/dist/menu/menu.controller.js +3 -3
- package/dist/menu/menu.controller.js.map +1 -1
- package/dist/menu/menu.service.d.ts +3 -3
- package/dist/menu/menu.service.d.ts.map +1 -1
- package/dist/menu/menu.service.js +35 -35
- package/dist/menu/menu.service.js.map +1 -1
- package/dist/menu/menu.service.spec.js +33 -37
- package/dist/menu/menu.service.spec.js.map +1 -1
- package/dist/role/dto/create.dto.d.ts +2 -2
- package/dist/role/dto/create.dto.d.ts.map +1 -1
- package/dist/role/dto/create.dto.js +2 -2
- package/dist/role/dto/create.dto.js.map +1 -1
- package/dist/role/guards/role.guard.js +4 -4
- package/dist/role/guards/role.guard.js.map +1 -1
- package/dist/role/role.controller.js +7 -7
- package/dist/role/role.controller.js.map +1 -1
- package/dist/role/role.service.d.ts +2 -2
- package/dist/role/role.service.d.ts.map +1 -1
- package/dist/role/role.service.js +35 -35
- package/dist/role/role.service.js.map +1 -1
- package/dist/role/role.service.spec.d.ts +0 -1
- package/dist/role/role.service.spec.js +400 -340
- package/dist/role/role.service.spec.js.map +1 -1
- package/dist/route/route.controller.js +3 -3
- package/dist/route/route.controller.js.map +1 -1
- package/dist/route/route.service.d.ts +2 -2
- package/dist/route/route.service.d.ts.map +1 -1
- package/dist/route/route.service.js +18 -18
- package/dist/route/route.service.js.map +1 -1
- package/dist/route/route.service.spec.js +27 -27
- package/dist/route/route.service.spec.js.map +1 -1
- package/dist/screen/screen.controller.js +4 -4
- package/dist/screen/screen.controller.js.map +1 -1
- package/dist/screen/screen.service.d.ts +2 -2
- package/dist/screen/screen.service.d.ts.map +1 -1
- package/dist/screen/screen.service.js +16 -16
- package/dist/screen/screen.service.js.map +1 -1
- package/dist/screen/screen.service.spec.js +16 -16
- package/dist/screen/screen.service.spec.js.map +1 -1
- package/dist/setting/dto/setting.dto.d.ts +9 -0
- package/dist/setting/dto/setting.dto.d.ts.map +1 -0
- package/dist/setting/dto/{settings.dto.js → setting.dto.js} +6 -6
- package/dist/setting/dto/setting.dto.js.map +1 -0
- package/dist/setting/{settings.controller.d.ts → setting.controller.d.ts} +6 -6
- package/dist/setting/setting.controller.d.ts.map +1 -0
- package/dist/setting/{settings.controller.js → setting.controller.js} +20 -20
- package/dist/setting/setting.controller.js.map +1 -0
- package/dist/setting/setting.module.d.ts +3 -0
- package/dist/setting/setting.module.d.ts.map +1 -0
- package/dist/setting/{settings.module.js → setting.module.js} +6 -6
- package/dist/setting/setting.module.js.map +1 -0
- package/dist/setting/{settings.service.d.ts → setting.service.d.ts} +4 -4
- package/dist/setting/setting.service.d.ts.map +1 -0
- package/dist/setting/{settings.service.js → setting.service.js} +42 -42
- package/dist/setting/setting.service.js.map +1 -0
- package/dist/setting/setting.service.spec.d.ts +2 -0
- package/dist/setting/setting.service.spec.d.ts.map +1 -0
- package/dist/setting/{settings.service.spec.js → setting.service.spec.js} +17 -17
- package/dist/setting/setting.service.spec.js.map +1 -0
- package/dist/user/user.controller.js +3 -3
- package/dist/user/user.controller.js.map +1 -1
- package/dist/user/user.service.d.ts +2 -2
- package/dist/user/user.service.d.ts.map +1 -1
- package/dist/user/user.service.js +9 -9
- package/dist/user/user.service.js.map +1 -1
- package/dist/user/user.service.spec.js +24 -24
- package/dist/user/user.service.spec.js.map +1 -1
- package/package.json +2 -2
- package/src/admin.module.ts +36 -0
- package/src/auth/auth.controller.ts +48 -0
- package/src/auth/auth.module.ts +39 -0
- package/src/auth/auth.service.spec.ts +196 -0
- package/src/auth/auth.service.ts +175 -0
- package/src/auth/decorators/public.decorator.ts +4 -0
- package/src/auth/decorators/user.decorator.ts +17 -0
- package/src/auth/dto/forget.dto.ts +6 -0
- package/src/auth/dto/login.dto.ts +15 -0
- package/src/auth/dto/otp.dto.ts +11 -0
- package/src/auth/enums/multifactor-type.enum.ts +4 -0
- package/src/auth/guards/auth.guard.ts +50 -0
- package/src/auth/types/user.type.ts +8 -0
- package/src/dto/delete.dto.ts +8 -0
- package/src/dto/update-ids.dto.ts +9 -0
- package/src/dto/with-locale.dto.ts +8 -0
- package/src/index.ts +34 -0
- package/src/locale/dto/create.dto.ts +12 -0
- package/src/locale/dto/delete.dto.ts +8 -0
- package/src/locale/dto/set-enabled.dto.ts +9 -0
- package/src/locale/dto/update.dto.ts +15 -0
- package/src/locale/index.ts +4 -0
- package/src/locale/locale.controller.ts +79 -0
- package/src/locale/locale.decorator.ts +8 -0
- package/src/locale/locale.middleware.ts +34 -0
- package/src/locale/locale.module.ts +23 -0
- package/src/locale/locale.service.spec.ts +193 -0
- package/src/locale/locale.service.ts +366 -0
- package/src/menu/dto/create.dto.ts +25 -0
- package/src/menu/dto/order.dto.ts +8 -0
- package/src/menu/dto/update.dto.ts +19 -0
- package/src/menu/menu.controller.ts +106 -0
- package/src/menu/menu.module.ts +18 -0
- package/src/menu/menu.service.spec.ts +247 -0
- package/src/menu/menu.service.ts +263 -0
- package/src/role/decorators/role.decorator.ts +4 -0
- package/src/role/dto/create.dto.ts +7 -0
- package/src/role/dto/update.dto.ts +4 -0
- package/src/role/guards/role.guard.ts +122 -0
- package/src/role/role.controller.ts +126 -0
- package/src/role/role.module.ts +28 -0
- package/src/role/role.service.spec.ts +417 -0
- package/src/role/role.service.ts +302 -0
- package/src/route/dto/create.dto.ts +13 -0
- package/src/route/dto/update.dto.ts +15 -0
- package/src/route/route.controller.ts +91 -0
- package/src/route/route.module.ts +18 -0
- package/src/route/route.service.spec.ts +299 -0
- package/src/route/route.service.ts +164 -0
- package/src/screen/dto/create.dto.ts +19 -0
- package/src/screen/dto/update.dto.ts +19 -0
- package/src/screen/screen.controller.ts +93 -0
- package/src/screen/screen.module.ts +18 -0
- package/src/screen/screen.service.spec.ts +298 -0
- package/src/screen/screen.service.ts +181 -0
- package/src/setting/dto/create.dto.ts +1 -0
- package/src/setting/dto/setting-user.dto.ts +6 -0
- package/src/setting/dto/setting.dto.ts +17 -0
- package/src/setting/dto/update.dto.ts +3 -0
- package/src/setting/setting.controller.ts +100 -0
- package/src/setting/setting.module.ts +18 -0
- package/src/setting/setting.service.spec.ts +183 -0
- package/src/setting/setting.service.ts +346 -0
- package/src/types/http-method.ts +8 -0
- package/src/user/constants/user.constants.ts +1 -0
- package/src/user/dto/create.dto.ts +24 -0
- package/src/user/dto/update.dto.ts +41 -0
- package/src/user/user.controller.ts +75 -0
- package/src/user/user.module.ts +18 -0
- package/src/user/user.service.spec.ts +294 -0
- package/src/user/user.service.ts +129 -0
- package/tsconfig.lib.json +9 -0
- package/tsconfig.production.json +20 -0
- package/dist/dto/with-locales.dto.d.ts +0 -4
- package/dist/dto/with-locales.dto.d.ts.map +0 -1
- package/dist/dto/with-locales.dto.js.map +0 -1
- package/dist/setting/dto/settings.dto.d.ts +0 -9
- package/dist/setting/dto/settings.dto.d.ts.map +0 -1
- package/dist/setting/dto/settings.dto.js.map +0 -1
- package/dist/setting/settings.controller.d.ts.map +0 -1
- package/dist/setting/settings.controller.js.map +0 -1
- package/dist/setting/settings.module.d.ts +0 -3
- package/dist/setting/settings.module.d.ts.map +0 -1
- package/dist/setting/settings.module.js.map +0 -1
- package/dist/setting/settings.service.d.ts.map +0 -1
- package/dist/setting/settings.service.js.map +0 -1
- package/dist/setting/settings.service.spec.d.ts +0 -2
- package/dist/setting/settings.service.spec.d.ts.map +0 -1
- 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
|
+
*/
|