Query All Services All Sessions for Next 2 Weeks?

I want to fill a collection that I can tie to a repeater. I want the collection to contain all of the information for all of the sessions for the next two weeks for all services offered.

I know how to query a list of services, and I know how, if I have a single service in mind, I can query a list of the sessions for the next two weeks for that one service, but I do not know how to build a collection that combines the two, (all sessions for all services for the next two weeks).

Can someone help me with the code?

Product:
Wix Editor

What have you already tried:
I know how to get a list of services:
queryServices()
And I know how to get the sessions for a single service:
wixBookings.getServiceAvailability(serviceId)).slots

Without taking to much time on your issue.

What do we know already ?

  1. We can get a SERVICE, which can include several → SESSIONS.

Let’s say you get a SERVICE… (simple coding preview mode)…

let SERVICES = await queryServices(); console.log(SERVICES);

You get an OBJECT including all informations, something like…

{
    serviceId:                  'xxxxxxxxxxxxxxx';
    serviceTitle:             'yyyyyyyyyyyyy';
    serviceWhatEver:  'zzzzzzzzzzzzzzz';
}

But we know that this service will also include several → SESSIONS…

{
serviceId: ‘xxxxxxxxxxxxxx’,
serviceTitle: ‘yyyyyyyyyyyyy’,
serviceWhatEver: ‘zzzzzzzzzzzzzzz’,
service_sessions: [
{ sessionID: ‘eeeeeeeeeeeeeeeeeeee’,
session_startTime: ‘specificTimeStampHere’,
session_endTime: ‘specificTimeStampHere’
},
{
session_startTime: ‘specificTimeStampHere’,
session_endTime: ‘specificTimeStampHere’
},
{
session_startTime: ‘specificTimeStampHere’,
session_endTime: ‘specificTimeStampHere’
}
]
}

Let us improve and expand our example a little bit more…
To scan the given object and find sessions that fall within a specific time window range, you can use JavaScript. The idea is to iterate through the service_sessions array, check each session’s start and end times against the desired time window, and then filter out the sessions that fall within this range.

Here’s an example of how you might do this:

// Sample data
const serviceData = {
    serviceId: 'xxxxxxxxxxxxxx',
    serviceTitle: 'yyyyyyyyyyyyy',
    serviceWhatEver: 'zzzzzzzzzzzzzzz',
    service_sessions: [
        {   
            sessionID: 'eeeeeeeeeeeeeeeeeeee',
            session_startTime: '2024-07-01T10:00:00Z',
            session_endTime: '2024-07-01T11:00:00Z'
        },
        {
            session_startTime: '2024-07-02T12:00:00Z',
            session_endTime: '2024-07-02T13:00:00Z'
        },
        {
            session_startTime: '2024-07-03T14:00:00Z',
            session_endTime: '2024-07-03T15:00:00Z'
        }
    ]
};

// Function to filter sessions within a specific time window
function filterSessionsWithinTimeWindow(data, startTime, endTime) {
    const start = new Date(startTime);
    const end = new Date(endTime);

    return data.service_sessions.filter(session => {
        const sessionStart = new Date(session.session_startTime);
        const sessionEnd = new Date(session.session_endTime);

        // Check if the session is within the time window
        return (sessionStart >= start && sessionStart <= end) || 
               (sessionEnd >= start && sessionEnd <= end) ||
               (sessionStart <= start && sessionEnd >= end);
    });
}

// Define the time window
const timeWindowStart = '2024-07-01T00:00:00Z';
const timeWindowEnd = '2024-07-02T00:00:00Z';

// Find sessions within the time window
const filteredSessions = filterSessionsWithinTimeWindow(serviceData, timeWindowStart, timeWindowEnd);

console.log(filteredSessions);

Explanation:

  1. Sample Data: The serviceData object is provided as an example, with service_sessions containing session start and end times.
  2. filterSessionsWithinTimeWindow Function: This function takes the data object and the time window (start and end times) as arguments.
  • It converts the startTime and endTime to Date objects.
  • It filters the service_sessions array by checking if each session’s start or end time falls within the given time window.
  • The filter condition checks three cases:
    • The session starts within the time window.
    • The session ends within the time window.
    • The session starts before the time window and ends after it, meaning it spans the entire time window.
  1. Define the Time Window: The desired time window is defined with timeWindowStart and timeWindowEnd.
  2. Filter Sessions: The function is called with the sample data and the time window, and the filtered sessions are logged to the console.

You can adjust the timeWindowStart and timeWindowEnd variables to any desired timestamps to filter the sessions accordingly.

You can also generate a new empty ARRAY, where you put in all matching items of a SESSION-ARRAY found on a specific SERVICE. Once the iteration is complete, you get an ARRAY filled with all the matching data, which you can use for further purposes.