diff --git a/src/invoice/invoice.controller.ts b/src/invoice/invoice.controller.ts index fb675db..100f285 100644 --- a/src/invoice/invoice.controller.ts +++ b/src/invoice/invoice.controller.ts @@ -1,11 +1,26 @@ -import { Controller, Get, Post, Body, Patch, Param, Delete } from "@nestjs/common"; +import { Controller, Get, Post, Body, Patch, Param, Delete, UseGuards, Request } from "@nestjs/common"; import { InvoiceService } from "./invoice.service"; +import { JwtAuthGuard } from "src/guard/auth.guard"; +import { RoleGuard } from "src/guard/role.guard"; @Controller("invoice") export class InvoiceController { constructor(private readonly invoiceService: InvoiceService) {} - @Get(":userId") - async getInvoices(@Param("userId") userId: number): Promise { + @UseGuards(JwtAuthGuard) + @Get() + async getInvoiceByUser(@Request() req) { + const userId = req.user.id; return this.invoiceService.getInvoiceByUser(userId); } + @UseGuards(RoleGuard) + @Get('list') + async getInvoices() { + return this.invoiceService.getInvoices(); + } + @UseGuards(RoleGuard) + @Get(':id') + async getUserInvoice(@Param('id') id:number) { + return this.invoiceService.getUserInvoices(id); + } + } diff --git a/src/invoice/invoice.module.ts b/src/invoice/invoice.module.ts index 69adf63..31959a3 100644 --- a/src/invoice/invoice.module.ts +++ b/src/invoice/invoice.module.ts @@ -4,11 +4,19 @@ import { InvoiceController } from "./invoice.controller"; import { InvoiceService } from "./invoice.service"; import { Invoice } from "./entities/invoice.entity"; import { CartModule } from "src/cart/cart.module"; +import { JwtModule } from "@nestjs/jwt"; +import { JwtAuthGuard } from "src/guard/auth.guard"; +import { RoleGuard } from "src/guard/role.guard"; @Module({ - imports: [SequelizeModule.forFeature([Invoice]), forwardRef(()=>CartModule)], + imports: [SequelizeModule.forFeature([Invoice]), + JwtModule.register({ + secret: process.env.JWT_SECRET, + signOptions: { expiresIn: "1h" }, + }), + forwardRef(()=>CartModule)], controllers: [InvoiceController], - providers: [InvoiceService], + providers: [InvoiceService,JwtAuthGuard,RoleGuard], exports: [InvoiceService], }) export class InvoiceModule {} diff --git a/src/invoice/invoice.service.ts b/src/invoice/invoice.service.ts index 3d6cb4f..8afe856 100644 --- a/src/invoice/invoice.service.ts +++ b/src/invoice/invoice.service.ts @@ -28,29 +28,29 @@ export class InvoiceService { 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 getInvoiceByUser(userId: number): Promise { + + async getInvoicePendingByUser(userId: number): Promise { try { - if (!userId ) { + if (!userId) { throw new HttpException("User ID are required.", HttpStatus.BAD_REQUEST); } const invoice = await this.invoiceModel.findOne({ - where: { userId, status:'pending' }, + where: { userId, status: "pending" }, }); if (!invoice) { @@ -65,5 +65,64 @@ export class InvoiceService { 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); + } + } }