Add endpoints for listing and viewing invoice details of a user by admin

master
nicekid1 1 month ago
parent 0359e22e40
commit 7d8998b2f1
  1. 21
      src/invoice/invoice.controller.ts
  2. 12
      src/invoice/invoice.module.ts
  3. 75
      src/invoice/invoice.service.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<any> {
@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);
}
}

@ -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 {}

@ -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<Invoice> {
async getInvoicePendingByUser(userId: number): Promise<Invoice> {
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);
}
}
}

Loading…
Cancel
Save