Refactor RolesController and RolesService to include restaurant-specific handling by adding RestId parameter to relevant methods; remove optional restaurant ID properties from DTOs for cleaner design; implement soft delete logic in remove method to ensure proper role management.
This commit is contained in:
@@ -8,6 +8,7 @@ import { EntityManager } from '@mikro-orm/postgresql';
|
||||
import { CreateRoleDto } from '../dto/create-role.dto';
|
||||
import { UpdateRoleDto } from '../dto/update-role.dto';
|
||||
import { FindRolesDto } from '../dto/find-roles.dto';
|
||||
import { RolePermission } from '../entities/rolePermission.entity';
|
||||
|
||||
@Injectable()
|
||||
export class RolesService {
|
||||
@@ -19,8 +20,8 @@ export class RolesService {
|
||||
private readonly em: EntityManager,
|
||||
) {}
|
||||
|
||||
async create(dto: CreateRoleDto) {
|
||||
const { name, title, restId, permissionIds } = dto;
|
||||
async create(restId: string, dto: CreateRoleDto) {
|
||||
const { name, title, permissionIds } = dto;
|
||||
|
||||
// Check if role already exists
|
||||
const existing = await this.roleRepository.findOne({ name, restaurant: restId ? { id: restId } : null });
|
||||
@@ -55,8 +56,8 @@ export class RolesService {
|
||||
return role;
|
||||
}
|
||||
|
||||
async findAll(dto: FindRolesDto) {
|
||||
const { name, restId, page = 1, limit = 20 } = dto;
|
||||
async findAll(restId: string, dto: FindRolesDto) {
|
||||
const { name, page = 1, limit = 20 } = dto;
|
||||
const offset = (page - 1) * limit;
|
||||
|
||||
// Build query dynamically
|
||||
@@ -86,16 +87,22 @@ export class RolesService {
|
||||
};
|
||||
}
|
||||
|
||||
async findOne(id: string) {
|
||||
const role = await this.roleRepository.findOne({ id }, { populate: ['permissions', 'restaurant'] });
|
||||
async findOne(restId: string, id: string) {
|
||||
const role = await this.roleRepository.findOne(
|
||||
{ id, restaurant: { id: restId } },
|
||||
{ populate: ['permissions', 'restaurant'] },
|
||||
);
|
||||
if (!role) {
|
||||
throw new NotFoundException('Role not found');
|
||||
}
|
||||
return role;
|
||||
}
|
||||
|
||||
async update(id: string, dto: UpdateRoleDto) {
|
||||
const role = await this.roleRepository.findOne({ id });
|
||||
async update(restId: string, id: string, dto: UpdateRoleDto) {
|
||||
const role = await this.roleRepository.findOne(
|
||||
{ id, restaurant: { id: restId } },
|
||||
{ populate: ['permissions', 'restaurant'] },
|
||||
);
|
||||
if (!role) {
|
||||
throw new NotFoundException('Role not found');
|
||||
}
|
||||
@@ -122,13 +129,23 @@ export class RolesService {
|
||||
return role;
|
||||
}
|
||||
|
||||
async remove(id: string) {
|
||||
const role = await this.roleRepository.findOne({ id });
|
||||
async remove(restId: string, id: string) {
|
||||
const role = await this.roleRepository.findOne(
|
||||
{ id, restaurant: { id: restId } },
|
||||
{ populate: ['permissions', 'restaurant'] },
|
||||
);
|
||||
if (!role) {
|
||||
throw new NotFoundException('Role not found');
|
||||
}
|
||||
if (!role.admins.isEmpty()) {
|
||||
throw new BadRequestException('Role has admins');
|
||||
}
|
||||
|
||||
await this.roleRepository.nativeUpdate({ id }, { deletedAt: new Date() });
|
||||
return { message: 'Role deleted successfully' };
|
||||
// Hard delete pivot table entries (role_permissions) before soft deleting the role
|
||||
await this.em.nativeDelete(RolePermission, { role: { id: role.id } });
|
||||
|
||||
// Soft delete the role
|
||||
role.deletedAt = new Date();
|
||||
return this.em.persistAndFlush(role);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user