From d8a6ec587ca576b52cc9b5a2534fd96f6b16a211 Mon Sep 17 00:00:00 2001 From: morteza-mortezai Date: Tue, 7 Apr 2026 12:24:07 +0330 Subject: [PATCH] fix bug in get one admin --- src/modules/admin/controllers/admin.controller.ts | 2 +- src/modules/admin/providers/admin.service.ts | 13 ++++++++++--- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/modules/admin/controllers/admin.controller.ts b/src/modules/admin/controllers/admin.controller.ts index 540c207..59ac46e 100644 --- a/src/modules/admin/controllers/admin.controller.ts +++ b/src/modules/admin/controllers/admin.controller.ts @@ -62,7 +62,7 @@ export class AdminController { @Permissions(Permission.MANAGE_ADMINS) @ApiOperation({ summary: 'Get an admin by ID' }) @ApiParam({ name: 'adminId', description: 'Admin ID' }) - getById(@Param('adminId') adminId: string, @ShopId() shopId: string): Promise { + getById(@Param('adminId') adminId: string, @ShopId() shopId: string) { return this.adminService.findById(adminId, shopId); } diff --git a/src/modules/admin/providers/admin.service.ts b/src/modules/admin/providers/admin.service.ts index 1db6b17..402ce7a 100644 --- a/src/modules/admin/providers/admin.service.ts +++ b/src/modules/admin/providers/admin.service.ts @@ -1,6 +1,6 @@ import { BadRequestException, ConflictException, Injectable, NotFoundException } from '@nestjs/common'; import { InjectRepository } from '@mikro-orm/nestjs'; -import { EntityRepository } from '@mikro-orm/core'; +import { EntityRepository, wrap } from '@mikro-orm/core'; import { Admin } from '../entities/admin.entity'; import { Role } from '../../roles/entities/role.entity'; import { Shop } from '../../shops/entities/shop.entity'; @@ -28,12 +28,19 @@ export class AdminService { return this.adminRepository.findOne({ phone: normalizedPhone }); } - async findById(adminId: string, shopId: string): Promise { + async findById(adminId: string, shopId: string) { const admin = await this.adminRepository.findOne( { id: adminId, roles: { shop: { id: shopId } } }, { populate: ['roles', 'roles.role'] }, ); - return admin; + if (!admin) { + throw new NotFoundException('Admin not found'); + } + + const adminPlain = wrap(admin).toObject(); + const adminRole = await this.adminRoleRepository.findOne({ admin: admin, shop: { id: shopId } }); + + return { ...adminPlain, role: adminRole?.role }; } async findAllByShopId(shopId: string): Promise {