Create session by code: error

I’m working on a bookings webpage, where there are two type of users:
The Suppliers and Consumers
The Suppliers would create their services through me (giving me the info, then i add the service to the website), and the Consumers would be able to book the sessions for the services.
I want the suppliers to be able to manage the services they’re in charge of, so among other things I want them to be able to create appointments.
I unfortunately there is no way to such staff permission, that does what I want. I know about “bookings staff member” but it doesn’t let them add new appintments


So i came up with a solution:
I created an admin page where members with the permissions can create new sessions using
wix-bookings-backend -s sessions.createSession() method and at first it looked like it works, when i want to book the session created by code a time slot appers on the Booking Calendar page but when i try to click it to book it an error occurs:

Uncaught (in promise) TypeError: Cannot read properties of undefined (reading ‘id’)
at Object.getBookingPreferenceOptionFromSlot (bookingPreferences.ts:61:60)
at eval (bookingPreferencesForSelectedTime.ts:105:66)
at Array.forEach ()
at nt (bookingPreferencesForSelectedTime.ts:104:33)
at eval (bookingPreferencesForSelectedTime.ts:54:23)
at Array.map ()
at tt (bookingPreferencesForSelectedTime.ts:50:29)
at et (bookingPreferencesForSelectedTime.ts:28:10)
at kt (bookingDetailsViewModel.tsx:168:30)
at Object.createViewModel (sidebarViewModel.ts:40:30)

It seems to be an internal error, i don’t know how to fix it

You can check it at www.fogli .hu
(click on the service “Macaron workshop” then try to book for january 30. 3:00 am and look at developer console)

Any help would be really appreciated either helping me fix the coding error, or if you have other ideas about how to make a different solution for my problem without using createService()

Thanks in advance!

Do you know that a session is being returned? Have you tested the session right after it’s returned? Does the session appear valid?

If you suspect that there is a bug the API, you will need to build a minimum scenario where you show the error. It might be that you are not presenting the session properly. You will need to check out what’s happening in your code by adding console.log() statements allowing you to inspect variables and other information. And keep the scenario very simple.

Hi!
I know that the session is returned, and I have looked at the returned session object previously and it appeared valid.
I have built a minimum scenario like you suggested. I added a new service called “test_class1”, and added a session to it through the calendar, then did a query to get the session to get the scheduleId.
After that i created a button in the home page and wrote the following code:

import {myCreateSession} from 'backend/bookingsFunctions.jsw'

$w.onReady(function () {
    $w('#button5').onClick((click)=>{
        console.log('adding to test class...');
        let infoAbTestSession={
            start:{
                timestamp: new Date (2022, 1, 24, 1),
            },
            end:{
                timestamp: new Date (2022, 1, 24, 2),
            },
            scheduleId: "bcaad087-c2b7-4158-a4c2-2afe5ab845c6",
            type: "EVENT",
        }
        myCreateSession(infoAbTestSession, {supressAuth:true})
        .then((res)=>{
            console.log(res);
        })
        .catch((err)=>{
            console.log('Something went wrong:', err);
        })
    });
});

The imported function from the backend looks like this:

import { sessions } from "wix-bookings-backend";

export function myCreateSession(sessionInfo, options){
    return sessions.createSession(sessionInfo, options);
}

I published, then went into my test account and pressed the button, and got the following logs on the console:

adding to test class...

end:
    timestamp: 
notes: ""
scheduleId: "bcaad087-c2b7-4158-a4c2-2afe5ab845c6"
scheduleOwnerId: "a0ff213f-9e12-4ca8-b146-6f1f269e54aa"
start:
    timestamp: 
tags: ['GROUP']
type: "EVENT"
_id: "2mmoW0vwKcSFyxtOfCdMu78iJroqb0nmyRBamHuVEyPL7RXC0gXv6tQ8NhlojvnHQWlMQ8d8KgGRmrA4zoQ2T1zOE9C55GNLiMhh"

And still if i go to book this class when i click the circled button on the screenshot the same error occures

Can you help me what might be wrong with my code? Or if it’s a bug in the API is there anything I can do about it?

Hi, I’m trying to create a recurring session and the session is being created as well without any error but the status of session is undefined always. Am I missing something ? can you help ?

{"end":{"localDateTime":        
{"year":2022,"monthOfYear":2,"dayOfMonth":24,"hourOfDay":2,"minutesOfHour":30}},"_id":"531ed9facda441b8b87abe907a6ac3cb-b1b67120faaf45fb86ee7f954a23cd6e","notes":"","recurrence":"FREQ=WEEKLY;INTERVAL=1;BYDAY=TH;UNTIL=20230101T000000Z","scheduleId":"531ed9fa-cda4-41b8-b87a-be907a6ac3cb","scheduleOwnerId":"28a99209-64c9-4e27-a24b-cdba79e43cf9","start":{"localDateTime":{"year":2022,"monthOfYear":2,"dayOfMonth":24,"hourOfDay":0,"minutesOfHour":30}},
"status":"UNDEFINED","tags":[],"type":"WORKING_HOURS"}

I think this is related, so adding it to this thread, but we could also start a new one. I am flabbergasted by the error from this code. Any insight?

import { sessions } from "wix-bookings-backend";
export async function createNonRecurringSessions(sched, sdate, edate) {
    let start = new Date(sdate)
    let end = new Date(edate)
    const sessionInfo = {
        scheduleId: sched,
        start: {
            timestamp: new Date(sdate)
        },
        end: {
            timestamp: new Date(edate)
        },
        type: "EVENT",
        tags: ["Blocked"]
    };

    const options = { suppressAuth: true };
    return sessions.createSession(sessionInfo, options)
        .then((session) => {
            return session;
        })
        .catch((error) => {
            console.error('Failed to create session:', error);
        });




Failed to create session: 

message: 
    'entity_not_found, 
    details: {
        "entity":"schedule",
        "scheduleId":"b263b6c9-efa9-407e-b096-114512ab2d03",
        "targetDate":"",
        "scheduleVersion":""
        }
     : Not Found'

details:   
    applicationError:     
        description: Not Found     
        code: NOT_FOUND     
        data: {}