import { forwardRef, HttpException, HttpStatus, Inject, Injectable } from "@nestjs/common"; import { InjectModel } from "@nestjs/sequelize"; import { Invoice } from "./entities/invoice.entity"; import { CartService } from "src/cart/cart.service"; import { User } from "src/users/entities/user.entity"; @Injectable() export class InvoiceService { constructor( @InjectModel(Invoice) private readonly invoiceModel: typeof Invoice, @Inject(forwardRef(() => CartService)) private cartService: CartService, ) {} async createInvoiceFromCart(userId: number): Promise { const user = await User.findByPk(userId); if (!user) { throw new HttpException("User not found", HttpStatus.NOT_FOUND); } try { const invoice = await this.invoiceModel.create({ userId, totalPaymentAmount: 0, }); if (!invoice) { throw new HttpException("Failed to create invoice", HttpStatus.INTERNAL_SERVER_ERROR); } return invoice; } catch (error) { console.error("Error during invoice creation:", error); throw new HttpException("An error occurred while creating the invoice.", HttpStatus.INTERNAL_SERVER_ERROR); } } async updateTotalPayment(userId: number) { const user = await User.findByPk(userId); if (!user) { throw new HttpException("User not found", HttpStatus.NOT_FOUND); } const userCartItems = await this.cartService.getUserOpenCart(userId); if (!userCartItems || !userCartItems.cartItems || userCartItems.cartItems.length === 0) { throw new HttpException("Cart is empty", HttpStatus.BAD_REQUEST); } let invoice = await this.invoiceModel.findOne({ where: { userId, status: "pending" } }); if (!invoice) { throw new HttpException("Invoice not found", HttpStatus.NOT_FOUND); } invoice.totalPaymentAmount = userCartItems.totalPrice; await invoice.save(); } async getInvoicePendingByUser(userId: number): Promise { try { if (!userId) { throw new HttpException("User ID are required.", HttpStatus.BAD_REQUEST); } const invoice = await this.invoiceModel.findOne({ where: { userId, status: "pending" }, }); if (!invoice) { throw new HttpException("Invoice not found for this user and cart.", HttpStatus.NOT_FOUND); } return invoice; } catch (error) { if (error instanceof HttpException) { throw error; } throw new HttpException("An error occurred while retrieving the invoice.", HttpStatus.INTERNAL_SERVER_ERROR); } } async getInvoiceByUser(userId: number) { try { if (!userId) { throw new HttpException("User ID are required.", HttpStatus.BAD_REQUEST); } const invoices = await this.invoiceModel.findAll({ where: { userId }, }); if (!invoices) { throw new HttpException("Invoice not found for this user and cart.", HttpStatus.NOT_FOUND); } return { invoices }; } catch (error) { if (error instanceof HttpException) { throw error; } throw new HttpException("An error occurred while retrieving the invoice.", HttpStatus.INTERNAL_SERVER_ERROR); } } async getInvoices() { try { const invoices = await this.invoiceModel.findAll(); if (!invoices) { throw new HttpException("Invoice not found for this user and cart.", HttpStatus.NOT_FOUND); } return { invoices }; } catch (error) { if (error instanceof HttpException) { throw error; } throw new HttpException("An error occurred while retrieving the invoice.", HttpStatus.INTERNAL_SERVER_ERROR); } } async getUserInvoices(userId: number) { try { if (!userId) { throw new HttpException("User ID are required.", HttpStatus.BAD_REQUEST); } const invoices = await this.invoiceModel.findAll({ where: { userId }, }); if (!invoices) { throw new HttpException("Invoice not found for this user and cart.", HttpStatus.NOT_FOUND); } return { invoices }; } catch (error) { if (error instanceof HttpException) { throw error; } throw new HttpException("An error occurred while retrieving the invoice.", HttpStatus.INTERNAL_SERVER_ERROR); } } }