From 8bbd2473d26ea09f3f79ebb05e12d748b1122148 Mon Sep 17 00:00:00 2001 From: xXpuma99Xx <51341582+xXpuma99Xx@users.noreply.github.com> Date: Thu, 2 Jun 2022 23:13:29 -0500 Subject: [PATCH] =?UTF-8?q?listo=20hora=20excepci=C3=B3n?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dto/hora-excepcion-create.dto.ts | 10 +- .../dto/hora-excepcion-delete.dto.ts | 6 + .../dto/hora-excepcion-get.dto.ts | 6 + .../dto/hora-excepcion-update.dto.ts | 14 ++ .../entity/hora-excepcion.entity.ts | 9 +- .../hora-excepcion.controller.ts | 90 +++++++++--- src/hora-excepcion/hora-excepcion.service.ts | 138 +++++++++++++++--- 7 files changed, 229 insertions(+), 44 deletions(-) create mode 100644 src/hora-excepcion/dto/hora-excepcion-delete.dto.ts create mode 100644 src/hora-excepcion/dto/hora-excepcion-get.dto.ts create mode 100644 src/hora-excepcion/dto/hora-excepcion-update.dto.ts diff --git a/src/hora-excepcion/dto/hora-excepcion-create.dto.ts b/src/hora-excepcion/dto/hora-excepcion-create.dto.ts index e3fff5b..425528c 100644 --- a/src/hora-excepcion/dto/hora-excepcion-create.dto.ts +++ b/src/hora-excepcion/dto/hora-excepcion-create.dto.ts @@ -1,6 +1,12 @@ -import { IsInt } from 'class-validator'; +import { IsInt, IsMilitaryTime } from 'class-validator'; -export class HoraExcepcionDto { +export class HoraExcepcionCreateDto { @IsInt() id_institucion_dia: number; + + @IsMilitaryTime() + hora_fin: string; + + @IsMilitaryTime() + hora_inicio: string; } diff --git a/src/hora-excepcion/dto/hora-excepcion-delete.dto.ts b/src/hora-excepcion/dto/hora-excepcion-delete.dto.ts new file mode 100644 index 0000000..93a19b9 --- /dev/null +++ b/src/hora-excepcion/dto/hora-excepcion-delete.dto.ts @@ -0,0 +1,6 @@ +import { IsInt } from 'class-validator'; + +export class HoraExcepcionDeleteDto { + @IsInt() + id_hora_excepcion: number; +} diff --git a/src/hora-excepcion/dto/hora-excepcion-get.dto.ts b/src/hora-excepcion/dto/hora-excepcion-get.dto.ts new file mode 100644 index 0000000..eaad62b --- /dev/null +++ b/src/hora-excepcion/dto/hora-excepcion-get.dto.ts @@ -0,0 +1,6 @@ +import { IsNumberString } from 'class-validator'; + +export class HoraExcepcionGetDto { + @IsNumberString() + id_institucion_dia: string; +} diff --git a/src/hora-excepcion/dto/hora-excepcion-update.dto.ts b/src/hora-excepcion/dto/hora-excepcion-update.dto.ts new file mode 100644 index 0000000..e599e59 --- /dev/null +++ b/src/hora-excepcion/dto/hora-excepcion-update.dto.ts @@ -0,0 +1,14 @@ +import { IsInt, IsMilitaryTime, IsOptional } from 'class-validator'; + +export class HoraExcepcionUpdateDto { + @IsInt() + id_hora_excepcion: number; + + @IsMilitaryTime() + @IsOptional() + hora_fin?: string; + + @IsMilitaryTime() + @IsOptional() + hora_inicio?: string; +} diff --git a/src/hora-excepcion/entity/hora-excepcion.entity.ts b/src/hora-excepcion/entity/hora-excepcion.entity.ts index 43e2b67..1960cdb 100644 --- a/src/hora-excepcion/entity/hora-excepcion.entity.ts +++ b/src/hora-excepcion/entity/hora-excepcion.entity.ts @@ -12,15 +12,16 @@ export class HoraExcepcion { @PrimaryGeneratedColumn() id_hora_excepcion: number; - @Column({ type: Date, nullable: false }) - hora_fin: Date; + @Column({ type: String, nullable: false }) + hora_fin: string; - @Column({ type: Date, nullable: false }) - hora_inicio: Date; + @Column({ type: String, nullable: false }) + hora_inicio: string; @ManyToOne( () => InstitucionDia, (institucionDia) => institucionDia.horasExcepcion, + { eager: true }, ) @JoinColumn({ name: 'id_institucion_dia' }) institucionDia: InstitucionDia; diff --git a/src/hora-excepcion/hora-excepcion.controller.ts b/src/hora-excepcion/hora-excepcion.controller.ts index 1282d03..3017ef5 100644 --- a/src/hora-excepcion/hora-excepcion.controller.ts +++ b/src/hora-excepcion/hora-excepcion.controller.ts @@ -1,30 +1,86 @@ -import { Body, Controller, Delete, Get, Post, Param } from '@nestjs/common'; +import { + Body, + Controller, + Delete, + Get, + Post, + Put, + Query, +} from '@nestjs/common'; +import { ApiBody, ApiOperation, ApiTags } from '@nestjs/swagger'; import { HoraExcepcionService } from './hora-excepcion.service'; -import { HoraExcepcionDto } from './dto/hora-excepcion-create.dto'; -import { ApiOperation, ApiTags } from '@nestjs/swagger'; +import { HoraExcepcionCreateDto } from './dto/hora-excepcion-create.dto'; +import { HoraExcepcionDeleteDto } from './dto/hora-excepcion-delete.dto'; +import { HoraExcepcionGetDto } from './dto/hora-excepcion-get.dto'; +import { HoraExcepcionUpdateDto } from './dto/hora-excepcion-update.dto'; @Controller('hora-excepcion') -// @ApiTags('hora-excepcion') +@ApiTags('hora-excepcion') export class HoraExcepcionController { constructor(private horaExcepcionService: HoraExcepcionService) {} @Post() - // @ApiOperation({ - // description: 'Creamos una institucion-dia con el id_institucion_dia', - // }) - create(@Body() body: HoraExcepcionDto) { - return this.horaExcepcionService.create(body.id_institucion_dia); + @ApiOperation({ + description: 'Endpoint que crea una hora excepción.', + }) + @ApiBody({ + description: 'Todas las variables son obligatorias.', + examples: { + ejemplo: { + value: { + id_institucion_dia: 217, + hora_inicio: ':', + hora_fin: ':', + }, + }, + }, + }) + create(@Body() body: HoraExcepcionCreateDto) { + return this.horaExcepcionService.create( + body.id_institucion_dia, + body.hora_inicio, + body.hora_fin, + ); } - @Delete(':id') - // @ApiOperation({ - // description: - // 'Con este delete borramos una hora_excepcion con ayuda de su id', - // }) - delete(@Param('id') id_hora_excepcion: number) { - return this.horaExcepcionService.delete(id_hora_excepcion); + @Delete() + @ApiOperation({ + description: 'Endpoint que elimina una hora excepción.', + }) + @ApiBody({ + description: 'Es obligatorio mandar la variable id_hora_excepcion.', + examples: { ejemplo: { value: { id_hora_excepcion: 1 } } }, + }) + delete(@Body() body: HoraExcepcionDeleteDto) { + return this.horaExcepcionService.delete(body.id_hora_excepcion); } @Get() - get() {} + @ApiOperation({ + description: + 'Endpoint que retorna las horas excepcion de un día de una institución.', + }) + get(@Query() query: HoraExcepcionGetDto) { + return this.horaExcepcionService.findAllByIdInstitucionDia( + parseInt(query.id_institucion_dia), + ); + } + + @Put() + @ApiOperation({ + description: + 'Endpoint que actualiza lan información de una hora excepcion.', + }) + @ApiBody({ + description: + 'Todoas las variables a excepción de id_hora_excepcion son opcionales pero se tiene que mandar forzosamente una hora.', + examples: { + ejemplo: { + value: { id_hora_excepcion: 1, hora_inicio: '', hora_fin: '' }, + }, + }, + }) + update(@Body() body: HoraExcepcionUpdateDto) { + return this.horaExcepcionService.update(body); + } } diff --git a/src/hora-excepcion/hora-excepcion.service.ts b/src/hora-excepcion/hora-excepcion.service.ts index a3d557b..81303c9 100644 --- a/src/hora-excepcion/hora-excepcion.service.ts +++ b/src/hora-excepcion/hora-excepcion.service.ts @@ -1,27 +1,54 @@ -import { ConflictException, NotFoundException, Injectable } from '@nestjs/common'; +import { + ConflictException, + NotFoundException, + Injectable, +} from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; -import { Repository } from 'typeorm'; +import { LessThanOrEqual, MoreThanOrEqual, Repository } from 'typeorm'; import { HoraExcepcion } from './entity/hora-excepcion.entity'; -import { InstitucionDiaService } from '../institucion-dia/institucion-dia.service' +import { InstitucionDiaService } from '../institucion-dia/institucion-dia.service'; +import { InstitucionDia } from 'src/institucion-dia/entity/institucion-dia.entity'; @Injectable() export class HoraExcepcionService { constructor( - @InjectRepository(HoraExcepcion) private repository: Repository, + @InjectRepository(HoraExcepcion) + private repository: Repository, private institucionDiaService: InstitucionDiaService, - ) { } + ) {} - async create(id_institucion_dia: number) { - const institucionDia = await this.institucionDiaService.findById(id_institucion_dia) - const nuevaHoraExcepcion = this.repository.create({institucionDia}); + async create( + id_institucion_dia: number, + hora_inicio: string, + hora_fin: string, + ) { + const institucionDia = await this.institucionDiaService.findById( + id_institucion_dia, + ); - return this.repository - .findOne({institucionDia}) - .then((existeHoraExcepcion) => { - if (existeHoraExcepcion) throw new ConflictException('Ya existe esta hora excepción') - return this.repository.save(nuevaHoraExcepcion) - }) - .then(() => ({message: 'Se creo correctamente la hora excepción'})) + if (hora_inicio > hora_fin) + throw new ConflictException( + 'La hora inicio no puede ser más grande que la hora fin.', + ); + return this.validarHoras(institucionDia, hora_inicio, hora_fin) + .then(() => + this.repository.save( + this.repository.create({ institucionDia, hora_inicio, hora_fin }), + ), + ) + .then((_) => ({ message: 'Se creó correctamente la hora excepción.' })); + } + + delete(id_hora_excepcion: number) { + return this.findById(id_hora_excepcion).then((horaExcepcion) => + this.repository.delete(horaExcepcion), + ); + } + + findAllByIdInstitucionDia(id_institucion_dia: number) { + return this.institucionDiaService + .findById(id_institucion_dia) + .then((institucionDia) => this.repository.find({ institucionDia })); } findById(id_hora_excepcion: number) { @@ -29,15 +56,84 @@ export class HoraExcepcionService { .findOne({ id_hora_excepcion }) .then((horaExcepcion) => { if (!horaExcepcion) - throw new NotFoundException('No existe la hora excepción'); + throw new NotFoundException('No existe la hora excepción.'); return horaExcepcion; }); } - delete(id_hora_excepcion: number) { - this.findById(id_hora_excepcion) - .then(() => { - return this.repository.delete(id_hora_excepcion) - }) + update(attrs: Partial) { + if (!attrs.hora_inicio && !attrs.hora_fin) + throw new ConflictException('No se mando nada para actualizar.'); + return this.findById(attrs.id_hora_excepcion) + .then(async (horaExecpcion) => { + if ( + (attrs.hora_inicio && + !attrs.hora_fin && + attrs.hora_inicio > horaExecpcion.hora_fin) || + (!attrs.hora_inicio && + attrs.hora_fin && + horaExecpcion.hora_inicio > attrs.hora_fin) || + (attrs.hora_inicio && + attrs.hora_fin && + attrs.hora_inicio > attrs.hora_fin) + ) + throw new ConflictException( + 'La hora inicio no puede ser más grande que la hora fin.', + ); + await this.validarHoras( + horaExecpcion.institucionDia, + attrs.hora_inicio, + attrs.hora_fin, + ); + Object.assign(horaExecpcion, attrs); + return this.repository.save(horaExecpcion); + }) + .then((_) => ({ + message: + 'Se guardo correctamente la información de esta hora excepción', + })); + } + + validarHoras( + institucionDia: InstitucionDia, + hora_inicio?: string, + hora_fin?: string, + ) { + const busqueda: object[] = [ + { + institucionDia, + hora_inicio, + hora_fin, + }, + ]; + + if (hora_inicio) + busqueda.push({ + institucionDia, + hora_inicio: LessThanOrEqual(hora_inicio), + hora_fin: MoreThanOrEqual(hora_inicio), + }); + if (hora_fin) + busqueda.push({ + institucionDia, + hora_inicio: LessThanOrEqual(hora_fin), + hora_fin: MoreThanOrEqual(hora_fin), + }); + 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 esta hora excepción.'); + throw new ConflictException( + 'Las horas escogídas se sobrelapan con otra hora excepción del mismo día.', + ); + } + }); } }