import { Controller, Get, Post, Put, Body, Query, UploadedFile, UploadedFiles, UseGuards, UseInterceptors, Res, ParseIntPipe, Param, } from '@nestjs/common'; import { FileInterceptor, FileFieldsInterceptor, } from '@nestjs/platform-express'; import { ServicioService } from './servicio.service'; import { AuthGuard } from '@nestjs/passport'; import { CrearServicioDto } from './dto/create-servicio.dto'; import { Response } from 'express'; import { ServiciosAdminDto } from './dto/servicios-admin.dto'; import { ServiciosResponsableDto } from './dto/servicios-responsable.dto'; @Controller('servicio') export class ServicioController { constructor(private readonly servicioService: ServicioService) {} // ------------------ POST ------------------ @Post('nuevo') @UseGuards(AuthGuard('jwt')) @UseInterceptors(FileInterceptor('cartaAceptacion')) async crearServicio( @UploadedFile() file: Express.Multer.File, @Body('alumno') alumnoJson: string, ) { const dto: CrearServicioDto = JSON.parse(alumnoJson); return await this.servicioService.registrarNuevoServicio(dto, file); } // ------------------ GET ------------------ @Get('todos') //@UseGuards(AuthGuard('jwt')) async obtenerTodosLosServicios() { return this.servicioService.obtenerTodosLosServicios(); } @Get('admin') @UseGuards(AuthGuard('jwt')) async obtenerServiciosAdmin(@Query() dto: ServiciosAdminDto) { return await this.servicioService.obtenerServiciosAdmin(dto); } @Get('servicios_responsable') //@UseGuards(AuthGuard('jwt')) async obtenerServiciosResponsable(@Query() dto: ServiciosResponsableDto) { return await this.servicioService.obtenerServiciosResponsable(dto); } @Get('admin/:idServicio') @UseGuards(AuthGuard('jwt')) async obtenerAdmin(@Param('idServicio') idServicio: number) { return this.servicioService.obtenerDetalleServicio(idServicio); } @Get('alumno') @UseGuards(AuthGuard('jwt')) async obtenerAlumno(@Query('idUsuario', ParseIntPipe) idUsuario: number) { return this.servicioService.obtenerServicioAlumno(idUsuario); } @Get('gustavo_baz_prada') @UseGuards(AuthGuard('jwt')) async gustavoBaz( @Query('year', ParseIntPipe) year: number, @Res() res: Response, ) { const path = await this.servicioService.generarGustavoBazPrada(year); return res.download(path); } @Get('reporte') @UseGuards(AuthGuard('jwt')) async generarReporte(@Query() query: any, @Res() res: Response) { const path = await this.servicioService.generarGustavoBazPrada(query.year); return res.download(path); } // ------------------ PUT ------------------ @Put('cancelar') @UseGuards(AuthGuard('jwt')) async cancelar(@Body() body: { idServicio: number; mensaje: string }) { return this.servicioService.cancelarServicio(body); } @Put('carta_aceptacion') @UseGuards(AuthGuard('jwt')) @UseInterceptors(FileInterceptor('cartaAceptacion')) async subirCartaAceptacion( @UploadedFile() file: Express.Multer.File, @Body('data') dataJson: string, ) { const body = JSON.parse(dataJson); return this.servicioService.cartaTermino(body.idServicio, file); } @Put('carta_termino') @UseGuards(AuthGuard('jwt')) @UseInterceptors(FileInterceptor('cartaTermino')) async subirCartaTermino( @UploadedFile() file: Express.Multer.File, @Body('data') dataJson: string, ) { const body = JSON.parse(dataJson); return this.servicioService.subirCartaTermino(body.idServicio, file); } @Put('informe_global') @UseGuards(AuthGuard('jwt')) @UseInterceptors(FileInterceptor('informeGlobal')) async subirInformeGlobal( @UploadedFile() file: Express.Multer.File, @Body('data') dataJson: string, ) { const body = JSON.parse(dataJson); return this.servicioService.subirInformeGlobal(body.idServicio, file); } @Put('liberacion') @UseGuards(AuthGuard('jwt')) async liberarServicio( @Body('idServicio', ParseIntPipe) idServicio: number, @Body('vistoBuenoAcatlan') vistoBuenoAcatlan: boolean, ) { return this.servicioService.liberarServicio(idServicio, vistoBuenoAcatlan); } @Put('rechazar_aceptacion') @UseGuards(AuthGuard('jwt')) async rechazarAceptacion( @Body() body: { idServicio: number; mensaje: string }, ) { return this.servicioService.rechazar(body.idServicio, body.mensaje); } @Put('rechazar_informe') @UseGuards(AuthGuard('jwt')) async rechazarInforme(@Body() body: { idServicio: number; mensaje: string }) { return this.servicioService.rechazarInforme(body.idServicio, body.mensaje); } @Put('rechazar_termino') @UseGuards(AuthGuard('jwt')) async rechazarTermino(@Body() body: { idServicio: number; mensaje: string }) { return this.servicioService.rechazarTermino(body.idServicio, body.mensaje); } @Put('update') @UseGuards(AuthGuard('jwt')) @UseInterceptors( FileFieldsInterceptor([ { name: 'cartaAceptacion', maxCount: 1 }, { name: 'cartaTermino', maxCount: 1 }, { name: 'informeGlobal', maxCount: 1 }, ]), ) async actualizarServicio( @UploadedFiles() files: Record, @Body('data') dataJson: string, ) { const data = JSON.parse(dataJson); return this.servicioService.update(data, files); } }