From 476a8f62d044b68b4d2ed6e45d2f7ca580f596ed Mon Sep 17 00:00:00 2001 From: Marco Date: Wed, 31 Aug 2022 18:17:08 -0500 Subject: [PATCH] passcode controller and service --- src/app.module.ts | 1 - .../dto/input/devolucion.dto.ts} | 5 +- .../dto/input/notificacion.dto.ts} | 7 +- src/passcode/dto/input/prestamo.dto.ts | 10 ++ src/passcode/dto/output/devolucion.dto.ts | 7 ++ src/passcode/dto/output/notificacion.dto.ts | 6 + src/passcode/dto/output/passcode.dto.ts | 15 +++ src/passcode/entity/passcode.entity.ts | 2 + src/passcode/passcode.controller.ts | 114 +++++++++++++++++- src/passcode/passcode.module.ts | 5 +- src/passcode/passcode.service.ts | 46 ++++++- src/prestamo/prestamo.controller.ts | 88 -------------- src/prestamo/prestamo.module.ts | 5 +- src/prestamo/prestamo.service.ts | 6 +- 14 files changed, 207 insertions(+), 110 deletions(-) rename src/{prestamo/dto/input/regresar-autoprestamo.dto.ts => passcode/dto/input/devolucion.dto.ts} (52%) rename src/{prestamo/dto/input/autoprestamo.dto.ts => passcode/dto/input/notificacion.dto.ts} (50%) create mode 100644 src/passcode/dto/input/prestamo.dto.ts create mode 100644 src/passcode/dto/output/devolucion.dto.ts create mode 100644 src/passcode/dto/output/notificacion.dto.ts create mode 100644 src/passcode/dto/output/passcode.dto.ts diff --git a/src/app.module.ts b/src/app.module.ts index 4f7aec6..eb58f3b 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -168,7 +168,6 @@ import { Usuario } from './usuario/entity/usuario.entity'; TipoUsuarioModule, UploadFileModule, UsuarioModule, - PasscodeModule, ], }) export class AppModule {} diff --git a/src/prestamo/dto/input/regresar-autoprestamo.dto.ts b/src/passcode/dto/input/devolucion.dto.ts similarity index 52% rename from src/prestamo/dto/input/regresar-autoprestamo.dto.ts rename to src/passcode/dto/input/devolucion.dto.ts index 80c688f..0635043 100644 --- a/src/prestamo/dto/input/regresar-autoprestamo.dto.ts +++ b/src/passcode/dto/input/devolucion.dto.ts @@ -2,10 +2,7 @@ import { IsInt } from 'class-validator'; - export class RegresarAutoprestamoDto { - @IsInt() - id_operador: number; - + export class DevolucionDto { @IsInt() passcode: number; } diff --git a/src/prestamo/dto/input/autoprestamo.dto.ts b/src/passcode/dto/input/notificacion.dto.ts similarity index 50% rename from src/prestamo/dto/input/autoprestamo.dto.ts rename to src/passcode/dto/input/notificacion.dto.ts index 20563e2..b758b22 100644 --- a/src/prestamo/dto/input/autoprestamo.dto.ts +++ b/src/passcode/dto/input/notificacion.dto.ts @@ -1,12 +1,9 @@ import { IsInt } from 'class-validator'; -export class AutoprestamoDto { +export class NotificacionDto { @IsInt() passcode: number; @IsInt() - id_locker: number; - - @IsInt() - id_operador: number; + estatus: number; } diff --git a/src/passcode/dto/input/prestamo.dto.ts b/src/passcode/dto/input/prestamo.dto.ts new file mode 100644 index 0000000..22f0c57 --- /dev/null +++ b/src/passcode/dto/input/prestamo.dto.ts @@ -0,0 +1,10 @@ +import { IsString } from 'class-validator'; + +export class PrestamoDto { + // verificar si es entero o string + @IsString() + passcode: string; + + @IsString() + id_locker: string; +} diff --git a/src/passcode/dto/output/devolucion.dto.ts b/src/passcode/dto/output/devolucion.dto.ts new file mode 100644 index 0000000..879a132 --- /dev/null +++ b/src/passcode/dto/output/devolucion.dto.ts @@ -0,0 +1,7 @@ +import { Expose } from 'class-transformer'; + +export class DevolucionOutputDto { + @Expose() + code; +} + diff --git a/src/passcode/dto/output/notificacion.dto.ts b/src/passcode/dto/output/notificacion.dto.ts new file mode 100644 index 0000000..7488eea --- /dev/null +++ b/src/passcode/dto/output/notificacion.dto.ts @@ -0,0 +1,6 @@ +import { Expose } from 'class-transformer'; + +export class NotificacionOutputDto { + @Expose() + code; +} diff --git a/src/passcode/dto/output/passcode.dto.ts b/src/passcode/dto/output/passcode.dto.ts new file mode 100644 index 0000000..4b6e658 --- /dev/null +++ b/src/passcode/dto/output/passcode.dto.ts @@ -0,0 +1,15 @@ +import { Expose } from 'class-transformer'; + +export class PasscodeOutputDto { + @Expose() + code; + + @Expose() + passcode; + + @Expose() + id_locker; + + @Expose() + door; +} diff --git a/src/passcode/entity/passcode.entity.ts b/src/passcode/entity/passcode.entity.ts index 2661286..7d0bd89 100644 --- a/src/passcode/entity/passcode.entity.ts +++ b/src/passcode/entity/passcode.entity.ts @@ -15,6 +15,8 @@ import { @Column({ type: String, nullable: false }) passcode: string; + @Column({ type: Boolean, nullable: true }) + abrio: boolean @OneToOne(() => Prestamo, (Prestamo) => Prestamo.id_prestamo, { eager: true, diff --git a/src/passcode/passcode.controller.ts b/src/passcode/passcode.controller.ts index 7e6405d..12902e8 100644 --- a/src/passcode/passcode.controller.ts +++ b/src/passcode/passcode.controller.ts @@ -1,4 +1,114 @@ -import { Controller } from '@nestjs/common'; +import { Body,Controller, ForbiddenException,Get, Query,Request, Put, UseGuards } from '@nestjs/common'; +import { + ApiBearerAuth, + ApiBody, + ApiOperation, + ApiQuery, + ApiTags, + } from '@nestjs/swagger'; +import { AuthGuard } from '@nestjs/passport'; +import { Serealize } from '../interceptors/serialize.interceptor'; +import { DevolucionDto } from './dto/input/devolucion.dto' +import { DevolucionOutputDto } from './dto/output/devolucion.dto'; +import { PasscodeService } from './passcode.service' +import { NotificacionDto } from './dto/input/notificacion.dto'; +import { NotificacionOutputDto } from './dto/output/notificacion.dto'; +import { Operador } from '../operador/entity/operador.entity'; +import { PrestamoDto } from './dto/input/prestamo.dto'; +import { PasscodeOutputDto } from './dto/output/passcode.dto'; + + @Controller('passcode') -export class PasscodeController {} +export class PasscodeController { + constructor(private passcodeService: PasscodeService) {} + @Serealize(PasscodeOutputDto) + @Get('prestamo') + @UseGuards(AuthGuard('jwt')) + @ApiOperation({ description: 'Endpoint que verifica la existencia y validez del passcode.' }) + @ApiBearerAuth('jwt') + @ApiQuery({ + description: 'Passcode del préstamo', + name: 'passcode', + type: 'string', + }) + @ApiQuery({ + description: 'id_locker, un locker hace referencia a un carrito.', + name: 'id_locker', + type: 'string', + }) + prestamo(@Request() req, @Query() query: PrestamoDto) { + const operador: Operador = req.user.operador; + + if ( + !operador || + (operador.tipoUsuario.id_tipo_usuario != 3 && + operador.tipoUsuario.id_tipo_usuario != 4) + ) + throw new ForbiddenException( + 'No tienes los permisos necesarios para realizar esta acción.', + ); + + + return this.passcodeService.findByPasscode(query.passcode, query.id_locker); + } + + @Serealize(NotificacionOutputDto) + @Put('notificacion') + @UseGuards(AuthGuard('jwt')) + @ApiOperation({ + description: 'Endpoint que actualiza el status del equipo a "En uso" por medio del autoprestamo.', + }) + @ApiBearerAuth('jwt') + @ApiBody({ + description: 'Ambas variables son obligatorias.', + examples: { ejemplo: { value: { passcode: 123456789012, estatus: 1 } } }, + }) + autoprestamo(@Request() req, @Body() body: NotificacionDto) { + const operador: Operador = req.user.operador; + + if ( + !operador || + (operador.tipoUsuario.id_tipo_usuario != 3 && + operador.tipoUsuario.id_tipo_usuario != 4) + ) + throw new ForbiddenException( + 'No tienes los permisos necesarios para realizar esta acción.', + ); + + return this.passcodeService.entregarEquipo(body.passcode); + } + + @Serealize(DevolucionOutputDto) + @Put('devolucion') + @UseGuards(AuthGuard('jwt')) + @ApiOperation({ description: 'Endpoint que da por terminado un préstamo.' }) + @ApiBearerAuth('jwt') + @ApiBody({ + description: 'Solo necesitamos el passcode.', + examples: { + ejemplo: { + value: { + passcode: 123456789012, + }, + }, + }, + }) + devolucion( + @Request() req, + @Body() body: DevolucionDto, + ) { + const operador: Operador = req.user.operador; + + if ( + !operador || + (operador.tipoUsuario.id_tipo_usuario != 3 && + operador.tipoUsuario.id_tipo_usuario != 4) + ) + throw new ForbiddenException( + 'No tienes los permisos necesarios para realizar esta acción.', + ); + + return this.passcodeService.devolverEquipo(body.passcode); + } +} diff --git a/src/passcode/passcode.module.ts b/src/passcode/passcode.module.ts index 44c6792..fda9a2e 100644 --- a/src/passcode/passcode.module.ts +++ b/src/passcode/passcode.module.ts @@ -1,10 +1,13 @@ -import { Module } from '@nestjs/common'; +import { forwardRef, Module } from '@nestjs/common'; import { TypeOrmModule } from '@nestjs/typeorm'; import { PasscodeController } from './passcode.controller'; import { PasscodeService } from './passcode.service'; import { Passcode } from './entity/passcode.entity' +import { PrestamoModule } from '../prestamo/prestamo.module'; + @Module({ imports: [ + forwardRef(()=>PrestamoModule), TypeOrmModule.forFeature([Passcode]), ], controllers: [PasscodeController], diff --git a/src/passcode/passcode.service.ts b/src/passcode/passcode.service.ts index 06768fc..047e9a3 100644 --- a/src/passcode/passcode.service.ts +++ b/src/passcode/passcode.service.ts @@ -1,14 +1,17 @@ -import { Injectable } from '@nestjs/common'; +import { BadRequestException, forwardRef, Injectable, Inject } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; -import { createQueryBuilder, Repository } from 'typeorm'; +import { Repository } from 'typeorm'; import { Passcode} from './entity/passcode.entity' -import { Prestamo } from '../prestamo/entity/prestamo.entity'; +import { PrestamoService } from '../prestamo/prestamo.service'; @Injectable() export class PasscodeService { constructor( @InjectRepository(Passcode) private repository: Repository, - ){} + @Inject(forwardRef(() => PrestamoService)) + private prestamoService: PrestamoService, + + ){} crearPasscode(id_tipo_carrito, id_prestamo){ const prestamo = { @@ -29,5 +32,38 @@ export class PasscodeService { }), ) return passcode - } + } + + entregarEquipo(passcode){ + passcode += '' + let id_prestamo = passcode.substring(2,8) + return this.prestamoService.entregar(parseInt(id_prestamo), 5); + } + + devolverEquipo(passcode){ + passcode += '' + const id_prestamo = passcode.substring(2,8) + + return this.prestamoService.regresarIdPrestamo( + 5, + parseInt(id_prestamo) + ); + } + + findByPasscode(passcode: string, id_locker: string){ + let door = "" + return this.repository.findOne({where: {passcode}}) + .then(async(passcode) => { + if(!passcode) + throw new BadRequestException( + `Este passcode no existe`, + ); + + await this.prestamoService.findById(passcode.prestamo.id_prestamo).then((prestamo)=>{ + door = prestamo.equipo.equipo.substring(1,3) + + }) + return {...passcode, code: 1, id_locker, door: parseInt(door)} + }) + } } diff --git a/src/prestamo/prestamo.controller.ts b/src/prestamo/prestamo.controller.ts index 94ee890..8b3959e 100644 --- a/src/prestamo/prestamo.controller.ts +++ b/src/prestamo/prestamo.controller.ts @@ -24,8 +24,6 @@ import { IdEquipoPaginaDto } from '../dto/id-equipo-pagina.dto'; import { IdUsuarioDto } from '../dto/id-usuario.dto'; import { IdUsuarioPaginaDto } from '../dto/id-usuario-pagina.dto'; import { NumeroInventarioDto } from '../dto/numero-inventario.dto'; -// import { PasscodeDto } from '../dto/passcode.dto'; -import { AutoprestamoDto} from './dto/input/autoprestamo.dto' import { ActivosDto } from './dto/input/activos.dto'; import { CancelarUsuarioDto } from './dto/input/cancelar-usuario.dto'; import { CancelarOperadorDto } from './dto/input/cancelar-operador.dto'; @@ -35,7 +33,6 @@ 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 { RegresarAutoprestamoDto } from './dto/input/regresar-autoprestamo.dto'; import { ActivosOutputDto } from './dto/output/activos.dto'; import { EntregaOutputDto } from './dto/output/entrega.dto'; import { PrestamoOutputDto } from './dto/output/prestamo.dto'; @@ -125,34 +122,6 @@ export class PrestamoController { return this.prestamoService.findAll(query); } - @Serealize(EntregaOutputDto) - @Put('autoprestamo') - @UseGuards(AuthGuard('jwt')) - @ApiOperation({ - description: 'Endpoint que actualiza el status del equipo a "En uso" por medio del autoprestamo.', - }) - @ApiBearerAuth('jwt') - @ApiBody({ - description: 'Ambas variables son obligatorias.', - examples: { ejemplo: { value: { passcode: 123456789012, id_locker: 30 } } }, - }) - autoprestamo(@Request() req, @Body() body: AutoprestamoDto) { - const operador: Operador = req.user.operador; - - if ( - !operador || - (operador.tipoUsuario.id_tipo_usuario != 3 && - operador.tipoUsuario.id_tipo_usuario != 4) - ) - throw new ForbiddenException( - 'No tienes los permisos necesarios para realizar esta acción.', - ); - const passcode = body.passcode + '' - let id_prestamo = passcode.substring(2,8) - - return this.prestamoService.entregar(parseInt(id_prestamo), body.id_operador); - } - @Put('cancelar-operador') @UseGuards(AuthGuard('jwt')) @ApiOperation({ @@ -506,63 +475,6 @@ export class PrestamoController { ); } - // prestamoPasscode( - // @Request() req, - // @Query() query: PasscodeDto, - // ) { - // const operador: Operador = req.user.operador; - - // if ( - // !operador || - // (operador.tipoUsuario.id_tipo_usuario != 1) - // ) - // throw new ForbiddenException( - // 'No tienes los permisos necesarios para realizar esta acción.', - // ); - // //revisar el tipo de dato del id_locker o id_carrito - // return this.prestamoService.findByNumeroInventario( - // parseInt(query.passcode), - // query.id_locker, - // ); - // } - - @Put('regresar-autoprestamo') - @UseGuards(AuthGuard('jwt')) - @ApiOperation({ description: 'Endpoint que desactiva un autopréstamo.' }) - @ApiBearerAuth('jwt') - @ApiBody({ - description: 'Solo necesitamos el passcode.', - examples: { - ejemplo: { - value: { - passcode: 123456789012, - }, - }, - }, - }) - regresarAutoprestamo( - @Request() req, - @Body() body: RegresarAutoprestamoDto, - ) { - const operador: Operador = req.user.operador; - - if ( - !operador || - (operador.tipoUsuario.id_tipo_usuario != 3 && - operador.tipoUsuario.id_tipo_usuario != 4) - ) - throw new ForbiddenException( - 'No tienes los permisos necesarios para realizar esta acción.', - ); - const passcode = body.passcode + '' - const id_prestamo = passcode.substring(2,8) - - return this.prestamoService.regresarIdPrestamo( - body.id_operador, - parseInt(id_prestamo) - ); - } - @Put('regresar-id-prestamo') @UseGuards(AuthGuard('jwt')) @ApiOperation({ description: 'Endpoint que desactiva un préstamo.' }) diff --git a/src/prestamo/prestamo.module.ts b/src/prestamo/prestamo.module.ts index 1075d4a..ed5a56e 100644 --- a/src/prestamo/prestamo.module.ts +++ b/src/prestamo/prestamo.module.ts @@ -12,11 +12,12 @@ import { InstitucionProgramaModule } from '../institucion-programa/institucion-p import { InstitucionTipoCarritoModule } from '../institucion-tipo-carrito/institucion-tipo-carrito.module'; import { InstitucionTipoEntradaModule } from '../institucion-tipo-entrada/institucion-tipo-entrada.module'; import { OperadorModule } from '../operador/operador.module'; -import { PasscodeModule } from '../passcode/passcode.module'; import { ModuloModule } from '../modulo/modulo.module'; import { MultaModule } from '../multa/multa.module'; +import { PasscodeModule } from '../passcode/passcode.module'; import { TipoUsuarioModule } from '../tipo-usuario/tipo-usuario.module'; import { UsuarioModule } from '../usuario/usuario.module'; + @Module({ imports: [ EquipoModule, @@ -28,7 +29,7 @@ import { UsuarioModule } from '../usuario/usuario.module'; ModuloModule, forwardRef(() => MultaModule), OperadorModule, - PasscodeModule, + forwardRef(() => PasscodeModule), PassportModule.register({ defaultStrategy: 'jwt' }), TypeOrmModule.forFeature([Prestamo]), TipoUsuarioModule, diff --git a/src/prestamo/prestamo.service.ts b/src/prestamo/prestamo.service.ts index c8f0289..5bc426b 100644 --- a/src/prestamo/prestamo.service.ts +++ b/src/prestamo/prestamo.service.ts @@ -47,6 +47,7 @@ export class PrestamoService { @Inject(forwardRef(() => MultaService)) private multaService: MultaService, private operadorService: OperadorService, + @Inject(forwardRef(() => PasscodeService)) private passcodeService : PasscodeService, private tipoUsuarioService: TipoUsuarioService, private usuarioService: UsuarioService, @@ -205,8 +206,9 @@ export class PrestamoService { ), ) .then((prestamo) => { - // if(){ - + // if(id_tipo_carrito === 4 || id_tipo_carrito === 5){ + // const passcode = this.passcodeService.crearPasscode(id_tipo_carrito.toString(), prestamo.id_prestamo.toString()) + // this.appGateway.actualizarOperador(modulo.institucion.id_institucion); // } const passcode = this.passcodeService.crearPasscode(id_tipo_carrito.toString(), prestamo.id_prestamo.toString()) this.appGateway.actualizarOperador(modulo.institucion.id_institucion);