|
|
@ -108,8 +108,8 @@ export class CartService { |
|
|
|
await this.cartModel.destroy({ where: { userId } }); |
|
|
|
await this.cartModel.destroy({ where: { userId } }); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//order(clearCart unable)
|
|
|
|
//order(clearCart disable)
|
|
|
|
async processOrder(userId: number, totalAmount: number): Promise<{ message: string; invoice: Invoice }> { |
|
|
|
async processOrder(userId: number, totalAmount: number): Promise<{ message: string; invoices: Invoice[] }> { |
|
|
|
try { |
|
|
|
try { |
|
|
|
// Deducting credit from wallet
|
|
|
|
// Deducting credit from wallet
|
|
|
|
await this.walletService.processPayment(userId, totalAmount); |
|
|
|
await this.walletService.processPayment(userId, totalAmount); |
|
|
@ -120,7 +120,8 @@ export class CartService { |
|
|
|
throw new HttpException("Cart is empty.", HttpStatus.BAD_REQUEST); |
|
|
|
throw new HttpException("Cart is empty.", HttpStatus.BAD_REQUEST); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Process each cart item and update stock
|
|
|
|
// Process each cart item and update stock, create invoice for each product
|
|
|
|
|
|
|
|
const invoices: Invoice[] = []; |
|
|
|
for (const cartItem of cartItems) { |
|
|
|
for (const cartItem of cartItems) { |
|
|
|
const { productId, quantity } = cartItem; |
|
|
|
const { productId, quantity } = cartItem; |
|
|
|
|
|
|
|
|
|
|
@ -134,14 +135,16 @@ export class CartService { |
|
|
|
throw new HttpException(`Insufficient stock for product ID ${productId}.`, HttpStatus.BAD_REQUEST); |
|
|
|
throw new HttpException(`Insufficient stock for product ID ${productId}.`, HttpStatus.BAD_REQUEST); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
product.quantity -= quantity; // Reduce stock
|
|
|
|
// Reduce stock
|
|
|
|
|
|
|
|
product.quantity -= quantity; |
|
|
|
await product.save(); |
|
|
|
await product.save(); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Create the invoice after processing the cart
|
|
|
|
// Create invoice for this product
|
|
|
|
const invoice = await this.invoiceService.createInvoiceFromCart(userId); |
|
|
|
const newInvoice = await this.invoiceService.createInvoiceFromCart(userId); // اصلاح اینجا
|
|
|
|
|
|
|
|
invoices.push(...newInvoice); // اضافه کردن همه فاکتورها به آرایه invoices
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return { message: "Order processed successfully", invoice }; |
|
|
|
return { message: "Order processed successfully", invoices }; |
|
|
|
} catch (error) { |
|
|
|
} catch (error) { |
|
|
|
console.log(error); |
|
|
|
console.log(error); |
|
|
|
if (error instanceof HttpException) { |
|
|
|
if (error instanceof HttpException) { |
|
|
|