142 lines
4.5 KiB
TypeScript
142 lines
4.5 KiB
TypeScript
import {
|
|
BadRequestException,
|
|
ConflictException,
|
|
ForbiddenException,
|
|
Injectable,
|
|
NotFoundException,
|
|
} from '@nestjs/common';
|
|
import { InjectRepository } from '@nestjs/typeorm';
|
|
import { LessThanOrEqual, MoreThanOrEqual, Repository } from 'typeorm';
|
|
import { HoraExcepcion } from './entity/hora-excepcion.entity';
|
|
import { Operador } from '../operador/entity/operador.entity';
|
|
import { InstitucionDiaService } from '../institucion-dia/institucion-dia.service';
|
|
|
|
@Injectable()
|
|
export class HoraExcepcionService {
|
|
constructor(
|
|
@InjectRepository(HoraExcepcion)
|
|
private repository: Repository<HoraExcepcion>,
|
|
private institucionDiaService: InstitucionDiaService,
|
|
) {}
|
|
|
|
async create(
|
|
admin: Operador,
|
|
id_institucion_dia: number,
|
|
hora_inicio: string,
|
|
hora_fin: string,
|
|
): Promise<{ message: string }> {
|
|
const institucionDia = await this.institucionDiaService.findById(
|
|
id_institucion_dia,
|
|
);
|
|
|
|
// Validamos que el día pertenezca a la institución del admin
|
|
if (
|
|
admin.institucion.id_institucion !=
|
|
institucionDia.institucion.id_institucion
|
|
)
|
|
throw new ForbiddenException(
|
|
'No puedes crear una hora excepeción en este día porque no pertenece a tu institución.',
|
|
);
|
|
// hora_inicio no puede ser mayor a hora fin
|
|
if (hora_inicio > hora_fin)
|
|
throw new BadRequestException(
|
|
'La hora inicio no puede ser mayor que la hora fin.',
|
|
);
|
|
// Buscamos una hora excepción que encierre las horas enviadas
|
|
return this.repository
|
|
.findOne({
|
|
where: [
|
|
{
|
|
hora_fin: MoreThanOrEqual(hora_inicio),
|
|
hora_inicio: LessThanOrEqual(hora_inicio),
|
|
institucionDia,
|
|
},
|
|
{
|
|
hora_fin: MoreThanOrEqual(hora_fin),
|
|
hora_inicio: LessThanOrEqual(hora_fin),
|
|
institucionDia,
|
|
},
|
|
],
|
|
})
|
|
.then((existeHoraExcepcion) => {
|
|
// Si encontramos uno sacamos error
|
|
if (existeHoraExcepcion) {
|
|
if (
|
|
existeHoraExcepcion.hora_fin === hora_fin &&
|
|
existeHoraExcepcion.hora_inicio === hora_inicio
|
|
)
|
|
throw new ConflictException(
|
|
'Ya existe un horario sin servicio con estas horas.',
|
|
);
|
|
throw new BadRequestException(
|
|
'Las horas escogídas se sobrelapan con otro horario sin servicio del mismo día.',
|
|
);
|
|
}
|
|
// Creamos y guardamos un registro
|
|
return this.repository.save(
|
|
this.repository.create({ institucionDia, hora_inicio, hora_fin }),
|
|
);
|
|
})
|
|
.then((_) => ({
|
|
message: 'Se creó correctamente un nuevo horario sin servicio.',
|
|
}));
|
|
}
|
|
|
|
delete(
|
|
admin: Operador,
|
|
id_hora_excepcion: number,
|
|
): Promise<{ message: string }> {
|
|
return this.findById(id_hora_excepcion)
|
|
.then((horaExcepcion) => {
|
|
// Validamos que la hora excepción pertenezca a la institución del admin
|
|
if (
|
|
admin.institucion.id_institucion !=
|
|
horaExcepcion.institucionDia.institucion.id_institucion
|
|
)
|
|
throw new ConflictException(
|
|
'No puedes eliminar esta horario sin servicio porque no pertenece a tu institución.',
|
|
);
|
|
// Eliminamos el registro
|
|
return this.repository.remove(horaExcepcion);
|
|
})
|
|
.then((_) => ({
|
|
message: 'Se eliminó correctamente este horario sin servicio.',
|
|
}));
|
|
}
|
|
|
|
findAllByIdInstitucionDia(
|
|
admin: Operador,
|
|
id_institucion_dia: number,
|
|
): Promise<HoraExcepcion[]> {
|
|
return this.institucionDiaService
|
|
.findById(id_institucion_dia)
|
|
.then((institucionDia) => {
|
|
// Validamos que el día pertenezca a la institución del admin
|
|
if (
|
|
admin.institucion.id_institucion !=
|
|
institucionDia.institucion.id_institucion
|
|
)
|
|
throw new ForbiddenException(
|
|
'No puedes acceder a esta información porque no le pertenece a tu institución.',
|
|
);
|
|
return this.repository.find({ where: { institucionDia } });
|
|
});
|
|
}
|
|
|
|
findById(id_hora_excepcion: number): Promise<HoraExcepcion> {
|
|
return this.repository
|
|
.findOne({
|
|
join: {
|
|
alias: 'he',
|
|
innerJoinAndSelect: { id: 'he.institucionDia', i: 'id.institucion' },
|
|
},
|
|
where: { id_hora_excepcion },
|
|
})
|
|
.then((horaExcepcion) => {
|
|
if (!horaExcepcion)
|
|
throw new NotFoundException('No existe este id hora excepcion.');
|
|
return horaExcepcion;
|
|
});
|
|
}
|
|
}
|