From b62f587b9db8eac48c3ce265af9a8222e6ac46fc Mon Sep 17 00:00:00 2001 From: IO Date: Tue, 25 Jun 2024 21:32:45 -0600 Subject: [PATCH] add lineas_investigacion,proyectos_academicos,datos_academicos to filter,register and update --- src/Profesor/dto/profesorDto.dto.ts | 40 ++++---- src/Profesor/entities/profesor.entity.ts | 4 +- src/Profesor/profesor.service.ts | 99 +++++++++++++++++-- .../entities/lineas_investigacion.entity.ts | 2 +- 4 files changed, 118 insertions(+), 27 deletions(-) diff --git a/src/Profesor/dto/profesorDto.dto.ts b/src/Profesor/dto/profesorDto.dto.ts index a4e95d5..b72d140 100644 --- a/src/Profesor/dto/profesorDto.dto.ts +++ b/src/Profesor/dto/profesorDto.dto.ts @@ -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,29 +80,33 @@ export class profesorDto{ @IsBoolean() @IsOptional() mostrar: boolean; + + proyectosAcademicos: ProyectoDto[]; + datosAcademicos: DatosAcademicos[]; + lineasInvestigacion: LineasInvestigacion[]; } -export class CreateProyectoDto { +export class ProyectoDto { @IsOptional() - @Length(500) + @Length(0, 500) proyecto: string; } -export class CreateDatosAcademicos{ +export class DatosAcademicos{ @IsOptional() - @Length(30) + @Length(0,30) grado_maximo: string; @IsOptional() - @Length(600) + @Length(0,600) grados_obtenidos: string; } -export class CreateLineasInvestigacion{ +export class LineasInvestigacion{ @IsOptional() - @Length(300) + @Length(0,300) lineas_inv: string; } \ No newline at end of file diff --git a/src/Profesor/entities/profesor.entity.ts b/src/Profesor/entities/profesor.entity.ts index 31e3264..07cc908 100644 --- a/src/Profesor/entities/profesor.entity.ts +++ b/src/Profesor/entities/profesor.entity.ts @@ -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[]; } diff --git a/src/Profesor/profesor.service.ts b/src/Profesor/profesor.service.ts index 11c862a..b000255 100644 --- a/src/Profesor/profesor.service.ts +++ b/src/Profesor/profesor.service.ts @@ -27,7 +27,7 @@ export class ProfesorService { //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){ @@ -40,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 @@ -59,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 @@ -120,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}%` }); diff --git a/src/lineas_investigacion/entities/lineas_investigacion.entity.ts b/src/lineas_investigacion/entities/lineas_investigacion.entity.ts index 7e713e7..dc9bb8d 100644 --- a/src/lineas_investigacion/entities/lineas_investigacion.entity.ts +++ b/src/lineas_investigacion/entities/lineas_investigacion.entity.ts @@ -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; }