81 lines
2.5 KiB
TypeScript
81 lines
2.5 KiB
TypeScript
import {
|
|
ConflictException,
|
|
Injectable,
|
|
NotFoundException,
|
|
} from '@nestjs/common';
|
|
import { InjectRepository } from '@nestjs/typeorm';
|
|
import { Repository } from 'typeorm';
|
|
import { InstitucionPrograma } from './entity/institucion-programa.entity';
|
|
import { Programa } from './entity/programa.entity';
|
|
import { InstitucionService } from '../institucion/institucion.service';
|
|
|
|
@Injectable()
|
|
export class InstitucionProgramaService {
|
|
constructor(
|
|
@InjectRepository(InstitucionPrograma)
|
|
private institucionProgramaRepository: Repository<InstitucionPrograma>,
|
|
@InjectRepository(Programa)
|
|
private programaRepository: Repository<Programa>,
|
|
private institucionService: InstitucionService,
|
|
) {}
|
|
|
|
create(programa: string) {
|
|
return this.programaRepository
|
|
.findOne({ programa })
|
|
.then((existePrograma) => {
|
|
if (existePrograma)
|
|
throw new ConflictException('Ya existe este programa.');
|
|
return this.programaRepository.save(
|
|
this.programaRepository.create({ programa }),
|
|
);
|
|
})
|
|
.then(async (programa) => {
|
|
const instituciones = await this.institucionService.findAll();
|
|
|
|
for (let i = 0; i < instituciones.length; i++)
|
|
await this.institucionProgramaRepository.save(
|
|
this.institucionProgramaRepository.create({
|
|
programa,
|
|
institucion: instituciones[i],
|
|
}),
|
|
);
|
|
return { message: 'Se creó correctamente el programa.' };
|
|
});
|
|
}
|
|
|
|
findAllProgramas() {
|
|
return this.programaRepository.find();
|
|
}
|
|
|
|
findProgramaById(id_programa: number) {
|
|
return this.programaRepository.findOne({ id_programa }).then((programa) => {
|
|
if (!programa) throw new NotFoundException('No existe este programa.');
|
|
return programa;
|
|
});
|
|
}
|
|
|
|
findProgramaByPrograma(programa: string, validarNoExiste = true) {
|
|
return this.programaRepository.findOne({ programa }).then((programa) => {
|
|
if (validarNoExiste && !programa)
|
|
throw new NotFoundException('No existe este programa.');
|
|
return programa;
|
|
});
|
|
}
|
|
|
|
findAllByIdInstitucion(id_institucion: number) {
|
|
return this.institucionService
|
|
.findById(id_institucion)
|
|
.then((institucion) =>
|
|
this.institucionProgramaRepository.find({ institucion }),
|
|
);
|
|
}
|
|
|
|
findAllByIdInstitucionMostrar(id_institucion: number) {
|
|
return this.institucionService
|
|
.findById(id_institucion)
|
|
.then((institucion) =>
|
|
this.institucionProgramaRepository.find({ institucion, mostrar: true }),
|
|
);
|
|
}
|
|
}
|