fix bug in get one admin

This commit is contained in:
2026-04-07 12:24:07 +03:30
parent 218e1edebe
commit d8a6ec587c
2 changed files with 11 additions and 4 deletions
@@ -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<Admin | null> {
getById(@Param('adminId') adminId: string, @ShopId() shopId: string) {
return this.adminService.findById(adminId, shopId);
}
+10 -3
View File
@@ -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<Admin | null> {
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<Admin[]> {