Interfaces fix 4

This commit is contained in:
TioSam77
2024-06-25 22:06:02 -06:00
17 changed files with 206 additions and 45 deletions
+40 -11
View File
@@ -8,47 +8,47 @@ export class profesorDto{
@IsString()
@IsOptional()
@Length(10)
@Length(0,10)
num_trabajador: string;
@IsString()
@IsNotEmpty()
@Length(80)
@Length(0,80)
nombre: string;
@IsString()
@IsOptional()
@Length(10)
@Length(0,10)
rfc: string;
@IsString()
@IsOptional()
@Length(3)
@Length(0,3)
homoclave: string;
@IsString()
@IsOptional()
@Length(13)
@Length(0,13)
tel_oficina: string;
@IsString()
@IsOptional()
@Length(10)
@Length(0,10)
extension: string;
@IsString()
@IsOptional()
@Length(13)
@Length(0,13)
tel_personal: string;
@IsString()
@IsOptional()
@Length(65)
@Length(0,65)
correo_pcp: string;
@IsString()
@IsOptional()
@Length(65)
@Length(0,65)
correo_per: string;
@IsNumber()
@@ -65,12 +65,12 @@ export class profesorDto{
@IsString()
@IsOptional()
@Length(256)
@Length(0,256)
ubicacion: string;
@IsString()
@IsOptional()
@Length(150)
@Length(0,150)
fotografia: string;
@IsBoolean()
@@ -80,4 +80,33 @@ export class profesorDto{
@IsBoolean()
@IsOptional()
mostrar: boolean;
proyectosAcademicos: ProyectoDto[];
datosAcademicos: DatosAcademicos[];
lineasInvestigacion: LineasInvestigacion[];
}
export class ProyectoDto {
@IsOptional()
@Length(0, 500)
proyecto: string;
}
export class DatosAcademicos{
@IsOptional()
@Length(0,30)
grado_maximo: string;
@IsOptional()
@Length(0,600)
grados_obtenidos: string;
}
export class LineasInvestigacion{
@IsOptional()
@Length(0,300)
lineas_inv: string;
}
+2 -2
View File
@@ -104,10 +104,10 @@ export class Profesor {
@OneToMany(() => DatosAcademicos, datosAcademicos => datosAcademicos.profesor)
datosAcademicos: DatosAcademicos[];
@OneToMany(() => LineasInvestigacion, lineasInvestigacion => lineasInvestigacion.profesor)
@OneToMany(() => LineasInvestigacion, linea => linea.profesor)
lineasInvestigacion: LineasInvestigacion[];
@OneToMany(() => ProyectosAcademicos, proyectosAcademicos => proyectosAcademicos.profesor)
@OneToMany(() => ProyectosAcademicos, proyecto => proyecto.profesor)
proyectosAcademicos: ProyectosAcademicos[];
}
+102 -6
View File
@@ -6,6 +6,9 @@ import { ProyectosAcademicosService } from '../proyectos_academicos/proyectos_ac
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { profesorDto } from './dto/profesorDto.dto';
import { ProyectosAcademicos } from '../proyectos_academicos/entities/proyectos_academicos.entity';
import { DatosAcademicos } from '../datos_academicos/entities/datos_academicos.entity'
import { LineasInvestigacion } from '../lineas_investigacion/entities/lineas_investigacion.entity'
@Injectable()
export class ProfesorService {
@@ -14,11 +17,17 @@ export class ProfesorService {
private datosAcademicosService: DatosAcademicosService,
private lineasInvestigacionService: LineasInvestigacionService,
private proyectosAcademicosService: ProyectosAcademicosService,
@InjectRepository(ProyectosAcademicos)
private readonly proyectosAcademicosRepository: Repository<ProyectosAcademicos>,
@InjectRepository(DatosAcademicos)
private readonly datosAcademicosRepository: Repository<DatosAcademicos>,
@InjectRepository(LineasInvestigacion)
private readonly lineasInvestigacionRepository: Repository<LineasInvestigacion>,
) {}
//register teacher
async register(data: profesorDto) {
const {num_trabajador,rfc } = data;
const {num_trabajador,rfc,proyectosAcademicos, datosAcademicos, lineasInvestigacion } = data;
const existrfc = await this.profesorRepository.findOne({where:{rfc}});
if(existrfc){
@@ -31,7 +40,45 @@ export class ProfesorService {
}
const newProfesor = this.profesorRepository.create(data);
return this.profesorRepository.save(newProfesor);
const savedProfesor = await this.profesorRepository.save(newProfesor);
if (proyectosAcademicos && proyectosAcademicos.length > 0) {
const proyectos = proyectosAcademicos.map(proyecto => {
const newProyecto = this.proyectosAcademicosRepository.create({
...proyecto,
profesor: savedProfesor,
id_profesor: savedProfesor.id_profesor,
});
return newProyecto;
});
await this.proyectosAcademicosRepository.save(proyectos);
}
if (datosAcademicos && datosAcademicos.length > 0) {
const datos = datosAcademicos.map(dato => {
const newDato = this.datosAcademicosRepository.create({
...dato,
profesor: savedProfesor,
id_profesor: savedProfesor.id_profesor,
});
return newDato;
});
await this.datosAcademicosRepository.save(datos);
}
if (lineasInvestigacion && lineasInvestigacion.length > 0) {
const lineas = lineasInvestigacion.map(linea => {
const newLinea = this.lineasInvestigacionRepository.create({
...linea,
profesor: savedProfesor,
id_profesor: savedProfesor.id_profesor,
});
return newLinea;
});
await this.lineasInvestigacionRepository.save(lineas);
}
return savedProfesor;
}
//brings all teachers
@@ -50,8 +97,50 @@ export class ProfesorService {
throw new HttpException('Profesor not found', 404);
}
Object.assign(profesor, data);
return this.profesorRepository.save(profesor);
const { proyectosAcademicos, datosAcademicos, lineasInvestigacion, ...profesorData } = data;
Object.assign(profesor, profesorData);
const savedProfesor = await this.profesorRepository.save(profesor);
if (proyectosAcademicos) {
await this.proyectosAcademicosRepository.delete({ profesor: { id_profesor } });
const proyectos = proyectosAcademicos.map(proyecto => {
const newProyecto = this.proyectosAcademicosRepository.create({
...proyecto,
profesor: savedProfesor,
id_profesor: savedProfesor.id_profesor,
});
return newProyecto;
});
await this.proyectosAcademicosRepository.save(proyectos);
}
if (datosAcademicos) {
await this.datosAcademicosRepository.delete({ profesor: { id_profesor } });
const datos = datosAcademicos.map(dato => {
const newDato = this.datosAcademicosRepository.create({
...dato,
profesor: savedProfesor,
id_profesor: savedProfesor.id_profesor,
});
return newDato;
});
await this.datosAcademicosRepository.save(datos);
}
if (lineasInvestigacion) {
await this.lineasInvestigacionRepository.delete({ profesor: { id_profesor } });
const lineas = lineasInvestigacion.map(linea => {
const newLinea = this.lineasInvestigacionRepository.create({
...linea,
profesor: savedProfesor,
id_profesor: savedProfesor.id_profesor,
});
return newLinea;
});
await this.lineasInvestigacionRepository.save(lineas);
}
return savedProfesor;
}
//remove teacher
@@ -111,8 +200,15 @@ export class ProfesorService {
return query.getMany();
}
async findAllPaginated(page: number, limit: number, filters: any): Promise<{ profesores: Profesor[], total: number, totalPages: number }> {
const query = this.profesorRepository.createQueryBuilder('profesor');
async findAllPaginated(
page: number,
limit: number,
filters: any
): Promise<{ profesores: Profesor[], total: number, totalPages: number }> {
const query = this.profesorRepository.createQueryBuilder('profesor')
.leftJoinAndSelect('profesor.proyectosAcademicos', 'proyectosAcademicos')
.leftJoinAndSelect('profesor.datosAcademicos', 'datosAcademicos')
.leftJoinAndSelect('profesor.lineasInvestigacion', 'lineasInvestigacion');
if (filters.nombre) {
query.andWhere('profesor.nombre LIKE :nombre', { nombre: `%${filters.nombre}%` });
+2 -2
View File
@@ -1,12 +1,12 @@
import { Test, TestingModule } from '@nestjs/testing';
import { AdscripcionService } from './adscripcion.service'; // Asegúrate de importar el servicio correcto
import { AdscripcionService } from './adscripcion.service';
describe('AdscripcionService', () => {
let service: AdscripcionService;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [AdscripcionService], // Asegúrate de incluir el servicio correcto aquí
providers: [AdscripcionService],
}).compile();
service = module.get<AdscripcionService>(AdscripcionService);
+2 -2
View File
@@ -19,7 +19,7 @@ import {
const request = context.switchToHttp().getRequest();
const token = this.extractTokenFromHeader(request);
if (!token) {
throw new UnauthorizedException();
throw new UnauthorizedException('token faltante');
}
try {
const payload = await this.jwtService.verifyAsync(
@@ -31,7 +31,7 @@ import {
request['user'] = payload;
} catch {
throw new UnauthorizedException();
throw new UnauthorizedException('algo fallo');
}
return true;
}
+3 -3
View File
@@ -51,9 +51,9 @@ export class AuthService {
}
const payload = {
idUser: user.id_usuario,
username: user.usuario,
permissions: user.id_tipo_usuario,
id_usuario: user.id_usuario,
usuario: user.usuario,
id_tipo_usuario: user.id_tipo_usuario,
};
const token = this.jwtService.sign(payload);
+1 -1
View File
@@ -1,5 +1,5 @@
import { Test, TestingModule } from '@nestjs/testing';
import { CategoriaController } from './categoria.controller'; // Asumiendo que el controlador se llama EdificioController
import { CategoriaController } from './categoria.controller';
describe('CategoriaController', () => {
let controller: CategoriaController;
+2 -2
View File
@@ -1,12 +1,12 @@
import { Test, TestingModule } from '@nestjs/testing';
import { CategoriaService } from './categoria.service'; // Asegúrate de importar el servicio correcto
import { CategoriaService } from './categoria.service';
describe('CategoriaService', () => {
let service: CategoriaService;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [CategoriaService], // Asegúrate de incluir el servicio correcto aquí
providers: [CategoriaService],
}).compile();
service = module.get<CategoriaService>(CategoriaService);
@@ -1,8 +1,12 @@
import { Controller, Delete, Param } from '@nestjs/common';
import { Controller, Get } from '@nestjs/common';
import { DatosAcademicosService } from './datos_academicos.service';
@Controller('datos-academicos')
export class DatosAcademicosController {
constructor(private readonly datosAcademicosService: DatosAcademicosService) {}
@Get()
findAll() {
return this.datosAcademicosService.findAll();
}
}
@@ -14,4 +14,8 @@ export class DatosAcademicosService {
async removeByProfesorId(id_profesor: number): Promise<void> {
await this.datosAcademicosRepository.delete({ profesor: { id_profesor } });
}
async findAll(): Promise<DatosAcademicos[]> {
return this.datosAcademicosRepository.find();
}
}
+1 -1
View File
@@ -1,5 +1,5 @@
import { Test, TestingModule } from '@nestjs/testing';
import { EdificioController } from './edificio.controller'; // Asumiendo que el controlador se llama EdificioController
import { EdificioController } from './edificio.controller';
describe('EdificioController', () => {
let controller: EdificioController;
@@ -16,7 +16,7 @@ export class LineasInvestigacion {
@Column({ type: 'varchar', length: 350, nullable: true })
lineas_inv_html: string;
@ManyToOne(() => Profesor, profesor => profesor.lineasInvestigacion)
@ManyToOne(() => Profesor, profesor => profesor.lineasInvestigacion)
@JoinColumn({ name: 'id_profesor' })
profesor: Profesor;
}
@@ -1,9 +1,12 @@
import { Controller, Delete, Param } from '@nestjs/common';
import { Controller, Get } from '@nestjs/common';
import { LineasInvestigacionService } from './lineas_investigacion.service';
@Controller('lineas-investigacion')
export class LineasInvestigacionController {
constructor(private readonly lineasInvestigacionService: LineasInvestigacionService) {}
@Get()
findAll() {
return this.lineasInvestigacionService.findAll();
}
}
@@ -14,4 +14,8 @@ export class LineasInvestigacionService {
async removeByProfesorId(id_profesor: number): Promise<void> {
await this.lineasInvestigacionRepository.delete({ profesor: { id_profesor } });
}
async findAll(): Promise<LineasInvestigacion[]> {
return this.lineasInvestigacionRepository.find();
}
}
+23 -9
View File
@@ -1,13 +1,12 @@
// roles.guard.ts
import { Injectable, CanActivate, ExecutionContext } from '@nestjs/common';
import { Injectable, CanActivate, ExecutionContext, UnauthorizedException } from '@nestjs/common';
import { Reflector } from '@nestjs/core';
import { JwtService } from '@nestjs/jwt';
import { Role } from './role.enum';
import { ROLES_KEY } from './roles.decorator'; // Asegúrate de importar el decorador correcto
import { ROLES_KEY } from './roles.decorator';
@Injectable()
export class RolesGuard implements CanActivate {
constructor(private reflector: Reflector) {}
constructor(private reflector: Reflector, private jwtService: JwtService) {}
canActivate(context: ExecutionContext): boolean {
const requiredRoles = this.reflector.getAllAndOverride<Role[]>(ROLES_KEY, [
@@ -20,13 +19,28 @@ export class RolesGuard implements CanActivate {
}
const request = context.switchToHttp().getRequest();
const permissionsHeader = request.headers['permissions'];
const authHeader = request.headers['authorization'];
if (!permissionsHeader) {
return false; // Si no hay encabezado 'permissions', se niega el acceso
if (!authHeader) {
throw new UnauthorizedException('No authorization header provided');
}
const userRoles: Role[] = JSON.parse(permissionsHeader);
const [, token] = authHeader.split(' '); // Asumiendo que el formato es 'Bearer TOKEN'
if (!token) {
throw new UnauthorizedException('No token provided');
}
let userPayload;
try {
userPayload = this.jwtService.verify(token);
} catch (error) {
throw new UnauthorizedException('Invalid token');
}
const userRoles: Role[] = userPayload.id_tipo_usuario;
if (!userRoles) {
return false; // Si el token no contiene roles, se niega el acceso
}
return requiredRoles.some(role => userRoles.includes(role));
}
@@ -1,9 +1,12 @@
import { Controller, Delete, Param } from '@nestjs/common';
import { Controller,Get } from '@nestjs/common';
import { ProyectosAcademicosService } from './proyectos_academicos.service';
@Controller('proyectos-academicos')
export class ProyectosAcademicosController {
constructor(private readonly proyectosAcademicosService: ProyectosAcademicosService) {}
@Get()
findAll() {
return this.proyectosAcademicosService.findAll();
}
}
@@ -14,4 +14,8 @@ export class ProyectosAcademicosService {
async removeByProfesorId(id_profesor: number): Promise<void> {
await this.proyectosAcademicosRepository.delete({ profesor: { id_profesor } });
}
async findAll(): Promise<ProyectosAcademicos[]> {
return this.proyectosAcademicosRepository.find();
}
}