listo hora excepción

This commit is contained in:
xXpuma99Xx 2022-06-02 23:13:29 -05:00
parent a308dc9c80
commit 8bbd2473d2
7 changed files with 229 additions and 44 deletions

View File

@ -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;
}

View File

@ -0,0 +1,6 @@
import { IsInt } from 'class-validator';
export class HoraExcepcionDeleteDto {
@IsInt()
id_hora_excepcion: number;
}

View File

@ -0,0 +1,6 @@
import { IsNumberString } from 'class-validator';
export class HoraExcepcionGetDto {
@IsNumberString()
id_institucion_dia: string;
}

View File

@ -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;
}

View File

@ -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;

View File

@ -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);
}
}

View File

@ -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<HoraExcepcion>,
@InjectRepository(HoraExcepcion)
private repository: Repository<HoraExcepcion>,
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<HoraExcepcion>) {
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.',
);
}
});
}
}