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 { JwtAuthGuard } from "src/guard/auth.guard";
import { WalletModule } from "src/wallet/wallet.module"; import { WalletModule } from "src/wallet/wallet.module";
import { InvoiceModule } from "src/invoice/invoice.module"; import { InvoiceModule } from "src/invoice/invoice.module";
import { Invoice } from "src/invoice/entities/invoice.entity";
@Module({ @Module({
imports: [ imports: [
SequelizeModule.forFeature([Cart, User, Product]), SequelizeModule.forFeature([Cart, User, Product,Invoice]),
JwtModule.register({ JwtModule.register({
secret: process.env.JWT_SECRET, secret: process.env.JWT_SECRET,
signOptions: { expiresIn: "1h" }, signOptions: { expiresIn: "1h" },

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

Loading…
Cancel
Save