From 288b0529cd4c47f5302d258e5bafe1d62be75f0a Mon Sep 17 00:00:00 2001 From: morteza-mortezai Date: Mon, 1 Dec 2025 15:53:04 +0330 Subject: [PATCH] clear cart --- .../cart/controllers/cart.controller.ts | 7 +++++++ src/modules/cart/providers/cart.service.ts | 4 ++++ src/modules/orders/orders.service.ts | 19 ++++++++----------- 3 files changed, 19 insertions(+), 11 deletions(-) diff --git a/src/modules/cart/controllers/cart.controller.ts b/src/modules/cart/controllers/cart.controller.ts index 67b0cc5..2d2993e 100644 --- a/src/modules/cart/controllers/cart.controller.ts +++ b/src/modules/cart/controllers/cart.controller.ts @@ -83,6 +83,13 @@ export class CartController { return this.cartService.removeItem(userId, restaurantId, foodId); } + @Delete() + @ApiOperation({ summary: 'Clear entire cart' }) + @ApiResponse({ status: 200, description: 'Cart cleared successfully' }) + async clearCart(@UserId() userId: string, @RestId() restaurantId: string) { + return this.cartService.clearCart(userId, restaurantId); + } + @Post('apply-coupon') @ApiOperation({ summary: 'Apply coupon to cart' }) @ApiBody({ type: ApplyCouponDto }) diff --git a/src/modules/cart/providers/cart.service.ts b/src/modules/cart/providers/cart.service.ts index f6b92cb..d8983af 100644 --- a/src/modules/cart/providers/cart.service.ts +++ b/src/modules/cart/providers/cart.service.ts @@ -71,6 +71,10 @@ export class CartService { } return cart; } + async findOne2(userId: string, restaurantId: string): Promise { + const cart = await this.getCartByRestaurant(userId, restaurantId); + return cart; + } /** * Add item to cart (increment quantity if item exists, otherwise create new) diff --git a/src/modules/orders/orders.service.ts b/src/modules/orders/orders.service.ts index 3047e99..513045b 100644 --- a/src/modules/orders/orders.service.ts +++ b/src/modules/orders/orders.service.ts @@ -23,7 +23,8 @@ export class OrdersService { private readonly paymentGatewayService: PaymentGatewayService, ) {} - async create(userId: string, restaurantId: string): Promise { + async create(userId: string, restaurantId: string) { + console.log('create order', userId, restaurantId); // Get cart const cart = await this.cartService.findOne(userId, restaurantId); @@ -31,7 +32,7 @@ export class OrdersService { const validationResult = await this.validateCartForOrder(userId, restaurantId, cart); // Create order within a transaction - return await this.em.transactional(async em => { + return this.em.transactional(async em => { // Create order entity const order = em.create(Order, { user: validationResult.user, @@ -76,15 +77,10 @@ export class OrdersService { // Flush all changes await em.flush(); - // Clear cart from cache after successful order creation - await this.cartService.clearCart(userId, restaurantId); - // Check if payment method is online and redirect to payment gateway if (validationResult.paymentMethod.paymentMethod.isOnline) { - const paymentRedirect = await this.paymentGatewayService.generateRedirectUrl( - order, - validationResult.paymentMethod, - ); + const paymentRedirect = this.paymentGatewayService.generateRedirectUrl(order, validationResult.paymentMethod); + // await this.cartService.clearCart(userId, restaurantId); // Return order with redirect URL for online payment return { @@ -92,12 +88,13 @@ export class OrdersService { redirectUrl: paymentRedirect.redirectUrl, }; } else { + // await this.cartService.clearCart(userId, restaurantId); + // For offline payment methods (cash on delivery, etc.), order is created and pending // Payment status will be updated when payment is confirmed // TODO: Implement offline payment confirmation flow if needed } - - return order; + return { order }; }); }