diff --git a/src/app.module.ts b/src/app.module.ts index e15c4a7..1d603e3 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -31,6 +31,7 @@ import { ModuloModule } from './modulo/modulo.module'; import { MultaModule } from './multa/multa.module'; import { NodemailerModule } from './nodemailer/nodemailer.module'; import { OperadorModule } from './operador/operador.module'; +import { PasscodeModule } from './passcode/passcode.module'; import { PrestamoModule } from './prestamo/prestamo.module'; import { StatusModule } from './status/status.module'; import { TipoUsuarioModule } from './tipo-usuario/tipo-usuario.module'; @@ -62,6 +63,7 @@ import { Modulo } from './modulo/entity/modulo.entity'; import { Multa } from './multa/entity/multa.entity'; import { Nivel } from './institucion-carrera/entity/nivel.entity'; import { Operador } from './operador/entity/operador.entity'; +import { Passcode } from './passcode/entity/passcode.entity'; import { Prestamo } from './prestamo/entity/prestamo.entity'; import { Programa } from './institucion-programa/entity/programa.entity'; import { Status } from './status/entity/status.entity'; @@ -121,6 +123,7 @@ import { Usuario } from './usuario/entity/usuario.entity'; Multa, Nivel, Operador, + Passcode, Prestamo, Programa, Status, @@ -157,11 +160,13 @@ import { Usuario } from './usuario/entity/usuario.entity'; MultaModule, NodemailerModule, OperadorModule, + PasscodeModule, PrestamoModule, StatusModule, TipoUsuarioModule, UploadFileModule, UsuarioModule, + PasscodeModule, ], }) export class AppModule {} diff --git a/src/passcode/entity/passcode.entity.ts b/src/passcode/entity/passcode.entity.ts new file mode 100644 index 0000000..2661286 --- /dev/null +++ b/src/passcode/entity/passcode.entity.ts @@ -0,0 +1,25 @@ +import { + Column, + Entity, + JoinColumn, + OneToOne, + PrimaryGeneratedColumn, + } from 'typeorm'; + import { Prestamo } from '../../prestamo/entity/prestamo.entity'; + + @Entity() + export class Passcode { + @PrimaryGeneratedColumn() + id_passcode: number; + + @Column({ type: String, nullable: false }) + passcode: string; + + + @OneToOne(() => Prestamo, (Prestamo) => Prestamo.id_prestamo, { + eager: true, + }) + @JoinColumn({ name: 'id_prestamo' }) + prestamo: Prestamo; + } + \ No newline at end of file diff --git a/src/passcode/passcode.controller.spec.ts b/src/passcode/passcode.controller.spec.ts new file mode 100644 index 0000000..9a58392 --- /dev/null +++ b/src/passcode/passcode.controller.spec.ts @@ -0,0 +1,18 @@ +import { Test, TestingModule } from '@nestjs/testing'; +import { PasscodeController } from './passcode.controller'; + +describe('PasscodeController', () => { + let controller: PasscodeController; + + beforeEach(async () => { + const module: TestingModule = await Test.createTestingModule({ + controllers: [PasscodeController], + }).compile(); + + controller = module.get(PasscodeController); + }); + + it('should be defined', () => { + expect(controller).toBeDefined(); + }); +}); diff --git a/src/passcode/passcode.controller.ts b/src/passcode/passcode.controller.ts new file mode 100644 index 0000000..7e6405d --- /dev/null +++ b/src/passcode/passcode.controller.ts @@ -0,0 +1,4 @@ +import { Controller } from '@nestjs/common'; + +@Controller('passcode') +export class PasscodeController {} diff --git a/src/passcode/passcode.module.ts b/src/passcode/passcode.module.ts new file mode 100644 index 0000000..44c6792 --- /dev/null +++ b/src/passcode/passcode.module.ts @@ -0,0 +1,14 @@ +import { 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' +@Module({ + imports: [ + TypeOrmModule.forFeature([Passcode]), + ], + controllers: [PasscodeController], + providers: [PasscodeService], + exports: [PasscodeService] +}) +export class PasscodeModule {} diff --git a/src/passcode/passcode.service.spec.ts b/src/passcode/passcode.service.spec.ts new file mode 100644 index 0000000..89747df --- /dev/null +++ b/src/passcode/passcode.service.spec.ts @@ -0,0 +1,18 @@ +import { Test, TestingModule } from '@nestjs/testing'; +import { PasscodeService } from './passcode.service'; + +describe('PasscodeService', () => { + let service: PasscodeService; + + beforeEach(async () => { + const module: TestingModule = await Test.createTestingModule({ + providers: [PasscodeService], + }).compile(); + + service = module.get(PasscodeService); + }); + + it('should be defined', () => { + expect(service).toBeDefined(); + }); +}); diff --git a/src/passcode/passcode.service.ts b/src/passcode/passcode.service.ts new file mode 100644 index 0000000..06768fc --- /dev/null +++ b/src/passcode/passcode.service.ts @@ -0,0 +1,33 @@ +import { Injectable } from '@nestjs/common'; +import { InjectRepository } from '@nestjs/typeorm'; +import { createQueryBuilder, Repository } from 'typeorm'; +import { Passcode} from './entity/passcode.entity' +import { Prestamo } from '../prestamo/entity/prestamo.entity'; + +@Injectable() +export class PasscodeService { + constructor( + @InjectRepository(Passcode) private repository: Repository, + ){} + + crearPasscode(id_tipo_carrito, id_prestamo){ + const prestamo = { + 1: "00000", + 2: "0000", + 3: "000", + 4: "00", + 5: "0", + } + + const passcode = "30" + prestamo[id_prestamo.length] + id_prestamo + Math.floor(Math.random() * 99999).toString() + // const passcode = id_tipo_carrito + prestamo[id_prestamo.length] + id_prestamo + Math.floor(Math.random() * 99999).toString() + // console.log(passcode) + this.repository.save( + this.repository.create({ + passcode, + prestamo: {id_prestamo}, + }), + ) + return passcode + } +} diff --git a/src/prestamo/dto/input/autoprestamo.dto.ts b/src/prestamo/dto/input/autoprestamo.dto.ts index 8f19c0b..20563e2 100644 --- a/src/prestamo/dto/input/autoprestamo.dto.ts +++ b/src/prestamo/dto/input/autoprestamo.dto.ts @@ -6,4 +6,7 @@ export class AutoprestamoDto { @IsInt() id_locker: number; + + @IsInt() + id_operador: number; } diff --git a/src/prestamo/dto/input/regresar-autoprestamo.dto.ts b/src/prestamo/dto/input/regresar-autoprestamo.dto.ts index a7cc3aa..80c688f 100644 --- a/src/prestamo/dto/input/regresar-autoprestamo.dto.ts +++ b/src/prestamo/dto/input/regresar-autoprestamo.dto.ts @@ -3,6 +3,9 @@ import { } from 'class-validator'; export class RegresarAutoprestamoDto { + @IsInt() + id_operador: number; + @IsInt() passcode: number; } diff --git a/src/prestamo/dto/output/prestamo.dto.ts b/src/prestamo/dto/output/prestamo.dto.ts index c83aac2..86f845e 100644 --- a/src/prestamo/dto/output/prestamo.dto.ts +++ b/src/prestamo/dto/output/prestamo.dto.ts @@ -20,6 +20,9 @@ export class PrestamoOutputDto { @Expose() hora_max_recoger; + @Expose() + passcode; + @Expose() @Type(() => EquipoMinOutputDto) equipo; diff --git a/src/prestamo/prestamo.controller.ts b/src/prestamo/prestamo.controller.ts index 8ecee2a..94ee890 100644 --- a/src/prestamo/prestamo.controller.ts +++ b/src/prestamo/prestamo.controller.ts @@ -24,7 +24,7 @@ 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 { 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'; @@ -138,19 +138,19 @@ export class PrestamoController { }) autoprestamo(@Request() req, @Body() body: AutoprestamoDto) { const operador: Operador = req.user.operador; - + if ( !operador || - (operador.tipoUsuario.id_tipo_usuario != 1) + (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,7) + let id_prestamo = passcode.substring(2,8) - return this.prestamoService.entregar(parseInt(id_prestamo), 1); + return this.prestamoService.entregar(parseInt(id_prestamo), body.id_operador); } @Put('cancelar-operador') @@ -548,15 +548,19 @@ export class PrestamoController { if ( !operador || - (operador.tipoUsuario.id_tipo_usuario != 1) + (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,7) + const id_prestamo = passcode.substring(2,8) - return this.prestamoService.findById(parseInt(id_prestamo)); + return this.prestamoService.regresarIdPrestamo( + body.id_operador, + parseInt(id_prestamo) + ); } @Put('regresar-id-prestamo') diff --git a/src/prestamo/prestamo.module.ts b/src/prestamo/prestamo.module.ts index aa849d5..1075d4a 100644 --- a/src/prestamo/prestamo.module.ts +++ b/src/prestamo/prestamo.module.ts @@ -12,6 +12,7 @@ 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 { TipoUsuarioModule } from '../tipo-usuario/tipo-usuario.module'; @@ -27,6 +28,7 @@ import { UsuarioModule } from '../usuario/usuario.module'; ModuloModule, forwardRef(() => MultaModule), OperadorModule, + PasscodeModule, PassportModule.register({ defaultStrategy: 'jwt' }), TypeOrmModule.forFeature([Prestamo]), TipoUsuarioModule, diff --git a/src/prestamo/prestamo.service.ts b/src/prestamo/prestamo.service.ts index b8b6616..1f200de 100644 --- a/src/prestamo/prestamo.service.ts +++ b/src/prestamo/prestamo.service.ts @@ -2,6 +2,7 @@ import * as moment from 'moment'; import { BadRequestException, ConflictException, + ConsoleLogger, ForbiddenException, forwardRef, Inject, @@ -15,6 +16,7 @@ import { Institucion } from '../institucion/entity/institucion.entity'; import { InstitucionUsuario } from '../institucion-usuario/entity/institucion-usuario.entity'; import { Modulo } from '../modulo/entity/modulo.entity'; import { Operador } from '../operador/entity/operador.entity'; +// import { Passcode } from '../passcode/entity/passcode.entity'; import { Prestamo } from './entity/prestamo.entity'; import { Usuario } from '../usuario/entity/usuario.entity'; import { EquipoService } from '../equipo/equipo.service'; @@ -28,6 +30,7 @@ import { MultaService } from '../multa/multa.service'; import { OperadorService } from '../operador/operador.service'; import { TipoUsuarioService } from '../tipo-usuario/tipo-usuario.service'; import { UsuarioService } from '../usuario/usuario.service'; +import { PasscodeService } from '../passcode/passcode.service'; @Injectable() export class PrestamoService { @@ -44,6 +47,7 @@ export class PrestamoService { @Inject(forwardRef(() => MultaService)) private multaService: MultaService, private operadorService: OperadorService, + private passcodeService : PasscodeService, private tipoUsuarioService: TipoUsuarioService, private usuarioService: UsuarioService, ) {} @@ -101,9 +105,6 @@ export class PrestamoService { }; }); } - private crearPasscode(){ - return 123 - } async create( usuario: Usuario, @@ -147,14 +148,14 @@ export class PrestamoService { throw new ConflictException( 'El día de hoy no hay servicio de préstamo de equipos.', ); - if (ahora < horaMin) - throw new ConflictException( - `El servicio inicia a las ${institucionDia.hora_inicio} el día de hoy.`, - ); - if (ahora >= horaExtra) - throw new ConflictException( - `El servicio terminó a las ${institucionDia.hora_extra} el día de hoy.`, - ); + // if (ahora < horaMin) + // throw new ConflictException( + // `El servicio inicia a las ${institucionDia.hora_inicio} el día de hoy.`, + // ); + // if (ahora >= horaExtra) + // throw new ConflictException( + // `El servicio terminó a las ${institucionDia.hora_extra} el día de hoy.`, + // ); for (let i = 0; i < institucionDia.horasExcepcion.length; i++) { const horaFin = moment( `${ahoraStr} ${institucionDia.horasExcepcion[i].hora_fin}`, @@ -200,13 +201,16 @@ export class PrestamoService { operadorEntrega: sistema, operadorRegreso: sistema, usuario, - // passcode: id_modulo === 30 ? this.crearPasscode() : null, }), ), ) .then((prestamo) => { + // if(){ + + // } + const passcode = this.passcodeService.crearPasscode(id_tipo_carrito.toString(), prestamo.id_prestamo.toString()) this.appGateway.actualizarOperador(modulo.institucion.id_institucion); - return prestamo; + return {...prestamo, passcode}; }); }