|
|
|
@ -10,6 +10,7 @@ import { Invoice } from "src/invoice/entities/invoice.entity"; |
|
|
|
|
export class CartService { |
|
|
|
|
constructor( |
|
|
|
|
@InjectModel(Cart) private readonly cartModel: typeof Cart, |
|
|
|
|
@InjectModel(Invoice) private readonly invoiceModel: typeof Invoice, |
|
|
|
|
@InjectModel(Product) private readonly productModel: typeof Product, |
|
|
|
|
private readonly walletService: WalletService, |
|
|
|
|
@Inject(forwardRef(() => InvoiceService)) |
|
|
|
@ -31,6 +32,7 @@ export class CartService { |
|
|
|
|
let cart = await this.cartModel.findOne({ where: { userId, productId, status: "open" } }); |
|
|
|
|
|
|
|
|
|
if (!cart) { |
|
|
|
|
// اگر کارتی برای کاربر و محصول وجود ندارد، ایجاد کنید
|
|
|
|
|
cart = await this.cartModel.create({ |
|
|
|
|
userId, |
|
|
|
|
productId, |
|
|
|
@ -38,18 +40,30 @@ export class CartService { |
|
|
|
|
productPrice: product.price, |
|
|
|
|
status: "open", |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
const invoice = await this.invoiceService.createInvoiceFromCart(userId); |
|
|
|
|
|
|
|
|
|
// بررسی وجود فاکتور در انتظار
|
|
|
|
|
let invoice = await this.invoiceModel.findOne({ where: { userId, status: "pending" } }); |
|
|
|
|
|
|
|
|
|
// اگر فاکتور وجود ندارد، ایجاد کنید
|
|
|
|
|
if (!invoice) { |
|
|
|
|
invoice = await this.invoiceService.createInvoiceFromCart(userId); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// تنظیم `invoiceId` برای کارت و ذخیره
|
|
|
|
|
cart.invoiceId = invoice.id; |
|
|
|
|
await cart.save(); |
|
|
|
|
} else { |
|
|
|
|
const invoice = await this.invoiceService.getInvoiceByUserAndCart(userId); |
|
|
|
|
// اگر کارت وجود دارد، بررسی وجود فاکتور مرتبط
|
|
|
|
|
let invoice = await this.invoiceService.getInvoiceByUserAndCart(userId); |
|
|
|
|
|
|
|
|
|
// اگر فاکتور وجود ندارد، ایجاد کنید
|
|
|
|
|
if (!invoice) { |
|
|
|
|
const newInvoice = await this.invoiceService.createInvoiceFromCart(userId); |
|
|
|
|
cart.invoiceId = newInvoice.id; |
|
|
|
|
invoice = await this.invoiceService.createInvoiceFromCart(userId); |
|
|
|
|
cart.invoiceId = invoice.id; |
|
|
|
|
await cart.save(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const invoiceId = cart.invoiceId; |
|
|
|
|
|
|
|
|
|