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 { 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; } } }