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