From 7dd1b61cac434f9a7f40709521ca6621f3a83188 Mon Sep 17 00:00:00 2001 From: morteza-mortezai Date: Sun, 23 Nov 2025 12:22:17 +0330 Subject: [PATCH] update admin --- .../admin/controllers/admin.controller.ts | 16 ++++- src/modules/admin/dto/update-admin.dto.ts | 4 ++ src/modules/admin/providers/admin.service.ts | 62 ++++++++++++++++++- 3 files changed, 79 insertions(+), 3 deletions(-) create mode 100644 src/modules/admin/dto/update-admin.dto.ts diff --git a/src/modules/admin/controllers/admin.controller.ts b/src/modules/admin/controllers/admin.controller.ts index b9df5c7..a689b6e 100644 --- a/src/modules/admin/controllers/admin.controller.ts +++ b/src/modules/admin/controllers/admin.controller.ts @@ -1,10 +1,12 @@ -import { Controller, Post, Body, HttpCode, HttpStatus, Get, UseGuards } from '@nestjs/common'; +import { Controller, Post, Body, HttpCode, HttpStatus, Get, UseGuards, Patch, Param } from '@nestjs/common'; import { AdminService } from '../providers/admin.service'; import { CreateAdminDto } from '../dto/create-admin.dto'; -import { ApiTags, ApiOperation, ApiBody, ApiResponse, ApiBearerAuth } from '@nestjs/swagger'; +import { UpdateAdminDto } from '../dto/update-admin.dto'; +import { ApiTags, ApiOperation, ApiBody, ApiResponse, ApiBearerAuth, ApiParam } from '@nestjs/swagger'; import { AdminAuthGuard } from 'src/modules/auth/guards/adminAuth.guard'; import { RestId } from 'src/common/decorators'; import { AdminId } from 'src/common/decorators/admin-id.decorator'; +import { Admin } from '../entities/admin.entity'; @UseGuards(AdminAuthGuard) @ApiBearerAuth() @@ -44,4 +46,14 @@ export class AdminController { }); return admin; } + + @Patch(':id') + @ApiOperation({ summary: 'Update an admin' }) + @ApiParam({ name: 'id', description: 'Admin ID' }) + @ApiBody({ type: UpdateAdminDto }) + @ApiResponse({ status: 200, description: 'Admin updated successfully' }) + @ApiResponse({ status: 404, description: 'Admin not found' }) + update(@Param('id') id: string, @Body() dto: UpdateAdminDto, @RestId() restId: string): Promise { + return this.adminService.update(id, restId, dto); + } } diff --git a/src/modules/admin/dto/update-admin.dto.ts b/src/modules/admin/dto/update-admin.dto.ts new file mode 100644 index 0000000..5fec45b --- /dev/null +++ b/src/modules/admin/dto/update-admin.dto.ts @@ -0,0 +1,4 @@ +import { PartialType } from '@nestjs/swagger'; +import { CreateAdminDto } from './create-admin.dto'; + +export class UpdateAdminDto extends PartialType(CreateAdminDto) {} diff --git a/src/modules/admin/providers/admin.service.ts b/src/modules/admin/providers/admin.service.ts index 5e36630..0f4e796 100644 --- a/src/modules/admin/providers/admin.service.ts +++ b/src/modules/admin/providers/admin.service.ts @@ -1,4 +1,4 @@ -import { Injectable } from '@nestjs/common'; +import { Injectable, NotFoundException } from '@nestjs/common'; import { InjectRepository } from '@mikro-orm/nestjs'; import { EntityRepository } from '@mikro-orm/core'; import { Admin } from '../entities/admin.entity'; @@ -7,6 +7,8 @@ import { Restaurant } from '../../restaurants/entities/restaurant.entity'; import { EntityManager } from '@mikro-orm/postgresql'; import { CacheService } from '../../utils/cache.service'; import { CreateMyRestaurantAdminDto } from '../dto/create-my-restaurant-admin.dto'; +import { UpdateAdminDto } from '../dto/update-admin.dto'; +import { AdminRole } from '../entities/adminRole.entity'; @Injectable() export class AdminService { @@ -74,4 +76,62 @@ export class AdminService { await this.em.persistAndFlush(admin); return admin; } + + async update(id: string, restId: string, dto: UpdateAdminDto): Promise { + const admin = await this.adminRepository.findOne({ id }, { populate: ['roles', 'roles.role', 'roles.restaurant'] }); + + if (!admin) { + throw new NotFoundException('Admin not found'); + } + + const { roleId, ...rest } = dto; + + // Update scalar fields (firstName, lastName, phone) + if (rest.firstName !== undefined) { + admin.firstName = rest.firstName; + } + if (rest.lastName !== undefined) { + admin.lastName = rest.lastName; + } + if (rest.phone !== undefined) { + admin.phone = rest.phone; + } + + // Update role if roleId is provided + if (roleId) { + const role = await this.em.findOne(Role, { id: roleId }); + if (!role) { + throw new NotFoundException('Role not found'); + } + + const restaurant = await this.em.findOne(Restaurant, { id: restId }); + if (!restaurant) { + throw new NotFoundException('Restaurant not found'); + } + + // Find existing AdminRole for this admin and restaurant + const existingAdminRole = await this.em.findOne(AdminRole, { + admin: { id }, + restaurant: { id: restId }, + }); + + if (existingAdminRole) { + // Update existing role + existingAdminRole.role = role; + await this.em.persistAndFlush(existingAdminRole); + } else { + // Create new AdminRole + const newAdminRole = this.em.create(AdminRole, { + admin, + role, + restaurant, + }); + admin.roles.add(newAdminRole); + await this.em.persistAndFlush(newAdminRole); + } + } + + await this.em.persistAndFlush(admin); + return admin; + } }