i-darrshan's picture
initial push of the update
8b105ad
const { DateTime } = require('luxon');
const createDateTimeSlotMapping = (data) => {
const dateTimeSlots = {};
data.forEach(dateObj => {
for (const [date, slots] of Object.entries(dateObj)) {
slots.forEach(slot => {
const [startTime, endTime] = slot.split(' - ');
const dateTime = `${date}T${startTime}`;
// Validate the dateTime before adding to the mapping
const dt = DateTime.fromISO(dateTime);
if (dt.isValid) {
dateTimeSlots[dateTime] = `${startTime} - ${endTime}`;
}
});
}
});
return dateTimeSlots;
};
async function slotsInUserZone(inter, timezone) {
const convertedData = {};
for (const [key, value] of Object.entries(inter)) {
const startTimeKolkata = DateTime.fromISO(key, { zone: "Asia/Kolkata" });
const startTimeUser = startTimeKolkata.setZone(timezone).toISO();
const [start, end] = value.split(" - ");
const startTime = DateTime.fromISO(key, { zone: "Asia/Kolkata" });
const localStartTime = startTime.setZone(timezone).toFormat("HH:mm");
const endTime = startTime.plus({ minutes: 30 });
const localEndTime = endTime.setZone(timezone).toFormat("HH:mm");
convertedData[startTimeUser] = `${localStartTime} - ${localEndTime} (${timezone})`;
}
return convertedData;
}
async function getDatesSlots(input) {
return Object.entries(input).reduce((acc, [timestamp, slot]) => {
// Validate timestamp
const dt = DateTime.fromISO(timestamp);
if (!dt.isValid) {
return acc; // Skip invalid timestamps
}
const date = timestamp.split('T')[0];
if (!acc[date]) {
acc[date] = [];
}
acc[date].push(slot);
return acc;
}, {});
}
module.exports = { createDateTimeSlotMapping, slotsInUserZone, getDatesSlots };