Debug issue with creating multiple invoices for carts

master
nicekid1 2 months ago
parent 4520299ef0
commit c5c334692d
  1. 3
      src/cart/cart.module.ts
  2. 24
      src/cart/cart.service.ts

@ -9,10 +9,11 @@ import { JwtModule } from "@nestjs/jwt";
import { JwtAuthGuard } from "src/guard/auth.guard";
import { WalletModule } from "src/wallet/wallet.module";
import { InvoiceModule } from "src/invoice/invoice.module";
import { Invoice } from "src/invoice/entities/invoice.entity";
@Module({
imports: [
SequelizeModule.forFeature([Cart, User, Product]),
SequelizeModule.forFeature([Cart, User, Product,Invoice]),
JwtModule.register({
secret: process.env.JWT_SECRET,
signOptions: { expiresIn: "1h" },

@ -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;

Loading…
Cancel
Save