2022-04-16 13:05:27 -05:00
|
|
|
import {
|
|
|
|
|
ConflictException,
|
|
|
|
|
Injectable,
|
|
|
|
|
NotFoundException,
|
|
|
|
|
} from '@nestjs/common';
|
2022-04-04 20:02:54 -05:00
|
|
|
import { InjectRepository } from '@nestjs/typeorm';
|
2022-04-16 11:21:52 -05:00
|
|
|
import { Repository } from 'typeorm';
|
2022-04-16 14:37:17 -05:00
|
|
|
import { Modulo } from './entity/modulo.entity';
|
2022-04-17 21:55:00 -05:00
|
|
|
import { InstitucionService } from '../institucion/institucion.service';
|
2022-04-04 20:02:54 -05:00
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
|
export class ModuloService {
|
|
|
|
|
constructor(
|
2022-04-16 11:21:52 -05:00
|
|
|
@InjectRepository(Modulo) private moduloRepository: Repository<Modulo>,
|
|
|
|
|
private institucionService: InstitucionService,
|
2022-04-04 20:02:54 -05:00
|
|
|
) {}
|
|
|
|
|
|
2022-04-16 11:21:52 -05:00
|
|
|
async create(id_institucion: number, modulo: string) {
|
|
|
|
|
const institucion = await this.institucionService.findById(id_institucion);
|
|
|
|
|
const nuevoModulo = this.moduloRepository.create({
|
|
|
|
|
institucion,
|
|
|
|
|
modulo,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return this.moduloRepository
|
|
|
|
|
.findOne({ modulo, institucion })
|
|
|
|
|
.then((existeModulo) => {
|
|
|
|
|
if (existeModulo)
|
|
|
|
|
throw new ConflictException(
|
|
|
|
|
'Ya existe un módulo con este nombre, intente con otro nombre.',
|
|
|
|
|
);
|
|
|
|
|
return this.moduloRepository.save(nuevoModulo);
|
2022-04-06 15:27:23 -05:00
|
|
|
})
|
2022-04-16 11:22:03 -05:00
|
|
|
.then(() => ({ message: 'Se creo correctamente el módulo.' }));
|
2022-04-04 20:02:54 -05:00
|
|
|
}
|
2022-04-16 13:05:27 -05:00
|
|
|
|
2022-04-16 14:37:17 -05:00
|
|
|
findAll() {
|
|
|
|
|
return this.moduloRepository.find();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
findAllByIdInstitucion(id_institucion: number) {
|
|
|
|
|
return this.institucionService
|
|
|
|
|
.findById(id_institucion)
|
|
|
|
|
.then((institucion) => this.moduloRepository.find({ institucion }));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
findById(id_modulo: number) {
|
|
|
|
|
return this.moduloRepository.findOne({ id_modulo }).then((modulo) => {
|
|
|
|
|
if (!modulo) throw new NotFoundException('No existe este módulo.');
|
|
|
|
|
return modulo;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-16 13:05:27 -05:00
|
|
|
async update(attrs: Partial<Modulo>) {
|
|
|
|
|
const modulo = await this.findById(attrs.id_modulo);
|
|
|
|
|
|
|
|
|
|
return this.moduloRepository
|
|
|
|
|
.findOne({ modulo: attrs.modulo, institucion: modulo.institucion })
|
|
|
|
|
.then((existeModulo) => {
|
|
|
|
|
if (existeModulo)
|
|
|
|
|
throw new ConflictException(
|
|
|
|
|
'Ya existe un módulo con este nombre, intente con otro nombre.',
|
|
|
|
|
);
|
|
|
|
|
Object.assign(modulo, attrs);
|
2022-04-16 13:07:55 -05:00
|
|
|
return this.moduloRepository.save(modulo);
|
|
|
|
|
})
|
|
|
|
|
.then(() => ({
|
|
|
|
|
message: 'Se actualizo correctamente la información del módulo.',
|
|
|
|
|
}));
|
2022-04-16 13:05:27 -05:00
|
|
|
}
|
2022-04-04 20:02:54 -05:00
|
|
|
}
|