institucion programa endpoints restringidos a usuario
This commit is contained in:
@@ -19,12 +19,12 @@ import {
|
||||
} from '@nestjs/swagger';
|
||||
import { Serealize } from '../interceptors/serialize.interceptor';
|
||||
import { InstitucionInfraccionService } from './institucion-infraccion.service';
|
||||
import { Operador } from 'src/operador/entity/operador.entity';
|
||||
import { IdInstitucionDto } from '../dto/id-institucion.dto';
|
||||
import { CreateInstitucionInfraccionDto } from './dto/input/create.dto';
|
||||
import { UpdateInstitucionInfraccionDto } from './dto/input/update.dto';
|
||||
import { InfraccionOutputDto } from './dto/output/infraccion.dto';
|
||||
import { InstitucionInfraccionOutputDto } from './dto/output/institucion-infraccion.dto';
|
||||
import { Operador } from 'src/operador/entity/operador.entity';
|
||||
|
||||
@Controller('institucion-infraccion')
|
||||
@ApiTags('institucion-infraccion')
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
import {
|
||||
Body,
|
||||
ConflictException,
|
||||
Controller,
|
||||
Get,
|
||||
Post,
|
||||
Put,
|
||||
Query,
|
||||
Request,
|
||||
UseGuards,
|
||||
} from '@nestjs/common';
|
||||
import { AuthGuard } from '@nestjs/passport';
|
||||
@@ -17,6 +19,8 @@ import {
|
||||
} from '@nestjs/swagger';
|
||||
import { Serealize } from '../interceptors/serialize.interceptor';
|
||||
import { InstitucionProgramaService } from './institucion-programa.service';
|
||||
import { Operador } from 'src/operador/entity/operador.entity';
|
||||
import { Usuario } from 'src/usuario/entity/usuario.entity';
|
||||
import { IdInstitucionDto } from '../dto/id-institucion.dto';
|
||||
import { CreateProgramaDto } from './dto/input/create.dto';
|
||||
import { UpdateProgramaDto } from './dto/input/update.dto';
|
||||
@@ -37,7 +41,11 @@ export class InstitucionProgramaController {
|
||||
description: 'Es obligatorio mandar la varible programa.',
|
||||
examples: { ejemplo: { value: { programa: '' } } },
|
||||
})
|
||||
create(@Body() body: CreateProgramaDto) {
|
||||
create(@Request() req, @Body() body: CreateProgramaDto) {
|
||||
const superAdmin: Operador = req.user.operador;
|
||||
|
||||
if (superAdmin.tipoUsuario.id_tipo_usuario != 2)
|
||||
throw new ConflictException('No tienes permiso de realizar esta acción.');
|
||||
return this.institucionProgramaService.create(body.programa);
|
||||
}
|
||||
|
||||
@@ -46,7 +54,16 @@ export class InstitucionProgramaController {
|
||||
@UseGuards(AuthGuard('jwt'))
|
||||
@ApiOperation({ description: 'Endpoint que retorna todos los programas.' })
|
||||
@ApiBearerAuth('jwt')
|
||||
get() {
|
||||
get(@Request() req) {
|
||||
const admin: Operador = req.user.operador;
|
||||
|
||||
if (
|
||||
admin.tipoUsuario.id_tipo_usuario != 2 &&
|
||||
admin.tipoUsuario.id_tipo_usuario != 3
|
||||
)
|
||||
throw new ConflictException(
|
||||
'No tienes permiso de acceder a esta información.',
|
||||
);
|
||||
return this.institucionProgramaService.findAllProgramas();
|
||||
}
|
||||
|
||||
@@ -62,7 +79,13 @@ export class InstitucionProgramaController {
|
||||
name: 'id_institucion',
|
||||
type: 'string',
|
||||
})
|
||||
programas(@Query() query: IdInstitucionDto) {
|
||||
programas(@Request() req, @Query() query: IdInstitucionDto) {
|
||||
const admin: Operador = req.user.operador;
|
||||
|
||||
if (admin.tipoUsuario.id_tipo_usuario != 3)
|
||||
throw new ConflictException(
|
||||
'No tienes permiso de acceder a esta información.',
|
||||
);
|
||||
return this.institucionProgramaService.findAllByIdInstitucion(
|
||||
parseInt(query.id_institucion),
|
||||
);
|
||||
@@ -81,7 +104,13 @@ export class InstitucionProgramaController {
|
||||
name: 'id_institucion',
|
||||
type: 'string',
|
||||
})
|
||||
programasMostrar(@Query() query: IdInstitucionDto) {
|
||||
programasMostrar(@Request() req, @Query() query: IdInstitucionDto) {
|
||||
const usuario: Usuario = req.user.operador;
|
||||
|
||||
if (usuario.tipoUsuario.id_tipo_usuario < 5)
|
||||
throw new ConflictException(
|
||||
'No tienes permiso de acceder a esta información.',
|
||||
);
|
||||
return this.institucionProgramaService.findAllByIdInstitucion(
|
||||
parseInt(query.id_institucion),
|
||||
true,
|
||||
@@ -101,7 +130,11 @@ export class InstitucionProgramaController {
|
||||
ejemplo: { value: { id_institucion_programa: 130, mostrar: true } },
|
||||
},
|
||||
})
|
||||
update(@Body() body: UpdateProgramaDto) {
|
||||
return this.institucionProgramaService.update(body);
|
||||
update(@Request() req, @Body() body: UpdateProgramaDto) {
|
||||
const admin: Operador = req.user.operador;
|
||||
|
||||
if (admin.tipoUsuario.id_tipo_usuario != 3)
|
||||
throw new ConflictException('No tienes permiso de realizar esta acción.');
|
||||
return this.institucionProgramaService.update(admin, body);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import {
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { Not, Repository } from 'typeorm';
|
||||
import { InstitucionPrograma } from './entity/institucion-programa.entity';
|
||||
import { Operador } from 'src/operador/entity/operador.entity';
|
||||
import { Programa } from './entity/programa.entity';
|
||||
import { InstitucionService } from '../institucion/institucion.service';
|
||||
|
||||
@@ -43,6 +44,23 @@ export class InstitucionProgramaService {
|
||||
});
|
||||
}
|
||||
|
||||
async findAllByIdInstitucion(id_institucion: number, mostrar = false) {
|
||||
const institucion = await this.institucionService.findById(id_institucion);
|
||||
const query = this.institucionProgramaRepository
|
||||
.createQueryBuilder('ip')
|
||||
.innerJoinAndSelect('ip.programa', 'p', 'p.id_programa != 1')
|
||||
.innerJoinAndSelect(
|
||||
'ip.institucion',
|
||||
'i',
|
||||
'i.id_institucion = :id_institucion',
|
||||
{ id_institucion: institucion.id_institucion },
|
||||
)
|
||||
.orderBy('p.programa');
|
||||
|
||||
if (mostrar) query.andWhere('ip.mostrar = 1');
|
||||
return query.getMany();
|
||||
}
|
||||
|
||||
findAllProgramas() {
|
||||
return this.programaRepository.find({
|
||||
where: { id_programa: Not(1) },
|
||||
@@ -52,7 +70,13 @@ export class InstitucionProgramaService {
|
||||
|
||||
findById(id_institucion_programa: number) {
|
||||
return this.institucionProgramaRepository
|
||||
.findOne({ id_institucion_programa })
|
||||
.findOne({
|
||||
join: {
|
||||
alias: 'ip',
|
||||
innerJoinAndSelect: { i: 'ip.institucion', p: 'ip.programa' },
|
||||
},
|
||||
where: { id_institucion_programa },
|
||||
})
|
||||
.then((institucionPrograma) => {
|
||||
if (!institucionPrograma)
|
||||
throw new NotFoundException('No existe esta institucion programa.');
|
||||
@@ -75,26 +99,16 @@ export class InstitucionProgramaService {
|
||||
});
|
||||
}
|
||||
|
||||
async findAllByIdInstitucion(id_institucion: number, mostrar = false) {
|
||||
const institucion = await this.institucionService.findById(id_institucion);
|
||||
const query = this.institucionProgramaRepository
|
||||
.createQueryBuilder('ip')
|
||||
.innerJoinAndSelect('ip.programa', 'p', 'p.id_programa != 1')
|
||||
.innerJoinAndSelect(
|
||||
'ip.institucion',
|
||||
'i',
|
||||
'i.id_institucion = :id_institucion',
|
||||
{ id_institucion: institucion.id_institucion },
|
||||
)
|
||||
.orderBy('p.programa');
|
||||
|
||||
if (mostrar) query.andWhere('ip.mostrar = 1');
|
||||
return query.getMany();
|
||||
}
|
||||
|
||||
update(attrs: Partial<InstitucionPrograma>) {
|
||||
update(admin: Operador, attrs: Partial<InstitucionPrograma>) {
|
||||
return this.findById(attrs.id_institucion_programa)
|
||||
.then((institucionPrograma) => {
|
||||
if (
|
||||
admin.institucion.id_institucion !=
|
||||
institucionPrograma.institucion.id_institucion
|
||||
)
|
||||
throw new ConflictException(
|
||||
'No puedes actualizar la información de este software porque no le corresponde a tu institución.',
|
||||
);
|
||||
Object.assign(institucionPrograma, attrs);
|
||||
return this.institucionProgramaRepository.save(institucionPrograma);
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user