carrera-programa validación token
This commit is contained in:
parent
c4fd8098e2
commit
fd9d5d3849
@ -24,6 +24,7 @@ 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';
|
||||
import { Usuario } from 'src/usuario/entity/usuario.entity';
|
||||
|
||||
@Controller('carrera-programa')
|
||||
@ApiTags('carrera-programa')
|
||||
@ -91,8 +92,20 @@ export class CarreraProgramaController {
|
||||
name: 'id_institucion',
|
||||
type: 'string',
|
||||
})
|
||||
get(@Query() query: IdInstitucionDto) {
|
||||
get(@Request() req, @Query() query: IdInstitucionDto) {
|
||||
const usuarioOperador: Operador | Usuario =
|
||||
req.user.operador || req.user.usuario;
|
||||
|
||||
if (
|
||||
!usuarioOperador ||
|
||||
(usuarioOperador.tipoUsuario.id_tipo_usuario != 3 &&
|
||||
usuarioOperador.tipoUsuario.id_tipo_usuario < 5)
|
||||
)
|
||||
throw new ForbiddenException(
|
||||
'No tienes los permisos necesarios para acceder a esta información.',
|
||||
);
|
||||
return this.carreraProgramaService.findByIdInstitucion(
|
||||
usuarioOperador,
|
||||
parseInt(query.id_institucion),
|
||||
);
|
||||
}
|
||||
|
@ -1,5 +1,6 @@
|
||||
import {
|
||||
ConflictException,
|
||||
ForbiddenException,
|
||||
Injectable,
|
||||
NotFoundException,
|
||||
} from '@nestjs/common';
|
||||
@ -7,6 +8,7 @@ import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { Repository } from 'typeorm';
|
||||
import { CarreraPrograma } from './entity/carrera-programa.entity';
|
||||
import { Operador } from 'src/operador/entity/operador.entity';
|
||||
import { Usuario } from 'src/usuario/entity/usuario.entity';
|
||||
import { InstitucionService } from '../institucion/institucion.service';
|
||||
import { InstitucionCarreraService } from '../institucion-carrera/institucion-carrera.service';
|
||||
import { InstitucionProgramaService } from '../institucion-programa/institucion-programa.service';
|
||||
@ -37,7 +39,7 @@ export class CarreraProgramaService {
|
||||
admin.institucion.id_institucion !=
|
||||
institucionCarrera.institucion.id_institucion
|
||||
)
|
||||
throw new ConflictException(
|
||||
throw new ForbiddenException(
|
||||
'Esta carrera no pertenece a tu institución.',
|
||||
);
|
||||
return this.repository
|
||||
@ -66,7 +68,7 @@ export class CarreraProgramaService {
|
||||
admin.institucion.id_institucion !=
|
||||
carreraPrograma.institucionCarrera.institucion.id_institucion
|
||||
)
|
||||
throw new ConflictException(
|
||||
throw new ForbiddenException(
|
||||
'No puedes eliminar esta asociación porque no pertenece a tu institución.',
|
||||
);
|
||||
return this.repository.delete(carreraPrograma);
|
||||
@ -86,11 +88,38 @@ export class CarreraProgramaService {
|
||||
});
|
||||
}
|
||||
|
||||
findByIdInstitucion(id_institucion: number) {
|
||||
findByIdInstitucion(
|
||||
usuarioOperador: Operador | Usuario,
|
||||
id_institucion: number,
|
||||
) {
|
||||
return this.institucionService
|
||||
.findById(id_institucion)
|
||||
.then((institucion) =>
|
||||
this.repository.find({
|
||||
.then((institucion) => {
|
||||
if (usuarioOperador instanceof Operador) {
|
||||
if (
|
||||
usuarioOperador.institucion.id_institucion !=
|
||||
institucion.id_institucion
|
||||
)
|
||||
throw new ForbiddenException(
|
||||
'No puedes acceder a esta información porque no te pertenece.',
|
||||
);
|
||||
} else {
|
||||
let lePertenece = false;
|
||||
|
||||
for (let i = 0; i < usuarioOperador.instituciones.length; i++)
|
||||
if (
|
||||
usuarioOperador.instituciones[i].institucionCarrera.institucion
|
||||
.id_institucion === institucion.id_institucion
|
||||
) {
|
||||
lePertenece = true;
|
||||
break;
|
||||
}
|
||||
if (!lePertenece)
|
||||
throw new ForbiddenException(
|
||||
'No puedes acceder a esta información porque no te pertenece.',
|
||||
);
|
||||
}
|
||||
return this.repository.find({
|
||||
join: {
|
||||
alias: 'cp',
|
||||
innerJoinAndSelect: {
|
||||
@ -102,7 +131,7 @@ export class CarreraProgramaService {
|
||||
where: {
|
||||
institucionCarrera: { institucion },
|
||||
},
|
||||
}),
|
||||
);
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user