carrer programa endpoints restringidos a usuario
This commit is contained in:
parent
fc73590ba9
commit
10ed8995e9
@ -1,4 +1,5 @@
|
|||||||
import { Body, Controller, Post } from '@nestjs/common';
|
import { Body, Controller, Get, Post, UseGuards } from '@nestjs/common';
|
||||||
|
import { AuthGuard } from '@nestjs/passport';
|
||||||
import { ApiBody, ApiOperation, ApiTags } from '@nestjs/swagger';
|
import { ApiBody, ApiOperation, ApiTags } from '@nestjs/swagger';
|
||||||
import { Serealize } from '../interceptors/serialize.interceptor';
|
import { Serealize } from '../interceptors/serialize.interceptor';
|
||||||
import { AuthService } from './auth.service';
|
import { AuthService } from './auth.service';
|
||||||
@ -70,4 +71,13 @@ export class AuthController {
|
|||||||
loginUsuario(@Body() body: LoginUsuarioDto) {
|
loginUsuario(@Body() body: LoginUsuarioDto) {
|
||||||
return this.authService.loginUsuario(body.usuario, body.password);
|
return this.authService.loginUsuario(body.usuario, body.password);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Get('validar-token')
|
||||||
|
@UseGuards(AuthGuard('jwt'))
|
||||||
|
@ApiOperation({
|
||||||
|
description: 'Endpoint utilizado para validar un token.',
|
||||||
|
})
|
||||||
|
validarToken() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,12 +3,11 @@ import { ConfigService } from '@nestjs/config';
|
|||||||
import { PassportStrategy } from '@nestjs/passport';
|
import { PassportStrategy } from '@nestjs/passport';
|
||||||
import { Strategy, ExtractJwt } from 'passport-jwt';
|
import { Strategy, ExtractJwt } from 'passport-jwt';
|
||||||
import { ModuloService } from 'src/modulo/modulo.service';
|
import { ModuloService } from 'src/modulo/modulo.service';
|
||||||
import { Operador } from 'src/operador/entity/operador.entity';
|
|
||||||
import { OperadorService } from 'src/operador/operador.service';
|
import { OperadorService } from 'src/operador/operador.service';
|
||||||
import { Usuario } from 'src/usuario/entity/usuario.entity';
|
|
||||||
import { UsuarioService } from 'src/usuario/usuario.service';
|
import { UsuarioService } from 'src/usuario/usuario.service';
|
||||||
import { Modulo } from '../../modulo/entity/modulo.entity';
|
import { Modulo } from '../../modulo/entity/modulo.entity';
|
||||||
|
import { Operador } from 'src/operador/entity/operador.entity';
|
||||||
|
import { Usuario } from 'src/usuario/entity/usuario.entity';
|
||||||
import { JwtPayload } from '../dto/jwt-payload';
|
import { JwtPayload } from '../dto/jwt-payload';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
@ -37,7 +36,8 @@ export class JwtStrategyService extends PassportStrategy(Strategy) {
|
|||||||
await this.operadorService
|
await this.operadorService
|
||||||
.findById(payload.id_operador)
|
.findById(payload.id_operador)
|
||||||
.then(async (operador) => {
|
.then(async (operador) => {
|
||||||
user.operador = operador;
|
if (!operador.activo)
|
||||||
|
throw new ConflictException('Esta cuenta esta desactivada.');
|
||||||
if (operador.tipoUsuario.id_tipo_usuario === 4) {
|
if (operador.tipoUsuario.id_tipo_usuario === 4) {
|
||||||
if (payload.id_modulo)
|
if (payload.id_modulo)
|
||||||
await this.moduloService
|
await this.moduloService
|
||||||
@ -48,6 +48,7 @@ export class JwtStrategyService extends PassportStrategy(Strategy) {
|
|||||||
'Credenciales no válidas, inicia sesión de nuevo.',
|
'Credenciales no válidas, inicia sesión de nuevo.',
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
user.operador = operador;
|
||||||
});
|
});
|
||||||
return user;
|
return user;
|
||||||
}
|
}
|
||||||
|
@ -1,10 +1,12 @@
|
|||||||
import {
|
import {
|
||||||
Body,
|
Body,
|
||||||
|
ConflictException,
|
||||||
Controller,
|
Controller,
|
||||||
Delete,
|
Delete,
|
||||||
Get,
|
Get,
|
||||||
Post,
|
Post,
|
||||||
Query,
|
Query,
|
||||||
|
Request,
|
||||||
UseGuards,
|
UseGuards,
|
||||||
} from '@nestjs/common';
|
} from '@nestjs/common';
|
||||||
import { AuthGuard } from '@nestjs/passport';
|
import { AuthGuard } from '@nestjs/passport';
|
||||||
@ -17,6 +19,7 @@ import {
|
|||||||
} from '@nestjs/swagger';
|
} from '@nestjs/swagger';
|
||||||
import { Serealize } from '../interceptors/serialize.interceptor';
|
import { Serealize } from '../interceptors/serialize.interceptor';
|
||||||
import { CarreraProgramaService } from './carrera-programa.service';
|
import { CarreraProgramaService } from './carrera-programa.service';
|
||||||
|
import { Operador } from 'src/operador/entity/operador.entity';
|
||||||
import { IdInstitucionDto } from '../dto/id-institucion.dto';
|
import { IdInstitucionDto } from '../dto/id-institucion.dto';
|
||||||
import { CreateCarreraProgramaDto } from './dto/input/create.dto';
|
import { CreateCarreraProgramaDto } from './dto/input/create.dto';
|
||||||
import { DeleteCarreraProgramaDto } from './dto/input/delete.dto';
|
import { DeleteCarreraProgramaDto } from './dto/input/delete.dto';
|
||||||
@ -40,8 +43,13 @@ export class CarreraProgramaController {
|
|||||||
ejemplo: { value: { id_institucion_carrera: 36, id_programa: 1 } },
|
ejemplo: { value: { id_institucion_carrera: 36, id_programa: 1 } },
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
create(@Body() body: CreateCarreraProgramaDto) {
|
create(@Request() req, @Body() body: CreateCarreraProgramaDto) {
|
||||||
|
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.carreraProgramaService.create(
|
return this.carreraProgramaService.create(
|
||||||
|
admin,
|
||||||
body.id_institucion_carrera,
|
body.id_institucion_carrera,
|
||||||
body.id_programa,
|
body.id_programa,
|
||||||
);
|
);
|
||||||
@ -58,8 +66,12 @@ export class CarreraProgramaController {
|
|||||||
description: 'Es obligatorio mandar la variable id_carrera_programa.',
|
description: 'Es obligatorio mandar la variable id_carrera_programa.',
|
||||||
examples: { ejemplo: { value: { id_carrera_programa: 1 } } },
|
examples: { ejemplo: { value: { id_carrera_programa: 1 } } },
|
||||||
})
|
})
|
||||||
delete(@Body() body: DeleteCarreraProgramaDto) {
|
delete(@Request() req, @Body() body: DeleteCarreraProgramaDto) {
|
||||||
return this.carreraProgramaService.delete(body.id_carrera_programa);
|
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.carreraProgramaService.delete(admin, body.id_carrera_programa);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Serealize(CarreraProgramaOutputDto)
|
@Serealize(CarreraProgramaOutputDto)
|
||||||
|
@ -6,6 +6,7 @@ import {
|
|||||||
import { InjectRepository } from '@nestjs/typeorm';
|
import { InjectRepository } from '@nestjs/typeorm';
|
||||||
import { Repository } from 'typeorm';
|
import { Repository } from 'typeorm';
|
||||||
import { CarreraPrograma } from './entity/carrera-programa.entity';
|
import { CarreraPrograma } from './entity/carrera-programa.entity';
|
||||||
|
import { Operador } from 'src/operador/entity/operador.entity';
|
||||||
import { InstitucionService } from '../institucion/institucion.service';
|
import { InstitucionService } from '../institucion/institucion.service';
|
||||||
import { InstitucionCarreraService } from '../institucion-carrera/institucion-carrera.service';
|
import { InstitucionCarreraService } from '../institucion-carrera/institucion-carrera.service';
|
||||||
import { InstitucionProgramaService } from '../institucion-programa/institucion-programa.service';
|
import { InstitucionProgramaService } from '../institucion-programa/institucion-programa.service';
|
||||||
@ -20,7 +21,11 @@ export class CarreraProgramaService {
|
|||||||
private institucionProgramaService: InstitucionProgramaService,
|
private institucionProgramaService: InstitucionProgramaService,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
async create(id_institucion_carrera: number, id_programa: number) {
|
async create(
|
||||||
|
admin: Operador,
|
||||||
|
id_institucion_carrera: number,
|
||||||
|
id_programa: number,
|
||||||
|
) {
|
||||||
const institucionCarrera = await this.institucionCarreraService.findById(
|
const institucionCarrera = await this.institucionCarreraService.findById(
|
||||||
id_institucion_carrera,
|
id_institucion_carrera,
|
||||||
);
|
);
|
||||||
@ -28,6 +33,13 @@ export class CarreraProgramaService {
|
|||||||
id_programa,
|
id_programa,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
if (
|
||||||
|
admin.institucion.id_institucion !=
|
||||||
|
institucionCarrera.institucion.id_institucion
|
||||||
|
)
|
||||||
|
throw new ConflictException(
|
||||||
|
'Esta carrera no pertenece a tu institución.',
|
||||||
|
);
|
||||||
return this.repository
|
return this.repository
|
||||||
.findOne({ institucionCarrera, programa })
|
.findOne({ institucionCarrera, programa })
|
||||||
.then((existeCarretaPrograma) => {
|
.then((existeCarretaPrograma) => {
|
||||||
@ -47,9 +59,18 @@ export class CarreraProgramaService {
|
|||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
delete(id_carrera_programa: number) {
|
delete(admin: Operador, id_carrera_programa: number) {
|
||||||
return this.findById(id_carrera_programa)
|
return this.findById(id_carrera_programa)
|
||||||
.then((carreraPrograma) => this.repository.delete(carreraPrograma))
|
.then((carreraPrograma) => {
|
||||||
|
if (
|
||||||
|
admin.institucion.id_institucion !=
|
||||||
|
carreraPrograma.institucionCarrera.institucion.id_institucion
|
||||||
|
)
|
||||||
|
throw new ConflictException(
|
||||||
|
'No puedes eliminar esta asociación porque no pertenece a tu institución.',
|
||||||
|
);
|
||||||
|
return this.repository.delete(carreraPrograma);
|
||||||
|
})
|
||||||
.then((_) => ({
|
.then((_) => ({
|
||||||
message: 'Se eliminó correctamente la asignación del programa.',
|
message: 'Se eliminó correctamente la asignación del programa.',
|
||||||
}));
|
}));
|
||||||
|
Loading…
Reference in New Issue
Block a user