import { Body, ConflictException, Controller, Delete, Get, Post, Query, Request, UseGuards, } from '@nestjs/common'; import { AuthGuard } from '@nestjs/passport'; import { ApiBearerAuth, ApiBody, ApiOperation, ApiQuery, ApiTags, } from '@nestjs/swagger'; import { Serealize } from '../interceptors/serialize.interceptor'; import { CarreraProgramaService } from './carrera-programa.service'; import { Operador } from 'src/operador/entity/operador.entity'; import { IdInstitucionDto } from '../dto/id-institucion.dto'; import { CreateCarreraProgramaDto } from './dto/input/create.dto'; import { DeleteCarreraProgramaDto } from './dto/input/delete.dto'; import { CarreraProgramaOutputDto } from './dto/output/carrera-programa.dto'; @Controller('carrera-programa') @ApiTags('carrera-programa') export class CarreraProgramaController { constructor(private carreraProgramaService: CarreraProgramaService) {} @Post() @UseGuards(AuthGuard('jwt')) @ApiOperation({ description: 'Endpoint que crea una asociación entre una carrera y un programa.', }) @ApiBearerAuth('jwt') @ApiBody({ description: 'Ambas variables son obligatorios.', examples: { ejemplo: { value: { id_institucion_carrera: 36, id_programa: 1 } }, }, }) create(@Request() req, @Body() body: CreateCarreraProgramaDto) { const admin: Operador = req.user.operador; if (!admin || admin.tipoUsuario.id_tipo_usuario != 3) throw new ConflictException( 'No tienes permisos para realizar esta acción.', ); return this.carreraProgramaService.create( admin, body.id_institucion_carrera, body.id_programa, ); } @Delete() @UseGuards(AuthGuard('jwt')) @ApiOperation({ description: 'Endpoint que borra la asociación entre una carrera y un programa.', }) @ApiBearerAuth('jwt') @ApiBody({ description: 'Es obligatorio mandar la variable id_carrera_programa.', examples: { ejemplo: { value: { id_carrera_programa: 1 } } }, }) delete(@Request() req, @Body() body: DeleteCarreraProgramaDto) { const admin: Operador = req.user.operador; if (!admin || admin.tipoUsuario.id_tipo_usuario != 3) throw new ConflictException( 'No tienes permisos para realizar esta acción.', ); return this.carreraProgramaService.delete(admin, body.id_carrera_programa); } @Serealize(CarreraProgramaOutputDto) @Get() @UseGuards(AuthGuard('jwt')) @ApiOperation({ description: 'Endpoint que retorna todos los programas reservados para una carrera de una institución.', }) @ApiBearerAuth('jwt') @ApiQuery({ description: 'Id de la institución.', name: 'id_institucion', type: 'string', }) get(@Query() query: IdInstitucionDto) { return this.carreraProgramaService.findByIdInstitucion( parseInt(query.id_institucion), ); } }