|
|
@ -1,27 +1,24 @@ |
|
|
|
import { Injectable, HttpException, HttpStatus, Inject, forwardRef } from "@nestjs/common"; |
|
|
|
import { Injectable, HttpException, HttpStatus } from "@nestjs/common"; |
|
|
|
import { InjectModel } from "@nestjs/sequelize"; |
|
|
|
import { InjectModel } from "@nestjs/sequelize"; |
|
|
|
import { Cart } from "./entities/cart.entity"; |
|
|
|
import { Cart } from "./entities/cart.entity"; |
|
|
|
|
|
|
|
import { CartResponse } from "./cart.response"; |
|
|
|
|
|
|
|
import { User } from "src/users/entities/user.entity"; |
|
|
|
import { Product } from "src/products/entities/product.entity"; |
|
|
|
import { Product } from "src/products/entities/product.entity"; |
|
|
|
import { WalletService } from "src/wallet/wallet.service"; |
|
|
|
import { console } from "inspector"; |
|
|
|
import { InvoiceService } from "src/invoice/invoice.service"; |
|
|
|
|
|
|
|
import { Invoice } from "src/invoice/entities/invoice.entity"; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Injectable() |
|
|
|
@Injectable() |
|
|
|
export class CartService { |
|
|
|
export class CartService { |
|
|
|
constructor( |
|
|
|
constructor( |
|
|
|
@InjectModel(Cart) private readonly cartModel: typeof Cart, |
|
|
|
@InjectModel(Cart) private readonly cartModel: typeof Cart, |
|
|
|
@InjectModel(Invoice) private readonly invoiceModel: typeof Invoice, |
|
|
|
|
|
|
|
@InjectModel(Product) private readonly productModel: typeof Product, |
|
|
|
@InjectModel(Product) private readonly productModel: typeof Product, |
|
|
|
private readonly walletService: WalletService, |
|
|
|
|
|
|
|
@Inject(forwardRef(() => InvoiceService)) |
|
|
|
|
|
|
|
private invoiceService: InvoiceService, |
|
|
|
|
|
|
|
) {} |
|
|
|
) {} |
|
|
|
//create a cart and add item to cart
|
|
|
|
|
|
|
|
async createAndAddItemToCart(addToCartDto: { userId: number; productId: number; quantity: number }): Promise<{ message: string; cartItem: Cart }> { |
|
|
|
// Add product to cart
|
|
|
|
|
|
|
|
async addToCart(addToCartDto: { userId: number; productId: number; quantity: number }): Promise<{ message: string; cartItem: Cart }> { |
|
|
|
const { userId, productId, quantity } = addToCartDto; |
|
|
|
const { userId, productId, quantity } = addToCartDto; |
|
|
|
|
|
|
|
|
|
|
|
if (!userId || !productId || !quantity || isNaN(Number(quantity)) || Number(quantity) <= 0) { |
|
|
|
if (!userId || !productId || !quantity) { |
|
|
|
throw new HttpException("Invalid parameters: userId, productId, and a positive quantity are required.", HttpStatus.BAD_REQUEST); |
|
|
|
throw new HttpException("Missing required parameters: userId, productId, and quantity are required.", HttpStatus.BAD_REQUEST); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
const product = await this.productModel.findByPk(productId); |
|
|
|
const product = await this.productModel.findByPk(productId); |
|
|
@ -29,49 +26,37 @@ export class CartService { |
|
|
|
throw new HttpException("Product not found!", HttpStatus.NOT_FOUND); |
|
|
|
throw new HttpException("Product not found!", HttpStatus.NOT_FOUND); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
let invoice = await this.invoiceModel.findOne({ where: { userId, status: "pending" } }); |
|
|
|
const existingCartItem = await this.cartModel.findOne({ |
|
|
|
if (!invoice) { |
|
|
|
where: { userId, productId }, |
|
|
|
invoice = await this.invoiceService.createInvoiceFromCart(userId); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
const invoiceId = invoice.id; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let cart = await this.cartModel.findOne({ where: { userId, productId, status: "open" } }); |
|
|
|
|
|
|
|
console.log(cart); |
|
|
|
|
|
|
|
if (!cart) { |
|
|
|
|
|
|
|
cart = await this.cartModel.create({ |
|
|
|
|
|
|
|
userId, |
|
|
|
|
|
|
|
productId, |
|
|
|
|
|
|
|
invoiceId, |
|
|
|
|
|
|
|
quantity, |
|
|
|
|
|
|
|
productPrice: product.price, |
|
|
|
|
|
|
|
status: "open", |
|
|
|
|
|
|
|
}); |
|
|
|
}); |
|
|
|
await cart.save(); |
|
|
|
|
|
|
|
} else { |
|
|
|
if (existingCartItem) { |
|
|
|
cart.quantity += Number(quantity); |
|
|
|
existingCartItem.quantity += Number(quantity); |
|
|
|
await cart.save(); |
|
|
|
existingCartItem.totalPrice = existingCartItem.quantity * existingCartItem.productPrice; |
|
|
|
|
|
|
|
await existingCartItem.save(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return { |
|
|
|
|
|
|
|
message: "Product quantity updated in cart successfully!", |
|
|
|
|
|
|
|
cartItem: existingCartItem, |
|
|
|
|
|
|
|
}; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
await this.invoiceService.updateTotalPayment(userId); |
|
|
|
const newCartItem = await this.cartModel.create({ userId, productId, quantity, productPrice: product.price, totalPrice: product.price * quantity, productName: product.name }); |
|
|
|
|
|
|
|
|
|
|
|
return { |
|
|
|
return { |
|
|
|
message: cart.id ? "Product quantity updated in cart successfully!" : "Product added to cart successfully!", |
|
|
|
message: "Product added to cart successfully!", |
|
|
|
cartItem: cart, |
|
|
|
cartItem: newCartItem, |
|
|
|
}; |
|
|
|
}; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Get user's cart
|
|
|
|
// Get user's cart
|
|
|
|
async getUserCart(userId: number): Promise<{ cartItems: Cart[]; totalPrice: number }> { |
|
|
|
async getUserCart(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({ |
|
|
|
const cartItems = await this.cartModel.findAll({ |
|
|
|
where: { userId }, |
|
|
|
where: { userId }, |
|
|
|
include: [ |
|
|
|
include: [ |
|
|
|
{ |
|
|
|
{ |
|
|
|
model: Product, |
|
|
|
model: Product, |
|
|
|
attributes: [], |
|
|
|
attributes: ["id", "name", "price", "description", "imageUrl"], |
|
|
|
}, |
|
|
|
}, |
|
|
|
], |
|
|
|
], |
|
|
|
}); |
|
|
|
}); |
|
|
@ -80,7 +65,7 @@ export class CartService { |
|
|
|
throw new HttpException("No cart items found for the specified user.", HttpStatus.NOT_FOUND); |
|
|
|
throw new HttpException("No cart items found for the specified user.", HttpStatus.NOT_FOUND); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
const totalPrice = cartItems.reduce((sum, item) => sum + (Number(item.productPrice * item.quantity) || 0), 0); |
|
|
|
const totalPrice = cartItems.reduce((sum, item) => sum + Number(item.totalPrice), 0); |
|
|
|
|
|
|
|
|
|
|
|
return { cartItems, totalPrice }; |
|
|
|
return { cartItems, totalPrice }; |
|
|
|
} |
|
|
|
} |
|
|
@ -94,73 +79,20 @@ export class CartService { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
cartItem.quantity = quantity; |
|
|
|
cartItem.quantity = quantity; |
|
|
|
|
|
|
|
cartItem.totalPrice = cartItem.quantity * cartItem.productPrice; |
|
|
|
await cartItem.save(); |
|
|
|
await cartItem.save(); |
|
|
|
await this.invoiceService.updateTotalPayment(userId); |
|
|
|
|
|
|
|
return cartItem; |
|
|
|
return cartItem; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Remove an item from cart
|
|
|
|
// Remove product from cart
|
|
|
|
async removeFromCart(userId: number, productId: number): Promise<{ message: string }> { |
|
|
|
async removeFromCart(userId: number, productId: number): Promise<{ message: string }> { |
|
|
|
const cartItem = await this.cartModel.findOne({ where: { userId, productId } }); |
|
|
|
const cartItem = await this.cartModel.findOne({ where: { userId, productId } }); |
|
|
|
|
|
|
|
|
|
|
|
if (!cartItem) { |
|
|
|
if (!cartItem) { |
|
|
|
throw new HttpException("Product not found in the cart.", HttpStatus.NOT_FOUND); |
|
|
|
throw new HttpException("Product not found in the cart.", HttpStatus.NOT_FOUND); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
await cartItem.destroy(); |
|
|
|
await cartItem.destroy(); |
|
|
|
await this.invoiceService.updateTotalPayment(userId); |
|
|
|
|
|
|
|
return { message: "Item deleted from your cart successfully." }; |
|
|
|
return { message: "Item deleted from your cart successfully." }; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//delete whole cart
|
|
|
|
|
|
|
|
async clearCart(userId: number): Promise<void> { |
|
|
|
|
|
|
|
await this.cartModel.destroy({ where: { userId } }); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//order(clearCart disable)
|
|
|
|
|
|
|
|
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); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Deducting credit from wallet
|
|
|
|
|
|
|
|
await this.walletService.processPayment(userId, totalAmount); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Retrieve cart items
|
|
|
|
|
|
|
|
const cartItems = await this.cartModel.findAll({ where: { userId } }); |
|
|
|
|
|
|
|
if (cartItems.length === 0) { |
|
|
|
|
|
|
|
throw new HttpException("Cart is empty.", HttpStatus.BAD_REQUEST); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Process each cart item and update stock
|
|
|
|
|
|
|
|
for (const cartItem of cartItems) { |
|
|
|
|
|
|
|
const { productId, quantity } = cartItem; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const product = await this.productModel.findOne({ where: { id: productId } }); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!product) { |
|
|
|
|
|
|
|
throw new HttpException(`Product with ID ${productId} not found.`, HttpStatus.NOT_FOUND); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (product.quantity < quantity) { |
|
|
|
|
|
|
|
throw new HttpException(`Insufficient stock for product ID ${productId}.`, HttpStatus.BAD_REQUEST); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
product.quantity -= quantity; // Reduce stock
|
|
|
|
|
|
|
|
await product.save(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Create the invoices for all cart
|
|
|
|
|
|
|
|
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) { |
|
|
|
|
|
|
|
throw error; |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
throw new HttpException(`An error occurred while processing the order: ${error.message}`, HttpStatus.INTERNAL_SERVER_ERROR); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|