2022-05-31 14:26:57 -05:00
|
|
|
import {
|
|
|
|
|
ConflictException,
|
|
|
|
|
Injectable,
|
|
|
|
|
NotFoundException,
|
|
|
|
|
} from '@nestjs/common';
|
2022-05-09 16:49:41 -05:00
|
|
|
import { InjectRepository } from '@nestjs/typeorm';
|
2022-06-24 18:42:05 -05:00
|
|
|
import { Not, Repository } from 'typeorm';
|
2022-05-09 16:49:41 -05:00
|
|
|
import { InstitucionPrograma } from './entity/institucion-programa.entity';
|
2022-08-15 14:29:53 -05:00
|
|
|
import { Operador } from '../operador/entity/operador.entity';
|
2022-05-31 14:26:57 -05:00
|
|
|
import { Programa } from './entity/programa.entity';
|
2022-05-31 15:07:57 -05:00
|
|
|
import { InstitucionService } from '../institucion/institucion.service';
|
2022-05-09 16:49:41 -05:00
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
|
export class InstitucionProgramaService {
|
|
|
|
|
constructor(
|
|
|
|
|
@InjectRepository(InstitucionPrograma)
|
2022-05-31 14:26:57 -05:00
|
|
|
private institucionProgramaRepository: Repository<InstitucionPrograma>,
|
|
|
|
|
@InjectRepository(Programa)
|
|
|
|
|
private programaRepository: Repository<Programa>,
|
2022-05-31 15:07:57 -05:00
|
|
|
private institucionService: InstitucionService,
|
2022-05-09 16:49:41 -05:00
|
|
|
) {}
|
2022-05-31 14:26:57 -05:00
|
|
|
|
|
|
|
|
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 }),
|
|
|
|
|
);
|
|
|
|
|
})
|
2022-05-31 15:07:57 -05:00
|
|
|
.then(async (programa) => {
|
|
|
|
|
const instituciones = await this.institucionService.findAll();
|
|
|
|
|
|
|
|
|
|
for (let i = 0; i < instituciones.length; i++)
|
|
|
|
|
await this.institucionProgramaRepository.save(
|
|
|
|
|
this.institucionProgramaRepository.create({
|
|
|
|
|
institucion: instituciones[i],
|
2022-06-23 22:23:54 -05:00
|
|
|
programa,
|
2022-05-31 15:07:57 -05:00
|
|
|
}),
|
|
|
|
|
);
|
2022-06-23 21:57:44 -05:00
|
|
|
return { message: 'Se creó correctamente un nuevo programa.' };
|
2022-05-31 15:07:57 -05:00
|
|
|
});
|
2022-05-31 14:26:57 -05:00
|
|
|
}
|
|
|
|
|
|
2022-07-31 23:04:44 -05:00
|
|
|
async findAllByIdInstitucion(id_institucion: number, mostrar = false) {
|
|
|
|
|
const institucion = await this.institucionService.findById(id_institucion);
|
|
|
|
|
const query = this.institucionProgramaRepository
|
|
|
|
|
.createQueryBuilder('ip')
|
|
|
|
|
.innerJoinAndSelect('ip.programa', 'p', 'p.id_programa != 1')
|
|
|
|
|
.innerJoinAndSelect(
|
|
|
|
|
'ip.institucion',
|
|
|
|
|
'i',
|
|
|
|
|
'i.id_institucion = :id_institucion',
|
|
|
|
|
{ id_institucion: institucion.id_institucion },
|
|
|
|
|
)
|
|
|
|
|
.orderBy('p.programa');
|
|
|
|
|
|
|
|
|
|
if (mostrar) query.andWhere('ip.mostrar = 1');
|
|
|
|
|
return query.getMany();
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-31 15:07:57 -05:00
|
|
|
findAllProgramas() {
|
2022-06-24 18:10:26 -05:00
|
|
|
return this.programaRepository.find({
|
|
|
|
|
where: { id_programa: Not(1) },
|
|
|
|
|
order: { programa: 'ASC' },
|
|
|
|
|
});
|
2022-05-31 14:26:57 -05:00
|
|
|
}
|
|
|
|
|
|
2022-06-01 11:42:57 -05:00
|
|
|
findById(id_institucion_programa: number) {
|
|
|
|
|
return this.institucionProgramaRepository
|
2022-07-31 23:04:44 -05:00
|
|
|
.findOne({
|
|
|
|
|
join: {
|
|
|
|
|
alias: 'ip',
|
|
|
|
|
innerJoinAndSelect: { i: 'ip.institucion', p: 'ip.programa' },
|
|
|
|
|
},
|
|
|
|
|
where: { id_institucion_programa },
|
|
|
|
|
})
|
2022-06-01 11:42:57 -05:00
|
|
|
.then((institucionPrograma) => {
|
|
|
|
|
if (!institucionPrograma)
|
|
|
|
|
throw new NotFoundException('No existe esta institucion programa.');
|
|
|
|
|
return institucionPrograma;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-31 15:07:57 -05:00
|
|
|
findProgramaById(id_programa: number) {
|
2022-05-31 14:26:57 -05:00
|
|
|
return this.programaRepository.findOne({ id_programa }).then((programa) => {
|
|
|
|
|
if (!programa) throw new NotFoundException('No existe este programa.');
|
|
|
|
|
return programa;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-31 15:07:57 -05:00
|
|
|
findProgramaByPrograma(programa: string, validarNoExiste = true) {
|
2022-05-31 14:26:57 -05:00
|
|
|
return this.programaRepository.findOne({ programa }).then((programa) => {
|
|
|
|
|
if (validarNoExiste && !programa)
|
|
|
|
|
throw new NotFoundException('No existe este programa.');
|
|
|
|
|
return programa;
|
|
|
|
|
});
|
|
|
|
|
}
|
2022-05-31 15:07:57 -05:00
|
|
|
|
2022-07-31 23:04:44 -05:00
|
|
|
update(admin: Operador, attrs: Partial<InstitucionPrograma>) {
|
2022-06-01 11:42:57 -05:00
|
|
|
return this.findById(attrs.id_institucion_programa)
|
|
|
|
|
.then((institucionPrograma) => {
|
2022-07-31 23:04:44 -05:00
|
|
|
if (
|
|
|
|
|
admin.institucion.id_institucion !=
|
|
|
|
|
institucionPrograma.institucion.id_institucion
|
|
|
|
|
)
|
|
|
|
|
throw new ConflictException(
|
2022-08-01 02:59:35 -05:00
|
|
|
'No puedes actualizar la información de este software porque no le pertenece a tu institución.',
|
2022-07-31 23:04:44 -05:00
|
|
|
);
|
2022-06-01 11:42:57 -05:00
|
|
|
Object.assign(institucionPrograma, attrs);
|
|
|
|
|
return this.institucionProgramaRepository.save(institucionPrograma);
|
|
|
|
|
})
|
|
|
|
|
.then((_) => ({
|
2022-06-23 21:57:44 -05:00
|
|
|
message: 'Se guardaron los cambios correctamente.',
|
2022-06-01 11:42:57 -05:00
|
|
|
}));
|
|
|
|
|
}
|
2022-05-09 16:49:41 -05:00
|
|
|
}
|