From 7fa178b293e12d52fb7473ba272f698b44326de7 Mon Sep 17 00:00:00 2001 From: nicekid1 <86746988+nicekid1@users.noreply.github.com> Date: Mon, 6 Jan 2025 12:56:41 +0330 Subject: [PATCH] Enhance invoicing system --- src/cart/cart.controller.ts | 29 ++++++++++++-------------- src/cart/cart.service.ts | 19 ++++++++++------- src/invoice/entities/invoice.entity.ts | 2 ++ src/invoice/invoice.service.ts | 17 ++++++--------- 4 files changed, 32 insertions(+), 35 deletions(-) diff --git a/src/cart/cart.controller.ts b/src/cart/cart.controller.ts index f0f6c87..22bce65 100644 --- a/src/cart/cart.controller.ts +++ b/src/cart/cart.controller.ts @@ -46,23 +46,20 @@ export class CartController { } @Post(':userId/checkout') -async processOrder( - @Param('userId') userId: number, - @Body('totalAmount') totalAmount: number, -): Promise<{ message: string; invoice: Invoice }> { - if (!totalAmount || totalAmount <= 0 || isNaN(totalAmount)) { - throw new HttpException('Invalid total amount.', HttpStatus.BAD_REQUEST); - } + async processOrder( + @Param('userId') userId: number, + @Body('totalAmount') totalAmount: number, + ): Promise<{ message: string; invoices: Invoice[] }> { + if (!totalAmount || totalAmount <= 0) { + throw new HttpException('Invalid total amount.', HttpStatus.BAD_REQUEST); + } - try { - const result = await this.cartService.processOrder(userId, totalAmount); - return result; - } catch (error) { - throw new HttpException( - error.message || 'An unexpected error occurred while processing the order.', - HttpStatus.INTERNAL_SERVER_ERROR, - ); + try { + const result = await this.cartService.processOrder(userId, totalAmount); + return result; + } catch (error) { + throw new HttpException(error.message || 'Order processing failed.', HttpStatus.INTERNAL_SERVER_ERROR); + } } -} } diff --git a/src/cart/cart.service.ts b/src/cart/cart.service.ts index 03f15ad..fb440c9 100644 --- a/src/cart/cart.service.ts +++ b/src/cart/cart.service.ts @@ -108,8 +108,8 @@ export class CartService { await this.cartModel.destroy({ where: { userId } }); } - //order(clearCart unable) - async processOrder(userId: number, totalAmount: number): Promise<{ message: string; invoice: Invoice }> { + //order(clearCart disable) + async processOrder(userId: number, totalAmount: number): Promise<{ message: string; invoices: Invoice[] }> { try { // Deducting credit from wallet await this.walletService.processPayment(userId, totalAmount); @@ -120,7 +120,8 @@ export class CartService { throw new HttpException("Cart is empty.", HttpStatus.BAD_REQUEST); } - // Process each cart item and update stock + // Process each cart item and update stock, create invoice for each product + const invoices: Invoice[] = []; for (const cartItem of cartItems) { const { productId, quantity } = cartItem; @@ -134,14 +135,16 @@ export class CartService { throw new HttpException(`Insufficient stock for product ID ${productId}.`, HttpStatus.BAD_REQUEST); } - product.quantity -= quantity; // Reduce stock + // Reduce stock + product.quantity -= quantity; await product.save(); - } - // Create the invoice after processing the cart - const invoice = await this.invoiceService.createInvoiceFromCart(userId); + // Create invoice for this product + const newInvoice = await this.invoiceService.createInvoiceFromCart(userId); // اصلاح اینجا + invoices.push(...newInvoice); // اضافه کردن همه فاکتورها به آرایه invoices + } - return { message: "Order processed successfully", invoice }; + return { message: "Order processed successfully", invoices }; } catch (error) { console.log(error); if (error instanceof HttpException) { diff --git a/src/invoice/entities/invoice.entity.ts b/src/invoice/entities/invoice.entity.ts index aec15c0..27ccbbe 100644 --- a/src/invoice/entities/invoice.entity.ts +++ b/src/invoice/entities/invoice.entity.ts @@ -3,6 +3,8 @@ import { User } from "../../users/entities/user.entity"; @Table export class Invoice extends Model { + + @ForeignKey(() => User) @Column userId: number; diff --git a/src/invoice/invoice.service.ts b/src/invoice/invoice.service.ts index 5fe4e3b..bc46c66 100644 --- a/src/invoice/invoice.service.ts +++ b/src/invoice/invoice.service.ts @@ -12,7 +12,7 @@ export class InvoiceService { private cartService: CartService, ) {} - async createInvoiceFromCart(userId: number): Promise { + async createInvoiceFromCart(userId: number): Promise { const user = await User.findByPk(userId); if (!user) { throw new HttpException("User not found", HttpStatus.NOT_FOUND); @@ -33,8 +33,10 @@ export class InvoiceService { }; }); + // ذخیره کردن فاکتورهای هر محصول و بازگشت آرایه فاکتورها + const invoices: Invoice[] = []; for (const product of products) { - await this.invoiceModel.create({ + const invoice = await this.invoiceModel.create({ userId, firstName: user.firstName, lastName: user.lastName, @@ -46,17 +48,10 @@ export class InvoiceService { price: product.price, productName: product.productName, }); + invoices.push(invoice); // ذخیره فاکتور برای هر محصول } - const newInvoice = new Invoice({ - userId, - firstName: user.firstName, - lastName: user.lastName, - phoneNumber: user.phoneNumber, - email: user.email, - totalAmount: userCartItems.totalPrice, - }); - return newInvoice; + return invoices; } async getInvoicesByUser(userId: number): Promise {