This commit is contained in:
2026-04-19 15:26:46 +03:30
parent fcdf659a31
commit 5d1a6ec901
2 changed files with 15 additions and 18 deletions
@@ -81,7 +81,7 @@ export class AdminController {
@ApiOperation({ summary: 'Get admins for a specific shop (Super Admin only)' })
@ApiParam({ name: 'shopId', description: 'Shop ID' })
getAdminForRestaurant(@Param('shopId') shopId: string): Promise<Admin[]> {
return this.adminService.getShopAdminsBySuperAdmin(shopId);
return this.adminService.findAllByShopId(shopId);
}
@UseGuards(SuperAdminAuthGuard)
+14 -17
View File
@@ -5,13 +5,12 @@ import { Admin } from '../entities/admin.entity';
import { Role } from '../../roles/entities/role.entity';
import { Shop } from '../../shops/entities/shop.entity';
import { EntityManager } from '@mikro-orm/postgresql';
import { CacheService } from '../../utils/cache.service';
import { CreateMyShopAdminDto } from '../dto/create-shop-admin.dto';
import { UpdateAdminDto } from '../dto/update-admin.dto';
import { AdminRole } from '../entities/adminRole.entity';
import { normalizePhone } from '../../utils/phone.util';
import { AdminRepository } from '../repositories/admin.repository';
import { AdminMessage, RoleMessage } from 'src/common/enums/message.enum';
import { AdminMessage } from 'src/common/enums/message.enum';
@Injectable()
export class AdminService {
@@ -20,7 +19,6 @@ export class AdminService {
@InjectRepository(AdminRole)
private readonly adminRoleRepository: EntityRepository<AdminRole>,
private readonly em: EntityManager,
private readonly cacheService: CacheService,
) { }
async findByPhone(phone: string): Promise<Admin | null> {
@@ -43,9 +41,10 @@ export class AdminService {
return { ...adminPlain, role: adminRole?.role };
}
async findAllByShopId(shopId: string): Promise<Admin[]> {
return this.adminRepository.find({ roles: { shop: { id: shopId } } }, { populate: ['roles', 'roles.role'] });
}
// async findAllByShopId(shopId: string): Promise<Admin[]> {
// return this.adminRepository.find({ roles: { shop: { id: shopId } } },
// { populate: ['roles', 'roles.role'], });
// }
async createAdminForShop(shopId: string, dto: CreateMyShopAdminDto) {
const { phone, firstName, lastName, roleId } = dto;
@@ -134,11 +133,14 @@ export class AdminService {
return admin;
}
async getShopAdminsBySuperAdmin(shopId: string): Promise<Admin[]> {
async findAllByShopId(shopId: string): Promise<Admin[]> {
const shop = await this.em.findOne(Shop, { id: shopId });
if (!shop) throw new NotFoundException(AdminMessage.SHOP_NOT_FOUND);
return this.adminRepository.find({ roles: { shop: shop } }, { populate: ['roles', 'roles.role'] });
return this.adminRepository.find({ roles: { shop: shop } }, {
populate: ['roles', 'roles.role'],
populateWhere: { roles: { shop: { id: shopId } } }
});
}
async update(adminId: string, shopId: string, dto: UpdateAdminDto): Promise<Admin> {
@@ -230,21 +232,16 @@ export class AdminService {
},
},
},
{ populate: ['roles', 'roles.role', 'roles.shop'] },
{
populate: ['roles', 'roles.role', 'roles.shop'],
populateWhere: { roles: { shop: { slug: shopSlug } } }
},
);
if (!admin) {
throw new BadRequestException(AdminMessage.ADMIN_NOT_FOUND);
}
// Ensure all roles are populated
// await adminRole.admin.roles.loadItems();
// for (const role of adminRole.admin.roles.getItems()) {
// if (role.role && role.role.permissions && !role.role.permissions.isInitialized()) {
// await role.role.permissions.loadItems();
// }
// }
return admin;
}