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); this._phone = normalizePhone(value);
} }
// Add the new role property
@OneToMany(() => AdminRole, adminRole => adminRole.admin, { @OneToMany(() => AdminRole, adminRole => adminRole.admin, {
cascade: [Cascade.ALL], cascade: [Cascade.ALL],
orphanRemoval: true, orphanRemoval: true,
}) })
roles = new Collection<AdminRole>(this); roles = new Collection<AdminRole>(this);
/** Restaurant-specific role (set when fetched in restaurant context, not persisted) */
role?: AdminRole;
} }
+4 -1
View File
@@ -30,8 +30,11 @@ export class AdminService {
async findById(adminId: string, restId: string): Promise<Admin | null> { async findById(adminId: string, restId: string): Promise<Admin | null> {
const admin = await this.adminRepository.findOne( const admin = await this.adminRepository.findOne(
{ id: adminId, roles: { restaurant: { id: restId } } }, { 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; return admin;
} }
@@ -23,8 +23,9 @@ export class AdminDetailTransformer {
return null; return null;
} }
// Extract role information // Extract role information (prefer restaurant-specific role when set)
const role = admin.roles.getItems().find(r => r.role)?.role; const adminRole = admin.role ?? admin.roles.getItems().find(r => r.role);
const role = adminRole?.role;
if (!role) { if (!role) {
return null; return null;
} }
@@ -39,9 +40,9 @@ export class AdminDetailTransformer {
}, },
permissions: role.permissions.getItems().map(p => p.name) || [], permissions: role.permissions.getItems().map(p => p.name) || [],
restaurant: { restaurant: {
id: role.restaurant?.id || '', id: adminRole?.restaurant?.id || role.restaurant?.id || '',
name: role.restaurant?.name || '', name: adminRole?.restaurant?.name || role.restaurant?.name || '',
slug: role.restaurant?.slug || '', slug: adminRole?.restaurant?.slug || role.restaurant?.slug || '',
}, },
}; };
} }