diff --git a/migrations/20250105085732-create-invoice.js b/migrations/20250105085732-create-invoice.js index af7f78d..76b70b2 100644 --- a/migrations/20250105085732-create-invoice.js +++ b/migrations/20250105085732-create-invoice.js @@ -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"); }, }; diff --git a/src/cart/cart.controller.ts b/src/cart/cart.controller.ts index 22bce65..9a95591 100644 --- a/src/cart/cart.controller.ts +++ b/src/cart/cart.controller.ts @@ -49,7 +49,7 @@ export class CartController { async processOrder( @Param('userId') userId: number, @Body('totalAmount') totalAmount: number, - ): Promise<{ message: string; invoices: Invoice[] }> { + ):Promise<{ message: string; invoices: Invoice[] }> { if (!totalAmount || totalAmount <= 0) { throw new HttpException('Invalid total amount.', HttpStatus.BAD_REQUEST); } diff --git a/src/cart/cart.service.ts b/src/cart/cart.service.ts index fb440c9..335e7f9 100644 --- a/src/cart/cart.service.ts +++ b/src/cart/cart.service.ts @@ -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 { } } + + } diff --git a/src/invoice/entities/invoice.entity.ts b/src/invoice/entities/invoice.entity.ts index 27ccbbe..37858fe 100644 --- a/src/invoice/entities/invoice.entity.ts +++ b/src/invoice/entities/invoice.entity.ts @@ -4,7 +4,7 @@ import { User } from "../../users/entities/user.entity"; @Table export class Invoice extends Model { - + @ForeignKey(() => User) @Column userId: number; @@ -37,7 +37,7 @@ export class Invoice extends Model { email: string; @Column - totalAmount: number; + totalPaymentAmount: number; @Column({ type: DataType.INTEGER, @@ -57,6 +57,12 @@ export class Invoice extends Model { }) price: number; + @Column({ + type: DataType.DECIMAL(10, 2), + allowNull: false, + }) + totalPrice: number; + @Column({ type: DataType.STRING, allowNull: false, diff --git a/src/invoice/invoice.service.ts b/src/invoice/invoice.service.ts index bc46c66..d00e28b 100644 --- a/src/invoice/invoice.service.ts +++ b/src/invoice/invoice.service.ts @@ -17,43 +17,36 @@ export class InvoiceService { if (!user) { throw new HttpException("User not found", HttpStatus.NOT_FOUND); } - + const userCartItems = await this.cartService.getUserCart(userId); if (!userCartItems || userCartItems.cartItems.length === 0) { 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) { + + const invoices: Invoice[] = []; + + 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 { try { if (!userId) {