diff --git a/src/prestamo/dto/prestamo-cancelar-operador.dto.ts b/src/prestamo/dto/prestamo-cancelar-operador.dto.ts new file mode 100644 index 0000000..8c7ab41 --- /dev/null +++ b/src/prestamo/dto/prestamo-cancelar-operador.dto.ts @@ -0,0 +1,9 @@ +import { IsInt } from 'class-validator'; + +export class PrestamoCancelarOperadorDto { + @IsInt() + id_operador: number; + + @IsInt() + id_prestamo: number; +} diff --git a/src/prestamo/dto/prestamo-cancelar-usuario.dto.ts b/src/prestamo/dto/prestamo-cancelar-usuario.dto.ts new file mode 100644 index 0000000..cfc06f3 --- /dev/null +++ b/src/prestamo/dto/prestamo-cancelar-usuario.dto.ts @@ -0,0 +1,6 @@ +import { IsInt } from 'class-validator'; + +export class PrestamoCancelarUsuarioDto { + @IsInt() + id_prestamo: number; +} diff --git a/src/prestamo/entity/prestamo.entity.ts b/src/prestamo/entity/prestamo.entity.ts index f7f4343..90d031b 100644 --- a/src/prestamo/entity/prestamo.entity.ts +++ b/src/prestamo/entity/prestamo.entity.ts @@ -38,13 +38,14 @@ export class Prestamo { @Column({ type: Date, nullable: false }) hora_max_recoger: Date; - @ManyToOne(() => Equipo, (equipo) => equipo.prestamos) + @ManyToOne(() => Equipo, (equipo) => equipo.prestamos, { eager: true }) @JoinColumn({ name: 'id_equipo' }) equipo: Equipo; @ManyToOne( () => Operador, (operadorEntrega) => operadorEntrega.prestamosOperadorEntrega, + { eager: true }, ) @JoinColumn({ name: 'id_operador_entrega' }) operadorEntrega: Operador; @@ -52,6 +53,7 @@ export class Prestamo { @ManyToOne( () => Operador, (operadorRegreso) => operadorRegreso.prestamosOperadorRegreso, + { eager: true }, ) @JoinColumn({ name: 'id_operador_regreso' }) operadorRegreso: Operador; diff --git a/src/prestamo/prestamo.controller.ts b/src/prestamo/prestamo.controller.ts index d5f6e53..9b08b8f 100644 --- a/src/prestamo/prestamo.controller.ts +++ b/src/prestamo/prestamo.controller.ts @@ -1,5 +1,5 @@ import { Body, Controller, Get, Post, Put, Query } from '@nestjs/common'; -import { ApiOperation, ApiQuery, ApiTags } from '@nestjs/swagger'; +import { ApiBody, ApiOperation, ApiQuery, ApiTags } from '@nestjs/swagger'; import { PrestamoService } from './prestamo.service'; import { PrestamoPedirDto } from './dto/prestamo-pedir.dto'; import { PrestamoActivosDto } from './dto/prestamo-activos.dto'; @@ -9,6 +9,8 @@ import { PrestamoNumeroInventarioDto } from './dto/prestamo-numero-inventario.dt import { PrestamoHistorialEquipoDto } from './dto/prestamo-historial-equipo.dto'; import { PrestamoHistorialUsuarioDto } from './dto/prestamo-historial-usuario.dto'; import { PrestamoHistorialDto } from './dto/prestamo-historial.dto'; +import { PrestamoCancelarOperadorDto } from './dto/prestamo-cancelar-operador.dto'; +import { PrestamoCancelarUsuarioDto } from './dto/prestamo-cancelar-usuario.dto'; @Controller('prestamo') @ApiTags('prestamo') @@ -81,13 +83,28 @@ export class PrestamoController { @ApiOperation({ description: 'Endpoint que cancela un prestamo por parte del operador.', }) - cancelarOperador() {} + @ApiBody({ + description: 'Ambas variables son obligatorias.', + examples: { ejemplo: { value: { id_prestamo: 1, id_operador: 1 } } }, + }) + cancelarOperador(@Body() body: PrestamoCancelarOperadorDto) { + return this.prestamoService.cancelarOperador( + body.id_prestamo, + body.id_operador, + ); + } @Put('cancelar-usuario') @ApiOperation({ description: 'Endpoint que cancela el prestamo del usuario.', }) - cancelarUsuario() {} + @ApiBody({ + description: 'La variable id_prestamo es obligatoria.', + examples: { ejemplo: { value: { id_prestamo: 1 } } }, + }) + cancelarUsuario(@Body() body: PrestamoCancelarUsuarioDto) { + return this.prestamoService.cancelarUsuario(body.id_prestamo); + } @Put('entregar') @ApiOperation({ @@ -214,7 +231,22 @@ export class PrestamoController { } @Post() - @ApiOperation({ description: 'Endpoint .' }) + @ApiOperation({ description: 'Endpoint que crea un prestamo nuevo.' }) + @ApiBody({ + description: + 'Todas las variables a excepción de id_programa y id_tipo_entrada son obligatorios.', + examples: { + ejemplo: { + value: { + id_usuario: 1, + id_modulo: 1, + id_tipo_carrito: 1, + id_programa: 1, + id_tipo_entrada: 1, + }, + }, + }, + }) pedir(@Body() body: PrestamoPedirDto) { return this.prestamoService.create( body.id_usuario, diff --git a/src/prestamo/prestamo.service.ts b/src/prestamo/prestamo.service.ts index fa89607..90ccea3 100644 --- a/src/prestamo/prestamo.service.ts +++ b/src/prestamo/prestamo.service.ts @@ -6,7 +6,6 @@ import { } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; import { FindOperator, Like, Repository } from 'typeorm'; -import { Carrera } from '../institucion-carrera/entity/carrera.entity'; import { Modulo } from '../modulo/entity/modulo.entity'; import { Operador } from '../operador/entity/operador.entity'; import { Prestamo } from './entity/prestamo.entity'; @@ -44,6 +43,36 @@ export class PrestamoService { private usuarioService: UsuarioService, ) {} + async cancelarOperador(id_prestamo: number, id_operador: number) { + const ahora = moment(); + const operadorRegreso = await this.operadorService.findById(id_operador); + const prestamo = await this.findById(id_prestamo); + + prestamo.activo = false; + prestamo.fecha_entrega = ahora.toDate(); + prestamo.cancelado_operador = true; + prestamo.operadorRegreso = operadorRegreso; + prestamo.equipo.status = await this.statusService.findById(6); + return this.equipoService + .update(prestamo.equipo) + .then((_) => this.repository.save(prestamo)) + .then((_) => ({ message: 'Se canceló correctamente este préstamo.' })); + } + + async cancelarUsuario(id_prestamo: number) { + const ahora = moment(); + const prestamo = await this.findById(id_prestamo); + + prestamo.activo = false; + prestamo.fecha_entrega = ahora.toDate(); + prestamo.cancelado_usuario = true; + prestamo.equipo.status = await this.statusService.findById(1); + return this.equipoService + .update(prestamo.equipo) + .then((_) => this.repository.save(prestamo)) + .then((_) => ({ message: 'Se canceló correctamente este préstamo.' })); + } + async create( id_usuario: number, id_modulo: number, @@ -115,7 +144,6 @@ export class PrestamoService { equipo?: string; fechaFin?: string; fechaInicio?: string; - // id_carrera?: string; id_institucion?: string; id_modulo?: string; id_operador_entrega?: string; @@ -156,21 +184,13 @@ export class PrestamoService { usuario: { usuario?: FindOperator; tipoUsuario?: TipoUsuario; - // institucionCarrera: { carrera?: Carrera }; }; operadorEntrega?: Operador; operadorRegreso?: Operador; } = { equipo: { carrito: {} }, - usuario: { - // institucionCarrera: {} - }, + usuario: {}, }; - // const carrera = filtros.id_carrera - // ? await this.institucionCarreraService.findCarreraByIdCarrera( - // parseInt(filtros.id_carrera), - // ) - // : null; const institucion = filtros.id_institucion ? await this.institucionService.findById(parseInt(filtros.id_institucion)) : null; @@ -201,9 +221,6 @@ export class PrestamoService { if (filtros.activo) { if (typeof filtros.activo === 'boolean') busqueda.activo = filtros.activo; else busqueda.activo = filtros.activo === 'true'; - } else { - // join.oe = 'p.operadorEntrega'; - // join.or = 'p.operadorRegreso'; } if (filtros.carrito) busqueda.equipo.carrito.carrito = Like(`%${filtros.carrito}%`); @@ -211,7 +228,6 @@ export class PrestamoService { if (filtros.id_prestamo) busqueda.id_prestamo = Like(filtros.id_prestamo); if (filtros.usuario) busqueda.usuario.usuario = Like(`%${filtros.usuario}%`); - // if (carrera) busqueda.usuario.institucionCarrera.carrera = carrera; if (institucion) busqueda.equipo.carrito.modulo = { institucion }; if (modulo) busqueda.equipo.carrito.modulo = modulo; if (operadorEntrega) busqueda.operadorEntrega = operadorEntrega;