|
|
|
@ -15,41 +15,74 @@ export class CartService { |
|
|
|
|
@Inject(forwardRef(() => InvoiceService)) |
|
|
|
|
private invoiceService: InvoiceService, |
|
|
|
|
) {} |
|
|
|
|
|
|
|
|
|
// Add product to cart
|
|
|
|
|
async addToCart(addToCartDto: { userId: number; productId: number; quantity: number }): Promise<{ message: string; cartItem: Cart }> { |
|
|
|
|
//create a cart and add item to cart
|
|
|
|
|
async createAndAddItemToCart(addToCartDto: { userId: number; productId: number; quantity: number }): Promise<{ message: string; cartItem: Cart }> { |
|
|
|
|
const { userId, productId, quantity } = addToCartDto; |
|
|
|
|
try { |
|
|
|
|
if (!userId || !productId || !quantity || isNaN(Number(quantity)) || Number(quantity) <= 0) { |
|
|
|
|
throw new HttpException("Invalid parameters: userId, productId, and a positive quantity are required.", HttpStatus.BAD_REQUEST); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (!userId || !productId || !quantity) { |
|
|
|
|
throw new HttpException("Missing required parameters: userId, productId, and quantity are required.", HttpStatus.BAD_REQUEST); |
|
|
|
|
} |
|
|
|
|
const product = await this.productModel.findByPk(productId); |
|
|
|
|
if (!product) { |
|
|
|
|
throw new HttpException("Product not found!", HttpStatus.NOT_FOUND); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
const product = await this.productModel.findByPk(productId); |
|
|
|
|
if (!product) { |
|
|
|
|
throw new HttpException("Product not found!", HttpStatus.NOT_FOUND); |
|
|
|
|
} |
|
|
|
|
let cart = await this.cartModel.findOne({ where: { userId, productId, status: "open" } }); |
|
|
|
|
|
|
|
|
|
const existingCartItem = await this.cartModel.findOne({ |
|
|
|
|
where: { userId, productId }, |
|
|
|
|
}); |
|
|
|
|
if (!cart) { |
|
|
|
|
cart = await this.cartModel.create({ |
|
|
|
|
userId, |
|
|
|
|
productId, |
|
|
|
|
quantity, |
|
|
|
|
productPrice: product.price, |
|
|
|
|
status: "open", |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
const invoice = await this.invoiceService.createInvoiceFromCart(userId); |
|
|
|
|
cart.invoiceId = invoice.id; |
|
|
|
|
await cart.save(); |
|
|
|
|
} else { |
|
|
|
|
const invoice = await this.invoiceService.getInvoiceByUserAndCart(userId); |
|
|
|
|
if (!invoice) { |
|
|
|
|
const newInvoice = await this.invoiceService.createInvoiceFromCart(userId); |
|
|
|
|
cart.invoiceId = newInvoice.id; |
|
|
|
|
await cart.save(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
const invoiceId = cart.invoiceId; |
|
|
|
|
|
|
|
|
|
let existingCartItem = await this.cartModel.findOne({ |
|
|
|
|
where: { userId, productId, invoiceId }, |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
if (existingCartItem) { |
|
|
|
|
existingCartItem.quantity += Number(quantity); |
|
|
|
|
existingCartItem.totalPrice = existingCartItem.quantity * existingCartItem.productPrice; |
|
|
|
|
await existingCartItem.save(); |
|
|
|
|
if (existingCartItem) { |
|
|
|
|
existingCartItem.quantity += Number(quantity); |
|
|
|
|
await existingCartItem.save(); |
|
|
|
|
|
|
|
|
|
return { |
|
|
|
|
message: "Product quantity updated in cart successfully!", |
|
|
|
|
cartItem: existingCartItem, |
|
|
|
|
}; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
const newCartItem = await this.cartModel.create({ |
|
|
|
|
userId, |
|
|
|
|
productId, |
|
|
|
|
invoiceId, |
|
|
|
|
quantity, |
|
|
|
|
productPrice: product.price, |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
return { |
|
|
|
|
message: "Product quantity updated in cart successfully!", |
|
|
|
|
cartItem: existingCartItem, |
|
|
|
|
message: "Product added to cart successfully!", |
|
|
|
|
cartItem: newCartItem, |
|
|
|
|
}; |
|
|
|
|
} catch (error) { |
|
|
|
|
console.error("Error adding product to cart:", error); |
|
|
|
|
throw new HttpException(error.message || "An error occurred while adding the product to the cart.", HttpStatus.INTERNAL_SERVER_ERROR); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
const newCartItem = await this.cartModel.create({ userId, productId, quantity, productPrice: product.price, totalPrice: product.price * quantity, productName: product.name }); |
|
|
|
|
|
|
|
|
|
return { |
|
|
|
|
message: "Product added to cart successfully!", |
|
|
|
|
cartItem: newCartItem, |
|
|
|
|
}; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Get user's cart
|
|
|
|
@ -63,7 +96,7 @@ export class CartService { |
|
|
|
|
include: [ |
|
|
|
|
{ |
|
|
|
|
model: Product, |
|
|
|
|
attributes: ["id", "name", "price", "description", "imageUrl"], |
|
|
|
|
attributes: [], |
|
|
|
|
}, |
|
|
|
|
], |
|
|
|
|
}); |
|
|
|
@ -72,7 +105,7 @@ export class CartService { |
|
|
|
|
throw new HttpException("No cart items found for the specified user.", HttpStatus.NOT_FOUND); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
const totalPrice = cartItems.reduce((sum, item) => sum + (Number(item.totalPrice) || 0), 0); |
|
|
|
|
const totalPrice = cartItems.reduce((sum, item) => sum + (Number(item.productPrice * item.quantity) || 0), 0); |
|
|
|
|
|
|
|
|
|
return { cartItems, totalPrice }; |
|
|
|
|
} |
|
|
|
@ -86,7 +119,6 @@ export class CartService { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
cartItem.quantity = quantity; |
|
|
|
|
cartItem.totalPrice = cartItem.quantity * cartItem.productPrice; |
|
|
|
|
await cartItem.save(); |
|
|
|
|
return cartItem; |
|
|
|
|
} |
|
|
|
@ -146,7 +178,7 @@ export class CartService { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Create the invoices for all cart
|
|
|
|
|
const invoices = await this.invoiceService.createInvoiceFromCart(userId,cartId); |
|
|
|
|
const invoices = await this.invoiceService.createInvoiceFromCart(userId); |
|
|
|
|
|
|
|
|
|
return { message: "Order processed successfully", invoices }; |
|
|
|
|
} catch (error) { |
|
|
|
|