From d894bae75639463bcc1f2a22c6b718347178b46c Mon Sep 17 00:00:00 2001 From: morteza-mortezai Date: Sun, 23 Nov 2025 15:25:36 +0330 Subject: [PATCH] shipment methods --- src/modules/admin/providers/admin.service.ts | 23 +++++++----- .../controllers/shipment-method.controller.ts | 35 +++++++++++++++++++ .../providers/shipment-methods.service.ts | 21 +++++++++++ .../shipment-method.repository.ts | 10 ++++++ src/modules/restaurants/restaurants.module.ts | 9 +++-- 5 files changed, 87 insertions(+), 11 deletions(-) create mode 100644 src/modules/restaurants/controllers/shipment-method.controller.ts create mode 100644 src/modules/restaurants/providers/shipment-methods.service.ts create mode 100644 src/modules/restaurants/repositories/shipment-method.repository.ts diff --git a/src/modules/admin/providers/admin.service.ts b/src/modules/admin/providers/admin.service.ts index bbca8ea..24c13df 100644 --- a/src/modules/admin/providers/admin.service.ts +++ b/src/modules/admin/providers/admin.service.ts @@ -41,16 +41,21 @@ export class AdminService { const { phone, firstName, lastName, roleId } = dto; const currentAdmin = await this.adminRepository.findOne({ phone, - roles: { restaurant: { id: restId } }, }); - if (currentAdmin) { - throw new ConflictException('This Phone Number is already Admin for this restaurant'); + if (!currentAdmin) { + await this.adminRepository.findOne({ + phone, + firstName, + lastName, + }); } const role = await this.em.findOne(Role, { id: roleId }); if (!role) throw new Error('Role not found'); + const restaurant = await this.em.findOne(Restaurant, { id: restId }); if (!restaurant) throw new Error('Restaurant not found'); const admin = this.adminRepository.create({ phone, firstName, lastName, roles: [{ role, restaurant }] }); + await this.em.persistAndFlush(admin); return admin; } @@ -101,7 +106,11 @@ export class AdminService { if (rest.lastName !== undefined) { admin.lastName = rest.lastName; } - if (rest.phone !== undefined) { + if (rest.phone !== undefined && rest.phone !== admin.phone) { + const exists = await this.adminRepository.findOne({ phone: rest.phone }); + if (exists) { + throw new ConflictException('This Phone Number is already Admin for this restaurant'); + } admin.phone = rest.phone; } @@ -126,7 +135,6 @@ export class AdminService { if (existingAdminRole) { // Update existing role existingAdminRole.role = role; - await this.em.persistAndFlush(existingAdminRole); } else { // Create new AdminRole const newAdminRole = this.em.create(AdminRole, { @@ -135,7 +143,6 @@ export class AdminService { restaurant, }); admin.roles.add(newAdminRole); - await this.em.persistAndFlush(newAdminRole); } } @@ -148,8 +155,6 @@ export class AdminService { if (!adminRole) { throw new NotFoundException('Admin role not found'); } - - await this.em.nativeDelete(AdminRole, { id: adminRole.id }); - await this.em.persistAndFlush(adminRole); + return this.em.removeAndFlush(adminRole); } } diff --git a/src/modules/restaurants/controllers/shipment-method.controller.ts b/src/modules/restaurants/controllers/shipment-method.controller.ts new file mode 100644 index 0000000..8ecfc85 --- /dev/null +++ b/src/modules/restaurants/controllers/shipment-method.controller.ts @@ -0,0 +1,35 @@ +import { Controller, Get, Param, UseGuards } from '@nestjs/common'; +import { ApiTags, ApiOperation, ApiOkResponse, ApiNotFoundResponse, ApiParam, ApiBearerAuth } from '@nestjs/swagger'; +import { AdminAuthGuard } from 'src/modules/auth/guards/adminAuth.guard'; +import { ShipmentMethod } from '../entities/shipment-method.entity'; +import { ShipmentMethodsService } from '../providers/shipment-methods.service'; + +@UseGuards(AdminAuthGuard) +@ApiBearerAuth() +@ApiTags('admin/shipment-methods') +@Controller('admin/shipment-methods') +export class ShipmentMethodController { + constructor(private readonly shipmentMethodsService: ShipmentMethodsService) {} + + @Get() + @ApiOperation({ summary: 'Get all shipment methods' }) + @ApiOkResponse({ + description: 'List of shipment methods', + type: [ShipmentMethod], + }) + findAll() { + return this.shipmentMethodsService.findAll(); + } + + @Get(':shipmentMethodId') + @ApiOperation({ summary: 'Get a shipment method by shipment method ID' }) + @ApiParam({ name: 'shipmentMethodId', description: 'Shipment method ID' }) + @ApiOkResponse({ + description: 'shipment method details', + type: ShipmentMethod, + }) + @ApiNotFoundResponse({ description: 'Shipment method not found' }) + findOne(@Param('shipmentMethodId') shipmentMethodId: string) { + return this.shipmentMethodsService.findOne(shipmentMethodId); + } +} diff --git a/src/modules/restaurants/providers/shipment-methods.service.ts b/src/modules/restaurants/providers/shipment-methods.service.ts new file mode 100644 index 0000000..67bff0e --- /dev/null +++ b/src/modules/restaurants/providers/shipment-methods.service.ts @@ -0,0 +1,21 @@ +import { Injectable } from '@nestjs/common'; + +import { EntityManager } from '@mikro-orm/postgresql'; + +import { ShipmentMethodRepository } from '../repositories/shipment-method.repository'; + +@Injectable() +export class ShipmentMethodsService { + constructor( + private readonly em: EntityManager, + private readonly shipmentMethodRepository: ShipmentMethodRepository, + ) {} + + findAll() { + return this.shipmentMethodRepository.findAll(); + } + + findOne(id: string) { + return this.shipmentMethodRepository.findOne({ id }); + } +} diff --git a/src/modules/restaurants/repositories/shipment-method.repository.ts b/src/modules/restaurants/repositories/shipment-method.repository.ts new file mode 100644 index 0000000..356c128 --- /dev/null +++ b/src/modules/restaurants/repositories/shipment-method.repository.ts @@ -0,0 +1,10 @@ +import { EntityManager, EntityRepository } from '@mikro-orm/postgresql'; + import { Injectable } from '@nestjs/common'; +import { ShipmentMethod } from '../entities/shipment-method.entity'; + +@Injectable() +export class ShipmentMethodRepository extends EntityRepository { + constructor(readonly em: EntityManager) { + super(em, ShipmentMethod); + } +} diff --git a/src/modules/restaurants/restaurants.module.ts b/src/modules/restaurants/restaurants.module.ts index 2199ccd..d459c66 100644 --- a/src/modules/restaurants/restaurants.module.ts +++ b/src/modules/restaurants/restaurants.module.ts @@ -18,7 +18,9 @@ import { RestaurantShipmentMethodRepository } from './repositories/restaurant-sh import { JwtModule } from '@nestjs/jwt'; import { AuthModule } from '../auth/auth.module'; import { PublicRestaurantShipmentMethodController } from './controllers/public-restaurant-shipment-method.controller'; - +import { ShipmentMethodController } from './controllers/shipment-method.controller'; +import { ShipmentMethodsService } from './providers/shipment-methods.service'; +import { ShipmentMethodRepository } from './repositories/shipment-method.repository'; @Module({ controllers: [ RestaurantsController, @@ -27,6 +29,7 @@ import { PublicRestaurantShipmentMethodController } from './controllers/public-r PublicScheduleController, RestaurantShipmentMethodController, PublicRestaurantShipmentMethodController, + ShipmentMethodController, ], providers: [ RestaurantsService, @@ -35,12 +38,14 @@ import { PublicRestaurantShipmentMethodController } from './controllers/public-r ScheduleService, RestaurantShipmentMethodService, RestaurantShipmentMethodRepository, + ShipmentMethodsService, + ShipmentMethodRepository, ], imports: [ MikroOrmModule.forFeature([Restaurant, Schedule, ShipmentMethod, RestaurantShipmentMethod]), JwtModule, forwardRef(() => AuthModule), ], - exports: [RestRepository, ScheduleRepository, ScheduleService], + exports: [RestRepository, ScheduleRepository, ScheduleService, ShipmentMethodsService], }) export class RestaurantsModule {}