update
This commit is contained in:
@@ -81,7 +81,7 @@ export class AdminController {
|
|||||||
@ApiOperation({ summary: 'Get admins for a specific shop (Super Admin only)' })
|
@ApiOperation({ summary: 'Get admins for a specific shop (Super Admin only)' })
|
||||||
@ApiParam({ name: 'shopId', description: 'Shop ID' })
|
@ApiParam({ name: 'shopId', description: 'Shop ID' })
|
||||||
getAdminForRestaurant(@Param('shopId') shopId: string): Promise<Admin[]> {
|
getAdminForRestaurant(@Param('shopId') shopId: string): Promise<Admin[]> {
|
||||||
return this.adminService.getShopAdminsBySuperAdmin(shopId);
|
return this.adminService.findAllByShopId(shopId);
|
||||||
}
|
}
|
||||||
|
|
||||||
@UseGuards(SuperAdminAuthGuard)
|
@UseGuards(SuperAdminAuthGuard)
|
||||||
|
|||||||
@@ -5,13 +5,12 @@ import { Admin } from '../entities/admin.entity';
|
|||||||
import { Role } from '../../roles/entities/role.entity';
|
import { Role } from '../../roles/entities/role.entity';
|
||||||
import { Shop } from '../../shops/entities/shop.entity';
|
import { Shop } from '../../shops/entities/shop.entity';
|
||||||
import { EntityManager } from '@mikro-orm/postgresql';
|
import { EntityManager } from '@mikro-orm/postgresql';
|
||||||
import { CacheService } from '../../utils/cache.service';
|
|
||||||
import { CreateMyShopAdminDto } from '../dto/create-shop-admin.dto';
|
import { CreateMyShopAdminDto } from '../dto/create-shop-admin.dto';
|
||||||
import { UpdateAdminDto } from '../dto/update-admin.dto';
|
import { UpdateAdminDto } from '../dto/update-admin.dto';
|
||||||
import { AdminRole } from '../entities/adminRole.entity';
|
import { AdminRole } from '../entities/adminRole.entity';
|
||||||
import { normalizePhone } from '../../utils/phone.util';
|
import { normalizePhone } from '../../utils/phone.util';
|
||||||
import { AdminRepository } from '../repositories/admin.repository';
|
import { AdminRepository } from '../repositories/admin.repository';
|
||||||
import { AdminMessage, RoleMessage } from 'src/common/enums/message.enum';
|
import { AdminMessage } from 'src/common/enums/message.enum';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class AdminService {
|
export class AdminService {
|
||||||
@@ -20,7 +19,6 @@ export class AdminService {
|
|||||||
@InjectRepository(AdminRole)
|
@InjectRepository(AdminRole)
|
||||||
private readonly adminRoleRepository: EntityRepository<AdminRole>,
|
private readonly adminRoleRepository: EntityRepository<AdminRole>,
|
||||||
private readonly em: EntityManager,
|
private readonly em: EntityManager,
|
||||||
private readonly cacheService: CacheService,
|
|
||||||
) { }
|
) { }
|
||||||
|
|
||||||
async findByPhone(phone: string): Promise<Admin | null> {
|
async findByPhone(phone: string): Promise<Admin | null> {
|
||||||
@@ -43,9 +41,10 @@ export class AdminService {
|
|||||||
return { ...adminPlain, role: adminRole?.role };
|
return { ...adminPlain, role: adminRole?.role };
|
||||||
}
|
}
|
||||||
|
|
||||||
async findAllByShopId(shopId: string): Promise<Admin[]> {
|
// async findAllByShopId(shopId: string): Promise<Admin[]> {
|
||||||
return this.adminRepository.find({ roles: { shop: { id: shopId } } }, { populate: ['roles', 'roles.role'] });
|
// return this.adminRepository.find({ roles: { shop: { id: shopId } } },
|
||||||
}
|
// { populate: ['roles', 'roles.role'], });
|
||||||
|
// }
|
||||||
|
|
||||||
async createAdminForShop(shopId: string, dto: CreateMyShopAdminDto) {
|
async createAdminForShop(shopId: string, dto: CreateMyShopAdminDto) {
|
||||||
const { phone, firstName, lastName, roleId } = dto;
|
const { phone, firstName, lastName, roleId } = dto;
|
||||||
@@ -134,11 +133,14 @@ export class AdminService {
|
|||||||
return admin;
|
return admin;
|
||||||
}
|
}
|
||||||
|
|
||||||
async getShopAdminsBySuperAdmin(shopId: string): Promise<Admin[]> {
|
async findAllByShopId(shopId: string): Promise<Admin[]> {
|
||||||
const shop = await this.em.findOne(Shop, { id: shopId });
|
const shop = await this.em.findOne(Shop, { id: shopId });
|
||||||
if (!shop) throw new NotFoundException(AdminMessage.SHOP_NOT_FOUND);
|
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> {
|
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) {
|
if (!admin) {
|
||||||
throw new BadRequestException(AdminMessage.ADMIN_NOT_FOUND);
|
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;
|
return admin;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user