i-darrshan's picture
initial push of the update
8b105ad
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');
// This is the part where the route is defined.
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 {
// Convert requiredMinDate to Asia/Kolkata timezone (starting of the day in Asia/Kolkata)
const currentDate = DateTime.fromISO(requiredMinDate).setZone('Asia/Kolkata').startOf('day');
console.log('Converted start date in Asia/Kolkata:', currentDate);
// Initialize the availableDates array
const availableDates = await getAvailableDates(currentDate);
console.log('Available dates:', availableDates);
if (availableDates){
//create an arrow function that creates an array of objects, where each object key is the availableDate and the value is the one returned by getAvailableSlots(availableDate)
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' });
}
});
// Export the router so it can be used in the server.js file.
module.exports = router;