feat: add money request cronjob, and edit money request entity

This commit is contained in:
Abdalhamid Alhamad
2025-01-02 11:22:33 +03:00
parent 557ef4cd33
commit aefa866ae7
12 changed files with 187 additions and 9 deletions

View File

@ -1,10 +1,11 @@
import { Module } from '@nestjs/common';
import { AllowanceModule } from '~/allowance/allowance.module';
import { MoneyRequestModule } from '~/money-request/money-request.module';
import { BaseCronService } from './services';
import { AllowanceTask } from './tasks';
import { AllowanceTask, MoneyRequestTask } from './tasks';
@Module({
imports: [AllowanceModule],
providers: [AllowanceTask, BaseCronService],
imports: [AllowanceModule, MoneyRequestModule],
providers: [AllowanceTask, MoneyRequestTask, BaseCronService],
})
export class CronModule {}

View File

@ -15,7 +15,7 @@ export class AllowanceTask extends BaseCronService {
private readonly logger = new Logger(AllowanceTask.name);
private readonly cronLockKey = `${AllowanceTask.name}-lock`;
private readonly cronLockTtl = TEN_MINUTES;
constructor(cacheService: CacheService, private allowanceService: AllowancesService) {
constructor(cacheService: CacheService, private readonly allowanceService: AllowancesService) {
super(cacheService);
}

View File

@ -1 +1,2 @@
export * from './allowance.task';
export * from './money-request.task';

View File

@ -0,0 +1,54 @@
import { Injectable, Logger } from '@nestjs/common';
import { Cron, CronExpression } from '@nestjs/schedule';
import moment from 'moment';
import { CacheService } from '~/common/modules/cache/services';
import { MoneyRequestsService } from '~/money-request/services';
import { BaseCronService } from '../services';
const TEN = 10;
const SIXTY = 60;
const THOUSAND = 1000;
const TEN_MINUTES = TEN * SIXTY * THOUSAND;
const CHUNK_SIZE = 50;
@Injectable()
export class MoneyRequestTask extends BaseCronService {
private readonly cronLockKey = `${MoneyRequestTask.name}-lock`;
private readonly cronLockTtl = TEN_MINUTES;
private readonly logger = new Logger(MoneyRequestTask.name);
constructor(cacheService: CacheService, private readonly moneyRequestService: MoneyRequestsService) {
super(cacheService);
}
@Cron(CronExpression.EVERY_DAY_AT_3AM)
async handleCron() {
try {
const isLockAcquired = await this.acquireLock(this.cronLockKey, this.cronLockTtl);
if (!isLockAcquired) {
this.logger.log('Lock already acquired. Skipping cron job for MoneyRequestTask.');
return;
}
this.logger.log('Processing cron job for MoneyRequestTask');
await this.processJob();
} catch (error) {
this.logger.error('Error processing MoneyRequestTask cron job', error);
} finally {
this.logger.log('Releasing lock for MoneyRequestTask');
await this.releaseLock(this.cronLockKey);
}
}
private async processJob() {
const today = moment().startOf('day');
const moneyRequestsChunks = await this.moneyRequestService.findMoneyRequestsChunks(CHUNK_SIZE);
for await (const moneyRequests of moneyRequestsChunks) {
for (const moneyRequest of moneyRequests) {
this.logger.log(
`Processing money request ${moneyRequest.id} with next payment date ${moneyRequest.nextPaymentDate}`,
);
// if today is the same as money request payment date
if (moment(moneyRequest.nextPaymentDate).startOf('day').isSame(today)) {
this.logger.log(`Today is the payment date for money request ${moneyRequest.id}`);
}
}
}
}
}