96 lines
3.0 KiB
TypeScript
96 lines
3.0 KiB
TypeScript
import {
|
|
ConflictException,
|
|
Injectable,
|
|
NotFoundException,
|
|
} from '@nestjs/common';
|
|
import { InjectRepository } from '@nestjs/typeorm';
|
|
import { FindOptionsWhere, IsNull, Not, 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>,
|
|
) {}
|
|
|
|
crearInstitucion(id_institucion: number): Institucion {
|
|
return this.repository.create({ id_institucion });
|
|
}
|
|
|
|
findAll(): Promise<Institucion[]> {
|
|
return this.repository.find({ select: ['id_institucion'] });
|
|
}
|
|
|
|
findById(id_institucion: number): Promise<Institucion> {
|
|
return this.repository
|
|
.findOne({ select: ['id_institucion'], where: { id_institucion } })
|
|
.then((institucion) => {
|
|
if (!institucion)
|
|
throw new NotFoundException('No existe este id institución.');
|
|
return institucion;
|
|
});
|
|
}
|
|
|
|
findFullInfoById(
|
|
admin: Operador,
|
|
id_institucion: number,
|
|
): Promise<Institucion> {
|
|
return this.repository
|
|
.findOne({ where: { id_institucion } })
|
|
.then((institucion) => {
|
|
if (!institucion)
|
|
throw new NotFoundException('No existe este id institución.');
|
|
// Validamos que la institución pertenezca al admin
|
|
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 perteneces a ella.',
|
|
);
|
|
return institucion;
|
|
});
|
|
}
|
|
|
|
findMinInfoAll(activo = false, responsable = false): Promise<Institucion[]> {
|
|
const busqueda: FindOptionsWhere<Institucion> = {};
|
|
|
|
if (activo) busqueda.activo = activo;
|
|
if (responsable) busqueda.responsable = Not(IsNull());
|
|
return this.repository.find({
|
|
select: [
|
|
'id_institucion',
|
|
'activo',
|
|
'correo',
|
|
'institucion',
|
|
'responsable',
|
|
],
|
|
where: busqueda,
|
|
order: { institucion: 'ASC' },
|
|
});
|
|
}
|
|
|
|
update(
|
|
admin: Operador,
|
|
attrs: Partial<Institucion>,
|
|
): Promise<{ message: string }> {
|
|
return this.findById(attrs.id_institucion)
|
|
.then((institucion) => {
|
|
// Validamos que la institución pertenezca al admin
|
|
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 perteneces a ella.',
|
|
);
|
|
// Asignamos valores enviados al objeto
|
|
Object.assign(institucion, attrs);
|
|
// Guardamos cambios
|
|
return this.repository.save(institucion);
|
|
})
|
|
.then((_) => ({ message: 'Se guardaron los cambios correctamente.' }));
|
|
}
|
|
}
|