This commit is contained in:
2026-04-07 15:30:09 +03:30
parent c573e9d1c0
commit 055ba55586
5 changed files with 18 additions and 36 deletions
@@ -24,15 +24,15 @@ import { Permissions } from 'src/common/decorators/permissions.decorator';
@ApiTags('Delivery') @ApiTags('Delivery')
@Controller() @Controller()
export class DeliveryController { export class DeliveryController {
constructor(private readonly deliveryService: DeliveryService) {} constructor(private readonly deliveryService: DeliveryService) { }
@UseGuards(AuthGuard) @UseGuards(AuthGuard)
@ApiBearerAuth() @ApiBearerAuth()
@Get('public/delivery-methods/restaurant') @Get('public/delivery-methods/restaurant')
@ApiOperation({ summary: 'Get restaurant delivery methods' }) @ApiOperation({ summary: 'Get restaurant delivery methods' })
@ApiHeader(API_HEADER_SLUG) @ApiHeader(API_HEADER_SLUG)
findAllDeliveryMethods(@RestId() restId: string) { findAllDeliveryMethods(@RestId() restId: string) {
return this.deliveryService.findAllForRestaurantId(restId); return this.deliveryService.findByRestaurantId(restId);
} }
/*** Admin ***/ /*** Admin ***/
@@ -52,7 +52,7 @@ export class DeliveryController {
@Get('admin/delivery-methods/restaurant') @Get('admin/delivery-methods/restaurant')
@ApiOperation({ summary: 'Get the restaurant delivery methods' }) @ApiOperation({ summary: 'Get the restaurant delivery methods' })
findRestaurantDeliveryMethods(@RestId() restId: string) { findRestaurantDeliveryMethods(@RestId() restId: string) {
return this.deliveryService.findAllForRestaurantId(restId); return this.deliveryService.findByRestaurantId(restId);
} }
@UseGuards(AdminAuthGuard) @UseGuards(AdminAuthGuard)
@@ -62,7 +62,7 @@ export class DeliveryController {
@ApiOperation({ summary: 'Get a restaurant delivery method by delivery ID' }) @ApiOperation({ summary: 'Get a restaurant delivery method by delivery ID' })
@ApiParam({ name: 'deliveryId', description: 'Delivery method ID' }) @ApiParam({ name: 'deliveryId', description: 'Delivery method ID' })
findOne(@Param('deliveryId') deliveryId: string, @RestId() restId: string) { findOne(@Param('deliveryId') deliveryId: string, @RestId() restId: string) {
return this.deliveryService.findOne(restId, deliveryId); return this.deliveryService.findOneOrFail(restId, deliveryId);
} }
@UseGuards(AdminAuthGuard) @UseGuards(AdminAuthGuard)
@@ -4,23 +4,20 @@ import { Delivery } from '../entities/delivery.entity';
import { DeliveryRepository } from '../repositories/delivery.repository'; import { DeliveryRepository } from '../repositories/delivery.repository';
import { CreateDeliveryDto } from '../dto/create-delivery.dto'; import { CreateDeliveryDto } from '../dto/create-delivery.dto';
import { UpdateDeliveryDto } from '../dto/update-delivery.dto'; import { UpdateDeliveryDto } from '../dto/update-delivery.dto';
import { RestRepository } from '../../restaurants/repositories/rest.repository';
import { DeliveryMethodEnum } from '../interface/delivery'; import { DeliveryMethodEnum } from '../interface/delivery';
import { DeliveryMessage } from 'src/common/enums/message.enum'; import { DeliveryMessage } from 'src/common/enums/message.enum';
import { RestaurantsService } from 'src/modules/restaurants/providers/restaurants.service';
@Injectable() @Injectable()
export class DeliveryService { export class DeliveryService {
constructor( constructor(
private readonly deliveryRepository: DeliveryRepository, private readonly deliveryRepository: DeliveryRepository,
private readonly restRepository: RestRepository, private readonly restService: RestaurantsService,
private readonly em: EntityManager, private readonly em: EntityManager,
) {} ) { }
async create(restId: string, createDto: CreateDeliveryDto): Promise<Delivery> { async create(restId: string, createDto: CreateDeliveryDto): Promise<Delivery> {
const restaurant = await this.restRepository.findOne({ id: restId }); const restaurant = await this.restService.findOneOrFail(restId)
if (!restaurant) {
throw new NotFoundException(DeliveryMessage.RESTAURANT_NOT_FOUND);
}
// Check if the delivery method with the same name already exists for this restaurant // Check if the delivery method with the same name already exists for this restaurant
const existing = await this.deliveryRepository.findOne({ const existing = await this.deliveryRepository.findOne({
@@ -50,11 +47,11 @@ export class DeliveryService {
return delivery; return delivery;
} }
async findAllForRestaurantId(restId: string): Promise<Delivery[]> { async findByRestaurantId(restId: string): Promise<Delivery[]> {
return this.deliveryRepository.find({ restaurant: { id: restId } }, { orderBy: { order: 'asc' } }); return this.deliveryRepository.find({ restaurant: { id: restId } }, { orderBy: { order: 'asc' } });
} }
async findOne(restId: string, deliveryId: string): Promise<Delivery> { async findOneOrFail(restId: string, deliveryId: string): Promise<Delivery> {
const delivery = await this.deliveryRepository.findOne({ const delivery = await this.deliveryRepository.findOne({
id: deliveryId, id: deliveryId,
restaurant: { id: restId }, restaurant: { id: restId },
@@ -68,15 +65,7 @@ export class DeliveryService {
} }
async update(restId: string, deliveryId: string, updateDto: UpdateDeliveryDto): Promise<Delivery> { async update(restId: string, deliveryId: string, updateDto: UpdateDeliveryDto): Promise<Delivery> {
const delivery = await this.deliveryRepository.findOne({ const delivery = await this.findOneOrFail(restId, deliveryId)
restaurant: { id: restId },
id: deliveryId,
});
if (!delivery) {
throw new NotFoundException(DeliveryMessage.DELIVERY_METHOD_NOT_FOUND);
}
// If method is being updated, check for conflicts // If method is being updated, check for conflicts
if (updateDto.method !== undefined && updateDto.method !== delivery.method) { if (updateDto.method !== undefined && updateDto.method !== delivery.method) {
const existing = await this.deliveryRepository.findOne({ const existing = await this.deliveryRepository.findOne({
@@ -97,14 +86,7 @@ export class DeliveryService {
} }
async remove(restId: string, deliveryId: string): Promise<void> { async remove(restId: string, deliveryId: string): Promise<void> {
const delivery = await this.deliveryRepository.findOne({ const delivery = await this.findOneOrFail(restId, deliveryId)
restaurant: { id: restId },
id: deliveryId,
});
if (!delivery) {
throw new NotFoundException(DeliveryMessage.DELIVERY_METHOD_NOT_FOUND);
}
await this.em.removeAndFlush(delivery); await this.em.removeAndFlush(delivery);
} }
@@ -42,7 +42,7 @@ export class RestaurantsController {
@ApiOperation({ summary: 'Get restaurant by ID from request' }) @ApiOperation({ summary: 'Get restaurant by ID from request' })
@Get('admin/restaurants/my-restaurant') @Get('admin/restaurants/my-restaurant')
async findOne(@RestId() restId: string) { async findOne(@RestId() restId: string) {
return await this.restaurantsService.findOne(restId); return await this.restaurantsService.findOneOrFail(restId);
} }
@UseGuards(AdminAuthGuard) @UseGuards(AdminAuthGuard)
@@ -96,7 +96,7 @@ export class RestaurantsController {
@ApiParam({ name: 'id', required: true, description: 'Restaurant ID' }) @ApiParam({ name: 'id', required: true, description: 'Restaurant ID' })
@Get('super-admin/restaurants/:id') @Get('super-admin/restaurants/:id')
findOneById(@Param('id') id: string) { findOneById(@Param('id') id: string) {
return this.restaurantsService.findOne(id); return this.restaurantsService.findOneOrFail(id);
} }
@UseGuards(SuperAdminAuthGuard) @UseGuards(SuperAdminAuthGuard)
@@ -128,7 +128,7 @@ export class RestaurantsService {
return restaurant; return restaurant;
} }
async findOne(id: string): Promise<Restaurant> { async findOneOrFail(id: string): Promise<Restaurant> {
const restaurant = await this.restRepository.findOne({ id }); const restaurant = await this.restRepository.findOne({ id });
if (!restaurant) { if (!restaurant) {
@@ -16,6 +16,6 @@ import { AuthModule } from '../auth/auth.module';
controllers: [RestaurantsController, ScheduleController], controllers: [RestaurantsController, ScheduleController],
providers: [RestaurantsService, RestRepository, ScheduleRepository, ScheduleService, RestaurantCrone], providers: [RestaurantsService, RestRepository, ScheduleRepository, ScheduleService, RestaurantCrone],
imports: [MikroOrmModule.forFeature([Restaurant, Schedule]), JwtModule, forwardRef(() => AuthModule)], imports: [MikroOrmModule.forFeature([Restaurant, Schedule]), JwtModule, forwardRef(() => AuthModule)],
exports: [RestRepository, ScheduleRepository, ScheduleService], exports: [RestRepository, ScheduleRepository, ScheduleService, RestaurantsService],
}) })
export class RestaurantsModule {} export class RestaurantsModule { }