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
4 changes: 4 additions & 0 deletions backend/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ import { DepartmentsModule } from './departments/departments.module';
import { AssetTransfersModule } from './asset-transfers/asset-transfers.module';
import { SearchModule } from './search/search.module';
import { ApiKeyModule } from './api-key/api-key.module';
import { NestModule } from './scheduled-jobs/nest/nest.module';
import { ScheduledJobsModule } from './scheduled-jobs/scheduled-jobs.module';

@Module({
imports: [
Expand Down Expand Up @@ -84,6 +86,8 @@ import { ApiKeyModule } from './api-key/api-key.module';
WebhooksModule,
AuditLogsModule,
ApiKeyModule,
NestModule,
ScheduledJobsModule,
],
controllers: [AppController],
providers: [
Expand Down
1 change: 1 addition & 0 deletions backend/src/scheduled-jobs/dto/create-scheduled-job.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export class CreateScheduledJobDto {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/* eslint-disable prettier/prettier */
import { IsOptional, IsString } from 'class-validator';

export class UpdateScheduleSettingsDto {
@IsOptional() @IsString() licenseCheckCron?: string;
@IsOptional() @IsString() insuranceReminderCron?: string;
@IsOptional() @IsString() maintenanceCheckCron?: string;
}
20 changes: 20 additions & 0 deletions backend/src/scheduled-jobs/entities/scheduled-settings.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/* eslint-disable prettier/prettier */
import { Entity, PrimaryGeneratedColumn, Column, UpdateDateColumn } from 'typeorm';

@Entity('schedule_settings')
export class ScheduleSettings {
@PrimaryGeneratedColumn('uuid')
id: string;

@Column({ default: '0 0 * * *' }) // every day at midnight
licenseCheckCron: string;

@Column({ default: '0 8 * * *' }) // every day at 8am
insuranceReminderCron: string;

@Column({ default: '0 9 * * MON' }) // every Monday at 9am
maintenanceCheckCron: string;

@UpdateDateColumn()
updatedAt: Date;
}
19 changes: 19 additions & 0 deletions backend/src/scheduled-jobs/scheduled-jobs.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/* eslint-disable prettier/prettier */
import { Controller, Get, Patch, Body } from '@nestjs/common';
import { ScheduledJobsService } from './scheduled-jobs.service';
import { UpdateScheduleSettingsDto } from './dto/update-scheduled-settings.dto';

@Controller('scheduled-jobs')
export class ScheduledJobsController {
constructor(private readonly scheduledJobsService: ScheduledJobsService) {}

@Get('settings')
getSettings() {
return this.scheduledJobsService.getSettings();
}

@Patch('settings')
updateSettings(@Body() dto: UpdateScheduleSettingsDto) {
return this.scheduledJobsService.updateSettings(dto);
}
}
17 changes: 17 additions & 0 deletions backend/src/scheduled-jobs/scheduled-jobs.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/* eslint-disable prettier/prettier */
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { ScheduleModule } from '@nestjs/schedule';
import { ScheduledJobsController } from './scheduled-jobs.controller';
import { ScheduledJobsService } from './scheduled-jobs.service';
import { ScheduleSettings } from './entities/scheduled-settings.entity';

@Module({
imports: [
TypeOrmModule.forFeature([ScheduleSettings]),
ScheduleModule.forRoot(), // enables cron support
],
controllers: [ScheduledJobsController],
providers: [ScheduledJobsService],
})
export class ScheduledJobsModule {}
49 changes: 49 additions & 0 deletions backend/src/scheduled-jobs/scheduled-jobs.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/* eslint-disable prettier/prettier */
import { Injectable, Logger } from '@nestjs/common';
import { Cron, CronExpression } from '@nestjs/schedule';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { ScheduleSettings } from './entities/scheduled-settings.entity';

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

constructor(
@InjectRepository(ScheduleSettings)
private readonly settingsRepo: Repository<ScheduleSettings>,
) {}

// 🕒 Daily check for license expiry
@Cron(CronExpression.EVERY_DAY_AT_MIDNIGHT)
async checkLicenseExpiry() {
this.logger.log('Running daily license expiry check...');
// TODO: Query licenses expiring soon, send reminders
}

// 🕗 Daily insurance reminder
@Cron(CronExpression.EVERY_DAY_AT_8AM)
async sendInsuranceReminders() {
this.logger.log('Running insurance reminder task...');
// TODO: Notify companies with upcoming insurance expiry
}

// 🧰 Weekly maintenance scheduling
@Cron(CronExpression.EVERY_DAY_AT_9AM)
async scheduleMaintenanceTasks() {
this.logger.log('Running weekly maintenance scheduler...');
// TODO: Identify assets due for maintenance and alert teams
}

// ⚙️ Update job intervals dynamically
async updateSettings(newSettings: Partial<ScheduleSettings>) {
const settings = await this.settingsRepo.findOne({});
if (!settings) return this.settingsRepo.save(newSettings);
Object.assign(settings, newSettings);
return this.settingsRepo.save(settings);
}

async getSettings() {
return this.settingsRepo.findOne({});
}
}