|
| 1 | +/* eslint-disable prettier/prettier */ |
| 2 | +import { Injectable, Logger } from '@nestjs/common'; |
| 3 | +import { Cron, CronExpression } from '@nestjs/schedule'; |
| 4 | +import { InjectRepository } from '@nestjs/typeorm'; |
| 5 | +import { Repository } from 'typeorm'; |
| 6 | +import { ScheduleSettings } from './entities/scheduled-settings.entity'; |
| 7 | + |
| 8 | +@Injectable() |
| 9 | +export class ScheduledJobsService { |
| 10 | + private readonly logger = new Logger(ScheduledJobsService.name); |
| 11 | + |
| 12 | + constructor( |
| 13 | + @InjectRepository(ScheduleSettings) |
| 14 | + private readonly settingsRepo: Repository<ScheduleSettings>, |
| 15 | + ) {} |
| 16 | + |
| 17 | + // 🕒 Daily check for license expiry |
| 18 | + @Cron(CronExpression.EVERY_DAY_AT_MIDNIGHT) |
| 19 | + async checkLicenseExpiry() { |
| 20 | + this.logger.log('Running daily license expiry check...'); |
| 21 | + // TODO: Query licenses expiring soon, send reminders |
| 22 | + } |
| 23 | + |
| 24 | + // 🕗 Daily insurance reminder |
| 25 | + @Cron(CronExpression.EVERY_DAY_AT_8AM) |
| 26 | + async sendInsuranceReminders() { |
| 27 | + this.logger.log('Running insurance reminder task...'); |
| 28 | + // TODO: Notify companies with upcoming insurance expiry |
| 29 | + } |
| 30 | + |
| 31 | + // 🧰 Weekly maintenance scheduling |
| 32 | + @Cron(CronExpression.EVERY_DAY_AT_9AM) |
| 33 | + async scheduleMaintenanceTasks() { |
| 34 | + this.logger.log('Running weekly maintenance scheduler...'); |
| 35 | + // TODO: Identify assets due for maintenance and alert teams |
| 36 | + } |
| 37 | + |
| 38 | + // ⚙️ Update job intervals dynamically |
| 39 | + async updateSettings(newSettings: Partial<ScheduleSettings>) { |
| 40 | + const settings = await this.settingsRepo.findOne({}); |
| 41 | + if (!settings) return this.settingsRepo.save(newSettings); |
| 42 | + Object.assign(settings, newSettings); |
| 43 | + return this.settingsRepo.save(settings); |
| 44 | + } |
| 45 | + |
| 46 | + async getSettings() { |
| 47 | + return this.settingsRepo.findOne({}); |
| 48 | + } |
| 49 | +} |
0 commit comments