feat(#216): Agenda B2 — soft-cancel + matrice + cron
Closes #216 Co-authored-by: Julien Martin <julien.martin@ptits-pas.fr>
This commit was merged in pull request #234.
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
import { Injectable, Logger } from '@nestjs/common';
|
||||
import { Cron, CronExpression } from '@nestjs/schedule';
|
||||
import { EvenementsAgendaService } from './evenements-agenda.service';
|
||||
|
||||
/**
|
||||
* Échéances Agenda (#216) : auto-valid congés J+7, soft-cancel refusés J+7.
|
||||
* Idempotent ; rattrapage au redémarrage via le prochain tick.
|
||||
*/
|
||||
@Injectable()
|
||||
export class AgendaExpirationCron {
|
||||
private readonly logger = new Logger(AgendaExpirationCron.name);
|
||||
private running = false;
|
||||
|
||||
constructor(private readonly evenements: EvenementsAgendaService) {}
|
||||
|
||||
@Cron(CronExpression.EVERY_HOUR)
|
||||
async tick(): Promise<void> {
|
||||
await this.runOnce();
|
||||
}
|
||||
|
||||
/** Exposé pour tests / appel manuel (double-appel = no-op côté rows déjà traités). */
|
||||
async runOnce(
|
||||
now = new Date(),
|
||||
): Promise<{ autoValid: number; softCancel: number }> {
|
||||
if (this.running) {
|
||||
this.logger.debug('Cron agenda déjà en cours — skip');
|
||||
return { autoValid: 0, softCancel: 0 };
|
||||
}
|
||||
this.running = true;
|
||||
try {
|
||||
const autoValid = await this.evenements.autoValiderCongesExpires(now);
|
||||
const softCancel = await this.evenements.softCancelRefusesExpires(now);
|
||||
return { autoValid, softCancel };
|
||||
} finally {
|
||||
this.running = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user