Files
TREK/server/src/nest/common/idempotency-cleanup.job.ts
T
Maurice 724c88c201 style: format server and shared with prettier
A mechanical prettier --write over server/src, server/tests, server/scripts
and shared/src with the workspaces' .prettierrc; nothing but layout and
import order changes, and every file keeps the same set of imports. shared
is formatted throughout and its format baseline is empty. The server
baseline keeps the 35 files that Prettier would grow past their lint:size
limit (they need a split first, which is not a formatting change), and the
generated entity files stay as their generator writes them.
2026-10-10 21:23:37 +02:00

42 lines
1.9 KiB
TypeScript

import { IdempotencyKeys } from '../../db/entities/IdempotencyKeys.entity';
import type { IdempotencyKeysRepository } from '../../db/repositories/IdempotencyKeys.repository';
import { logInfo, logError } from '../audit/audit-log.logger';
import { CronRegistrarService } from '../scheduling/cron-registrar.service';
import { purgeExpiredIdempotencyKeys } from './idempotency-cleanup';
import { InjectRepository } from '@mikro-orm/nestjs';
import { Injectable, type OnApplicationBootstrap } from '@nestjs/common';
/**
* Nightly 3 AM purge of expired idempotency keys (moved from src/scheduler.ts).
* Registered as an AppModule provider beside the global IdempotencyInterceptor
* that writes the keys — common/ has no module of its own.
*/
@Injectable()
export class IdempotencyCleanupJob implements OnApplicationBootstrap {
constructor(
@InjectRepository(IdempotencyKeys) private readonly idempotencyKeys: IdempotencyKeysRepository,
private readonly registrar: CronRegistrarService,
) {}
onApplicationBootstrap(): void {
if (!this.registrar.isEnabled()) return;
// Plan 4 Task 1: `tick()` now reads/writes through `IdempotencyKeysRepository`
// (see `purgeExpiredIdempotencyKeys`) rather than raw `DatabaseService.prepare`
// — the tick itself already runs inside a request context —
// CronRegistrarService.register wraps every job's onTick in one place, so
// this needs no wrapper of its own even with a repository read/write.
this.registrar.register('idempotency-cleanup', '0 3 * * *', () => this.tick());
}
async tick(): Promise<void> {
try {
const removed = await purgeExpiredIdempotencyKeys(undefined, undefined, this.idempotencyKeys);
if (removed > 0) {
logInfo(`Idempotency cleanup: removed ${removed} expired key(s)`);
}
} catch (err: unknown) {
logError(`Idempotency cleanup: ${err instanceof Error ? err.message : err}`);
}
}
}