"Syncing" Databases With On-Page Code

We have 5 databases to work from.

Bookings/Services
Bookings/Staff
Bookings/Schedule

Tutors
Courses

The Tutors DB shows all of the tutors. When clicking on a tutor, you’re shown all of courses that this tutor offers.

The course data is pulled from the Courses database.

When selecting a course from the tutors page, the viewer is led to the Bookings/Services servicepageUrl to allow for the standard Wix Bookings flow.

The only reason the Bookings/Schedule collection is being used is because I believe this is called through using “GetServiceAvailability” - however perhaps the collection itself does not need to be included? Within “GetServiceAvailability” there is a field key titled staffMemberId which shows the Bookings/Staff._id item values. When running GetServiceAvailability, the individual serviceId also comes through. (In reality, I think these are the only 2 keys that I really need to be “syncd”. I mainly just need the database to show that this serviceId is connected to this staffId.)

I need to be able to have all Bookings/Services data to update existing items, insert new items, and remove old items from and to the Courses database collection.

After this happens, I also need to have the Bookings/Staff data to update, insert, and remove old items when needed within both, the Courses database and the Tutors database.

Here is my main question: How do I do this while having all items updated to their collections Correctly? Right now I know how to randomly spurt out all of the data into the collections but, I do not know how to have the correct data applied to the correct items here in these collections.

Here is some code that I have been working with to try and test.


import wixData from 'wix-data';
import wixBookings from 'wix-bookings';

export function button1_click(event) {

    console.log("Starting query")

    wixData.query("Bookings/Services")
        .find()
        .then((results) => {

 let items = results.items;
 let item = items[0]

            items.forEach((eachItem) => {

 let eachId = eachItem._id

                console.log(eachId)

                wixBookings.getServiceAvailability(eachId)
                    .then((availability) => {

 let slots = availability.slots;
 let firstSlot = availability.slots[0];
 let staffId = slots.staffMemberId;

                       slots.forEach((eachAvail) => {

 let eachstaffId = eachAvail.staffMemberId

 
 //wixData.insert("Courses", item)

                            console.log(eachstaffId)
//viewing slots instead will show arrays of data that include both the serviceId and the staffMemberId so naturally, these 2 items are connected. I need to take this data to send it to the correct fields within our courses and tutors database.

                        })
            })
            })
            })
 
}
//wixData.insert("Courses", item)

If I return the eachstaffId to the console, I get about 75 items showing the same staffMemberId.

How would I have this only show unique items? To where if id #1 = id #2, it skips until it does not =.

I did not fully get the first part of the question what is the question?
Regarding your second post:

You can use lodash uniqBy function to filter duplicates in your array https://lodash.com/docs/4.17.11#uniqBy

...
const staffIds = ...
const uniqStaffIds =  _.uniqBy(staffIds, '_id'); 

Idk how I did this but, this seems to work perfectly,

import wixData from 'wix-data';
import wixBookings from 'wix-bookings';

export function button1_click(event) {

 

    wixData.query("Bookings/Services")
        .find()
        .then((results) => {

 let items = results.items;
 let item = items[0]

            items.forEach((eachItem) => {

 let eachId = eachItem._id;
 
 let serviceName = eachItem.serviceName;
 let description = eachItem.description;
 let servicePageUrl = eachItem.servicePageUrl;
 let serviceType = eachItem.serviceType;
 let tagLine = eachItem.tagLine;
 let maxNumberOfParticipantsPerReservation = eachItem.maxNumberOfParticipantsPerReservation;
 let paymentType = eachItem.paymentType;
 let slug = eachItem.slug;
 let partOfPricingPlan = eachItem.partOfPricingPlan;
 let priceSummary = eachItem.priceSummary;
 let depositAmount = eachItem.depositAmount;
 let maxSessionCapacity = eachItem.maxSessionCapacity;
 let bookingsFlowEntryUrl = eachItem.bookingsFlowEntryUrl;
 let priceAmount = eachItem.priceAmount;
 let form = eachItem.form;
 let imageURL = eachItem.imageURL;
 
 
 
 

                wixBookings.getServiceAvailability(eachId)
                    .then((availability) => {

 let slots = availability.slots;
 let firstSlot = availability.slots[0];
 let staffId = slots[0].staffMemberId;
 let serviceId = slots[0].serviceId;

 

 
 let toInsert = {
 "imageURL": imageURL,
 "form": form,
 "priceAmount": priceAmount,
 "bookingsFlowEntryUrl": bookingsFlowEntryUrl,
 "maxSessionCapacity": maxSessionCapacity,
 "depositAmount": depositAmount,
 "priceSummary": priceSummary,
 "partOfPricingPlan": partOfPricingPlan,
 "slug": slug,
 "paymentType": paymentType,
 "maxNumberOfParticipantsPerReservation": maxNumberOfParticipantsPerReservation,
 "tagLine": tagLine,
 "serviceType": serviceType,
 "serviceName": serviceName,
 "_id": eachId,                                  
 "staffId": staffId,
 "description": description,
 "servicePageUrl": servicePageUrl,
 


                               };

                                wixData.update("Courses", toInsert)
                                    .then((update) => {
 let updateResults = results; //see item below

                                        console.log(updateResults)


                                   })
                            })
                    })
            })


wixData.query("Bookings/Staff")
        .find()
        .then((results) => {

 let items = results.items;
 let item = items[0]

            items.forEach((eachItem) => {

 let eachId = eachItem._id;
 let eachstaffName = eachItem.name

let toInsertTutors = {


"_id": eachId,
"staffId": eachId,
"title": eachstaffName,
"staffName": eachstaffName,


}

wixData.insert("Tutors", toInsertTutors)
wixData.update("Tutors", toInsertTutors)


})
        })
}

You might have a problem here

wixData.insert("Tutors", toInsertTutors)
wixData.update("Tutors", toInsertTutors)

Because the 2 operation will run simultaneously. It will insert and update at the same time. If you are lucky it will happen in that order but I can happen the orderway around.

Also Why do you need to insert and update the same item?

Thanks for the reply!

I actually did have a problem. On my test site with 3 items this worked perfectly.

With the live site having about 87 services and 30 staff members, this code failed. The tutors actually updated correctly, but not the services “courses”.

I was running insert and update to try and add new items and update existing items (in case description text was updated or something).

I did not know that this would make them run simultaneously. Perhaps add in a .then?

I also need to add a .remove function somewhere so that items in the databases, but not in the bookings collections will be removed.