shipment methods

This commit is contained in:
2025-11-23 15:25:36 +03:30
parent a3f0593443
commit d894bae756
5 changed files with 87 additions and 11 deletions
+14 -9
View File
@@ -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);
}
}
@@ -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);
}
}
@@ -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 });
}
}
@@ -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<ShipmentMethod> {
constructor(readonly em: EntityManager) {
super(em, ShipmentMethod);
}
}
@@ -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 {}