This commit is contained in:
2025-11-23 14:48:58 +03:30
parent 7dd1b61cac
commit a3f0593443
4 changed files with 48 additions and 13 deletions
+26 -8
View File
@@ -1,4 +1,4 @@
import { Injectable, NotFoundException } from '@nestjs/common';
import { ConflictException, Injectable, NotFoundException } from '@nestjs/common';
import { InjectRepository } from '@mikro-orm/nestjs';
import { EntityRepository } from '@mikro-orm/core';
import { Admin } from '../entities/admin.entity';
@@ -15,6 +15,8 @@ export class AdminService {
constructor(
@InjectRepository(Admin)
private readonly adminRepository: EntityRepository<Admin>,
@InjectRepository(AdminRole)
private readonly adminRoleRepository: EntityRepository<AdminRole>,
private readonly em: EntityManager,
private readonly cacheService: CacheService,
) {}
@@ -23,8 +25,11 @@ export class AdminService {
return this.adminRepository.findOne({ phone });
}
async findById(id: string): Promise<Admin | null> {
const admin = await this.adminRepository.findOne({ id }, { populate: ['roles', 'roles.role'] });
async findById(adminId: string, restId: string): Promise<Admin | null> {
const admin = await this.adminRepository.findOne(
{ id: adminId, roles: { restaurant: { id: restId } } },
{ populate: ['roles', 'roles.role'] },
);
return admin;
}
@@ -36,10 +41,10 @@ export class AdminService {
const { phone, firstName, lastName, roleId } = dto;
const currentAdmin = await this.adminRepository.findOne({
phone,
roles: { role: { id: roleId }, restaurant: { id: restId } },
roles: { restaurant: { id: restId } },
});
if (currentAdmin) {
return currentAdmin;
throw new ConflictException('This Phone Number is already Admin for this restaurant');
}
const role = await this.em.findOne(Role, { id: roleId });
if (!role) throw new Error('Role not found');
@@ -77,8 +82,11 @@ export class AdminService {
return admin;
}
async update(id: string, restId: string, dto: UpdateAdminDto): Promise<Admin> {
const admin = await this.adminRepository.findOne({ id }, { populate: ['roles', 'roles.role', 'roles.restaurant'] });
async update(adminId: string, restId: string, dto: UpdateAdminDto): Promise<Admin> {
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');
@@ -111,7 +119,7 @@ export class AdminService {
// Find existing AdminRole for this admin and restaurant
const existingAdminRole = await this.em.findOne(AdminRole, {
admin: { id },
admin: { id: adminId },
restaurant: { id: restId },
});
@@ -134,4 +142,14 @@ export class AdminService {
await this.em.persistAndFlush(admin);
return admin;
}
async remove(adminId: string, restId: string): Promise<void> {
const adminRole = await this.adminRoleRepository.findOne({ admin: { id: adminId }, restaurant: { id: restId } });
if (!adminRole) {
throw new NotFoundException('Admin role not found');
}
await this.em.nativeDelete(AdminRole, { id: adminRole.id });
await this.em.persistAndFlush(adminRole);
}
}