i-darrshan's picture
initial update of the vite site
62c3fe0
const { runSelectQuery } = require('../utils/queries'); // Assuming db.js handles DB queries
const {isHoliday} = require("../controller/holidays")
const { DateTime } = require('luxon');
async function checkDateAvailability(date) {
const checkQuery = `SELECT COUNT(*) AS count FROM demo_requests WHERE demo_date = ?;`;
const results = await runSelectQuery(checkQuery, [date]);
return results.length === 0 || results[0].count < 11;
}
async function getAvailableDates(currentDate) {
const availableDates = [];
// Loop until we collect 14 available dates
while (availableDates.length < 14) {
// Check if the current date is a holiday in India
const isHolidayResult = await isHoliday(currentDate.toFormat('yyyy-MM-dd')); // Pass as JS Date
const isSundayResult = currentDate.weekday === 7; // Sunday in Luxon is 7
const isSaturdayResult = currentDate.weekday === 6; // Saturday in Luxon is 6
// Count which Saturday it is in the month
let saturdayCount = 0;
let tempDate = DateTime.local(currentDate.year, currentDate.month, 1);
while (tempDate < currentDate) {
if (tempDate.weekday === 6) { // If it's a Saturday
saturdayCount++;
}
tempDate = tempDate.plus({ days: 1 });
}
const isFirstThirdOrFifthSaturday = isSaturdayResult && [0, 2, 4].includes(saturdayCount);
// Check if the date is available
const isAvailable = await checkDateAvailability(currentDate.toJSDate());
// If it's not a holiday, not a Sunday, not a Saturday (if 1st, 3rd, or 5th Saturday), and it's available, add to available dates
if (!isHolidayResult && !isSundayResult && !isFirstThirdOrFifthSaturday && isAvailable) {
// Add the date in 'yyyy-MM-dd' format
availableDates.push(currentDate.toFormat('yyyy-MM-dd'));
console.log('Added date:', currentDate);
} else {
console.log('Date skipped:', currentDate.toISODate());
}
// Move to the next day (still in Asia/Kolkata timezone)
currentDate = currentDate.plus({ days: 1 });
}
return availableDates;
}
module.exports = { checkDateAvailability, getAvailableDates };