|
const express = require('express'); |
|
const {DateTime} = require('luxon'); |
|
const { getAvailableDates } = require('../controller/dateAvailability'); |
|
const { getAvailableSlots } = require('../controller/slots'); |
|
const { createDateTimeSlotMapping, slotsInUserZone, getDatesSlots } = require('../controller/utils'); |
|
const { isHoliday } = require('../controller/holidays'); |
|
|
|
|
|
const router = express.Router(); |
|
|
|
router.post('/get-available-dates-and-slots', async (req, res) => { |
|
const { requiredMinDate, timezone } = req.body; |
|
console.log('Received request to get available dates:', req.body); |
|
|
|
if (!requiredMinDate) { |
|
console.log("Minimum date is required"); |
|
return res.status(400).send({ error: 'Minimum date is required' }); |
|
} |
|
|
|
try { |
|
|
|
const currentDate = DateTime.fromISO(requiredMinDate).setZone('Asia/Kolkata').startOf('day'); |
|
console.log('Converted start date in Asia/Kolkata:', currentDate); |
|
|
|
|
|
const availableDates = await getAvailableDates(currentDate); |
|
|
|
console.log('Available dates:', availableDates); |
|
if (availableDates){ |
|
|
|
const datesAndSlots = await Promise.all(availableDates.map(async availableDate => { |
|
const slots = await getAvailableSlots(availableDate); |
|
return { [availableDate]: slots }; |
|
})); |
|
|
|
const inter = createDateTimeSlotMapping(datesAndSlots); |
|
console.log(inter); |
|
|
|
const userSlots = await slotsInUserZone(inter, timezone); |
|
console.log(userSlots); |
|
|
|
const finalDateSlots = await getDatesSlots(userSlots); |
|
console.log(finalDateSlots); |
|
|
|
const finalDates = Object.keys(finalDateSlots); |
|
console.log(finalDates); |
|
|
|
const response = { |
|
finalDateSlots: finalDateSlots, |
|
finalDates: finalDates |
|
}; |
|
res.json(response); |
|
} |
|
|
|
} catch (error) { |
|
console.error('Error fetching available dates:', error.message); |
|
res.status(500).send({ error: 'Failed to fetch available dates' }); |
|
} |
|
}); |
|
|
|
|
|
module.exports = router; |