248 lines
8.2 KiB
TypeScript
248 lines
8.2 KiB
TypeScript
import { HttpException, Injectable, Logger } from "@nestjs/common";
|
|
import { Profesor } from "./entities/profesor.entity";
|
|
import { DatosAcademicosService } from "../datos_academicos/datos_academicos.service";
|
|
import { LineasInvestigacionService } from "../lineas_investigacion/lineas_investigacion.service";
|
|
import { ProyectosAcademicosService } from "../proyectos_academicos/proyectos_academicos.service";
|
|
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 {
|
|
constructor(
|
|
@InjectRepository(Profesor)
|
|
private readonly profesorRepository: Repository<Profesor>,
|
|
private readonly datosAcademicosService: DatosAcademicosService,
|
|
private readonly lineasInvestigacionService: LineasInvestigacionService,
|
|
private readonly proyectosAcademicosService: ProyectosAcademicosService,
|
|
@InjectRepository(DatosAcademicos)
|
|
private readonly datosAcademicosRepository: Repository<DatosAcademicos>,
|
|
@InjectRepository(LineasInvestigacion)
|
|
private readonly lineasInvestigacionRepository: Repository<LineasInvestigacion>,
|
|
@InjectRepository(ProyectosAcademicos)
|
|
private readonly proyectosAcademicosRepository: Repository<ProyectosAcademicos>
|
|
) {
|
|
}
|
|
|
|
private async findAvailableId(): Promise<number> {
|
|
let id = 1;
|
|
let exists = true;
|
|
|
|
while (exists) {
|
|
if (!await this.profesorRepository.findOne({ where: { id_profesor: id } })) {
|
|
Logger.debug(`id_profesor = ${id}`);
|
|
exists = false;
|
|
return id;
|
|
}
|
|
id++;
|
|
}
|
|
}
|
|
|
|
//register teacher
|
|
async register(data: profesorDto): Promise<Profesor | undefined> {
|
|
const { num_trabajador, rfc, proyectosAcademicos, datosAcademicos, lineasInvestigacion } = data;
|
|
|
|
if (await this.profesorRepository.findOne({ where: { rfc } })) {
|
|
throw new HttpException("rfc already exist", 403);
|
|
}
|
|
|
|
if (await this.profesorRepository.findOne({ where: { num_trabajador } })) {
|
|
throw new HttpException("number of worker already exist", 403);
|
|
}
|
|
|
|
const availableId = await this.findAvailableId();
|
|
data.id_profesor = availableId;
|
|
|
|
const savedProfesor = await this.profesorRepository.save(data);
|
|
|
|
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 (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);
|
|
}
|
|
|
|
if (datosAcademicos && datosAcademicos.length > 0) {
|
|
const datos = datosAcademicos.map(datosAcademicos => {
|
|
const newDato = this.datosAcademicosRepository.create({
|
|
...datosAcademicos,
|
|
profesor: savedProfesor,
|
|
id_profesor: savedProfesor.id_profesor
|
|
});
|
|
return newDato;
|
|
});
|
|
await this.datosAcademicosRepository.save(datos);
|
|
}
|
|
|
|
return savedProfesor;
|
|
}
|
|
|
|
//brings all teachers
|
|
async findAll(): Promise<Profesor[]> {
|
|
return this.profesorRepository.find({
|
|
order: {
|
|
nombre: "ASC"
|
|
}
|
|
});
|
|
}
|
|
|
|
//modify teacher
|
|
async modify(id_profesor: number, data: profesorDto) {
|
|
const profesor = await this.profesorRepository.findOne({ where: { id_profesor } });
|
|
if (!profesor) {
|
|
throw new HttpException("Profesor not found", 404);
|
|
}
|
|
|
|
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 (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);
|
|
}
|
|
|
|
if (datosAcademicos) {
|
|
await this.datosAcademicosRepository.delete({ profesor: { id_profesor } });
|
|
const datos = datosAcademicos.map(datosAcademicos => {
|
|
const newDato = this.datosAcademicosRepository.create({
|
|
...datosAcademicos,
|
|
profesor: savedProfesor,
|
|
id_profesor: savedProfesor.id_profesor
|
|
});
|
|
return newDato;
|
|
});
|
|
await this.datosAcademicosRepository.save(datos);
|
|
}
|
|
|
|
return savedProfesor;
|
|
}
|
|
|
|
//remove teacher
|
|
async remove(id_profesor: number) {
|
|
const profesor = await this.profesorRepository.findOne({ where: { id_profesor }, relations: ["datosAcademicos"] });
|
|
if (!profesor) {
|
|
throw new HttpException("Profesor not found", 404);
|
|
}
|
|
|
|
await this.datosAcademicosService.removeByProfesorId(id_profesor);
|
|
await this.lineasInvestigacionService.removeByProfesorId(id_profesor);
|
|
await this.proyectosAcademicosService.removeByProfesorId(id_profesor);
|
|
|
|
await this.profesorRepository.remove(profesor);
|
|
return { message: "Profesor removed successfully" };
|
|
}
|
|
|
|
//brings teachers by id
|
|
async profile(id_profesor: number) {
|
|
const profesor = await this.profesorRepository.findOne({
|
|
where: { id_profesor },
|
|
relations: ["proyectosAcademicos", "datosAcademicos", "lineasInvestigacion"]
|
|
});
|
|
|
|
if (!profesor) {
|
|
throw new HttpException("Profesor not found", 404);
|
|
}
|
|
|
|
return profesor;
|
|
}
|
|
|
|
async findAllPaginated(
|
|
page: number,
|
|
limit: number,
|
|
filters: any
|
|
): Promise<{ profesores: Profesor[], total: number, totalPages: number }> {
|
|
const query = this.profesorRepository.createQueryBuilder("profesor");
|
|
|
|
if (filters.nombre) {
|
|
Logger.debug('fiter by name');
|
|
|
|
query.andWhere("profesor.nombre LIKE :nombre", { nombre: `%${filters.nombre}%` });
|
|
}
|
|
|
|
if (filters.adscripcion) {
|
|
Logger.debug("fiter by adscription");
|
|
|
|
query.andWhere("profesor.adscripcion = :adscripcion", { adscripcion: filters.adscripcion });
|
|
}
|
|
|
|
if (filters.categoria) {
|
|
Logger.debug("fiter by category");
|
|
|
|
query.andWhere("profesor.categoria = :categoria", { categoria: filters.categoria });
|
|
}
|
|
|
|
if (filters.edificio) {
|
|
Logger.debug("fiter by building");
|
|
|
|
query.andWhere("profesor.edificio = :edificio", { edificio: filters.edificio });
|
|
}
|
|
|
|
if (typeof filters.activo !== "undefined") {
|
|
Logger.debug("fiter by is active");
|
|
|
|
query.andWhere("profesor.activo = :activo", { activo: filters.activo });
|
|
} else {
|
|
query.orderBy("profesor.activo", "DESC");
|
|
}
|
|
|
|
query.addOrderBy("profesor.nombre", "ASC");
|
|
|
|
const [profesores, total] = await query
|
|
.skip((page - 1) * limit)
|
|
.take(limit)
|
|
.getManyAndCount();
|
|
|
|
const totalPages = Math.ceil(total / limit);
|
|
|
|
return { profesores, total, totalPages };
|
|
}
|
|
|
|
|
|
async updateImageURL(id_profesor: number, imageUrl: string): Promise<void> {
|
|
await this.profesorRepository.update(id_profesor, { fotografia: imageUrl });
|
|
}
|
|
}
|