From 72665cebda05eb58715636132b9ca94d0eed3e3c Mon Sep 17 00:00:00 2001 From: nicekid1 <86746988+nicekid1@users.noreply.github.com> Date: Wed, 8 Jan 2025 13:01:43 +0330 Subject: [PATCH] Debug issue with getting user cart --- src/cart/cart.controller.ts | 6 +++--- src/cart/cart.service.ts | 6 ++---- src/invoice/invoice.service.ts | 2 +- 3 files changed, 6 insertions(+), 8 deletions(-) diff --git a/src/cart/cart.controller.ts b/src/cart/cart.controller.ts index 2d123c4..4cc0884 100644 --- a/src/cart/cart.controller.ts +++ b/src/cart/cart.controller.ts @@ -20,9 +20,9 @@ export class CartController { @UseGuards(JwtAuthGuard) @Get() - async getUserCart(@Request() req: any): Promise<{ cartItems: Cart[]; totalPrice: number }> { + async getUserOpenCart(@Request() req: any): Promise<{ cartItems: Cart[]; totalPrice: number }> { const userId = req.user.id; - return this.cartService.getUserCart(userId); + return this.cartService.getUserOpenCart(userId); } @UseGuards(JwtAuthGuard) @@ -51,7 +51,7 @@ export class CartController { async processOrder(@Request() req: any): Promise<{ message: string; invoice: Invoice }> { const userId = req.user.id; try { - const totalAmount = (await this.cartService.getUserCart(userId)).totalPrice + const totalAmount = (await this.cartService.getUserOpenCart(userId)).totalPrice const result = await this.cartService.processOrder(userId, totalAmount); return result; } catch (error) { diff --git a/src/cart/cart.service.ts b/src/cart/cart.service.ts index e8a25c4..a47e1d1 100644 --- a/src/cart/cart.service.ts +++ b/src/cart/cart.service.ts @@ -59,15 +59,14 @@ export class CartService { cartItem: cart, }; } - // Get user's cart - async getUserCart(userId: number): Promise<{ cartItems: Cart[]; totalPrice: number }> { + async getUserOpenCart(userId: number): Promise<{ cartItems: Cart[]; totalPrice: number }> { if (!userId) { throw new HttpException("User ID is required.", HttpStatus.BAD_REQUEST); } const cartItems = await this.cartModel.findAll({ - where: { userId }, + where: { userId,status:"open" }, include: [ { model: Product, @@ -120,7 +119,6 @@ export class CartService { async processOrder(userId: number, totalAmount: number): Promise<{ message: string; invoice: Invoice }> { try { const carts = await this.cartModel.findAll({ where: { userId, status: "open" } }); - if (!carts || carts.length === 0) { throw new HttpException("No open carts found for this user.", HttpStatus.NOT_FOUND); } diff --git a/src/invoice/invoice.service.ts b/src/invoice/invoice.service.ts index 0d95a82..3d6cb4f 100644 --- a/src/invoice/invoice.service.ts +++ b/src/invoice/invoice.service.ts @@ -29,7 +29,7 @@ export class InvoiceService { throw new HttpException("User not found", HttpStatus.NOT_FOUND); } - const userCartItems = await this.cartService.getUserCart(userId); + const userCartItems = await this.cartService.getUserOpenCart(userId); if (!userCartItems || !userCartItems.cartItems || userCartItems.cartItems.length === 0) { throw new HttpException("Cart is empty", HttpStatus.BAD_REQUEST); }