From a7b087050c5ee543768f0e8b4fdac13f4c37e939 Mon Sep 17 00:00:00 2001 From: Bastian Wagner Date: Tue, 4 Aug 2026 19:41:44 +0200 Subject: [PATCH] feat: add notification retention scheduler --- .../logging/model/logging-event.type.ts | 4 ++ .../notification-retention.scheduler.spec.ts | 55 +++++++++++++++++++ .../notification-retention.scheduler.ts | 41 ++++++++++++++ .../src/notifications/notifications.module.ts | 3 +- 4 files changed, 102 insertions(+), 1 deletion(-) create mode 100644 myteamwallet_backend/src/notifications/notification-retention.scheduler.spec.ts create mode 100644 myteamwallet_backend/src/notifications/notification-retention.scheduler.ts diff --git a/myteamwallet_backend/src/database/logging/model/logging-event.type.ts b/myteamwallet_backend/src/database/logging/model/logging-event.type.ts index f2cd9a0..5603f75 100644 --- a/myteamwallet_backend/src/database/logging/model/logging-event.type.ts +++ b/myteamwallet_backend/src/database/logging/model/logging-event.type.ts @@ -40,6 +40,8 @@ export type LOGEVENT = | 'log_retention_cleanup_run' | 'log_retention_cleanup_run_fail' | 'notification_create_fail' + | 'notification_retention_cleanup_run' + | 'notification_retention_cleanup_run_fail' | 'public_access_enabled' | 'public_access_rotated'; @@ -84,6 +86,8 @@ export const LOGEVENT_VALUES: LOGEVENT[] = [ 'log_retention_cleanup_run', 'log_retention_cleanup_run_fail', 'notification_create_fail', + 'notification_retention_cleanup_run', + 'notification_retention_cleanup_run_fail', 'public_access_enabled', 'public_access_rotated', ]; diff --git a/myteamwallet_backend/src/notifications/notification-retention.scheduler.spec.ts b/myteamwallet_backend/src/notifications/notification-retention.scheduler.spec.ts new file mode 100644 index 0000000..3539c15 --- /dev/null +++ b/myteamwallet_backend/src/notifications/notification-retention.scheduler.spec.ts @@ -0,0 +1,55 @@ +import { LessThan } from 'typeorm'; +import { NotificationRetentionScheduler } from './notification-retention.scheduler'; + +describe('NotificationRetentionScheduler', () => { + const repository = { delete: jest.fn() }; + const configService = { get: jest.fn() }; + const logger = { info: jest.fn(), error: jest.fn() }; + let scheduler: NotificationRetentionScheduler; + + beforeEach(() => { + jest.clearAllMocks(); + jest.useFakeTimers().setSystemTime(new Date('2026-08-04T12:00:00.000Z')); + configService.get.mockReturnValue(365); + repository.delete.mockResolvedValue({ affected: 3 }); + scheduler = new NotificationRetentionScheduler(repository as any, configService as any, logger as any); + }); + + afterEach(() => { + jest.useRealTimers(); + }); + + it('deletes notifications older than the configured retention window', async () => { + await scheduler.cleanupOldNotifications(); + + expect(configService.get).toHaveBeenCalledWith('app.logRetentionDays'); + expect(repository.delete).toHaveBeenCalledWith({ + createdAt: LessThan(new Date('2025-08-04T12:00:00.000Z')), + }); + }); + + it('logs the number of deleted notifications', async () => { + repository.delete.mockResolvedValue({ affected: 7 }); + + await scheduler.cleanupOldNotifications(); + + expect(logger.info).toHaveBeenCalledWith({ + event: 'notification_retention_cleanup_run', + details: 'deletedCount=7 retentionDays=365', + userId: -1, + }); + }); + + it('logs and does not rethrow when the delete fails', async () => { + repository.delete.mockRejectedValue(new Error('connection reset')); + + await expect(scheduler.cleanupOldNotifications()).resolves.toBeUndefined(); + + expect(logger.error).toHaveBeenCalledWith({ + event: 'notification_retention_cleanup_run_fail', + details: 'connection reset', + userId: -1, + }); + expect(logger.info).not.toHaveBeenCalled(); + }); +}); diff --git a/myteamwallet_backend/src/notifications/notification-retention.scheduler.ts b/myteamwallet_backend/src/notifications/notification-retention.scheduler.ts new file mode 100644 index 0000000..890f0d0 --- /dev/null +++ b/myteamwallet_backend/src/notifications/notification-retention.scheduler.ts @@ -0,0 +1,41 @@ +import { Injectable } from '@nestjs/common'; +import { Cron, CronExpression } from '@nestjs/schedule'; +import { ConfigService } from '@nestjs/config'; +import { InjectRepository } from '@nestjs/typeorm'; +import { LessThan, Repository } from 'typeorm'; +import { LoggingService } from 'src/database/logging/logging.service'; +import { Notification } from './entities/notification.entity'; + +@Injectable() +export class NotificationRetentionScheduler { + constructor( + @InjectRepository(Notification) + private readonly repository: Repository, + private readonly configService: ConfigService, + private readonly logger: LoggingService, + ) {} + + @Cron(CronExpression.EVERY_DAY_AT_5AM) + async cleanupOldNotifications(): Promise { + const retentionDays = this.configService.get('app.logRetentionDays'); + const cutoff = new Date(); + cutoff.setUTCDate(cutoff.getUTCDate() - retentionDays); + + try { + const result = await this.repository.delete({ createdAt: LessThan(cutoff) }); + + await this.logger.info({ + event: 'notification_retention_cleanup_run', + details: `deletedCount=${result.affected ?? 0} retentionDays=${retentionDays}`, + userId: -1, + }); + } catch (error) { + const errorMessage = error instanceof Error ? error.message : String(error); + await this.logger.error({ + event: 'notification_retention_cleanup_run_fail', + details: errorMessage, + userId: -1, + }); + } + } +} diff --git a/myteamwallet_backend/src/notifications/notifications.module.ts b/myteamwallet_backend/src/notifications/notifications.module.ts index 4db0483..37df3f7 100644 --- a/myteamwallet_backend/src/notifications/notifications.module.ts +++ b/myteamwallet_backend/src/notifications/notifications.module.ts @@ -8,6 +8,7 @@ import { NotificationRecipient } from './entities/notification-recipient.entity' import { NotificationsController } from './notifications.controller'; import { NotificationsListener } from './notifications.listener'; import { NotificationsService } from './notifications.service'; +import { NotificationRetentionScheduler } from './notification-retention.scheduler'; @Module({ imports: [ @@ -16,6 +17,6 @@ import { NotificationsService } from './notifications.service'; TeamsModule, ], controllers: [NotificationsController], - providers: [NotificationsService, NotificationsListener], + providers: [NotificationsService, NotificationsListener, NotificationRetentionScheduler], }) export class NotificationsModule {}