diff --git a/src/hora-excepcion/hora-excepcion.service.ts b/src/hora-excepcion/hora-excepcion.service.ts index ac7b045..8616be5 100644 --- a/src/hora-excepcion/hora-excepcion.service.ts +++ b/src/hora-excepcion/hora-excepcion.service.ts @@ -3,6 +3,7 @@ import { NotFoundException, Injectable, BadRequestException, + ForbiddenException, } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; import { @@ -33,24 +34,52 @@ export class HoraExcepcionService { const institucionDia = await this.institucionDiaService.findById( id_institucion_dia, ); + const busqueda: { + hora_fin: FindOperator; + hora_inicio: FindOperator; + institucionDia: InstitucionDia; + }[] = []; if ( admin.institucion.id_institucion != institucionDia.institucion.id_institucion ) - throw new ConflictException( + throw new ForbiddenException( 'No puedes crear una horario sin servicio en este día porque no le pertenece a tu institución.', ); if (hora_inicio > hora_fin) - throw new ConflictException( - 'La hora inicio no puede ser más mayor que la hora fin.', + throw new BadRequestException( + 'La hora inicio no puede ser mayor que la hora fin.', ); - return this.validarHoras(institucionDia, hora_inicio, hora_fin) - .then(() => - this.repository.save( + if (hora_inicio) + busqueda.push({ + hora_fin: MoreThanOrEqual(hora_inicio), + hora_inicio: LessThanOrEqual(hora_inicio), + institucionDia, + }); + if (hora_fin) + busqueda.push({ + hora_fin: MoreThanOrEqual(hora_fin), + hora_inicio: LessThanOrEqual(hora_fin), + institucionDia, + }); + return this.repository + .findOne({ where: busqueda }) + .then((existeHoraExcepcion) => { + if (existeHoraExcepcion) { + if ( + existeHoraExcepcion.hora_fin === hora_fin && + existeHoraExcepcion.hora_inicio === hora_inicio + ) + throw new ConflictException('Ya existe este horario sin servicio.'); + throw new BadRequestException( + 'Las horas escogídas se sobrelapan con otro horario sin servicio del mismo día.', + ); + } + return this.repository.save( this.repository.create({ institucionDia, hora_inicio, hora_fin }), - ), - ) + ); + }) .then((_) => ({ message: 'Se creó correctamente un nuevo horario sin servicio.', })); @@ -94,45 +123,4 @@ export class HoraExcepcionService { return horaExcepcion; }); } - - validarHoras( - institucionDia: InstitucionDia, - hora_inicio?: string, - hora_fin?: string, - ) { - const busqueda: { - hora_fin: FindOperator; - hora_inicio: FindOperator; - institucionDia: InstitucionDia; - }[] = []; - - if (hora_inicio) - busqueda.push({ - hora_fin: MoreThanOrEqual(hora_inicio), - hora_inicio: LessThanOrEqual(hora_inicio), - institucionDia, - }); - if (hora_fin) - busqueda.push({ - hora_fin: MoreThanOrEqual(hora_fin), - hora_inicio: LessThanOrEqual(hora_fin), - institucionDia, - }); - return this.repository - .findOne({ - where: busqueda, - }) - .then((existeHoraExcepcion) => { - if (existeHoraExcepcion) { - if ( - existeHoraExcepcion.hora_fin === hora_fin && - existeHoraExcepcion.hora_inicio === hora_inicio - ) - throw new ConflictException('Ya existe este horario sin servicio.'); - throw new BadRequestException( - 'Las horas escogídas se sobrelapan con otro horario sin servicio del mismo día.', - ); - } - }); - } } diff --git a/src/institucion-dia/dto/input/update.dto.ts b/src/institucion-dia/dto/input/update.dto.ts index 369bd30..2b16929 100644 --- a/src/institucion-dia/dto/input/update.dto.ts +++ b/src/institucion-dia/dto/input/update.dto.ts @@ -8,6 +8,10 @@ export class UpdateInstitucionDiaDto { @IsOptional() activo?: boolean; + @IsMilitaryTime() + @IsOptional() + hora_extra?: string; + @IsMilitaryTime() @IsOptional() hora_fin?: string; @@ -15,4 +19,8 @@ export class UpdateInstitucionDiaDto { @IsMilitaryTime() @IsOptional() hora_inicio?: string; + + @IsMilitaryTime() + @IsOptional() + hora_tope?: string; } diff --git a/src/institucion-dia/dto/output/institucion-carrera.dto.ts b/src/institucion-dia/dto/output/institucion-carrera.dto.ts index c8515f9..ef40c64 100644 --- a/src/institucion-dia/dto/output/institucion-carrera.dto.ts +++ b/src/institucion-dia/dto/output/institucion-carrera.dto.ts @@ -9,12 +9,18 @@ export class InstitucionCarreaOutputDto { @Expose() activo; + @Expose() + hora_extra; + @Expose() hora_fin; @Expose() hora_inicio; + @Expose() + hora_tope; + @Expose() @Type(() => DiaOutputDto) dia; diff --git a/src/institucion-dia/entity/institucion-dia.entity.ts b/src/institucion-dia/entity/institucion-dia.entity.ts index b9859d9..88e22d9 100644 --- a/src/institucion-dia/entity/institucion-dia.entity.ts +++ b/src/institucion-dia/entity/institucion-dia.entity.ts @@ -18,12 +18,18 @@ export class InstitucionDia { @Column({ type: Boolean, nullable: false, default: false }) activo: boolean; + @Column({ type: String, nullable: false, default: '18:45', length: 5 }) + hora_extra: string; + @Column({ type: String, nullable: false, default: '17:45', length: 5 }) hora_fin: string; @Column({ type: String, nullable: false, default: '08:50', length: 5 }) hora_inicio: string; + @Column({ type: String, nullable: false, default: '20:00', length: 5 }) + hora_tope: string; + @ManyToOne(() => Dia, (dia) => dia.institucionesDias, { eager: true }) @JoinColumn({ name: 'id_dia' }) dia: Dia; diff --git a/src/institucion-dia/institucion-dia.controller.ts b/src/institucion-dia/institucion-dia.controller.ts index 49bc55f..3e7572d 100644 --- a/src/institucion-dia/institucion-dia.controller.ts +++ b/src/institucion-dia/institucion-dia.controller.ts @@ -91,8 +91,10 @@ export class InstitucionDiaController { value: { id_institucion_dia: 217, _activo: true, + _hora_extra: '18:45', _hora_fin: '17:45', - _hora_inicio: '09:00', + _hora_inicio: '08:50', + _hora_tope: '20:00', }, }, }, diff --git a/src/institucion-dia/institucion-dia.service.ts b/src/institucion-dia/institucion-dia.service.ts index 896d922..90d2ff8 100644 --- a/src/institucion-dia/institucion-dia.service.ts +++ b/src/institucion-dia/institucion-dia.service.ts @@ -1,11 +1,17 @@ import * as moment from 'moment'; import { + BadRequestException, ConflictException, Injectable, NotFoundException, } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; -import { Repository } from 'typeorm'; +import { + FindOperator, + LessThanOrEqual, + MoreThanOrEqual, + Repository, +} from 'typeorm'; import { Institucion } from '../institucion/entity/institucion.entity'; import { InstitucionDia } from './entity/institucion-dia.entity'; import { Operador } from 'src/operador/entity/operador.entity'; @@ -74,6 +80,31 @@ export class InstitucionDiaService { throw new ConflictException( 'No puedes actualizar la información de este día porque no le pertenece a tu institución.', ); + + if ( + (attrs.hora_inicio || attrs.hora_fin) && + (attrs.hora_inicio || institucionDia.hora_inicio) > + (attrs.hora_fin || institucionDia.hora_fin) + ) + throw new BadRequestException( + 'La hora inicio no puede ser mayor que la hora fin.', + ); + if ( + (attrs.hora_fin || attrs.hora_extra) && + (attrs.hora_fin || institucionDia.hora_fin) > + (attrs.hora_extra || institucionDia.hora_extra) + ) + throw new BadRequestException( + 'La hora fin no puede ser mayor que la hora extra.', + ); + if ( + (attrs.hora_extra || attrs.hora_tope) && + (attrs.hora_extra || institucionDia.hora_extra) > + (attrs.hora_tope || institucionDia.hora_tope) + ) + throw new BadRequestException( + 'La hora extra no puede ser mayor que la hora tope.', + ); Object.assign(institucionDia, attrs); return this.repository.save(institucionDia); })