import { BadRequestException, forwardRef, Inject, Injectable, } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; import { Repository } from 'typeorm'; import { Passcode } from './entity/passcode.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_carrito, id_prestamo) { const prestamo = { 1: '00000', 2: '0000', 3: '000', 4: '00', 5: '0', }; const passcode = id_carrito + prestamo[id_prestamo.length] + id_prestamo + Math.floor(Math.random() * 99999 + 10000).toString(); this.repository.save( this.repository.create({ passcode, prestamo: { id_prestamo }, }), ); return passcode; } async devolverEquipo(passcode, operador) { const passcodeInterno = await this.findByPasscode(passcode, true); // return this.prestamoService // .regresarIdPrestamo( // operador.id_operador, // passcodeInterno.prestamo.id_prestamo, // ) // .then((_) => { // return { code: 1 }; // }); } async entregarEquipo(passcode, estatus, operador) { const passcodeInterno = await this.findByPasscode(passcode, false); if (estatus) { if (passcodeInterno.abrio != estatus) passcodeInterno.abrio = estatus === 1 ? true : false; // return this.prestamoService // .entregar(passcodeInterno.prestamo.id_prestamo, operador.id_operador) // .then((_) => { // return this.repository.save(passcodeInterno).then((_) => { // return { code: 1 }; // }); // }); } } findByPasscode(passcode: string, abrio?: boolean, id_locker?: string) { let door = ''; return this.repository .findOne({ where: { passcode, abrio } }) .then(async (passcode) => { if (!passcode) throw new BadRequestException({ message: `Este passcode no existe`, code: 6, }); if (id_locker) { await this.prestamoService .findById(passcode.prestamo.id_prestamo) .then((prestamo) => { if (prestamo.equipo.carrito.id_carrito != parseInt(id_locker)) throw new BadRequestException({ message: `El id_locker no corresponde`, code: 4, }); door = prestamo.equipo.equipo.substring(1, 3); }); return { ...passcode, code: 1, id_locker, door: parseInt(door) }; } return passcode; }); } findPasscode(id_prestamo: number) { return this.repository .findOne({ where: { prestamo: { id_prestamo } } }) .then(async (passcode) => { if (!passcode) throw new BadRequestException({ message: `Este passcode no existe`, code: 6, }); return passcode.passcode; }); } }