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. 65
      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 { InvoiceService } from "./invoice.service";
import { JwtAuthGuard } from "src/guard/auth.guard";
import { RoleGuard } from "src/guard/role.guard";
@Controller("invoice") @Controller("invoice")
export class InvoiceController { export class InvoiceController {
constructor(private readonly invoiceService: InvoiceService) {} constructor(private readonly invoiceService: InvoiceService) {}
@Get(":userId") @UseGuards(JwtAuthGuard)
async getInvoices(@Param("userId") userId: number): Promise<any> { @Get()
async getInvoiceByUser(@Request() req) {
const userId = req.user.id;
return this.invoiceService.getInvoiceByUser(userId); 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 { InvoiceService } from "./invoice.service";
import { Invoice } from "./entities/invoice.entity"; import { Invoice } from "./entities/invoice.entity";
import { CartModule } from "src/cart/cart.module"; 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({ @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], controllers: [InvoiceController],
providers: [InvoiceService], providers: [InvoiceService,JwtAuthGuard,RoleGuard],
exports: [InvoiceService], exports: [InvoiceService],
}) })
export class InvoiceModule {} export class InvoiceModule {}

@ -43,14 +43,14 @@ export class InvoiceService {
await invoice.save(); await invoice.save();
} }
async getInvoiceByUser(userId: number): Promise<Invoice> { async getInvoicePendingByUser(userId: number): Promise<Invoice> {
try { try {
if (!userId ) { if (!userId) {
throw new HttpException("User ID are required.", HttpStatus.BAD_REQUEST); throw new HttpException("User ID are required.", HttpStatus.BAD_REQUEST);
} }
const invoice = await this.invoiceModel.findOne({ const invoice = await this.invoiceModel.findOne({
where: { userId, status:'pending' }, where: { userId, status: "pending" },
}); });
if (!invoice) { if (!invoice) {
@ -65,5 +65,64 @@ export class InvoiceService {
throw new HttpException("An error occurred while retrieving the invoice.", HttpStatus.INTERNAL_SERVER_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);
}
}
} }

Loading…
Cancel
Save