|
|
|
@ -4,6 +4,7 @@ import { Cart } from "./entities/cart.entity"; |
|
|
|
|
import { Product } from "src/products/entities/product.entity"; |
|
|
|
|
import { WalletService } from "src/wallet/wallet.service"; |
|
|
|
|
import { InvoiceService } from "src/invoice/invoice.service"; |
|
|
|
|
import { Invoice } from "src/invoice/entities/invoice.entity"; |
|
|
|
|
|
|
|
|
|
@Injectable() |
|
|
|
|
export class CartService { |
|
|
|
@ -12,7 +13,7 @@ export class CartService { |
|
|
|
|
@InjectModel(Product) private readonly productModel: typeof Product, |
|
|
|
|
private readonly walletService: WalletService, |
|
|
|
|
@Inject(forwardRef(() => InvoiceService)) |
|
|
|
|
private invoiceService: InvoiceService |
|
|
|
|
private invoiceService: InvoiceService, |
|
|
|
|
) {} |
|
|
|
|
|
|
|
|
|
// Add product to cart
|
|
|
|
@ -108,33 +109,47 @@ export class CartService { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
//order(clearCart unable)
|
|
|
|
|
async processOrder(userId: number, totalAmount: number): Promise<string> { |
|
|
|
|
// Deducting credit from wallet
|
|
|
|
|
await this.walletService.processPayment(userId, totalAmount); |
|
|
|
|
//Reduce the number purchased from the number of products
|
|
|
|
|
const cartItems = await this.cartModel.findAll({ where: { userId } }); |
|
|
|
|
if (cartItems.length === 0) { |
|
|
|
|
throw new HttpException("Cart is empty.", HttpStatus.BAD_REQUEST); |
|
|
|
|
} |
|
|
|
|
for (const cartItem of cartItems) { |
|
|
|
|
const { productId, quantity } = cartItem; |
|
|
|
|
async processOrder(userId: number, totalAmount: number): Promise<{ message: string; invoice: Invoice }> { |
|
|
|
|
try { |
|
|
|
|
// 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); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
const product = await this.productModel.findOne({ where: { id: productId } }); |
|
|
|
|
// Process each cart item and update stock
|
|
|
|
|
for (const cartItem of cartItems) { |
|
|
|
|
const { productId, quantity } = cartItem; |
|
|
|
|
|
|
|
|
|
if (!product) { |
|
|
|
|
throw new HttpException(`Product with ID ${productId} not found.`, HttpStatus.NOT_FOUND); |
|
|
|
|
} |
|
|
|
|
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); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (product.quantity < quantity) { |
|
|
|
|
throw new HttpException(`Insufficient stock for product ID ${productId}.`, HttpStatus.BAD_REQUEST); |
|
|
|
|
product.quantity -= quantity; // Reduce stock
|
|
|
|
|
await product.save(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
product.quantity -= quantity; |
|
|
|
|
await product.save(); |
|
|
|
|
// await this.clearCart(userId);
|
|
|
|
|
} |
|
|
|
|
const invoice = await this.invoiceService.createInvoiceFromCart(userId) |
|
|
|
|
// Create the invoice after processing the cart
|
|
|
|
|
const invoice = await this.invoiceService.createInvoiceFromCart(userId); |
|
|
|
|
|
|
|
|
|
return "Order processed successfully"; |
|
|
|
|
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); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|