File size: 1,984 Bytes
8b105ad
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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 };