mirror of
https://github.com/HamzaSha1/zod-backend.git
synced 2025-08-25 21:59:40 +00:00
feat: add cron job for allowance and implement cron lock
This commit is contained in:
@ -1,3 +1,4 @@
|
||||
import moment from 'moment';
|
||||
import {
|
||||
Column,
|
||||
CreateDateColumn,
|
||||
@ -13,7 +14,6 @@ import { Guardian } from '~/guardian/entities/guradian.entity';
|
||||
import { Junior } from '~/junior/entities';
|
||||
import { AllowanceFrequency, AllowanceType } from '../enums';
|
||||
import { AllowanceChangeRequest } from './allowance-change-request.entity';
|
||||
|
||||
@Entity('allowances')
|
||||
export class Allowance {
|
||||
@PrimaryGeneratedColumn('uuid')
|
||||
@ -71,4 +71,31 @@ export class Allowance {
|
||||
|
||||
@DeleteDateColumn({ name: 'deleted_at', type: 'timestamp with time zone', nullable: true })
|
||||
deletedAt?: Date;
|
||||
|
||||
get nextPaymentDate(): Date | null {
|
||||
const startDate = moment(this.startDate).clone().startOf('day');
|
||||
const endDate = this.endDate ? moment(this.endDate).endOf('day') : null;
|
||||
const now = moment().startOf('day');
|
||||
|
||||
if (endDate && moment().isAfter(endDate)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const calculateNextDate = (unit: moment.unitOfTime.Diff) => {
|
||||
const diff = now.diff(startDate, unit);
|
||||
const nextDate = startDate.clone().add(diff, unit);
|
||||
return nextDate.isSameOrAfter(now) ? nextDate.toDate() : nextDate.add('1', unit).toDate();
|
||||
};
|
||||
|
||||
switch (this.frequency) {
|
||||
case AllowanceFrequency.DAILY:
|
||||
return calculateNextDate('days');
|
||||
case AllowanceFrequency.WEEKLY:
|
||||
return calculateNextDate('weeks');
|
||||
case AllowanceFrequency.MONTHLY:
|
||||
return calculateNextDate('months');
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user