From c573e9d1c0e7bb543c46206a6505c1f19914d327 Mon Sep 17 00:00:00 2001 From: morteza-mortezai Date: Tue, 7 Apr 2026 14:59:50 +0330 Subject: [PATCH] clean codes --- .../admin/controllers/admin.controller.ts | 2 +- .../admin/entities/adminRole.entity.ts | 1 - src/modules/admin/providers/admin.service.ts | 41 ++++++++----------- 3 files changed, 18 insertions(+), 26 deletions(-) diff --git a/src/modules/admin/controllers/admin.controller.ts b/src/modules/admin/controllers/admin.controller.ts index f621b7d..f94ee74 100644 --- a/src/modules/admin/controllers/admin.controller.ts +++ b/src/modules/admin/controllers/admin.controller.ts @@ -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) diff --git a/src/modules/admin/entities/adminRole.entity.ts b/src/modules/admin/entities/adminRole.entity.ts index 1e001c3..5185ac0 100644 --- a/src/modules/admin/entities/adminRole.entity.ts +++ b/src/modules/admin/entities/adminRole.entity.ts @@ -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'] }) diff --git a/src/modules/admin/providers/admin.service.ts b/src/modules/admin/providers/admin.service.ts index 9c5f803..393f93e 100644 --- a/src/modules/admin/providers/admin.service.ts +++ b/src/modules/admin/providers/admin.service.ts @@ -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 + } }