|
const { runSelectQuery } = require('../utils/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 = []; |
|
|
|
while (availableDates.length < 14) { |
|
|
|
const isHolidayResult = await isHoliday(currentDate.toFormat('yyyy-MM-dd')); |
|
const isSundayResult = currentDate.weekday === 7; |
|
const isSaturdayResult = currentDate.weekday === 6; |
|
|
|
|
|
let saturdayCount = 0; |
|
let tempDate = DateTime.local(currentDate.year, currentDate.month, 1); |
|
|
|
while (tempDate < currentDate) { |
|
if (tempDate.weekday === 6) { |
|
saturdayCount++; |
|
} |
|
tempDate = tempDate.plus({ days: 1 }); |
|
} |
|
|
|
const isFirstThirdOrFifthSaturday = isSaturdayResult && [0, 2, 4].includes(saturdayCount); |
|
|
|
|
|
const isAvailable = await checkDateAvailability(currentDate.toJSDate()); |
|
|
|
|
|
if (!isHolidayResult && !isSundayResult && !isFirstThirdOrFifthSaturday && isAvailable) { |
|
|
|
availableDates.push(currentDate.toFormat('yyyy-MM-dd')); |
|
console.log('Added date:', currentDate); |
|
} else { |
|
console.log('Date skipped:', currentDate.toISODate()); |
|
} |
|
|
|
|
|
currentDate = currentDate.plus({ days: 1 }); |
|
} |
|
|
|
return availableDates; |
|
} |
|
|
|
module.exports = { checkDateAvailability, getAvailableDates }; |