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
62 changes: 58 additions & 4 deletions backend/package-lock.json

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

4 changes: 3 additions & 1 deletion backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,14 @@
},
"dependencies": {
"@cloudinary-util/types": "^1.6.0",
"@nestjs/axios": "^4.0.1",
"@nestjs/common": "^10.0.0",
"@nestjs/config": "^4.0.2",
"@nestjs/core": "^10.0.0",
"@nestjs/jwt": "^11.0.0",
"@nestjs/passport": "^11.0.5",
"@nestjs/platform-express": "^10.0.0",
"@nestjs/schedule": "^6.0.1",
"@nestjs/swagger": "^7.4.2",
"@nestjs/throttler": "^6.4.0",
"@nestjs/typeorm": "^11.0.0",
Expand Down Expand Up @@ -70,7 +72,7 @@
"@types/express": "^5.0.0",
"@types/jest": "^29.5.2",
"@types/multer": "^2.0.0",
"@types/node": "^20.3.1",
"@types/node": "^20.19.19",
"@types/passport-jwt": "^4.0.1",
"@types/passport-local": "^1.0.38",
"@types/supertest": "^6.0.0",
Expand Down
2 changes: 2 additions & 0 deletions backend/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { JwtAuthGuard } from './auth/guards/jwt.guard';
import { ThrottlerModule, ThrottlerGuard } from '@nestjs/throttler';
import { AuditsModule } from './audits/audits.module';
import { AuditLogsModule } from './audit-logs/audit-logs.module';
import { ScheduledJobsModule } from './scheduled-jobs/scheduled-jobs.module';

@Module({
imports: [
Expand Down Expand Up @@ -64,6 +65,7 @@ import { AuditLogsModule } from './audit-logs/audit-logs.module';
EmailModule,
NewsletterModule,
AuditsModule,
ScheduledJobsModule,
AuditLogsModule,
],
controllers: [AppController],
Expand Down
7 changes: 7 additions & 0 deletions backend/src/scheduled-jobs/scheduled-jobs.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Module } from '@nestjs/common';
import { ScheduledJobsService } from './scheduled-jobs.service';

@Module({
providers: [ScheduledJobsService]
})
export class ScheduledJobsModule {}
18 changes: 18 additions & 0 deletions backend/src/scheduled-jobs/scheduled-jobs.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Test, TestingModule } from '@nestjs/testing';
import { ScheduledJobsService } from './scheduled-jobs.service';

describe('ScheduledJobsService', () => {
let service: ScheduledJobsService;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [ScheduledJobsService],
}).compile();

service = module.get<ScheduledJobsService>(ScheduledJobsService);
});

it('should be defined', () => {
expect(service).toBeDefined();
});
});
53 changes: 53 additions & 0 deletions backend/src/scheduled-jobs/scheduled-jobs.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// src/scheduled-jobs/scheduled-jobs.service.ts
import { Injectable, Logger } from '@nestjs/common';
import { Cron, CronExpression, Interval } from '@nestjs/schedule';
import { SettingsService } from '../settings/settings.service';
import { NotificationService } from '../notifications/notification.service';

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

constructor(
private readonly settingsService: SettingsService,
private readonly notificationService: NotificationService,
) {}

//Run every day at midnight
@Cron(CronExpression.EVERY_DAY_AT_MIDNIGHT)
async checkLicenseExpiry() {
this.logger.log('Running license expiry check...');
const expiredLicenses = await this.findExpiringLicenses();
await this.notifyUsers(expiredLicenses, 'Your license will expire soon.');
}

// Run every Monday at 9 AM
@Cron(CronExpression.EVERY_WEEK)
async sendMaintenanceReminders() {
this.logger.log('Running maintenance reminder job...');
const vehicles = await this.findVehiclesNeedingMaintenance();
await this.notifyUsers(vehicles, 'Maintenance is due soon.');
}

//Example of dynamic interval using settings
@Interval(1000 * 60 * 10) // fallback 10 min interval
async insuranceCheck() {
const interval = await this.settingsService.get('insuranceReminderInterval');
if (interval) {
this.logger.log(`Running insurance check every ${interval} minutes...`);
}
const expiringInsurances = await this.findExpiringInsurances();
await this.notifyUsers(expiringInsurances, 'Your insurance is expiring soon.');
}

// Dummy methods to simulate logic
private async findExpiringLicenses() { return []; }
private async findVehiclesNeedingMaintenance() { return []; }
private async findExpiringInsurances() { return []; }

private async notifyUsers(items: any[], message: string) {
for (const item of items) {
await this.notificationService.send(item.userId, message);
}
}
}