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