Fix issue with ID in invoice module

master
nicekid1 2 months ago
parent 7fa178b293
commit 7da8f22cb2
  1. 27
      migrations/20250105085732-create-invoice.js
  2. 17
      src/cart/cart.service.ts
  3. 8
      src/invoice/entities/invoice.entity.ts
  4. 31
      src/invoice/invoice.service.ts

@ -1,10 +1,10 @@
'use strict';
"use strict";
module.exports = {
up: async (queryInterface, Sequelize) => {
await queryInterface.dropTable('Invoices', { cascade: true });
await queryInterface.dropTable("Invoices", { cascade: true });
await queryInterface.createTable('Invoices', {
await queryInterface.createTable("Invoices", {
id: {
type: Sequelize.INTEGER,
autoIncrement: true,
@ -15,10 +15,10 @@ module.exports = {
type: Sequelize.INTEGER,
allowNull: false,
references: {
model: 'Users',
key: 'id',
model: "Users",
key: "id",
},
onDelete: 'CASCADE',
onDelete: "CASCADE",
},
firstName: {
type: Sequelize.STRING,
@ -37,8 +37,8 @@ module.exports = {
allowNull: false,
unique: false,
},
totalAmount: {
type: Sequelize.FLOAT,
totalPaymentAmount: {
type: Sequelize.DECIMAL(10, 2),
allowNull: true,
},
productId: {
@ -53,6 +53,10 @@ module.exports = {
type: Sequelize.DECIMAL(10, 2),
allowNull: false,
},
totalPrice: {
type: Sequelize.DECIMAL(10, 2),
allowNull: false,
},
productName: {
type: Sequelize.STRING,
allowNull: false,
@ -60,18 +64,17 @@ module.exports = {
createdAt: {
type: Sequelize.DATE,
allowNull: false,
defaultValue: Sequelize.fn('NOW'),
defaultValue: Sequelize.fn("NOW"),
},
updatedAt: {
type: Sequelize.DATE,
allowNull: false,
defaultValue: Sequelize.fn('NOW'),
defaultValue: Sequelize.fn("NOW"),
},
});
},
down: async (queryInterface, Sequelize) => {
await queryInterface.dropTable('Invoices');
await queryInterface.dropTable("Invoices");
},
};

@ -120,8 +120,7 @@ export class CartService {
throw new HttpException("Cart is empty.", HttpStatus.BAD_REQUEST);
}
// Process each cart item and update stock, create invoice for each product
const invoices: Invoice[] = [];
// Process each cart item and update stock
for (const cartItem of cartItems) {
const { productId, quantity } = cartItem;
@ -135,16 +134,14 @@ export class CartService {
throw new HttpException(`Insufficient stock for product ID ${productId}.`, HttpStatus.BAD_REQUEST);
}
// Reduce stock
product.quantity -= quantity;
product.quantity -= quantity; // Reduce stock
await product.save();
// Create invoice for this product
const newInvoice = await this.invoiceService.createInvoiceFromCart(userId); // اصلاح اینجا
invoices.push(...newInvoice); // اضافه کردن همه فاکتورها به آرایه invoices
}
return { message: "Order processed successfully", invoices };
// Create the invoices for all cart items
const invoices = await this.invoiceService.createInvoiceFromCart(userId);
return { message: "Order processed successfully", invoices }; // Return invoices as an array
} catch (error) {
console.log(error);
if (error instanceof HttpException) {
@ -155,4 +152,6 @@ export class CartService {
}
}
}

@ -37,7 +37,7 @@ export class Invoice extends Model<Invoice> {
email: string;
@Column
totalAmount: number;
totalPaymentAmount: number;
@Column({
type: DataType.INTEGER,
@ -57,6 +57,12 @@ export class Invoice extends Model<Invoice> {
})
price: number;
@Column({
type: DataType.DECIMAL(10, 2),
allowNull: false,
})
totalPrice: number;
@Column({
type: DataType.STRING,
allowNull: false,

@ -23,37 +23,30 @@ export class InvoiceService {
throw new HttpException("Cart is empty", HttpStatus.BAD_REQUEST);
}
const products = userCartItems.cartItems.map(item => {
return {
productId: item.productId,
quantity: item.quantity,
price: item.productPrice,
productName: item.productName,
totalPrice: item.totalPrice,
};
});
// ذخیره کردن فاکتورهای هر محصول و بازگشت آرایه فاکتورها
const invoices: Invoice[] = [];
for (const product of products) {
for (const cartItem of userCartItems.cartItems) {
const invoice = await this.invoiceModel.create({
userId,
firstName: user.firstName,
lastName: user.lastName,
phoneNumber: user.phoneNumber,
email: user.email,
totalAmount: userCartItems.totalPrice,
productId: product.productId,
quantity: product.quantity,
price: product.price,
productName: product.productName,
totalPaymentAmount: userCartItems.totalPrice,
productId: cartItem.productId,
quantity: cartItem.quantity,
price: cartItem.productPrice,
totalPrice:(cartItem.quantity*cartItem.productPrice),
productName: cartItem.productName,
});
invoices.push(invoice); // ذخیره فاکتور برای هر محصول
invoices.push(invoice);
}
return invoices;
return invoices; // بازگرداندن آرایهای از فاکتورها
}
async getInvoicesByUser(userId: number): Promise<Invoice[]> {
try {
if (!userId) {

Loading…
Cancel
Save