From 426ca54328b10da6dcf1cdc1dedf39a919266e71 Mon Sep 17 00:00:00 2001 From: xXpuma99Xx <51341582+xXpuma99Xx@users.noreply.github.com> Date: Tue, 14 Jun 2022 20:57:28 -0500 Subject: [PATCH] multa --- src/multa/multa.module.ts | 4 +- src/multa/multa.service.ts | 30 ++++--- ...update-operador.dto.ts => entregar.dto.ts} | 2 +- .../dto/input/regresar-id-prestamo.dto.ts | 17 ++++ .../input/regresar-numero-inventario.dto.ts | 17 ++++ src/prestamo/prestamo.controller.ts | 66 ++++++++++++--- src/prestamo/prestamo.module.ts | 4 +- src/prestamo/prestamo.service.ts | 82 ++++++++++++++++++- 8 files changed, 192 insertions(+), 30 deletions(-) rename src/prestamo/dto/input/{update-operador.dto.ts => entregar.dto.ts} (77%) create mode 100644 src/prestamo/dto/input/regresar-id-prestamo.dto.ts create mode 100644 src/prestamo/dto/input/regresar-numero-inventario.dto.ts diff --git a/src/multa/multa.module.ts b/src/multa/multa.module.ts index b2cc546..a01bd50 100644 --- a/src/multa/multa.module.ts +++ b/src/multa/multa.module.ts @@ -1,4 +1,4 @@ -import { Module } from '@nestjs/common'; +import { forwardRef, Module } from '@nestjs/common'; import { TypeOrmModule } from '@nestjs/typeorm'; import { MultaController } from './multa.controller'; import { MultaService } from './multa.service'; @@ -16,7 +16,7 @@ import { UsuarioModule } from '../usuario/usuario.module'; InstitucionModule, InstitucionInfraccionModule, OperadorModule, - PrestamoModule, + forwardRef(() => PrestamoModule), TypeOrmModule.forFeature([Multa]), UsuarioModule, ], diff --git a/src/multa/multa.service.ts b/src/multa/multa.service.ts index f3bbf90..9e27a59 100644 --- a/src/multa/multa.service.ts +++ b/src/multa/multa.service.ts @@ -27,22 +27,30 @@ export class MultaService { ) {} async create( - id_prestamo: number, - id_operador: number, - retraso?: boolean, + id_prestamo: number | Prestamo, + id_operador: number | Operador, + descripcion: string, + retraso?: number, id_institucion_infraccion?: number, ) { const ahora = moment(); - const prestamo = await this.prestamoService.findById(id_prestamo); - const operador = await this.operadorService.findById(id_operador); + const prestamo = + typeof id_prestamo === 'number' + ? await this.prestamoService.findById(id_prestamo) + : id_prestamo; + const operador = + typeof id_operador === 'number' + ? await this.operadorService.findById(id_operador) + : id_operador; const data: { - prestamo: Prestamo; - operador: Operador; + descripcion: string; fecha_inicio: Date; + operador: Operador; + prestamo: Prestamo; + fecha_fin?: Date; institucionInfraccion?: InstitucionInfraccion; retraso?: boolean; - fecha_fin?: Date; - } = { fecha_inicio: ahora.toDate(), prestamo, operador }; + } = { descripcion, fecha_inicio: ahora.toDate(), operador, prestamo }; const institucionInfraccion = id_institucion_infraccion ? await this.institucionInfraccionService.findById( id_institucion_infraccion, @@ -59,8 +67,8 @@ export class MultaService { 'Ya existe una multa asignada a este prestamo.', ); if (retraso) { - data.retraso = retraso; - ahora.add(operador.institucion.dias_multa_retraso, 'd'); + data.retraso = true; + ahora.add(retraso * operador.institucion.dias_multa_retraso, 'd'); } if (institucionInfraccion) { data.institucionInfraccion = institucionInfraccion; diff --git a/src/prestamo/dto/input/update-operador.dto.ts b/src/prestamo/dto/input/entregar.dto.ts similarity index 77% rename from src/prestamo/dto/input/update-operador.dto.ts rename to src/prestamo/dto/input/entregar.dto.ts index b3693cc..21f87c3 100644 --- a/src/prestamo/dto/input/update-operador.dto.ts +++ b/src/prestamo/dto/input/entregar.dto.ts @@ -1,6 +1,6 @@ import { IsInt } from 'class-validator'; -export class UpdateOperadorDto { +export class EntregarDto { @IsInt() id_operador: number; diff --git a/src/prestamo/dto/input/regresar-id-prestamo.dto.ts b/src/prestamo/dto/input/regresar-id-prestamo.dto.ts new file mode 100644 index 0000000..7507ef1 --- /dev/null +++ b/src/prestamo/dto/input/regresar-id-prestamo.dto.ts @@ -0,0 +1,17 @@ +import { IsInt, IsOptional, IsString } from 'class-validator'; + +export class RegresarIdPrestamoDto { + @IsInt() + id_operador: number; + + @IsInt() + id_prestamo: number; + + @IsInt() + @IsOptional() + id_institucion_infraccion?: number; + + @IsString() + @IsOptional() + descripcion?: string; +} diff --git a/src/prestamo/dto/input/regresar-numero-inventario.dto.ts b/src/prestamo/dto/input/regresar-numero-inventario.dto.ts new file mode 100644 index 0000000..ebb6eb8 --- /dev/null +++ b/src/prestamo/dto/input/regresar-numero-inventario.dto.ts @@ -0,0 +1,17 @@ +import { IsInt, IsOptional, IsString } from 'class-validator'; + +export class RegresarNumeroInventarioDto { + @IsInt() + id_operador: number; + + @IsString() + numero_inventairo: string; + + @IsInt() + @IsOptional() + id_institucion_infraccion?: number; + + @IsString() + @IsOptional() + descripcion?: string; +} diff --git a/src/prestamo/prestamo.controller.ts b/src/prestamo/prestamo.controller.ts index 9ed0625..2437e87 100644 --- a/src/prestamo/prestamo.controller.ts +++ b/src/prestamo/prestamo.controller.ts @@ -2,17 +2,21 @@ import { Body, Controller, Get, Post, Put, Query } from '@nestjs/common'; import { ApiBody, ApiOperation, ApiQuery, ApiTags } from '@nestjs/swagger'; import { Serealize } from '../interceptors/serialize.interceptor'; import { PrestamoService } from './prestamo.service'; -import { PedirDto } from './dto/input/pedir.dto'; + import { ActivosDto } from './dto/input/activos.dto'; -import { IdUsuarioDto } from './dto/input/id-usuario.dto'; -import { IdPrestamoDto } from './dto/input/id-prestamo.dto'; -import { NumeroInventarioDto } from './dto/input/numero-inventario.dto'; +import { CancelarUsuarioDto } from './dto/input/cancelar-usuario.dto'; +import { CancelarOperadorDto } from './dto/input/cancelar-operador.dto'; import { HistorialDto } from './dto/input/historial.dto'; import { HistorialEquipoDto } from './dto/input/historial-equipo.dto'; import { HistorialUsuarioDto } from './dto/input/historial-usuario.dto'; -import { UpdateOperadorDto } from './dto/input/update-operador.dto'; -import { CancelarUsuarioDto } from './dto/input/cancelar-usuario.dto'; -import { CancelarOperadorDto } from './dto/input/cancelar-operador.dto'; +import { IdPrestamoDto } from './dto/input/id-prestamo.dto'; +import { IdUsuarioDto } from './dto/input/id-usuario.dto'; +import { NumeroInventarioDto } from './dto/input/numero-inventario.dto'; +import { PedirDto } from './dto/input/pedir.dto'; +import { EntregarDto } from './dto/input/entregar.dto'; +import { RegresarIdPrestamoDto } from './dto/input/regresar-id-prestamo.dto'; +import { RegresarNumeroInventarioDto } from './dto/input/regresar-numero-inventario.dto'; + import { PrestamoOutputDto } from './dto/output/prestamo.dto'; import { PrestamoActivoOutputDto } from './dto/output/prestamo-activo.dto'; import { PrestamoUsuarioOutputDto } from './dto/output/prestamo-usuario.dto'; @@ -122,7 +126,7 @@ export class PrestamoController { description: 'Ambas variables son obligatorias.', examples: { ejemplo: { value: { id_prestamo: 1, id_operador: 4 } } }, }) - entregar(@Body() body: UpdateOperadorDto) { + entregar(@Body() body: EntregarDto) { return this.prestamoService.entregar(body.id_prestamo, body.id_operador); } @@ -321,14 +325,52 @@ export class PrestamoController { ); } - @Put('regresar') + @Put('regresar-id-prestamo') @ApiOperation({ description: 'Endpoint que desactiva un préstamo.' }) @ApiBody({ description: 'Ambas variables son obligatorias.', - examples: { ejemplo: { value: { id_prestamo: 1, id_operador: 4 } } }, + examples: { + ejemplo: { + value: { + id_operador: 4, + id_prestamo: 1, + _descripcion: '', + _id_institucion_infraccion: 603, + }, + }, + }, }) - regresar(@Body() body: UpdateOperadorDto) { - return this.prestamoService.regresar(body.id_prestamo, body.id_operador); + regresarIdPrestamo(@Body() body: RegresarIdPrestamoDto) { + return this.prestamoService.regresarIdPrestamo( + body.id_operador, + body.id_prestamo, + body.descripcion, + body.id_institucion_infraccion, + ); + } + + @Put('regresar-numero-inventario') + @ApiOperation({ description: 'Endpoint que desactiva un préstamo.' }) + @ApiBody({ + description: 'Ambas variables son obligatorias.', + examples: { + ejemplo: { + value: { + id_operador: 4, + numero_inventario: '', + _descripcion: '', + _id_institucion_infraccion: 603, + }, + }, + }, + }) + regresar(@Body() body: RegresarNumeroInventarioDto) { + return this.prestamoService.regresarNumeroInventario( + body.id_operador, + body.numero_inventairo, + body.descripcion, + body.id_institucion_infraccion, + ); } // @Get('reporte') diff --git a/src/prestamo/prestamo.module.ts b/src/prestamo/prestamo.module.ts index e7e6775..b109ac5 100644 --- a/src/prestamo/prestamo.module.ts +++ b/src/prestamo/prestamo.module.ts @@ -1,4 +1,4 @@ -import { Module } from '@nestjs/common'; +import { forwardRef, Module } from '@nestjs/common'; import { TypeOrmModule } from '@nestjs/typeorm'; import { PrestamoController } from './prestamo.controller'; import { PrestamoService } from './prestamo.service'; @@ -12,6 +12,7 @@ import { InstitucionTipoEntradaModule } from '../institucion-tipo-entrada/instit import { OperadorModule } from '../operador/operador.module'; import { ModuloModule } from '../modulo/modulo.module'; import { MotivoModule } from '../motivo/motivo.module'; +import { MultaModule } from 'src/multa/multa.module'; import { StatusModule } from '../status/status.module'; import { TipoUsuarioModule } from '../tipo-usuario/tipo-usuario.module'; import { UsuarioModule } from '../usuario/usuario.module'; @@ -25,6 +26,7 @@ import { UsuarioModule } from '../usuario/usuario.module'; InstitucionTipoEntradaModule, ModuloModule, MotivoModule, + forwardRef(() => MultaModule), OperadorModule, StatusModule, TypeOrmModule.forFeature([Prestamo]), diff --git a/src/prestamo/prestamo.service.ts b/src/prestamo/prestamo.service.ts index 23f3401..e96188c 100644 --- a/src/prestamo/prestamo.service.ts +++ b/src/prestamo/prestamo.service.ts @@ -1,6 +1,8 @@ import * as moment from 'moment'; import { ConflictException, + forwardRef, + Inject, Injectable, NotFoundException, } from '@nestjs/common'; @@ -20,6 +22,7 @@ import { InstitucionTipoCarritoService } from '../institucion-tipo-carrito/insti import { InstitucionTipoEntradaService } from '../institucion-tipo-entrada/institucion-tipo-entrada.service'; import { ModuloService } from '../modulo/modulo.service'; import { MotivoService } from '../motivo/motivo.service'; +import { MultaService } from '../multa/multa.service'; import { OperadorService } from '../operador/operador.service'; import { StatusService } from '../status/status.service'; import { TipoUsuarioService } from '../tipo-usuario/tipo-usuario.service'; @@ -37,6 +40,8 @@ export class PrestamoService { private institucionTipoEntradaService: InstitucionTipoEntradaService, private moduloService: ModuloService, private motivoService: MotivoService, + @Inject(forwardRef(() => MultaService)) + private multaService: MultaService, private operadorService: OperadorService, private statusService: StatusService, private tipoUsuarioService: TipoUsuarioService, @@ -386,18 +391,52 @@ export class PrestamoService { }); } - async regresar(id_prestamo: number, id_operador: number) { + async regresar( + prestamo: Prestamo, + operadorRegreso: Operador, + descripcion?: string, + id_institucion_infraccion?: number, + ) { const ahora = moment(); - const operadorRegreso = await this.operadorService.findById(id_operador); - const prestamo = await this.findById(id_prestamo); + const tardanza = Math.trunc(ahora.diff(moment(prestamo.hora_fin)) / 60000); + const semanasCastigo = Math.trunc( + tardanza / operadorRegreso.institucion.tiempo_entrega, + ); this.validacionBasicaPrestamo(prestamo); if (prestamo.equipo.status.id_status === 2) throw new ConflictException('Aun no se entrega el equipo al usuario.'); + if (id_institucion_infraccion && !descripcion) + throw new ConflictException('No se mandó la descripción de lo ocurrido.'); + id_institucion_infraccion && !descripcion; prestamo.activo = false; prestamo.fecha_entrega = ahora.toDate(); prestamo.operadorRegreso = operadorRegreso; prestamo.equipo.status = await this.statusService.findById(1); + if (semanasCastigo > 0) { + if (id_institucion_infraccion) + await this.multaService.create( + prestamo, + operadorRegreso, + `El usaurio se tardó: ${tardanza} minutos en entregar el equipo de cómputo. ${descripcion}`, + semanasCastigo, + id_institucion_infraccion, + ); + else + await this.multaService.create( + prestamo, + operadorRegreso, + `El usaurio se tardó: ${tardanza} minutos en entregar el equipo de cómputo.`, + semanasCastigo, + ); + } else if (id_institucion_infraccion) + await this.multaService.create( + prestamo, + operadorRegreso, + descripcion, + null, + id_institucion_infraccion, + ); return this.equipoService .update(prestamo.equipo) .then((_) => this.repository.save(prestamo)) @@ -406,6 +445,43 @@ export class PrestamoService { })); } + async regresarIdPrestamo( + id_operador: number, + id_prestamo: number, + descripcion?: string, + id_institucion_infraccion?: number, + ) { + const operador = await this.operadorService.findById(id_operador); + const prestamo = await this.findById(id_prestamo); + + return this.regresar( + prestamo, + operador, + descripcion, + id_institucion_infraccion, + ); + } + + async regresarNumeroInventario( + id_operador: number, + numero_inventario: string, + descripcion?: string, + id_institucion_infraccion?: number, + ) { + const operador = await this.operadorService.findById(id_operador); + const prestamo = await this.findByNumeroInventario( + operador.institucion, + numero_inventario, + ); + + return this.regresar( + prestamo, + operador, + descripcion, + id_institucion_infraccion, + ); + } + validacionBasicaPrestamo(prestamo: Prestamo) { if (prestamo.cancelado_usuario) throw new ConflictException(