passcode service final
This commit is contained in:
parent
516561afa7
commit
815035750b
@ -44,7 +44,7 @@ export class Carrito {
|
||||
@JoinColumn({ name: 'id_modelo' })
|
||||
modelo: Modelo;
|
||||
|
||||
@ManyToOne(() => Modulo, (modulo) => modulo.carritos)
|
||||
@ManyToOne(() => Modulo, (modulo) => modulo.carritos, { eager: true })
|
||||
@JoinColumn({ name: 'id_modulo' })
|
||||
modulo: Modulo;
|
||||
|
||||
|
@ -18,7 +18,7 @@ export class Passcode {
|
||||
@Column({ type: String, nullable: true, default: null })
|
||||
passcode: string;
|
||||
|
||||
@OneToOne(() => Prestamo, (Prestamo) => Prestamo.id_prestamo, {
|
||||
@OneToOne(() => Prestamo, (prestamo) => prestamo.id_prestamo, {
|
||||
eager: true,
|
||||
})
|
||||
@JoinColumn({ name: 'id_prestamo' })
|
||||
|
@ -58,7 +58,7 @@ export class PasscodeController {
|
||||
@ApiBearerAuth('jwt')
|
||||
@ApiBody({
|
||||
description: 'Variables que necesita el endpoint.',
|
||||
examples: { ejemplo: { value: { passcode: "123456789012", estatus: 1 } } },
|
||||
examples: { ejemplo: { value: { passcode: '123456789012', estatus: 1 } } },
|
||||
})
|
||||
autoprestamo(@Request() req, @Body() body: NotificacionDto) {
|
||||
const operador: Operador = req.user.operador;
|
||||
@ -94,10 +94,9 @@ export class PasscodeController {
|
||||
const operador: Operador = req.user.operador;
|
||||
|
||||
this.validarUsuarioService.validarSoloOperador(operador);
|
||||
return this.passcodeService.findByPasscode(
|
||||
return this.passcodeService.findPasscode(
|
||||
query.passcode,
|
||||
false,
|
||||
query.id_locker,
|
||||
parseInt(query.id_locker),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,6 @@
|
||||
import {
|
||||
BadRequestException,
|
||||
ConflictException,
|
||||
forwardRef,
|
||||
Inject,
|
||||
Injectable,
|
||||
@ -20,38 +21,44 @@ export class PasscodeService {
|
||||
private prestamoService: PrestamoService,
|
||||
) {}
|
||||
|
||||
create(prestamo: Prestamo) {
|
||||
create(prestamo: Prestamo): Promise<Passcode> {
|
||||
const aux = ['00000', '0000', '000', '00', '0', ''];
|
||||
const carrito = `${prestamo.equipo.carrito.id_carrito < 10 ? '0' : ''}${
|
||||
prestamo.equipo.carrito.id_carrito
|
||||
}`;
|
||||
const random = Math.floor(Math.random() * 99999); // Generamos un número random entre 0-99999
|
||||
const randomStr = `${aux[random.toString().length]}${random}`; // Llenamos con los ceros faltantes
|
||||
|
||||
// Creamos y guardamos un registro
|
||||
return this.repository
|
||||
.save(this.repository.create({ prestamo }))
|
||||
.then((passcodeInterno) => {
|
||||
const aux = {
|
||||
1: '00000',
|
||||
2: '0000',
|
||||
3: '000',
|
||||
4: '00',
|
||||
5: '0',
|
||||
6: '',
|
||||
};
|
||||
.then((passcode) => {
|
||||
const id = `${aux[passcode.id_passcode.toString().length - 1]}${
|
||||
passcode.id_passcode
|
||||
}`; // LLenamos con los ceros faltantes
|
||||
|
||||
passcodeInterno.passcode = `0${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);
|
||||
// Asignamos valor a passcode
|
||||
passcode.passcode = `${carrito}${id}${randomStr}`;
|
||||
// Guardamos cambios
|
||||
return this.repository.save(passcode);
|
||||
});
|
||||
}
|
||||
|
||||
devolverEquipo(operador: Operador, modulo: Modulo, passcode: string) {
|
||||
devolverEquipo(
|
||||
operador: Operador,
|
||||
modulo: Modulo,
|
||||
passcode: string,
|
||||
): Promise<{ code: number }> {
|
||||
return this.findByPasscode(passcode, true)
|
||||
.then((passcodeInterno) =>
|
||||
this.prestamoService.regresar(
|
||||
.then((passcode) => {
|
||||
this.validacionBasica(passcode.prestamo, operador, modulo);
|
||||
return this.prestamoService.regresar(
|
||||
operador,
|
||||
modulo,
|
||||
passcodeInterno.prestamo,
|
||||
passcode.prestamo,
|
||||
1,
|
||||
),
|
||||
)
|
||||
);
|
||||
})
|
||||
.then((_) => ({ code: 1 }));
|
||||
}
|
||||
|
||||
@ -60,48 +67,90 @@ export class PasscodeService {
|
||||
modulo: Modulo,
|
||||
passcode: string,
|
||||
estatus: number,
|
||||
) {
|
||||
const passcodeInterno = await this.findByPasscode(passcode, false);
|
||||
|
||||
if (estatus) {
|
||||
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 }));
|
||||
}
|
||||
): Promise<{ code: number }> {
|
||||
return this.findByPasscode(passcode, false).then((passcode) => {
|
||||
this.validacionBasica(passcode.prestamo, operador, modulo);
|
||||
if (estatus) {
|
||||
if (passcode.abrio != (estatus === 1)) passcode.abrio = estatus === 1;
|
||||
return this.prestamoService
|
||||
.entregar(operador, modulo, passcode.prestamo)
|
||||
.then((_) => this.repository.save(passcode))
|
||||
.then((_) => ({ code: 1 }));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
findByPasscode(passcode: string, abrio?: boolean, id_locker?: string) {
|
||||
let door = '';
|
||||
|
||||
findByPasscode(passcode: string, abrio: boolean): Promise<Passcode> {
|
||||
return this.repository
|
||||
.findOne({ where: { passcode, abrio } })
|
||||
.then(async (passcode) => {
|
||||
.then((passcode) => {
|
||||
if (!passcode)
|
||||
throw new BadRequestException({
|
||||
message: `Este passcode no existe`,
|
||||
code: 6,
|
||||
code: 8,
|
||||
message: `No existe este passcode.`,
|
||||
});
|
||||
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;
|
||||
});
|
||||
}
|
||||
|
||||
findByPrestamo(prestamo: Prestamo) {
|
||||
return this.repository.findOne({ where: { prestamo } });
|
||||
findByPrestamo(prestamo: Prestamo): Promise<Passcode> {
|
||||
return this.repository.findOne({
|
||||
select: ['id_passcode', 'passcode'],
|
||||
where: { prestamo },
|
||||
});
|
||||
}
|
||||
|
||||
findPasscode(passcode: string, id_locker: number) {
|
||||
return this.findByPasscode(passcode, false).then((passcode) => {
|
||||
if (passcode.prestamo.equipo.carrito.id_carrito != id_locker)
|
||||
throw new BadRequestException({
|
||||
code: 2,
|
||||
message: `El id_locker no corresponde`,
|
||||
});
|
||||
return {
|
||||
code: 1,
|
||||
door: parseInt(passcode.prestamo.equipo.equipo.substring(1, 3)),
|
||||
id_locker,
|
||||
passcode: passcode.passcode,
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
private validacionBasica(
|
||||
prestamo: Prestamo,
|
||||
operador: Operador,
|
||||
modulo?: Modulo,
|
||||
): void {
|
||||
// Validamos si el préstamo ya fue cancelado
|
||||
if (
|
||||
prestamo.cancelado_operador ||
|
||||
prestamo.cancelado_usuario ||
|
||||
prestamo.cancelado_repeticion
|
||||
)
|
||||
throw new ConflictException({
|
||||
code: 4,
|
||||
message: 'Este préstamo fue cancelado.',
|
||||
});
|
||||
// Validamos si el préstamo esta inactivo
|
||||
if (!prestamo.activo)
|
||||
throw new ConflictException({
|
||||
code: 7,
|
||||
message: 'Este préstamo ya no se encuentra activo.',
|
||||
});
|
||||
// Validamos que el prestamo pertenezca a la institución del operador
|
||||
if (
|
||||
operador.institucion.id_institucion !=
|
||||
prestamo.equipo.carrito.modulo.institucion.id_institucion
|
||||
)
|
||||
throw new ConflictException({
|
||||
code: 2,
|
||||
message: 'Este préstamo no pertenece a esta institución.',
|
||||
});
|
||||
// Validamos que el prestamo pertenezca al módulo del operador
|
||||
if (modulo && prestamo.equipo.carrito.modulo.id_modulo != modulo.id_modulo)
|
||||
throw new ConflictException({
|
||||
code: 2,
|
||||
message: 'Este préstamo no pertenece a tu módulo.',
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@ -265,11 +265,14 @@ export class PrestamoService {
|
||||
async entregar(
|
||||
operadorEntrega: Operador,
|
||||
modulo: Modulo,
|
||||
id_prestamo: number,
|
||||
id_prestamo: number | Prestamo,
|
||||
): Promise<Equipo> {
|
||||
const ahora = moment();
|
||||
const ahoraStr = ahora.format('YYYY-MM-DD');
|
||||
const prestamo = await this.findById(id_prestamo);
|
||||
const prestamo =
|
||||
typeof id_prestamo === 'number'
|
||||
? await this.findById(id_prestamo)
|
||||
: id_prestamo;
|
||||
const institucionDia = await this.institucionDiaService.hoy(
|
||||
operadorEntrega.institucion,
|
||||
);
|
||||
@ -280,8 +283,13 @@ export class PrestamoService {
|
||||
? moment(`${ahoraStr} ${institucionDia.hora_fin}`)
|
||||
: null;
|
||||
|
||||
this.validacionBasicaPrestamo(prestamo);
|
||||
this.validacionOperadorPrestamo(prestamo, operadorEntrega, modulo);
|
||||
if (
|
||||
prestamo.equipo.carrito.tipoCarrito.id_tipo_carrito != 4 &&
|
||||
prestamo.equipo.carrito.tipoCarrito.id_tipo_carrito != 5
|
||||
) {
|
||||
this.validacionBasicaPrestamo(prestamo);
|
||||
this.validacionOperadorPrestamo(prestamo, operadorEntrega, modulo);
|
||||
}
|
||||
// Si el equipo esta en status 3 ya no se puede ejecutar este endpoint de nuevo
|
||||
// con este préstamo
|
||||
if (prestamo.equipo.status.id_status === 3)
|
||||
@ -724,8 +732,13 @@ export class PrestamoService {
|
||||
tardanza / modulo.institucion.tiempo_entrega,
|
||||
); // Semanas de castigo correspondiente al tiempo de entrega de cada institución
|
||||
|
||||
this.validacionBasicaPrestamo(prestamo);
|
||||
this.validacionOperadorPrestamo(prestamo, operadorRegreso, modulo);
|
||||
if (
|
||||
prestamo.equipo.carrito.tipoCarrito.id_tipo_carrito != 4 &&
|
||||
prestamo.equipo.carrito.tipoCarrito.id_tipo_carrito != 5
|
||||
) {
|
||||
this.validacionBasicaPrestamo(prestamo);
|
||||
this.validacionOperadorPrestamo(prestamo, operadorRegreso, modulo);
|
||||
}
|
||||
// No se puede regresar el equipo si aun no se entrega
|
||||
if (prestamo.equipo.status.id_status === 2)
|
||||
throw new ConflictException(
|
||||
|
Loading…
Reference in New Issue
Block a user