cron listo

This commit is contained in:
xXpuma99Xx 2022-06-16 08:02:37 -05:00
parent 2888afedd9
commit fd0f87476a
5 changed files with 74 additions and 32 deletions

View File

@ -1,11 +1,17 @@
import { Module } from '@nestjs/common'; import { Module } from '@nestjs/common';
import { ScheduleModule } from '@nestjs/schedule'; import { ScheduleModule } from '@nestjs/schedule';
import { CronService } from './cron.service'; import { CronService } from './cron.service';
import { EquipoModule } from '../equipo/equipo.module';
import { MultaModule } from '../multa/multa.module'; import { MultaModule } from '../multa/multa.module';
import { UsuarioModule } from '../usuario/usuario.module'; import { PrestamoModule } from '../prestamo/prestamo.module';
@Module({ @Module({
imports: [MultaModule, ScheduleModule.forRoot(), UsuarioModule], imports: [
EquipoModule,
MultaModule,
PrestamoModule,
ScheduleModule.forRoot(),
],
providers: [CronService], providers: [CronService],
}) })
export class CronModule {} export class CronModule {}

View File

@ -1,22 +1,30 @@
import { Injectable } from '@nestjs/common'; import { Injectable } from '@nestjs/common';
import { Cron, SchedulerRegistry } from '@nestjs/schedule'; import { Cron, SchedulerRegistry } from '@nestjs/schedule';
import { EquipoService } from '../equipo/equipo.service';
import { MultaService } from '../multa/multa.service'; import { MultaService } from '../multa/multa.service';
import { UsuarioService } from '../usuario/usuario.service'; import { PrestamoService } from '../prestamo/prestamo.service';
@Injectable() @Injectable()
export class CronService { export class CronService {
constructor( constructor(
private schedulerRegistry: SchedulerRegistry, private schedulerRegistry: SchedulerRegistry,
private equipoService: EquipoService,
private multaService: MultaService, private multaService: MultaService,
private usuarioService: UsuarioService, private prestamoService: PrestamoService,
) {} ) {}
// @Cron('0 6 * * * *', { name: 'multas' }) @Cron('0 */5 8-20 * * 1-5', { name: 'prestamos_desactivacion' })
// revisarMultas() {} desactivarPrestamos() {
return this.prestamoService.desactivarPrestamos();
}
// @Cron('0 6 * * * *', { name: '' }) @Cron('0 0 0 * * *', { name: 'equipos_reseteo' })
// desactivarPrestamos() {} resetearEquipos() {
return this.equipoService.reseteoTotal();
}
// @Cron('0 6 * * * *', { name: '' }) @Cron('0 0 0 * * *', { name: 'multas_reseteo' })
// desactivarActualizarEquipos() {} revisarMultas() {
return this.multaService.desactivarMultas();
}
} }

View File

@ -267,6 +267,14 @@ export class EquipoService {
}); });
} }
reseteoTotal() {
return this.repository
.createQueryBuilder()
.update()
.set({ prestado: false })
.execute();
}
async findEquipoByEquipo( async findEquipoByEquipo(
id_carrito: number | Carrito, id_carrito: number | Carrito,
equipo: string, equipo: string,

View File

@ -172,4 +172,19 @@ export class MultaService {
}), }),
); );
} }
desactivarMultas() {
const ahora = moment();
return this.repository.find({ activo: true }).then(async (multas) => {
for (let i = 0; i < multas.length; i++)
if (ahora.diff(moment(multas[i].fecha_fin)) > 0) {
multas[i].activo = false;
multas[i].prestamo.usuario.multa = false;
await this.usuarioService
.update(multas[i].prestamo.usuario)
.then((_) => this.repository.save(multas[i]));
}
});
}
} }

View File

