Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 48 additions & 25 deletions backend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"@nestjs/jwt": "^11.0.2",
"@nestjs/passport": "^11.0.5",
"@nestjs/platform-express": "^11.0.1",
"@nestjs/schedule": "^6.1.1",
"@nestjs/swagger": "^11.2.4",
"@nestjs/typeorm": "^11.0.0",
"@types/multer": "^2.0.0",
Expand Down
6 changes: 5 additions & 1 deletion backend/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { DonationsModule } from './donations/donations.module';
import { User } from './auth/entities/user.entity';
import { Donation } from './donations/entities/donation.entity';
import { CacheModule } from '@nestjs/cache-manager';
import { ScheduleModule } from '@nestjs/schedule';
import * as redisStore from 'cache-manager-redis-store';

@Module({
Expand All @@ -22,7 +23,10 @@ import * as redisStore from 'cache-manager-redis-store';
// 1. Load .env variables
ConfigModule.forRoot({ isGlobal: true }),

// 2. Connect to Postgres (using variables from docker-compose)
// 2. Enable Cron Jobs
ScheduleModule.forRoot(),

// 3. Connect to Postgres (using variables from docker-compose)
TypeOrmModule.forRoot({
type: 'postgres',
host: process.env.DATABASE_HOST || 'postgres',
Expand Down
28 changes: 28 additions & 0 deletions backend/src/donations/donations.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { Inject, Injectable, Logger, NotFoundException, BadRequestException, For
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { CreateDonationDto, ClaimDonationDto } from './dto/donations.dto';
import { Cron, CronExpression } from '@nestjs/schedule';
import { LessThan } from 'typeorm';
import { Donation, DonationStatus } from './entities/donation.entity';
import { User, UserRole } from '../auth/entities/user.entity';
import { CACHE_MANAGER } from '@nestjs/cache-manager';
Expand Down Expand Up @@ -355,4 +357,30 @@ export class DonationsService {
return newBadges;
}

@Cron('CronExpression.EVERY_DAY_AT_MIDNIGHT')
async handleDataRetentionPolicy() {
this.logger.log('Running Data Retention Cron Job...');

const thirtyDaysAgo = new Date();
thirtyDaysAgo.setDate(thirtyDaysAgo.getDate() - 30);

try {
const oldDonations = await this.donationsRepository.find({
where: {
status: DonationStatus.DELIVERED,
deliveredAt: LessThan(thirtyDaysAgo),
},
});

if (oldDonations.length > 0) {
await this.donationsRepository.remove(oldDonations);
await this.invalidateCache();
this.logger.log(`Successfully deleted ${oldDonations.length} old donations older than 30 days.`);
} else {
this.logger.log('No old data found to archive today.');
}
} catch (error: any) {
this.logger.error('Failed to run data retention job', error.stack);
}
}
}