mirror of
https://github.com/HamzaSha1/zod-backend.git
synced 2025-08-26 06:09:41 +00:00
refactor: remove unsed code
This commit is contained in:
@ -1,11 +1,8 @@
|
||||
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, MoneyRequestTask } from './tasks';
|
||||
|
||||
@Module({
|
||||
imports: [AllowanceModule, MoneyRequestModule],
|
||||
providers: [AllowanceTask, MoneyRequestTask, BaseCronService],
|
||||
imports: [],
|
||||
providers: [BaseCronService],
|
||||
})
|
||||
export class CronModule {}
|
||||
|
@ -1,54 +0,0 @@
|
||||
import { Injectable, Logger } from '@nestjs/common';
|
||||
import { Cron, CronExpression } from '@nestjs/schedule';
|
||||
import moment from 'moment';
|
||||
import { AllowancesService } from '~/allowance/services';
|
||||
import { CacheService } from '~/common/modules/cache/services';
|
||||
import { BaseCronService } from '../services/base-cron.service';
|
||||
const TEN = 10;
|
||||
const SIXTY = 60;
|
||||
const THOUSAND = 1000;
|
||||
const TEN_MINUTES = TEN * SIXTY * THOUSAND;
|
||||
const CHUNK_SIZE = 50;
|
||||
|
||||
@Injectable()
|
||||
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 readonly allowanceService: AllowancesService) {
|
||||
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.');
|
||||
return;
|
||||
}
|
||||
this.logger.log('Processing cron job');
|
||||
await this.processJob();
|
||||
} catch (error) {
|
||||
this.logger.error('Error processing cron job', error);
|
||||
} finally {
|
||||
this.logger.log('Releasing lock');
|
||||
await this.releaseLock(this.cronLockKey);
|
||||
}
|
||||
}
|
||||
|
||||
private async processJob() {
|
||||
const today = moment().startOf('day');
|
||||
const allowancesChunks = await this.allowanceService.findAllowancesChunks(CHUNK_SIZE);
|
||||
for await (const allowances of allowancesChunks) {
|
||||
for (const allowance of allowances) {
|
||||
this.logger.log(`Processing allowance ${allowance.id} with next payment date ${allowance.nextPaymentDate}`);
|
||||
// if today is the same as allowance payment date
|
||||
if (moment(allowance.nextPaymentDate).startOf('day').isSame(today)) {
|
||||
this.logger.log(`Today is the payment date for allowance ${allowance.id}`);
|
||||
//@TODO: Implement payment logic
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -1,2 +0,0 @@
|
||||
export * from './allowance.task';
|
||||
export * from './money-request.task';
|
||||
|
@ -1,54 +0,0 @@
|
||||
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}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user