@ -58,16 +58,7 @@ export class PrestamoService {
const operadorRegreso = await this.operadorService.findById(id_operador); const operadorRegreso = await this.operadorService.findById(id_operador);
const prestamo = await this.findById(id_prestamo); const prestamo = await this.findById(id_prestamo);
if (prestamo.cancelado_usuario) this.validacionBasicaPrestamo(prestamo);
throw new ConflictException(
'Este préstamo ya fue cancelado por el usuario.',
);
if (prestamo.cancelado_operador)
throw new ConflictException(
'Este préstamo ya fue cancelado por un operador.',
);
if (!prestamo.activo)
throw new ConflictException('Este préstamo ya no se encuentra activo.');
prestamo.activo = false; prestamo.activo = false;
prestamo.fecha_entrega = ahora.toDate(); prestamo.fecha_entrega = ahora.toDate();
prestamo.cancelado_operador = true; prestamo.cancelado_operador = true;
@ -91,16 +82,7 @@ export class PrestamoService {
const ahora = moment(); const ahora = moment();
const prestamo = await this.findById(id_prestamo); const prestamo = await this.findById(id_prestamo);
if (prestamo.cancelado_usuario) this.validacionBasicaPrestamo(prestamo);
throw new ConflictException(
'Este préstamo ya fue cancelado por el usuario.',
);
if (prestamo.cancelado_operador)
throw new ConflictException(
'Este préstamo ya fue cancelado por un operador.',
);
if (!prestamo.activo)
throw new ConflictException('Este préstamo ya no se encuentra activo.');
if (prestamo.equipo.status.id_status === 3) if (prestamo.equipo.status.id_status === 3)
throw new ConflictException( throw new ConflictException(
'No se puede cancelar el préstamo una vez se te entregó el equipo.', 'No se puede cancelar el préstamo una vez se te entregó el equipo.',
@ -140,7 +122,7 @@ export class PrestamoService {
// if (ahora.weekday() === 0 || ahora.weekday() === 6) // if (ahora.weekday() === 0 || ahora.weekday() === 6)
// throw new ConflictException( // throw new ConflictException(
// 'No se puede pedir equipos de cómputo los días sabados ni domingos.', // 'No se puede pedir equipo de cómputo los sabados y domingos.',
// ); // );
// else { // else {
// const institucionDia = await this.institucionDiaService.findDia( // const institucionDia = await this.institucionDiaService.findDia(
@ -156,7 +138,7 @@ export class PrestamoService {
// if (!institucionDia.activo) // if (!institucionDia.activo)
// throw new ConflictException( // throw new ConflictException(
// 'El día de hoy no se esta realizando préstamo de equipos.', // 'El día de hoy no se esta realizando préstamos de equipos.',
// ); // );
// if (ahora > horaMax) // if (ahora > horaMax)
// throw new Error('Ya no se puede realizar un préstamo el día de hoy.'); // throw new Error('Ya no se puede realizar un préstamo el día de hoy.');
@ -208,6 +190,29 @@ export class PrestamoService {
); );
} }
async desactivarPrestamos() {
const ahora = moment();
const operadorRegreso = await this.operadorService.findById(1);
return this.repository
.find({
join: { alias: 'p', innerJoin: { e: 'p.equipo' } },
where: { activo: true, equipo: { status: { id_status: 2 } } },
})
.then(async (prestamos) => {
for (let i = 0; i < prestamos.length; i++)
if (ahora.diff(moment(prestamos[i].hora_max_recoger)) > 0) {
prestamos[i].activo = false;
prestamos[i].cancelado_operador = true;
prestamos[i].operadorRegreso = operadorRegreso;
prestamos[i].equipo.status = await this.statusService.findById(1);
await this.equipoService
.update(prestamos[i].equipo)
.then((_) => this.repository.save(prestamos[i]));
}
});
}
async entregar(id_prestamo: number, id_operador: number) { async entregar(id_prestamo: number, id_operador: number) {
const ahora = moment(); const ahora = moment();
const operadorEntrega = await this.operadorService.findById(id_operador); const operadorEntrega = await this.operadorService.findById(id_operador);