diff --git a/src/prestamo/dto/prestamo-activos.dto.ts b/src/prestamo/dto/prestamo-activos.dto.ts index 596b79d..fd2149a 100644 --- a/src/prestamo/dto/prestamo-activos.dto.ts +++ b/src/prestamo/dto/prestamo-activos.dto.ts @@ -12,9 +12,6 @@ export class PrestamoActivosDto { @IsNumberString() pagina: string; - @IsNumberString() - id_modulo: string; - @IsString() @IsOptional() carrito?: string; @@ -23,6 +20,14 @@ export class PrestamoActivosDto { @IsOptional() equipo?: string; + @IsNumberString() + @IsOptional() + id_institucion?: string; + + @IsNumberString() + @IsOptional() + id_modulo?: string; + @IsNumberString() @IsOptional() id_prestamo?: string; diff --git a/src/prestamo/dto/prestamo-id-prestamo.dto.ts b/src/prestamo/dto/prestamo-id-prestamo.dto.ts new file mode 100644 index 0000000..c201b1b --- /dev/null +++ b/src/prestamo/dto/prestamo-id-prestamo.dto.ts @@ -0,0 +1,6 @@ +import { IsNumberString } from 'class-validator'; + +export class PrestamoIdPrestamoDto { + @IsNumberString() + id_prestamo: string; +} diff --git a/src/prestamo/dto/prestamo-id-usuario.dto.ts b/src/prestamo/dto/prestamo-id-usuario.dto.ts new file mode 100644 index 0000000..ff066af --- /dev/null +++ b/src/prestamo/dto/prestamo-id-usuario.dto.ts @@ -0,0 +1,6 @@ +import { IsNumberString } from 'class-validator'; + +export class PrestamoIdUsuarioDto { + @IsNumberString() + id_usuario: string; +} diff --git a/src/prestamo/dto/prestamo-numero-inventario.dto.ts b/src/prestamo/dto/prestamo-numero-inventario.dto.ts new file mode 100644 index 0000000..ad68dcd --- /dev/null +++ b/src/prestamo/dto/prestamo-numero-inventario.dto.ts @@ -0,0 +1,9 @@ +import { IsNumberString, IsString } from 'class-validator'; + +export class PrestamoNumeroInventarioDto { + @IsNumberString() + id_institucion: string; + + @IsString() + numero_inventario: string; +} diff --git a/src/prestamo/prestamo.controller.ts b/src/prestamo/prestamo.controller.ts index d5bbb0f..dbbb885 100644 --- a/src/prestamo/prestamo.controller.ts +++ b/src/prestamo/prestamo.controller.ts @@ -1,8 +1,11 @@ import { Body, Controller, Get, Post, Put, Query } from '@nestjs/common'; -import { ApiTags } from '@nestjs/swagger'; +import { 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'; +import { PrestamoIdUsuarioDto } from './dto/prestamo-id-usuario.dto'; +import { PrestamoIdPrestamoDto } from './dto/prestamo-id-prestamo.dto'; +import { PrestamoNumeroInventarioDto } from './dto/prestamo-numero-inventario.dto'; @Controller('prestamo') @ApiTags('prestamo') @@ -44,13 +47,47 @@ export class PrestamoController { } @Get('prestamo-id-prestamo') - prestamoIdPrestamo() {} + @ApiOperation({ description: 'Endpoint que retorna un préstamo por su id.' }) + @ApiQuery({ description: '', name: '', type: '' }) + prestamoIdPrestamo(@Query() query: PrestamoIdPrestamoDto) { + return this.prestamoService.findById(parseInt(query.id_prestamo)); + } @Get('prestamo-id-usuario') - prestamoIdUsuario() {} + @ApiOperation({ + description: + 'Endpoint que retorna un préstamo activo por el id del usuario.', + }) + @ApiQuery({ + description: 'Id del usuario.', + name: 'id_usuario', + type: 'string', + }) + prestamoIdUsuario(@Query() query: PrestamoIdUsuarioDto) { + return this.prestamoService.findByIdUsuario(parseInt(query.id_usuario)); + } @Get('prestamo-numero-inventario') - prestamoNumeroInventario() {} + @ApiOperation({ + description: + 'Endpoint que retorna un préstamo activo por el número de inventario de un equipo.', + }) + @ApiQuery({ + description: 'Id de la institución.', + name: 'id_institucion', + type: 'string', + }) + @ApiQuery({ + description: 'El número de inventario del equipo.', + name: 'numero_inventario', + type: 'string', + }) + prestamoNumeroInventario(@Query() query: PrestamoNumeroInventarioDto) { + return this.prestamoService.findByNumeroInventario( + parseInt(query.id_institucion), + query.numero_inventario, + ); + } @Put('regresar') regresar() {} diff --git a/src/prestamo/prestamo.service.ts b/src/prestamo/prestamo.service.ts index e8e693d..d34e49c 100644 --- a/src/prestamo/prestamo.service.ts +++ b/src/prestamo/prestamo.service.ts @@ -115,7 +115,7 @@ export class PrestamoService { equipo?: string; fechaFin?: string; fechaInicio?: string; - id_carrera?: string; + // id_carrera?: string; id_institucion?: string; id_modulo?: string; id_operador_entrega?: string; @@ -156,16 +156,21 @@ export class PrestamoService { usuario: { usuario?: FindOperator; tipoUsuario?: TipoUsuario; - institucionCarrera: { carrera?: Carrera }; + // institucionCarrera: { carrera?: Carrera }; }; operadorEntrega?: Operador; operadorRegreso?: Operador; - } = { equipo: { carrito: {} }, usuario: { institucionCarrera: {} } }; - const carrera = filtros.id_carrera - ? await this.institucionCarreraService.findCarreraByIdCarrera( - parseInt(filtros.id_carrera), - ) - : null; + } = { + equipo: { carrito: {} }, + usuario: { + // institucionCarrera: {} + }, + }; + // 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; @@ -206,7 +211,7 @@ export class PrestamoService { if (filtros.equipo) busqueda.id_prestamo = parseInt(filtros.id_prestamo); if (filtros.usuario) busqueda.usuario.usuario = Like(`%${filtros.usuario}%`); - if (carrera) busqueda.usuario.institucionCarrera.carrera = carrera; + // 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; @@ -230,4 +235,35 @@ export class PrestamoService { return prestamo; }); } + + findByIdUsuario(id_usuario: number) { + return this.usuarioService + .findById(id_usuario, true, true) + .then((usuario) => this.repository.findOne({ usuario, activo: true })) + .then((prestamo) => { + if (!prestamo) + throw new NotFoundException( + 'Este usuario no tiene un prestamo activo.', + ); + return prestamo; + }); + } + + async findByNumeroInventario( + id_institucion: number, + numero_inventario: string, + ) { + const institucion = await this.institucionService.findById(id_institucion); + + return this.equipoService + .findByNumeroInventario(institucion, numero_inventario) + .then((equipo) => this.repository.findOne({ equipo, activo: true })) + .then((prestamo) => { + if (!prestamo) + throw new NotFoundException( + 'No existe este un préstamo activo con este equipo de cómputo.', + ); + return prestamo; + }); + } }