Skip to content
Open
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
2 changes: 2 additions & 0 deletions apps/backend/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { ManufacturerModule } from './foodManufacturers/manufacturer.module';
import { DonationModule } from './donations/donations.module';
import { DonationItemsModule } from './donationItems/donationItems.module';
import { AllocationModule } from './allocations/allocations.module';
import { ScheduleModule } from '@nestjs/schedule';

@Module({
imports: [
Expand All @@ -29,6 +30,7 @@ import { AllocationModule } from './allocations/allocations.module';
useFactory: async (configService: ConfigService) =>
configService.get('typeorm'),
}),
ScheduleModule.forRoot(),
UsersModule,
AuthModule,
PantriesModule,
Expand Down
8 changes: 7 additions & 1 deletion apps/backend/src/donations/donations.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,19 @@ import { DonationService } from './donations.service';
import { DonationsController } from './donations.controller';
import { ManufacturerModule } from '../foodManufacturers/manufacturer.module';
import { FoodManufacturer } from '../foodManufacturers/manufacturer.entity';
import { DonationsSchedulerService } from './donations.scheduler';

@Module({
imports: [
TypeOrmModule.forFeature([Donation, FoodManufacturer]),
ManufacturerModule,
],
controllers: [DonationsController],
providers: [DonationService, AuthService, JwtStrategy],
providers: [
DonationService,
AuthService,
JwtStrategy,
DonationsSchedulerService,
],
})
export class DonationModule {}
22 changes: 22 additions & 0 deletions apps/backend/src/donations/donations.scheduler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Injectable, Logger } from '@nestjs/common';
import { DonationService } from './donations.service';
import { Cron } from '@nestjs/schedule';

@Injectable()
export class DonationsSchedulerService {
private readonly logger = new Logger(DonationsSchedulerService.name);

constructor(private readonly donationService: DonationService) {}

// cron pattern:
// supported: *, ranges/#'s (e.g. 1-3, 5), steps (e.g. */2)
// * indicates the method should be run every _ unit of time
// range/# indicates method should be run on the _ unit of time/between the _ and _ unit of time
// step indicates the method should be run every _ unit of time
// fields in order: second, minute, hour, day of month, month, day of week
@Cron('0 30 10 * * *') // Runs every day at 10:30 AM
async handleDailyRecurringDonations() {
this.logger.log('Running daily donation reminder cron job');

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the user of a logger. Any reason why we are doing this now over console logging like we had done previously?

await this.donationService.handleRecurringDonations();
}
}
5 changes: 5 additions & 0 deletions apps/backend/src/donations/donations.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,9 @@ export class DonationService {
donation.status = DonationStatus.FULFILLED;
return this.repo.save(donation);
}

async handleRecurringDonations(): Promise<void> {
console.log('Accessing donation service from cron job');
// TODO: Implement logic for sending reminder emails
}
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"@nestjs/core": "^10.0.2",
"@nestjs/passport": "^10.0.2",
"@nestjs/platform-express": "^10.0.2",
"@nestjs/schedule": "^6.1.0",
"@nestjs/swagger": "^7.4.2",
"@nestjs/typeorm": "^10.0.0",
"@types/google-libphonenumber": "^7.4.30",
Expand Down
Loading