102 lines
3.3 KiB
TypeScript
102 lines
3.3 KiB
TypeScript
import {
|
|
ConflictException,
|
|
Injectable,
|
|
NotFoundException,
|
|
} from '@nestjs/common';
|
|
import { InjectRepository } from '@nestjs/typeorm';
|
|
import { FindOperator, Not, Repository } from 'typeorm';
|
|
import { Institucion } from 'src/institucion/entity/institucion.entity';
|
|
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 un nuevo programa.' };
|
|
});
|
|
}
|
|
|
|
findAllProgramas() {
|
|
return this.programaRepository.find();
|
|
}
|
|
|
|
findById(id_institucion_programa: number) {
|
|
return this.institucionProgramaRepository
|
|
.findOne({ id_institucion_programa })
|
|
.then((institucionPrograma) => {
|
|
if (!institucionPrograma)
|
|
throw new NotFoundException('No existe esta institucion programa.');
|
|
return institucionPrograma;
|
|
});
|
|
}
|
|
|
|
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;
|
|
});
|
|
}
|
|
|
|
async findAllByIdInstitucion(id_institucion: number, mostrar = false) {
|
|
const institucion = await this.institucionService.findById(id_institucion);
|
|
const busqueda: {
|
|
institucion: Institucion;
|
|
programa: { id_programa: FindOperator<number> };
|
|
mostrar?: boolean;
|
|
} = {
|
|
institucion,
|
|
programa: { id_programa: Not(1) },
|
|
};
|
|
|
|
if (mostrar) busqueda.mostrar = mostrar;
|
|
return this.institucionProgramaRepository.find(busqueda);
|
|
}
|
|
|
|
update(attrs: Partial<InstitucionPrograma>) {
|
|
return this.findById(attrs.id_institucion_programa)
|
|
.then((institucionPrograma) => {
|
|
Object.assign(institucionPrograma, attrs);
|
|
return this.institucionProgramaRepository.save(institucionPrograma);
|
|
})
|
|
.then((_) => ({
|
|
message: 'Se guardaron los cambios correctamente.',
|
|
}));
|
|
}
|
|
}
|