This commit is contained in:
@@ -707,6 +707,10 @@ export const enum AdminMessage {
|
||||
ROLE_NAME_LENGTH = "نام نقش باید بین ۳ تا ۱۵۰ کاراکتر باشد",
|
||||
ROLE_EXIST = "نقش با این نام قبلا ثبت شده است",
|
||||
ROLE_CREATED = "نقش با موفقیت ایجاد شد",
|
||||
ROLE_NOT_FOUND = "نقشی با این شناسه یافت نشد",
|
||||
ROLE_UPDATED = "نقش با موفقیت به روز رسانی شد",
|
||||
ROLE_DELETED = "نقش با موفقیت حذف شد",
|
||||
ROLE_HAS_USERS = "امکان حذف نقش وجود ندارد زیرا به کاربرانی اختصاص داده شده است",
|
||||
ADMIN_UPDATED = "مدیر با موفقیت به روز رسانی شد",
|
||||
ADMIN_NOT_FOUND = "مدیری با این شناسه یافت نشد",
|
||||
ADMIN_DELETED = "مدیر با موفقیت حذف شد",
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
import { PartialType } from "@nestjs/swagger";
|
||||
|
||||
import { CreateRoleDto } from "./create-role.dto";
|
||||
|
||||
export class UpdateRoleDto extends PartialType(CreateRoleDto) {}
|
||||
@@ -13,6 +13,7 @@ import { CreateRoleDto } from "../DTO/create-role.dto";
|
||||
import { SearchAdminQueryDto } from "../DTO/search-admins-query.dto";
|
||||
import { SearchRolesQueryDto } from "../DTO/search-roles.dto";
|
||||
import { UpdateAdminDto } from "../DTO/update-admin.dto";
|
||||
import { UpdateRoleDto } from "../DTO/update-role.dto";
|
||||
import { RoleEnum } from "../enums/role.enum";
|
||||
import { PermissionsRepository } from "../repositories/permissions.repository";
|
||||
import { RoleRepository } from "../repositories/roles.repository";
|
||||
@@ -211,6 +212,62 @@ export class AdminsService {
|
||||
|
||||
/************************************************************ */
|
||||
|
||||
async getRoleById(id: string) {
|
||||
const role = await this.rolesRepository.findOne({
|
||||
where: { id, isAdmin: true },
|
||||
relations: { permissions: true },
|
||||
});
|
||||
if (!role) throw new BadRequestException(AdminMessage.ROLE_NOT_FOUND);
|
||||
return { role };
|
||||
}
|
||||
|
||||
/************************************************************ */
|
||||
|
||||
async updateRole(id: string, updateDto: UpdateRoleDto) {
|
||||
const role = await this.rolesRepository.findOne({
|
||||
where: { id, isAdmin: true },
|
||||
relations: { permissions: true },
|
||||
});
|
||||
if (!role) throw new BadRequestException(AdminMessage.ROLE_NOT_FOUND);
|
||||
|
||||
if (updateDto.name && updateDto.name !== role.name) {
|
||||
const existRole = await this.rolesRepository.findOne({ where: { name: updateDto.name, id: Not(id) } });
|
||||
if (existRole) throw new BadRequestException(AdminMessage.ROLE_EXIST);
|
||||
role.name = updateDto.name;
|
||||
}
|
||||
|
||||
if (updateDto.permissions) {
|
||||
const permissions = await this.permissionsRepository.findBy({ id: In(updateDto.permissions) });
|
||||
if (permissions.length !== updateDto.permissions.length) throw new BadRequestException(AdminMessage.PERMISSIONS_NOT_FOUND);
|
||||
role.permissions = permissions;
|
||||
}
|
||||
|
||||
await this.rolesRepository.save(role);
|
||||
return {
|
||||
message: AdminMessage.ROLE_UPDATED,
|
||||
role,
|
||||
};
|
||||
}
|
||||
|
||||
/************************************************************ */
|
||||
|
||||
async deleteRole(id: string) {
|
||||
const role = await this.rolesRepository.findOne({ where: { id, isAdmin: true } });
|
||||
if (!role) throw new BadRequestException(AdminMessage.ROLE_NOT_FOUND);
|
||||
|
||||
if (role.name === RoleEnum.SUPER_ADMIN) throw new BadRequestException(AdminMessage.NOT_ALLOWED);
|
||||
|
||||
const userCount = await this.userRepository.count({ where: { roles: { id } } });
|
||||
if (userCount > 0) throw new BadRequestException(AdminMessage.ROLE_HAS_USERS);
|
||||
|
||||
await this.rolesRepository.remove(role);
|
||||
return {
|
||||
message: AdminMessage.ROLE_DELETED,
|
||||
};
|
||||
}
|
||||
|
||||
/************************************************************ */
|
||||
|
||||
async getAdmins(queryDto: SearchAdminQueryDto) {
|
||||
const [users, count] = await this.userRepository.getAdmins(queryDto);
|
||||
|
||||
|
||||
@@ -14,6 +14,7 @@ import { SearchCustomersDto } from "./DTO/search-customers.dto";
|
||||
import { SearchRolesQueryDto } from "./DTO/search-roles.dto";
|
||||
import { UpdateAdminDto } from "./DTO/update-admin.dto";
|
||||
import { UpdateCustomerDto } from "./DTO/update-customer.dto";
|
||||
import { UpdateRoleDto } from "./DTO/update-role.dto";
|
||||
import { UpdateProfileDto } from "./DTO/update-profile.dto";
|
||||
import { CreateUserGroupDto } from "./DTO/user-group.dto";
|
||||
import { EmailVerifyQueryDto } from "./DTO/verify-email-query.dto";
|
||||
@@ -222,6 +223,13 @@ export class UsersController {
|
||||
return this.adminsService.getRoles(queryDto);
|
||||
}
|
||||
|
||||
@ApiOperation({ summary: "get role by id ==> admin route" })
|
||||
@PermissionsDec(PermissionEnum.ADMINS, PermissionEnum.CUSTOMERS)
|
||||
@Get("roles/:id")
|
||||
getRoleById(@Param() paramDto: ParamDto) {
|
||||
return this.adminsService.getRoleById(paramDto.id);
|
||||
}
|
||||
|
||||
@ApiOperation({ summary: "create role ==> admin route" })
|
||||
@PermissionsDec(PermissionEnum.ADMINS, PermissionEnum.CUSTOMERS)
|
||||
@Post("roles")
|
||||
@@ -229,5 +237,17 @@ export class UsersController {
|
||||
return this.adminsService.createRole(createDto);
|
||||
}
|
||||
|
||||
|
||||
@ApiOperation({ summary: "update role ==> admin route" })
|
||||
@PermissionsDec(PermissionEnum.ADMINS, PermissionEnum.CUSTOMERS)
|
||||
@Patch("roles/:id")
|
||||
updateRole(@Param() paramDto: ParamDto, @Body() updateDto: UpdateRoleDto) {
|
||||
return this.adminsService.updateRole(paramDto.id, updateDto);
|
||||
}
|
||||
|
||||
@ApiOperation({ summary: "delete role ==> admin route" })
|
||||
@PermissionsDec(PermissionEnum.ADMINS, PermissionEnum.CUSTOMERS)
|
||||
@Delete("roles/:id")
|
||||
deleteRole(@Param() paramDto: ParamDto) {
|
||||
return this.adminsService.deleteRole(paramDto.id);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user