Enhance role management by adding 'title' property to CreateRoleDto, UpdateRoleDto, and Role entity; update RoleService to handle title in create and update operations; modify DatabaseSeeder to include titles for predefined roles.

This commit is contained in:
2025-11-18 10:24:45 +03:30
parent 7bc94b4126
commit 6f22dddcec
7 changed files with 342 additions and 8 deletions
+5
View File
@@ -7,6 +7,11 @@ export class CreateRoleDto {
@IsString()
name!: string;
@ApiProperty({ description: 'Role title' })
@IsNotEmpty()
@IsString()
title!: string;
@ApiProperty({ description: 'Restaurant id (optional)', required: false })
@IsOptional()
@IsString()
+5
View File
@@ -7,6 +7,11 @@ export class UpdateRoleDto {
@IsString()
name?: string;
@ApiProperty({ description: 'Role title', required: false })
@IsOptional()
@IsString()
title?: string;
@ApiProperty({ description: 'List of permission IDs', isArray: true, required: false })
@IsOptional()
@IsArray()
@@ -12,6 +12,9 @@ export class Role extends BaseEntity {
@Property()
name!: string;
@Property()
title!: string;
@ManyToMany({ entity: () => Permission, pivotEntity: () => RolePermission, inversedBy: p => p.roles })
permissions = new Collection<Permission>(this);
+12 -3
View File
@@ -20,7 +20,7 @@ export class RoleService {
) {}
async create(dto: CreateRoleDto) {
const { name, restId, permissionIds } = dto;
const { name, title, restId, permissionIds } = dto;
// Check if role already exists
const existing = await this.roleRepository.findOne({ name, restaurant: restId ? { id: restId } : null });
@@ -38,6 +38,7 @@ export class RoleService {
const role = this.roleRepository.create({
name,
title,
restaurant,
});
@@ -68,10 +69,14 @@ export class RoleService {
populate: ['permissions', 'restaurant'],
});
// Filter by name in memory if provided
// Filter by name or title in memory if provided
let filtered = roles;
if (name) {
filtered = roles.filter(r => r.name.toLowerCase().includes(name.toLowerCase()));
filtered = roles.filter(
r =>
r.name.toLowerCase().includes(name.toLowerCase()) ||
r.title.toLowerCase().includes(name.toLowerCase()),
);
}
return {
@@ -101,6 +106,10 @@ export class RoleService {
role.name = dto.name;
}
if (dto.title) {
role.title = dto.title;
}
if (dto.permissionIds && dto.permissionIds.length >= 0) {
// Clear existing permissions and add new ones
role.permissions.removeAll();