This commit is contained in:
2022-11-03 02:48:47 -06:00
parent f9a13021fc
commit aef87ffe94
14 changed files with 106 additions and 94 deletions
+3 -3
View File
@@ -1,6 +1,6 @@
import { IsInt } from 'class-validator';
import { IsString } from 'class-validator';
export class DevolucionDto {
@IsInt()
passcode: number;
@IsString()
passcode: string;
}
+4 -4
View File
@@ -1,9 +1,9 @@
import { IsInt } from 'class-validator';
import { IsInt, IsString } from 'class-validator';
export class NotificacionDto {
@IsInt()
passcode: number;
@IsInt()
estatus: number;
@IsString()
passcode: string;
}
+2 -2
View File
@@ -2,8 +2,8 @@ import { IsString } from 'class-validator';
export class PrestamoDto {
@IsString()
passcode: string;
id_locker: string;
@IsString()
id_locker: string;
passcode: string;
}
@@ -1,6 +1,6 @@
import { Expose } from 'class-transformer';
export class DevolucionOutputDto {
export class CodeOutputDto {
@Expose()
code;
}
@@ -1,6 +0,0 @@
import { Expose } from 'class-transformer';
export class NotificacionOutputDto {
@Expose()
code;
}
+2 -2
View File
@@ -5,11 +5,11 @@ export class PasscodeOutputDto {
code;
@Expose()
passcode;
door;
@Expose()
id_locker;
@Expose()
door;
passcode;
}
+3 -3
View File
@@ -12,12 +12,12 @@ export class Passcode {
@PrimaryGeneratedColumn()
id_passcode: number;
@Column({ type: String, nullable: false })
passcode: string;
@Column({ type: Boolean, nullable: true, default: false })
abrio: boolean;
@Column({ type: String, nullable: true, default: null })
passcode: string;
@OneToOne(() => Prestamo, (Prestamo) => Prestamo.id_prestamo, {
eager: true,
})
+16 -13
View File
@@ -7,6 +7,7 @@ import {
Request,
UseGuards,
} from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';
import {
ApiBearerAuth,
ApiBody,
@@ -14,16 +15,15 @@ import {
ApiQuery,
ApiTags,
} from '@nestjs/swagger';
import { AuthGuard } from '@nestjs/passport';
import { Serealize } from '../interceptors/serialize.interceptor';
import { ValidarUsuarioService } from '../validar-usuario/validar-usuario.service';
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 { ValidarUsuarioService } from '../validar-usuario/validar-usuario.service';
import { Modulo } from '../modulo/entity/modulo.entity';
import { Operador } from '../operador/entity/operador.entity';
import { DevolucionDto } from './dto/input/devolucion.dto';
import { NotificacionDto } from './dto/input/notificacion.dto';
import { PrestamoDto } from './dto/input/prestamo.dto';
import { CodeOutputDto } from './dto/output/code.dto';
import { PasscodeOutputDto } from './dto/output/passcode.dto';
@Controller('passcode')
@@ -54,7 +54,7 @@ export class PasscodeController {
prestamo(@Request() req, @Query() query: PrestamoDto) {
const operador: Operador = req.user.operador;
this.validarUsuarioService.validarAdminOperador(operador);
this.validarUsuarioService.validarSoloOperador(operador);
return this.passcodeService.findByPasscode(
query.passcode,
false,
@@ -62,7 +62,7 @@ export class PasscodeController {
);
}
@Serealize(NotificacionOutputDto)
@Serealize(CodeOutputDto)
@Put('notificacion')
@UseGuards(AuthGuard('jwt'))
@ApiOperation({
@@ -76,16 +76,18 @@ export class PasscodeController {
})
autoprestamo(@Request() req, @Body() body: NotificacionDto) {
const operador: Operador = req.user.operador;
this.validarUsuarioService.validarAdminOperador(operador);
const modulo: Modulo = req.user.modulo;
this.validarUsuarioService.validarSoloOperador(operador);
return this.passcodeService.entregarEquipo(
operador,
modulo,
body.passcode,
body.estatus,
operador,
);
}
@Serealize(DevolucionOutputDto)
@Serealize(CodeOutputDto)
@Put('devolucion')
@UseGuards(AuthGuard('jwt'))
@ApiOperation({ description: 'Endpoint que da por terminado un préstamo.' })
@@ -102,8 +104,9 @@ export class PasscodeController {
})
devolucion(@Request() req, @Body() body: DevolucionDto) {
const operador: Operador = req.user.operador;
const modulo: Modulo = req.user.modulo;
this.validarUsuarioService.validarAdminOperador(operador);
return this.passcodeService.devolverEquipo(body.passcode, operador);
this.validarUsuarioService.validarSoloOperador(operador);
return this.passcodeService.devolverEquipo(operador, modulo, body.passcode);
}
}
+52 -47
View File
@@ -6,7 +6,10 @@ import {
} from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { Modulo } from '../modulo/entity/modulo.entity';
import { Operador } from '../operador/entity/operador.entity';
import { Passcode } from './entity/passcode.entity';
import { Prestamo } from '../prestamo/entity/prestamo.entity';
import { PrestamoService } from '../prestamo/prestamo.service';
@Injectable()
@@ -17,54 +20,56 @@ export class PasscodeService {
private prestamoService: PrestamoService,
) {}
crearPasscode(id_carrito, id_prestamo) {
const prestamo = {
crearPasscode(prestamo: Prestamo) {
const aux = {
1: '00000',
2: '0000',
3: '000',
4: '00',
5: '0',
6: '',
};
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;
return this.repository
.save(this.repository.create({ prestamo }))
.then((passcodeInterno) => {
passcodeInterno.passcode = `${prestamo.equipo.carrito.id_carrito}${
aux[passcodeInterno.id_passcode.toString().length]
}${passcodeInterno.id_passcode}${Math.floor(
Math.random() * 99999 + 10000,
)}`;
return this.repository.save(passcodeInterno);
});
}
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 };
// });
devolverEquipo(operador: Operador, modulo: Modulo, passcode: string) {
return this.findByPasscode(passcode, true)
.then((passcodeInterno) =>
this.prestamoService.regresar(
operador,
modulo,
passcodeInterno.prestamo,
1,
),
)
.then((_) => ({ code: 1 }));
}
async entregarEquipo(passcode, estatus, operador) {
async entregarEquipo(
operador: Operador,
modulo: Modulo,
passcode: string,
estatus: number,
) {
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 };
// });
// });
if (passcodeInterno.abrio != (estatus === 1))
passcodeInterno.abrio = estatus === 1;
return this.prestamoService
.entregar(operador, modulo, passcodeInterno.prestamo.id_prestamo)
.then((_) => this.repository.save(passcodeInterno))
.then((_) => ({ code: 1 }));
}
}
@@ -96,16 +101,16 @@ export class PasscodeService {
});
}
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;
});
}
// 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;
// });
// }
}