return admin role
This commit is contained in:
@@ -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;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ export class AdminService {
|
|||||||
private readonly adminRoleRepository: EntityRepository<AdminRole>,
|
private readonly adminRoleRepository: EntityRepository<AdminRole>,
|
||||||
private readonly em: EntityManager,
|
private readonly em: EntityManager,
|
||||||
private readonly cacheService: CacheService,
|
private readonly cacheService: CacheService,
|
||||||
) {}
|
) { }
|
||||||
|
|
||||||
async findByPhone(phone: string): Promise<Admin | null> {
|
async findByPhone(phone: string): Promise<Admin | null> {
|
||||||
const normalizedPhone = normalizePhone(phone);
|
const normalizedPhone = normalizePhone(phone);
|
||||||
@@ -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 || '',
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user