From a3f0593443ded9f9cbcf3c6adb9ed5a2616aebb7 Mon Sep 17 00:00:00 2001 From: morteza-mortezai Date: Sun, 23 Nov 2025 14:48:58 +0330 Subject: [PATCH] update --- src/modules/admin/admin.module.ts | 3 +- .../admin/controllers/admin.controller.ts | 22 ++++++++++-- .../admin/entities/adminRole.entity.ts | 2 +- src/modules/admin/providers/admin.service.ts | 34 ++++++++++++++----- 4 files changed, 48 insertions(+), 13 deletions(-) diff --git a/src/modules/admin/admin.module.ts b/src/modules/admin/admin.module.ts index 74f3c00..669d4ff 100644 --- a/src/modules/admin/admin.module.ts +++ b/src/modules/admin/admin.module.ts @@ -10,12 +10,13 @@ import { RolePermission } from '../roles/entities/rolePermission.entity'; import { AdminController } from './controllers/admin.controller'; import { UtilsModule } from '../utils/utils.module'; import { RestaurantsModule } from '../restaurants/restaurants.module'; +import { AdminRole } from './entities/adminRole.entity'; @Module({ providers: [AdminService, AdminRepository], controllers: [AdminController], imports: [ - MikroOrmModule.forFeature([Admin, Role, Permission, RolePermission]), + MikroOrmModule.forFeature([Admin, Role, Permission, RolePermission, AdminRole]), JwtModule, UtilsModule, RestaurantsModule, diff --git a/src/modules/admin/controllers/admin.controller.ts b/src/modules/admin/controllers/admin.controller.ts index a689b6e..e0af469 100644 --- a/src/modules/admin/controllers/admin.controller.ts +++ b/src/modules/admin/controllers/admin.controller.ts @@ -1,4 +1,4 @@ -import { Controller, Post, Body, HttpCode, HttpStatus, Get, UseGuards, Patch, Param } from '@nestjs/common'; +import { Controller, Post, Body, HttpCode, HttpStatus, Get, UseGuards, Patch, Param, Delete } from '@nestjs/common'; import { AdminService } from '../providers/admin.service'; import { CreateAdminDto } from '../dto/create-admin.dto'; import { UpdateAdminDto } from '../dto/update-admin.dto'; @@ -26,8 +26,8 @@ export class AdminController { @Get('profile') @ApiOperation({ summary: 'admin' }) @ApiResponse({ status: 201, description: 'Admins' }) - async getMe(@AdminId() adminId: string) { - const admin = await this.adminService.findById(adminId); + async getMe(@AdminId() adminId: string, @RestId() restId: string) { + const admin = await this.adminService.findById(adminId, restId); return admin; } @@ -56,4 +56,20 @@ export class AdminController { update(@Param('id') id: string, @Body() dto: UpdateAdminDto, @RestId() restId: string): Promise { return this.adminService.update(id, restId, dto); } + @Get(':adminId') + @ApiOperation({ summary: 'Get an admin by ID' }) + @ApiParam({ name: 'id', description: 'Admin ID' }) + @ApiResponse({ status: 200, description: 'Admin found successfully' }) + @ApiResponse({ status: 404, description: 'Admin not found' }) + getById(@Param('adminId') adminId: string, @RestId() restId: string): Promise { + return this.adminService.findById(adminId, restId); + } + @Delete(':adminId') + @ApiOperation({ summary: 'Delete an admin by ID' }) + @ApiParam({ name: 'id', description: 'Admin ID' }) + @ApiResponse({ status: 200, description: 'Admin deleted successfully' }) + @ApiResponse({ status: 404, description: 'Admin not found' }) + deleteById(@Param('adminId') adminId: string, @RestId() restId: string): Promise { + return this.adminService.remove(adminId, restId); + } } diff --git a/src/modules/admin/entities/adminRole.entity.ts b/src/modules/admin/entities/adminRole.entity.ts index 788a7b3..4653aad 100644 --- a/src/modules/admin/entities/adminRole.entity.ts +++ b/src/modules/admin/entities/adminRole.entity.ts @@ -5,7 +5,7 @@ import { Restaurant } from 'src/modules/restaurants/entities/restaurant.entity'; import { Admin } from './admin.entity'; @Entity({ tableName: 'admin_roles' }) -@Unique({ properties: ['admin', 'role', 'restaurant'] }) +@Unique({ properties: ['admin', 'restaurant'] }) export class AdminRole extends BaseEntity { @ManyToOne(() => Admin) admin!: Admin; diff --git a/src/modules/admin/providers/admin.service.ts b/src/modules/admin/providers/admin.service.ts index 0f4e796..bbca8ea 100644 --- a/src/modules/admin/providers/admin.service.ts +++ b/src/modules/admin/providers/admin.service.ts @@ -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, + @InjectRepository(AdminRole) + private readonly adminRoleRepository: EntityRepository, 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 { - const admin = await this.adminRepository.findOne({ id }, { populate: ['roles', 'roles.role'] }); + async findById(adminId: string, restId: string): Promise { + 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 { - const admin = await this.adminRepository.findOne({ id }, { populate: ['roles', 'roles.role', 'roles.restaurant'] }); + async update(adminId: string, restId: string, dto: UpdateAdminDto): Promise { + 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 { + 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); + } }