This commit is contained in:
2025-12-03 10:28:21 +03:30
parent afbe480db6
commit 46d1f96115
2 changed files with 43 additions and 29 deletions
+18 -20
View File
@@ -1,7 +1,6 @@
import { Controller, Get, Post, Body, Patch, Param, Delete, UseGuards } from '@nestjs/common';
import { ApiTags, ApiOperation, ApiResponse, ApiBearerAuth } from '@nestjs/swagger';
import { Controller, Get, Post, Param, UseGuards } from '@nestjs/common';
import { ApiTags, ApiOperation, ApiBearerAuth } from '@nestjs/swagger';
import { OrdersService } from './orders.service';
import { UpdateOrderDto } from './dto/update-order.dto';
import { AuthGuard } from '../auth/guards/auth.guard';
import { UserId } from '../../common/decorators/user-id.decorator';
import { RestId } from 'src/common/decorators/rest-id.decorator';
@@ -13,35 +12,34 @@ export class OrdersController {
@UseGuards(AuthGuard)
@ApiBearerAuth()
@Post('public/orders')
@ApiOperation({ summary: 'Create a new order from cart' })
@ApiResponse({ status: 201, description: 'Order created successfully' })
@ApiResponse({ status: 400, description: 'Bad request - cart validation failed' })
@ApiResponse({ status: 404, description: 'Cart, user, restaurant, address, or payment method not found' })
create(@UserId() userId: string, @RestId() restaurantId: string) {
@Post('public/checkout')
@ApiOperation({ summary: 'Checkout : create order and payment record' })
checkout(@UserId() userId: string, @RestId() restaurantId: string) {
return this.ordersService.checkout(userId, restaurantId);
}
@UseGuards(AuthGuard)
@ApiBearerAuth()
@Get('public/orders')
@ApiOperation({ summary: 'Get all orders' })
@ApiResponse({ status: 200, description: 'Orders fetched successfully' })
@ApiResponse({ status: 404, description: 'No orders found' })
findAll() {
return this.ordersService.findAll();
}
@Get(':id')
@UseGuards(AuthGuard)
@ApiBearerAuth()
@Get('public/orders/:id')
findOne(@Param('id') id: string) {
return this.ordersService.findOne(+id);
}
@Patch(':id')
update(@Param('id') id: string, @Body() updateOrderDto: UpdateOrderDto) {
return this.ordersService.update(+id, updateOrderDto);
}
// @Patch(':id')
// update(@Param('id') id: string, @Body() updateOrderDto: UpdateOrderDto) {
// return this.ordersService.update(+id, updateOrderDto);
// }
@Delete(':id')
remove(@Param('id') id: string) {
return this.ordersService.remove(+id);
}
// @Delete(':id')
// remove(@Param('id') id: string) {
// return this.ordersService.remove(+id);
// }
}
+25 -9
View File
@@ -13,6 +13,8 @@ import { Cart } from '../cart/interfaces/cart.interface';
import { PaymentMethod } from '../payments/entities/payment-method.entity';
// import { PaymentGatewayService } from '../payments/services/payment-gateway.service.tss';
import { PaymentsService } from '../payments/services/payments.service';
import { DeliveryMethodEnum } from '../delivery/interface/delivery';
import { Delivery } from '../delivery/entities/delivery.entity';
@Injectable()
export class OrdersService {
@@ -105,7 +107,7 @@ export class OrdersService {
): Promise<{
user: User;
restaurant: Restaurant;
address: UserAddress;
address: UserAddress | null;
paymentMethod: PaymentMethod;
orderItemsData: Array<{ food: Food; quantity: number; unitPrice: number; discount: number }>;
}> {
@@ -115,8 +117,8 @@ export class OrdersService {
}
// Validate address is set
if (!cart.addressId) {
throw new BadRequestException('Address is required. Please set a delivery address before creating an order.');
if (!cart.deliveryMethodId) {
throw new NotFoundException('Delivery method not found');
}
// Validate payment method is set
@@ -137,14 +139,28 @@ export class OrdersService {
throw new NotFoundException('Restaurant not found');
}
const address = await this.em.findOne(UserAddress, { id: cart.addressId }, { populate: ['user'] });
if (!address) {
throw new NotFoundException('Address not found');
const delivery = await this.em.findOne(Delivery, { id: cart.deliveryMethodId });
if (!delivery) {
throw new NotFoundException('Delivery not found');
}
// Verify address belongs to the user
if (address.user.id !== userId) {
throw new BadRequestException('Address does not belong to the current user');
if (delivery.method === DeliveryMethodEnum.DeliveryCourier && !cart.addressId) {
throw new BadRequestException('Address is required. Please set a delivery address before creating an order.');
}
let address: UserAddress | null = null;
if (cart.addressId) {
address = await this.em.findOne(UserAddress, { id: cart.addressId }, { populate: ['user'] });
if (!address) {
throw new NotFoundException('Address not found');
}
// Verify address belongs to the user
if (address.user.id !== userId) {
throw new BadRequestException('Address does not belong to the current user');
}
// Verify address belongs to the user
if (address.user.id !== userId) {
throw new BadRequestException('Address does not belong to the current user');
}
}
const paymentMethod = await this.em.findOne(