queryReservations( )
Creates a query to retrieve a list of reservations.
Description
The queryReservations()
function builds a query to retrieve a list of reservations and returns a ReservationsQueryBuilder
object.
The returned object contains the query definition, which is used to run the query using the find() function.
You can refine the query by chaining ReservationsQueryBuilder
functions onto the query. ReservationsQueryBuilder
functions enable you to filter, sort, and control the results that queryReservations()
returns.
queryReservations()
runs with the following ReservationsQueryBuilder
defaults, which you can override:
The following ReservationsQueryBuilder
functions are supported for queryReservations()
. For a full description of the reservation object, see the object returned for the items
property in ReservationsQueryResult
.
PROPERTY | SUPPORTED FILTERS & SORTING |
---|---|
_id | eq() ,ne() ,in() |
status | eq() ,ne() ,in() |
details.startDate | eq() ,ne() ,in() ,lt() ,gt() ,le() ,ge() ,ascending() ,descending() |
This function requires elevated permissions to run. This function is not universal and runs only on the backend.
Syntax
function queryReservations(): ReservationsQueryBuilder
queryReservations Parameters
This function does not take any parameters.
Returns
Return Type:
Was this helpful?
This function call requires elevation to get the necessary permissions.
1import { reservations } from 'wix-table-reservations.v2';2import { elevate } from 'wix-auth';3import { Permissions, webMethod } from 'wix-web-module';45export const myQueryReservationsFunction = webMethod(Permissions.Anyone, async () => {6 const elevatedQueryReservations = elevate(reservations.queryReservations);78 try {9 const retrievedReservations = await elevatedQueryReservations()10 .gt('details.startDate', new Date("2024-12-04T10:30:00Z"))11 .lt('details.startDate', new Date("2024-12-04T17:30:00Z"))12 .find();1314 const nextCursor = retrievedReservations._nextCursor;15 const status = retrievedReservations._items[0].status;1617 return retrievedReservations;18 } catch (error) {19 console.error(error);20 // Handle the error21 }22});2324/* Promise resolves to:25 * {26 * "_items": [27 * {28 * "status": "RESERVED",29 * "source": "ONLINE",30 * "details": {31 * "reservationLocationId": "fab8cc1f-31cf-462f-b5bb-392594624bf2",32 * "tableIds": [33 * "1ed802ae-708f-4da6-9177-54c3df5d3dd5"34 * ],35 * "startDate": "2024-12-04T12:30:00.000Z",36 * "endDate": "2024-12-04T14:00:00.000Z",37 * "partySize": 238 * },39 * "revision": "1",40 * "migrationNotes": [],41 * "tablesWithReservationConflicts": [],42 * "_id": "1e3db995-5614-4748-8903-cbe7c74c5753",43 * "_createdDate": "2024-01-22T07:25:51.008Z",44 * "_updatedDate": "2024-01-22T07:25:51.008Z"45 * },46 * {47 * "status": "RESERVED",48 * "source": "ONLINE",49 * "details": {50 * "reservationLocationId": "fab8cc1f-31cf-462f-b5bb-392594624bf2",51 * "tableIds": [52 * "1ed802ae-708f-4da6-9177-54c3df5d3dd5"53 * ],54 * "startDate": "2024-12-04T14:30:00.000Z",55 * "endDate": "2024-12-04T16:00:00.000Z",56 * "partySize": 257 * },58 * "revision": "1",59 * "migrationNotes": [],60 * "tablesWithReservationConflicts": [],61 * "_id": "3e09e2fb-0cc8-476e-9874-f134b3add055",62 * "_createdDate": "2024-01-22T07:33:10.651Z",63 * "_updatedDate": "2024-01-22T07:33:10.651Z"64 * }65 * ],66 * "_originQuery": {67 * "filterTree": {68 * "$and": [69 * {70 * "details.startDate": {71 * "$gt": "2024-12-04T10:30:00.000Z"72 * }73 * },74 * {75 * "details.startDate": {76 * "$lt": "2024-12-04T17:30:00.000Z"77 * }78 * }79 * ]80 * },81 * "invalidArguments": [],82 * "encoder": {},83 * "transformationPaths": {},84 * "sort": [],85 * "paging": {},86 * "pagingMethod": "CURSOR",87 * "builderOptions": {88 * "cursorWithEmptyFilterAndSort": true89 * }90 * },91 * "_limit": 50,92 * "_nextCursor": "",93 * "_prevCursor": "",94 * "cursors": {95 * "next": "",96 * "prev": ""97 * }98 * }99 */
This function call requires additional permissions and can only be called without elevation from a dashboard page.
1import { myQueryReservationsFunction } from 'backend/reservations.jsw';23myQueryReservationsFunction()4 .then((retrievedReservations) => {5 const nextCursor = retrievedReservations._nextCursor;6 const status = retrievedReservations._items[0].status;78 console.log('Success! Retrieved reservations:', retrievedReservations);9 return retrievedReservations;10 })11 .catch((error) => {12 console.error(error);13 // Handle the error14 });1516/* Promise resolves to:17 * {18 * "_items": [19 * {20 * "status": "RESERVED",21 * "source": "ONLINE",22 * "details": {23 * "reservationLocationId": "fab8cc1f-31cf-462f-b5bb-392594624bf2",24 * "tableIds": [25 * "1ed802ae-708f-4da6-9177-54c3df5d3dd5"26 * ],27 * "startDate": "2024-12-04T12:30:00.000Z",28 * "endDate": "2024-12-04T14:00:00.000Z",29 * "partySize": 230 * },31 * "revision": "1",32 * "migrationNotes": [],33 * "tablesWithReservationConflicts": [],34 * "_id": "1e3db995-5614-4748-8903-cbe7c74c5753",35 * "_createdDate": "2024-01-22T07:25:51.008Z",36 * "_updatedDate": "2024-01-22T07:25:51.008Z"37 * },38 * {39 * "status": "RESERVED",40 * "source": "ONLINE",41 * "details": {42 * "reservationLocationId": "fab8cc1f-31cf-462f-b5bb-392594624bf2",43 * "tableIds": [44 * "1ed802ae-708f-4da6-9177-54c3df5d3dd5"45 * ],46 * "startDate": "2024-12-04T14:30:00.000Z",47 * "endDate": "2024-12-04T16:00:00.000Z",48 * "partySize": 249 * },50 * "revision": "1",51 * "migrationNotes": [],52 * "tablesWithReservationConflicts": [],53 * "_id": "3e09e2fb-0cc8-476e-9874-f134b3add055",54 * "_createdDate": "2024-01-22T07:33:10.651Z",55 * "_updatedDate": "2024-01-22T07:33:10.651Z"56 * }57 * ],58 * "_originQuery": {59 * "filterTree": {60 * "$and": [61 * {62 * "details.startDate": {63 * "$gt": "2024-12-04T10:30:00.000Z"64 * }65 * },66 * {67 * "details.startDate": {68 * "$lt": "2024-12-04T17:30:00.000Z"69 * }70 * }71 * ]72 * },73 * "invalidArguments": [],74 * "encoder": {},75 * "transformationPaths": {},76 * "sort": [],77 * "paging": {},78 * "pagingMethod": "CURSOR",79 * "builderOptions": {80 * "cursorWithEmptyFilterAndSort": true81 * }82 * },83 * "_limit": 50,84 * "_nextCursor": "",85 * "_prevCursor": "",86 * "cursors": {87 * "next": "",88 * "prev": ""89 * }90 * }91 */92
This scenario sets up an interface that allows a site owner to select a reservation location and a date, and then close the restaurant on that date. We start by creating a day-long special hour period during which reservations can't be made. We then cancel all reservations on that date.
1/* This example requires the following elements:2 * A dropdown element named `locationDropdown`.3 * A date picker element named 'datePicker`.4 * A button named `confirmButton`.5 * A text element named `cancellationNotice`.6 */78/**********************************************9 * Backend code - reservationLocations.web.js *10 **********************************************/1112import { Permissions, webMethod } from 'wix-web-module';13import { reservationLocations } from 'wix-table-reservations.v2';14import { locations } from 'wix-business-tools.v2';15import { elevate } from 'wix-auth';1617export const getRestaurantLocation = webMethod(Permissions.Anyone, async (location_id) => {18 const elevatedGetLocation = elevate(locations.getLocation);1920 try {21 const result = await elevatedGetLocation(location_id);22 return result;23 } catch (error) {24 console.error(error);25 // Handle the error26 }27});2829export const getRestautantDetails = webMethod(Permissions.Anyone, async (reservationLocationId, options) => {30 const elevatedGetReservationLocations = elevate(reservationLocations.getReservationLocation);3132 try {33 const result = await elevatedGetReservationLocations(reservationLocationId, options);34 return result;35 } catch (error) {36 console.error(error);37 // Handle the error38 }39});4041export const updateRestaurantDetails = webMethod(Permissions.Anyone, async (reservationLocationId, reservationLocationObject) => {42 const elevatedUpdateReservationLocation = elevate(reservationLocations.updateReservationLocation);4344 try {45 const result = await elevatedUpdateReservationLocation(reservationLocationId, reservationLocationObject);46 return result;47 } catch (error) {48 console.error(error);49 // Handle the error50 }51});5253/**************************************54 * Backend code - reservations.web.js *55 *************************************/5657import { Permissions, webMethod } from 'wix-web-module';58import { reservations } from 'wix-table-reservations.v2';59import { elevate } from 'wix-auth';6061export const queryReservationsInDateRange = webMethod(Permissions.Anyone, async (startDate, endDate) => {62 const elevatedQueryReservations = elevate(reservations.queryReservations);6364 try {65 const result = await elevatedQueryReservations()66 .ge('details.startDate', startDate)67 .lt('details.startDate', endDate)68 .find();69 return result;70 } catch (error) {71 console.error(error);72 // Handle the error73 }74});7576/*************77 * Page code *78 *************/7980import { reservationLocations } from 'wix-table-reservations.v2';81import { reservations } from 'wix-table-reservations.v2';8283import { getRestaurantLocation } from 'backend/reservationLocations.web';84import { getRestautantDetails } from 'backend/reservationLocations.web';85import { updateRestaurantDetails } from 'backend/reservationLocations.web';86import { queryReservationsInDateRange } from 'backend/reservations.web';8788$w.onReady(async function () {89 $w('#datePicker').hide();90 $w('#confirmButton').hide();91 $w('#cancellationNotice').hide();9293 // Set the earliest selectable date to the day after the current date94 var tomorrowsDate = new Date();95 tomorrowsDate.setDate(tomorrowsDate.getDate() + 1);96 $w('#datePicker').minDate = tomorrowsDate;97 $w('#datePicker').value = tomorrowsDate;9899 //Get the list of reservation locations100 const myReservationLocationList = await (async () => {101 try {102 let fullListObject = await reservationLocations.listReservationLocations();103 return fullListObject.reservationLocations;104 } catch (error) {105 console.error(error);106 // Handle the error107 }108 })();109110 //Create an array to hold names and values for our dropdown list111 let dropdownOptions = [];112 for (const object in myReservationLocationList) {113 //Use the _id stored in the reservation location's location object to get the location's name from the `locations` service114 const locationObject = await getRestaurantLocation(myReservationLocationList[object].location._id);115 dropdownOptions.push({116 "label": locationObject.name,117 "value": myReservationLocationList[object]._id118 })119 }120 //Populate our dropdown list with the array121 $w("#locationDropdown").options = dropdownOptions;122123 $w('#locationDropdown').onChange((event) => {124 if ($w('#locationDropdown').value) {125 $w('#datePicker').show();126 $w('#confirmButton').show();127 }128 });129130 $w('#confirmButton').onClick(async (event) => {131 const options = {132 "fieldsets": "FULL"133 }134 const selectedLocation = await getRestautantDetails($w('#locationDropdown').value, options)135136 //Create a new special hour period to push to the list in our reservation location's business schedule137 const selectedDate = $w('#datePicker').value;138 const dayAfterSelectedDate = new Date(selectedDate);139 dayAfterSelectedDate.setDate(selectedDate.getDate() + 1);140 const mySpecialHourPeriod = {141 "startDate": selectedDate,142 "endDate": dayAfterSelectedDate,143 "isClosed": true144 }145146 if (!("businessSchedule" in selectedLocation.configuration.onlineReservations)) {147 //Handle business schedule configuration148 }149 selectedLocation.configuration.onlineReservations.businessSchedule.specialHourPeriod.push(mySpecialHourPeriod);150151 await updateRestaurantDetails(selectedLocation._id, selectedLocation);152153 //Create a variable to hold of names and phone numbers of canceled reservations.154 let canceledReservationsNote = "Notify of cancellation:\n";155156 //Query for reservations on the selected date and cancel any at our selected reservation location157 let reservationsOnSelectedDate = await queryReservationsInDateRange(selectedDate, dayAfterSelectedDate);158 for (let reservation in reservationsOnSelectedDate._items) {159 if (reservationsOnSelectedDate._items[reservation].details.reservationLocationId == selectedLocation._id) {160 let options = {161 "phone": reservationsOnSelectedDate._items[reservation].reservee.phone162 }163164 const canceledReservation = await (async () => {165 try {166 return reservations.cancelReservation(reservationsOnSelectedDate._items[reservation]._id, reservationsOnSelectedDate._items[reservation].revision, options)167 } catch (error) {168 console.error(error);169 // Handle the error170 return false;171 }172 })();173174 if (canceledReservation) {175 canceledReservationsNote += "First name: " + reservationsOnSelectedDate._items[reservation].reservee.firstName +176 ". Last name: " + reservationsOnSelectedDate._items[reservation].reservee.lastName +177 ". Phone: " + reservationsOnSelectedDate._items[reservation].reservee.phone + "\n";178 }179 }180 }181182 $w('#cancellationNotice').text = canceledReservationsNote;183 $w('#cancellationNotice').show();184 });185});186