clean codes
This commit is contained in:
@@ -75,7 +75,7 @@ export class AdminController {
|
||||
@ApiOperation({ summary: 'Get admins for a specific restaurant (Super Admin only)' })
|
||||
@ApiParam({ name: 'restaurantId', description: 'Restaurant ID' })
|
||||
getAdminForRestaurant(@Param('restaurantId') restaurantId: string) {
|
||||
return this.adminService.getAdminsForRestaurantBySuperAdmin(restaurantId);
|
||||
return this.adminService.findAllByRestaurantId(restaurantId);
|
||||
}
|
||||
|
||||
@UseGuards(SuperAdminAuthGuard)
|
||||
|
||||
@@ -5,7 +5,6 @@ import { Restaurant } from 'src/modules/restaurants/entities/restaurant.entity';
|
||||
import { Admin } from './admin.entity';
|
||||
|
||||
@Entity({ tableName: 'admin_roles' })
|
||||
@Unique({ properties: ['admin', 'restaurant'] })
|
||||
@Index({ properties: ['admin', 'restaurant'] })
|
||||
@Index({ properties: ['admin'] })
|
||||
@Index({ properties: ['restaurant'] })
|
||||
|
||||
@@ -28,21 +28,16 @@ export class AdminService {
|
||||
adminId: string,
|
||||
restId: string,
|
||||
) {
|
||||
const admin = await this.adminRepository.findOne(
|
||||
{ id: adminId, roles: { restaurant: { id: restId } } },
|
||||
{ populate: ['roles'], exclude: ['roles'] },
|
||||
);
|
||||
if (!admin) {
|
||||
throw new NotFoundException('Admin not found');
|
||||
}
|
||||
const admin = await this.findOneByIdOrFail(adminId)
|
||||
const adminPlain = wrap(admin).toObject();
|
||||
const adminRole = await this.adminRoleRepository.findOne({ admin: admin, restaurant: { id: restId } });
|
||||
const adminRole = await this.adminRoleRepository.findOne({ admin, restaurant: { id: restId } });
|
||||
|
||||
return { ...adminPlain, role: adminRole?.role };
|
||||
}
|
||||
|
||||
async findAllByRestaurantId(restId: string) {
|
||||
return this.adminRepository.find({ roles: { restaurant: { id: restId } } }, { populate: ['roles', 'roles.role'] });
|
||||
return this.adminRepository.find({ roles: { restaurant: { id: restId } } },
|
||||
{ populate: ['roles', 'roles.role'], populateFilter: { roles: { restaurant: { id: restId } } } });
|
||||
}
|
||||
|
||||
async createAdminForMyRestaurant(restId: string, dto: CreateMyRestaurantAdminDto) {
|
||||
@@ -125,22 +120,8 @@ export class AdminService {
|
||||
return admin;
|
||||
}
|
||||
|
||||
async getAdminsForRestaurantBySuperAdmin(restId: string) {
|
||||
const restaurant = await this.em.findOne(Restaurant, { id: restId });
|
||||
if (!restaurant) throw new NotFoundException('Restaurant not found');
|
||||
|
||||
return this.adminRepository.find({ roles: { restaurant: restaurant } }, { populate: ['roles', 'roles.role'] });
|
||||
}
|
||||
|
||||
async update(adminId: string, restId: string, dto: UpdateAdminDto) {
|
||||
const admin = await this.adminRepository.findOne(
|
||||
{ id: adminId, roles: { restaurant: { id: restId } } },
|
||||
{ populate: ['roles', 'roles.role', 'roles.restaurant'] },
|
||||
);
|
||||
|
||||
if (!admin) {
|
||||
throw new NotFoundException('Admin not found');
|
||||
}
|
||||
const admin = await this.findOneByIdOrFail(adminId)
|
||||
|
||||
const { roleId, ...rest } = dto;
|
||||
|
||||
@@ -200,4 +181,16 @@ export class AdminService {
|
||||
}
|
||||
return this.em.removeAndFlush(adminRole);
|
||||
}
|
||||
|
||||
async findOneByIdOrFail(adminId: string) {
|
||||
const admin = await this.adminRepository.findOne(
|
||||
{ id: adminId },
|
||||
{ populate: ['roles', 'roles.role', 'roles.restaurant'] },
|
||||
);
|
||||
|
||||
if (!admin) {
|
||||
throw new NotFoundException('Admin not found');
|
||||
}
|
||||
return admin
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user