From ee10b49cf8a36a34bd0c44b722b9406f5b65f1d1 Mon Sep 17 00:00:00 2001 From: nicekid1 <86746988+nicekid1@users.noreply.github.com> Date: Tue, 7 Jan 2025 16:20:44 +0330 Subject: [PATCH] Fix issue with totalPayment calculation in invoice module --- src/cart/cart.controller.ts | 2 +- src/cart/cart.service.ts | 14 ++++++-------- src/invoice/invoice.service.ts | 23 +---------------------- 3 files changed, 8 insertions(+), 31 deletions(-) diff --git a/src/cart/cart.controller.ts b/src/cart/cart.controller.ts index d4fe4c2..a8a09d5 100644 --- a/src/cart/cart.controller.ts +++ b/src/cart/cart.controller.ts @@ -47,7 +47,7 @@ export class CartController { } @Post(":userId/checkout") - async processOrder(@Param("userId") userId: number, @Body("totalAmount") totalAmount: number): Promise<{ message: string; invoices: Invoice }> { + async processOrder(@Param("userId") userId: number, @Body("totalAmount") totalAmount: number): Promise<{ message: string; invoice: Invoice }> { if (!totalAmount || totalAmount <= 0) { throw new HttpException("Invalid total amount.", HttpStatus.BAD_REQUEST); } diff --git a/src/cart/cart.service.ts b/src/cart/cart.service.ts index 00eecfa..c141231 100644 --- a/src/cart/cart.service.ts +++ b/src/cart/cart.service.ts @@ -36,7 +36,7 @@ export class CartService { const invoiceId = invoice.id; let cart = await this.cartModel.findOne({ where: { userId, productId, status: "open" } }); - console.log(cart) + console.log(cart); if (!cart) { cart = await this.cartModel.create({ userId, @@ -95,6 +95,7 @@ export class CartService { cartItem.quantity = quantity; await cartItem.save(); + await this.invoiceService.updateTotalPayment(userId); return cartItem; } @@ -105,8 +106,8 @@ export class CartService { if (!cartItem) { throw new HttpException("Product not found in the cart.", HttpStatus.NOT_FOUND); } - await cartItem.destroy(); + await this.invoiceService.updateTotalPayment(userId); return { message: "Item deleted from your cart successfully." }; } @@ -116,15 +117,13 @@ export class CartService { } //order(clearCart disable) - async processOrder(userId: number, totalAmount: number): Promise<{ message: string; invoices: Invoice }> { + async processOrder(userId: number, totalAmount: number): Promise<{ message: string; invoice: Invoice }> { try { const cart = await this.cartModel.findOne({ where: { userId } }); if (!cart) { throw new HttpException("Cart not found for this user.", HttpStatus.NOT_FOUND); } - const cartId = cart.id; - // Deducting credit from wallet await this.walletService.processPayment(userId, totalAmount); @@ -153,9 +152,8 @@ export class CartService { } // Create the invoices for all cart - const invoices = await this.invoiceService.createInvoiceFromCart(userId); - - return { message: "Order processed successfully", invoices }; + const invoice = await this.invoiceModel.findOne({ where: { userId, status: "pending" } }); + return { message: "Order processed successfully", invoice }; } catch (error) { console.log(error); if (error instanceof HttpException) { diff --git a/src/invoice/invoice.service.ts b/src/invoice/invoice.service.ts index 6406c19..0765107 100644 --- a/src/invoice/invoice.service.ts +++ b/src/invoice/invoice.service.ts @@ -18,12 +18,6 @@ export class InvoiceService { if (!user) { throw new HttpException("User not found", HttpStatus.NOT_FOUND); } - - // const userCartItems = await this.cartService.getUserCart(userId); - // if (!userCartItems || userCartItems.cartItems.length === 0) { - // throw new HttpException("Cart is empty", HttpStatus.BAD_REQUEST); - // } - const invoice = await this.invoiceModel.create({ userId, totalPaymentAmount: 0, @@ -73,20 +67,5 @@ export class InvoiceService { throw new HttpException("An error occurred while retrieving the invoice.", HttpStatus.INTERNAL_SERVER_ERROR); } } - async getInvoiceByUserAndCartOrFalse(userId: number, cartId: number): Promise { - try { - const invoice = await this.invoiceModel.findOne({ - where: { userId, id: cartId }, - }); - - if (!invoice) { - return false; - } - - return invoice; - } catch (error) { - console.error("Error getting invoice:", error); - return false; - } - } + }