251123:0200 T6.1 to DO

This commit is contained in:
2025-11-23 02:23:38 +07:00
parent 17d9f172d4
commit 23006898d9
58 changed files with 3221 additions and 502 deletions
@@ -0,0 +1,39 @@
import { Injectable, Logger } from '@nestjs/common';
import { Cron, CronExpression } from '@nestjs/schedule';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository, LessThan } from 'typeorm';
import { Notification } from './entities/notification.entity';
@Injectable()
export class NotificationCleanupService {
private readonly logger = new Logger(NotificationCleanupService.name);
constructor(
@InjectRepository(Notification)
private notificationRepo: Repository<Notification>,
) {}
/**
* ลบแจ้งเตือนที่ "อ่านแล้ว" และเก่ากว่า 30 วัน
* รันทุกวันเวลาเที่ยงคืน
*/
@Cron(CronExpression.EVERY_DAY_AT_MIDNIGHT)
async handleCleanup() {
this.logger.log('Running notification cleanup...');
const daysAgo = 30;
const dateThreshold = new Date();
dateThreshold.setDate(dateThreshold.getDate() - daysAgo);
try {
const result = await this.notificationRepo.delete({
isRead: true,
createdAt: LessThan(dateThreshold),
});
this.logger.log(`Deleted ${result.affected} old read notifications.`);
} catch (error) {
this.logger.error('Failed to cleanup notifications', error);
}
}
}