55 lines
1.6 KiB
TypeScript
55 lines
1.6 KiB
TypeScript
import {
|
|
ConflictException,
|
|
Injectable,
|
|
NotFoundException,
|
|
} from '@nestjs/common';
|
|
import { InjectRepository } from '@nestjs/typeorm';
|
|
import { Repository } from 'typeorm';
|
|
import { Institucion } from './entity/institucion.entity';
|
|
import { Operador } from '../operador/entity/operador.entity';
|
|
|
|
@Injectable()
|
|
export class InstitucionService {
|
|
constructor(
|
|
@InjectRepository(Institucion) private repository: Repository<Institucion>,
|
|
) {}
|
|
|
|
findAll(activo = false) {
|
|
const busqueda: { activo?: boolean } = {};
|
|
|
|
if (activo) busqueda.activo = activo;
|
|
return this.repository.find({
|
|
where: busqueda,
|
|
order: { institucion: 'ASC' },
|
|
});
|
|
}
|
|
|
|
findById(id_institucion: number) {
|
|
return this.repository
|
|
.findOne({ where: { id_institucion } })
|
|
.then((institucion) => {
|
|
if (!institucion)
|
|
throw new NotFoundException('No existe este id institución.');
|
|
return institucion;
|
|
});
|
|
}
|
|
|
|
update(admin: Operador, attrs: Partial<Institucion>) {
|
|
return this.findById(attrs.id_institucion)
|
|
.then((institucion) => {
|
|
if (
|
|
admin.tipoUsuario.id_tipo_usuario === 3 &&
|
|
admin.institucion.id_institucion != institucion.id_institucion
|
|
)
|
|
throw new ConflictException(
|
|
'No puedes actualizar la información de esta institución porque no correspondes a ella.',
|
|
);
|
|
Object.assign(institucion, attrs);
|
|
return this.repository.save(institucion);
|
|
})
|
|
.then((_) => ({
|
|
message: 'Se guardaron los cambios correctamente.',
|
|
}));
|
|
}
|
|
}
|