123 lines
3.7 KiB
TypeScript
123 lines
3.7 KiB
TypeScript
import {
|
|
Body,
|
|
ConflictException,
|
|
Controller,
|
|
Get,
|
|
Post,
|
|
Put,
|
|
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 { InstitucionProgramaService } from './institucion-programa.service';
|
|
import { Operador } from 'src/operador/entity/operador.entity';
|
|
import { IdInstitucionDto } from '../dto/id-institucion.dto';
|
|
import { CreateProgramaDto } from './dto/input/create.dto';
|
|
import { UpdateProgramaDto } from './dto/input/update.dto';
|
|
import { InstitucionProgramaOutputDto } from './dto/output/institucion-programa.dto';
|
|
import { InstitucionProgramaMinOutputDto } from './dto/output/institucion-programa-min.dto';
|
|
import { ProgramaOutputDto } from './dto/output/programa.dto';
|
|
|
|
@Controller('institucion-programa')
|
|
@ApiTags('institucion-programa')
|
|
export class InstitucionProgramaController {
|
|
constructor(private institucionProgramaService: InstitucionProgramaService) {}
|
|
|
|
@Post()
|
|
@UseGuards(AuthGuard('jwt'))
|
|
@ApiOperation({ description: 'Endpoint que crea un programa nuevo.' })
|
|
@ApiBearerAuth('jwt')
|
|
@ApiBody({
|
|
description: 'Es obligatorio mandar la varible programa.',
|
|
examples: { ejemplo: { value: { programa: '' } } },
|
|
})
|
|
create(@Request() req, @Body() body: CreateProgramaDto) {
|
|
const superAdmin: Operador = req.user.operador;
|
|
|
|
if (!superAdmin || superAdmin.tipoUsuario.id_tipo_usuario != 2)
|
|
throw new ConflictException(
|
|
'No tienes permisos para realizar esta acción.',
|
|
);
|
|
return this.institucionProgramaService.create(body.programa);
|
|
}
|
|
|
|
@Serealize(ProgramaOutputDto)
|
|
@Get()
|
|
@UseGuards(AuthGuard('jwt'))
|
|
@ApiOperation({ description: 'Endpoint que retorna todos los programas.' })
|
|
@ApiBearerAuth('jwt')
|
|
get() {
|
|
return this.institucionProgramaService.findAllProgramas();
|
|
}
|
|
|
|
@Serealize(InstitucionProgramaOutputDto)
|
|
@Get('programas')
|
|
@UseGuards(AuthGuard('jwt'))
|
|
@ApiOperation({
|
|
description: 'Endpoint que retorna todos los programas de una institución.',
|
|
})
|
|
@ApiBearerAuth('jwt')
|
|
@ApiQuery({
|
|
description: 'Id de la institución.',
|
|
name: 'id_institucion',
|
|
type: 'string',
|
|
})
|
|
programas(@Query() query: IdInstitucionDto) {
|
|
return this.institucionProgramaService.findAllByIdInstitucion(
|
|
parseInt(query.id_institucion),
|
|
);
|
|
}
|
|
|
|
@Serealize(InstitucionProgramaMinOutputDto)
|
|
@Get('programas-mostrar')
|
|
@UseGuards(AuthGuard('jwt'))
|
|
@ApiOperation({
|
|
description:
|
|
'Endpoint que retorna todos los programas de una institución que tengan el campo mostrar en true.',
|
|
})
|
|
@ApiBearerAuth('jwt')
|
|
@ApiQuery({
|
|
description: 'Id de la institución.',
|
|
name: 'id_institucion',
|
|
type: 'string',
|
|
})
|
|
programasMostrar(@Query() query: IdInstitucionDto) {
|
|
return this.institucionProgramaService.findAllByIdInstitucion(
|
|
parseInt(query.id_institucion),
|
|
true,
|
|
);
|
|
}
|
|
|
|
@Put()
|
|
@UseGuards(AuthGuard('jwt'))
|
|
@ApiOperation({
|
|
description:
|
|
'Endpoint que actualiza la información de una institución programa.',
|
|
})
|
|
@ApiBearerAuth('jwt')
|
|
@ApiBody({
|
|
description: 'Es obligatorio mandar la varible programa.',
|
|
examples: {
|
|
ejemplo: { value: { id_institucion_programa: 130, mostrar: true } },
|
|
},
|
|
})
|
|
update(@Request() req, @Body() body: UpdateProgramaDto) {
|
|
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.institucionProgramaService.update(admin, body);
|
|
}
|
|
}
|