/* eslint-disable no-await-in-loop */ /* eslint-disable @typescript-eslint/naming-convention */ import { Injectable } from '@nestjs/common'; import { Cron } from '@nestjs/schedule'; import moment from 'moment'; import { EmployeeService } from '../employee/employee.service'; import { MonthlyRecordService } from '../monthly-record/monthly-record.service'; @Injectable() export class InsertMonthlyRecord { constructor( private monthlyRecordService: MonthlyRecordService, private employeeService: EmployeeService, ) {} isCronRunning = false; @Cron('0 0 0 * * *') //Everyday check 1 time async insertMonthlyRecord() { if (this.isCronRunning === true) { return; } this.isCronRunning = true; const employees = await this.employeeService.findManyWithoutPagination({ isActive: true, }); if (employees.length === 0) { this.isCronRunning = false; return; } const currentDate = moment.utc().startOf('month'); for (const employee of employees) { if (employee.dateResign && moment.utc().toDate() > employee.dateResign) { continue; } await this.monthlyRecordService.createMonthlyRecordByFunction(employee, { date: currentDate.toDate(), restDayWages: 0, }); } this.isCronRunning = false; } }