Fix issue with totalPayment calculation in invoice module

master
nicekid1 2 months ago
parent 548f161f7c
commit ee10b49cf8
  1. 2
      src/cart/cart.controller.ts
  2. 14
      src/cart/cart.service.ts
  3. 23
      src/invoice/invoice.service.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);
}

@ -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) {

@ -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<Invoice | false> {
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;
}
}
}

Loading…
Cancel
Save