Setting the file. One moment.
Subchapter 2.74
references/service-plugin/BOOKINGS-STAFF-SORTING.mdMarkdown3 KBView on GitHub
The Staff Sorting Provider SPI lets you implement custom staff assignment algorithms for Wix Bookings. When a booking slot has multiple available staff members, Wix calls your plugin to determine the priority order. Implement the sortStaffMembers handler — it returns the available staff members reordered by priority.
FQDN: wix.interfaces.resources.sorting.v1.staff_sorting_provider
Before implementing, call ReadFullDocsMethodSchema on the docs URL to get the full request/response types.
Important constraints:
availableResourceIds, reordered by priorityThis example sorts staff members to balance workload by prioritizing those with fewer recent bookings.
import { staffSorting } from "@wix/bookings/service-plugins";
import { auth } from "@wix/essentials";
import { extendedBookings } from "@wix/bookings";
staffSorting.provideHandlers({
sortStaffMembers: async (payload) => {
const { request } = payload;
const { availableResourceIds, slot } = request;
const sevenDaysAgo = new Date(Date.now() - 7 * 24 * 60 * 60 * 1000).toISOString();
const elevatedQuery = auth.elevate(extendedBookings.query);
const result = await elevatedQuery({
filter: {
"bookedEntity.item.slot.resource.id": { "$in": availableResourceIds },
"startDate": { "$gte": sevenDaysAgo },
},
cursorPaging: { limit: 100 },
});
const recentBookings = result.extendedBookings ?? [];
// Count bookings per staff member
const bookingCounts = new Map<string, number>();
for (const id of availableResourceIds) {
bookingCounts.set(id, 0);
}
for (const booking of recentBookings) {
const resourceId = booking.booking?.bookedEntity?.slot?.resource?._id;
if (resourceId && bookingCounts.has(resourceId)) {
bookingCounts.set(resourceId, (bookingCounts.get(resourceId) ?? 0) + 1);
}
}
// Sort by fewest bookings first (balance workload)
const sorted = [...availableResourceIds].sort(
(a, b) => (bookingCounts.get(a) ?? 0) - (bookingCounts.get(b) ?? 0)
);
return {
staff: sorted.map((resourceId) => ({ resourceId })),
};
},
});availableResourceIds, just reorderedauth.elevate when querying Wix APIs from the handler