Files
Isuru Bimsara 81d0e65686 first commit
2026-08-14 22:46:02 +05:30

88 lines
1.8 KiB
JavaScript

/**
* Copyright (c) 2026 Niolla
* All rights reserved.
*
* This source code is proprietary and confidential.
* Unauthorized copying, modification, distribution, or use
* of this file, via any medium, is strictly prohibited.
*/
// app/utils/calendar.util.js
const db = require("../models");
const { log } = require("./consoleLog.utill");
const Calendar = db.calender;
const CalenderHoliday = db.calenderHolidays;
const calculateDeadline = async () => {
let deadlineDate = new Date();
const currentYear = deadlineDate.getFullYear();
// Get calendar for current year
const calendar = await Calendar.findOne({
where: {
year: currentYear
}
});
if (!calendar) {
throw new Error(`Calendar for year ${currentYear} not found`);
}
deadlineDate.setDate(deadlineDate.getDate() + 1);
while (true) {
const month = deadlineDate.getMonth() + 1;
const day = deadlineDate.getDate();
// Get holidays for this month
const holidays = await CalenderHoliday.findAll({
where: {
calender_id: calendar.calender_id,
month: month,
shouldConsiderAsHoliday: true
}
});
const isWeekend =
deadlineDate.getDay() === 0 ||
deadlineDate.getDay() === 6;
const holiday = holidays.find(
h => h.day === day
);
const isHoliday =
isWeekend || !!holiday;
log(
`${deadlineDate.toISOString().split("T")[0]} => Holiday: ${isHoliday}`
);
if (!isHoliday) {
return deadlineDate;
}
// Move one more day
deadlineDate.setDate(
deadlineDate.getDate() + 1
);
}
};
module.exports = {
calculateDeadline
};