Files
negareh-api/src/modules/print-form/controller/print-form.controller.ts
T
2026-07-02 21:31:02 +03:30

80 lines
2.5 KiB
TypeScript

import { Controller, Get, Post, Body, Patch, Param, Delete, UseGuards, Res } from '@nestjs/common';
import { PrintFormService } from '../provider/print-form.service';
import { CreateSectionDto } from '../dto/create-section.dto';
import { ApiBearerAuth, ApiOperation } from '@nestjs/swagger';
import { AdminAuthGuard } from '../../auth/guards/adminAuth.guard';
import { UpdateSectionDto } from '../dto/update-section.dto';
import { Permissions } from 'src/common/decorators/permissions.decorator';
import { PermissionEnum } from 'src/common/enums/permission.enum';
import type { FastifyReply } from 'fastify';
@Controller('admin')
@UseGuards(AdminAuthGuard)
@ApiBearerAuth()
export class PrintFormController {
constructor(
private readonly printFormService: PrintFormService,
) { }
/*================================ Section ========================== */
@Post('section')
@Permissions(PermissionEnum.VIEW_PRINT_FORM)
@ApiOperation({ summary: 'Create section' })
create(@Body() dto: CreateSectionDto) {
return this.printFormService.create(dto);
}
@Get('section')
@ApiOperation({ summary: 'find all section' })
@Permissions(PermissionEnum.VIEW_PRINT_FORM)
findAll() {
return this.printFormService.findAll();
}
@Get('section/:id')
@ApiOperation({ summary: 'Find one section' })
@Permissions(PermissionEnum.VIEW_PRINT_FORM)
findOne(@Param('id') id: string) {
return this.printFormService.finOrFail(id);
}
@Patch('section/:id')
@Permissions(PermissionEnum.VIEW_PRINT_FORM)
@ApiOperation({ summary: 'Update section' })
update(@Param('id') id: string, @Body() dto: UpdateSectionDto) {
return this.printFormService.update(id, dto);
}
@Delete('section/:id')
@Permissions(PermissionEnum.VIEW_PRINT_FORM)
@ApiOperation({ summary: 'Remove section' })
remove(@Param('id') id: string) {
return this.printFormService.remove(id);
}
/// front end must send form with its selected fields
@Post('pdf')
@Permissions(PermissionEnum.VIEW_PRINT_FORM)
@ApiOperation({ summary: 'Create Pdf' })
async createPDf(@Body() dto: CreateSectionDto, @Res() res: FastifyReply) {
const html = `
<html>
<body>
<h1>Hello from NestJS PDF</h1>
<p>This PDF was generated using Puppeteer.</p>
</body>
</html>
`;
const pdfBuffer = await this.printFormService.generatePdf(html);
res
.header('Content-Type', 'application/pdf')
.header('Content-Disposition', 'attachment; filename="file.pdf"')
.send(pdfBuffer);
}
}