listo hora excepción
This commit is contained in:
parent
a308dc9c80
commit
8bbd2473d2
@ -1,6 +1,12 @@
|
|||||||
import { IsInt } from 'class-validator';
|
import { IsInt, IsMilitaryTime } from 'class-validator';
|
||||||
|
|
||||||
export class HoraExcepcionDto {
|
export class HoraExcepcionCreateDto {
|
||||||
@IsInt()
|
@IsInt()
|
||||||
id_institucion_dia: number;
|
id_institucion_dia: number;
|
||||||
|
|
||||||
|
@IsMilitaryTime()
|
||||||
|
hora_fin: string;
|
||||||
|
|
||||||
|
@IsMilitaryTime()
|
||||||
|
hora_inicio: string;
|
||||||
}
|
}
|
||||||
|
6
src/hora-excepcion/dto/hora-excepcion-delete.dto.ts
Normal file
6
src/hora-excepcion/dto/hora-excepcion-delete.dto.ts
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
import { IsInt } from 'class-validator';
|
||||||
|
|
||||||
|
export class HoraExcepcionDeleteDto {
|
||||||
|
@IsInt()
|
||||||
|
id_hora_excepcion: number;
|
||||||
|
}
|
6
src/hora-excepcion/dto/hora-excepcion-get.dto.ts
Normal file
6
src/hora-excepcion/dto/hora-excepcion-get.dto.ts
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
import { IsNumberString } from 'class-validator';
|
||||||
|
|
||||||
|
export class HoraExcepcionGetDto {
|
||||||
|
@IsNumberString()
|
||||||
|
id_institucion_dia: string;
|
||||||
|
}
|
14
src/hora-excepcion/dto/hora-excepcion-update.dto.ts
Normal file
14
src/hora-excepcion/dto/hora-excepcion-update.dto.ts
Normal 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;
|
||||||
|
}
|
@ -12,15 +12,16 @@ export class HoraExcepcion {
|
|||||||
@PrimaryGeneratedColumn()
|
@PrimaryGeneratedColumn()
|
||||||
id_hora_excepcion: number;
|
id_hora_excepcion: number;
|
||||||
|
|
||||||
@Column({ type: Date, nullable: false })
|
@Column({ type: String, nullable: false })
|
||||||
hora_fin: Date;
|
hora_fin: string;
|
||||||
|
|
||||||
@Column({ type: Date, nullable: false })
|
@Column({ type: String, nullable: false })
|
||||||
hora_inicio: Date;
|
hora_inicio: string;
|
||||||
|
|
||||||
@ManyToOne(
|
@ManyToOne(
|
||||||
() => InstitucionDia,
|
() => InstitucionDia,
|
||||||
(institucionDia) => institucionDia.horasExcepcion,
|
(institucionDia) => institucionDia.horasExcepcion,
|
||||||
|
{ eager: true },
|
||||||
)
|
)
|
||||||
@JoinColumn({ name: 'id_institucion_dia' })
|
@JoinColumn({ name: 'id_institucion_dia' })
|
||||||
institucionDia: InstitucionDia;
|
institucionDia: InstitucionDia;
|
||||||
|
@ -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 { HoraExcepcionService } from './hora-excepcion.service';
|
||||||
import { HoraExcepcionDto } from './dto/hora-excepcion-create.dto';
|
import { HoraExcepcionCreateDto } from './dto/hora-excepcion-create.dto';
|
||||||
import { ApiOperation, ApiTags } from '@nestjs/swagger';
|
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')
|
@Controller('hora-excepcion')
|
||||||
// @ApiTags('hora-excepcion')
|
@ApiTags('hora-excepcion')
|
||||||
export class HoraExcepcionController {
|
export class HoraExcepcionController {
|
||||||
constructor(private horaExcepcionService: HoraExcepcionService) {}
|
constructor(private horaExcepcionService: HoraExcepcionService) {}
|
||||||
|
|
||||||
@Post()
|
@Post()
|
||||||
// @ApiOperation({
|
@ApiOperation({
|
||||||
// description: 'Creamos una institucion-dia con el id_institucion_dia',
|
description: 'Endpoint que crea una hora excepción.',
|
||||||
// })
|
})
|
||||||
create(@Body() body: HoraExcepcionDto) {
|
@ApiBody({
|
||||||
return this.horaExcepcionService.create(body.id_institucion_dia);
|
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')
|
@Delete()
|
||||||
// @ApiOperation({
|
@ApiOperation({
|
||||||
// description:
|
description: 'Endpoint que elimina una hora excepción.',
|
||||||
// 'Con este delete borramos una hora_excepcion con ayuda de su id',
|
})
|
||||||
// })
|
@ApiBody({
|
||||||
delete(@Param('id') id_hora_excepcion: number) {
|
description: 'Es obligatorio mandar la variable id_hora_excepcion.',
|
||||||
return this.horaExcepcionService.delete(id_hora_excepcion);
|
examples: { ejemplo: { value: { id_hora_excepcion: 1 } } },
|
||||||
|
})
|
||||||
|
delete(@Body() body: HoraExcepcionDeleteDto) {
|
||||||
|
return this.horaExcepcionService.delete(body.id_hora_excepcion);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Get()
|
@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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,27 +1,54 @@
|
|||||||
import { ConflictException, NotFoundException, Injectable } from '@nestjs/common';
|
import {
|
||||||
|
ConflictException,
|
||||||
|
NotFoundException,
|
||||||
|
Injectable,
|
||||||
|
} from '@nestjs/common';
|
||||||
import { InjectRepository } from '@nestjs/typeorm';
|
import { InjectRepository } from '@nestjs/typeorm';
|
||||||
import { Repository } from 'typeorm';
|
import { LessThanOrEqual, MoreThanOrEqual, Repository } from 'typeorm';
|
||||||
import { HoraExcepcion } from './entity/hora-excepcion.entity';
|
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()
|
@Injectable()
|
||||||
export class HoraExcepcionService {
|
export class HoraExcepcionService {
|
||||||
constructor(
|
constructor(
|
||||||
@InjectRepository(HoraExcepcion) private repository: Repository<HoraExcepcion>,
|
@InjectRepository(HoraExcepcion)
|
||||||
|
private repository: Repository<HoraExcepcion>,
|
||||||
private institucionDiaService: InstitucionDiaService,
|
private institucionDiaService: InstitucionDiaService,
|
||||||
) { }
|
) {}
|
||||||
|
|
||||||
async create(id_institucion_dia: number) {
|
async create(
|
||||||
const institucionDia = await this.institucionDiaService.findById(id_institucion_dia)
|
id_institucion_dia: number,
|
||||||
const nuevaHoraExcepcion = this.repository.create({institucionDia});
|
hora_inicio: string,
|
||||||
|
hora_fin: string,
|
||||||
|
) {
|
||||||
|
const institucionDia = await this.institucionDiaService.findById(
|
||||||
|
id_institucion_dia,
|
||||||
|
);
|
||||||
|
|
||||||
return this.repository
|
if (hora_inicio > hora_fin)
|
||||||
.findOne({institucionDia})
|
throw new ConflictException(
|
||||||
.then((existeHoraExcepcion) => {
|
'La hora inicio no puede ser más grande que la hora fin.',
|
||||||
if (existeHoraExcepcion) throw new ConflictException('Ya existe esta hora excepción')
|
);
|
||||||
return this.repository.save(nuevaHoraExcepcion)
|
return this.validarHoras(institucionDia, hora_inicio, hora_fin)
|
||||||
})
|
.then(() =>
|
||||||
.then(() => ({message: 'Se creo correctamente la hora excepción'}))
|
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) {
|
findById(id_hora_excepcion: number) {
|
||||||
@ -29,15 +56,84 @@ export class HoraExcepcionService {
|
|||||||
.findOne({ id_hora_excepcion })
|
.findOne({ id_hora_excepcion })
|
||||||
.then((horaExcepcion) => {
|
.then((horaExcepcion) => {
|
||||||
if (!horaExcepcion)
|
if (!horaExcepcion)
|
||||||
throw new NotFoundException('No existe la hora excepción');
|
throw new NotFoundException('No existe la hora excepción.');
|
||||||
return horaExcepcion;
|
return horaExcepcion;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
delete(id_hora_excepcion: number) {
|
update(attrs: Partial<HoraExcepcion>) {
|
||||||
this.findById(id_hora_excepcion)
|
if (!attrs.hora_inicio && !attrs.hora_fin)
|
||||||
.then(() => {
|
throw new ConflictException('No se mando nada para actualizar.');
|
||||||
return this.repository.delete(id_hora_excepcion)
|
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.',
|
||||||
|
);
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user