return admin role

This commit is contained in:
2026-02-26 18:02:09 +03:30
parent f4ffcd254a
commit b4de4a82e0
3 changed files with 14 additions and 8 deletions
+3 -1
View File
@@ -22,10 +22,12 @@ export class Admin extends BaseEntity {
this._phone = normalizePhone(value);
}
// Add the new role property
@OneToMany(() => AdminRole, adminRole => adminRole.admin, {
cascade: [Cascade.ALL],
orphanRemoval: true,
})
roles = new Collection<AdminRole>(this);
/** Restaurant-specific role (set when fetched in restaurant context, not persisted) */
role?: AdminRole;
}
+5 -2
View File
@@ -20,7 +20,7 @@ export class AdminService {
private readonly adminRoleRepository: EntityRepository<AdminRole>,
private readonly em: EntityManager,
private readonly cacheService: CacheService,
) {}
) { }
async findByPhone(phone: string): Promise<Admin | null> {
const normalizedPhone = normalizePhone(phone);
@@ -30,8 +30,11 @@ export class AdminService {
async findById(adminId: string, restId: string): Promise<Admin | null> {
const admin = await this.adminRepository.findOne(
{ id: adminId, roles: { restaurant: { id: restId } } },
{ populate: ['roles', 'roles.role'] },
{ populate: ['roles', 'roles.role', 'roles.restaurant'] },
);
if (!admin) return null;
const adminRole = admin.roles.getItems().find(r => r.restaurant?.id === restId);
admin.role = adminRole;
return admin;
}
@@ -23,8 +23,9 @@ export class AdminDetailTransformer {
return null;
}
// Extract role information
const role = admin.roles.getItems().find(r => r.role)?.role;
// Extract role information (prefer restaurant-specific role when set)
const adminRole = admin.role ?? admin.roles.getItems().find(r => r.role);
const role = adminRole?.role;
if (!role) {
return null;
}
@@ -39,9 +40,9 @@ export class AdminDetailTransformer {
},
permissions: role.permissions.getItems().map(p => p.name) || [],
restaurant: {
id: role.restaurant?.id || '',
name: role.restaurant?.name || '',
slug: role.restaurant?.slug || '',
id: adminRole?.restaurant?.id || role.restaurant?.id || '',
name: adminRole?.restaurant?.name || role.restaurant?.name || '',
slug: adminRole?.restaurant?.slug || role.restaurant?.slug || '',
},
};
}