Search.../

queryReservations( )

Developer Preview

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.

PROPERTYSUPPORTED FILTERS & SORTING
_ideq(),ne(),in()
statuseq(),ne(),in()
details.startDateeq(),ne(),in(),lt(),gt(),le(),ge(),ascending(),descending()
Admin Method

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

Was this helpful?

Query reservations within a date range (backend)

This function call requires elevation to get the necessary permissions.

Copy Code
1import { reservations } from 'wix-table-reservations.v2';
2import { elevate } from 'wix-auth';
3import { Permissions, webMethod } from 'wix-web-module';
4
5export const myQueryReservationsFunction = webMethod(Permissions.Anyone, async () => {
6 const elevatedQueryReservations = elevate(reservations.queryReservations);
7
8 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();
13
14 const nextCursor = retrievedReservations._nextCursor;
15 const status = retrievedReservations._items[0].status;
16
17 return retrievedReservations;
18 } catch (error) {
19 console.error(error);
20 // Handle the error
21 }
22});
23
24/* 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": 2
38 * },
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": 2
57 * },
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": true
89 * }
90 * },
91 * "_limit": 50,
92 * "_nextCursor": "",
93 * "_prevCursor": "",
94 * "cursors": {
95 * "next": "",
96 * "prev": ""
97 * }
98 * }
99 */
Query reservations in a date range (dashboard page)

This function call requires additional permissions and can only be called without elevation from a dashboard page.

Copy Code
1import { myQueryReservationsFunction } from 'backend/reservations.jsw';
2
3myQueryReservationsFunction()
4 .then((retrievedReservations) => {
5 const nextCursor = retrievedReservations._nextCursor;
6 const status = retrievedReservations._items[0].status;
7
8 console.log('Success! Retrieved reservations:', retrievedReservations);
9 return retrievedReservations;
10 })
11 .catch((error) => {
12 console.error(error);
13 // Handle the error
14 });
15
16/* 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": 2
30 * },
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": 2
49 * },
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": true
81 * }
82 * },
83 * "_limit": 50,
84 * "_nextCursor": "",
85 * "_prevCursor": "",
86 * "cursors": {
87 * "next": "",
88 * "prev": ""
89 * }
90 * }
91 */
92
Close a restaurant and cancel all reservations on a given day

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.

Copy Code
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 */
7
8/**********************************************
9 * Backend code - reservationLocations.web.js *
10 **********************************************/
11
12import { 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';
16
17export const getRestaurantLocation = webMethod(Permissions.Anyone, async (location_id) => {
18 const elevatedGetLocation = elevate(locations.getLocation);
19
20 try {
21 const result = await elevatedGetLocation(location_id);
22 return result;
23 } catch (error) {
24 console.error(error);
25 // Handle the error
26 }
27});
28
29export const getRestautantDetails = webMethod(Permissions.Anyone, async (reservationLocationId, options) => {
30 const elevatedGetReservationLocations = elevate(reservationLocations.getReservationLocation);
31
32 try {
33 const result = await elevatedGetReservationLocations(reservationLocationId, options);
34 return result;
35 } catch (error) {
36 console.error(error);
37 // Handle the error
38 }
39});
40
41export const updateRestaurantDetails = webMethod(Permissions.Anyone, async (reservationLocationId, reservationLocationObject) => {
42 const elevatedUpdateReservationLocation = elevate(reservationLocations.updateReservationLocation);
43
44 try {
45 const result = await elevatedUpdateReservationLocation(reservationLocationId, reservationLocationObject);
46 return result;
47 } catch (error) {
48 console.error(error);
49 // Handle the error
50 }
51});
52
53/**************************************
54 * Backend code - reservations.web.js *
55 *************************************/
56
57import { Permissions, webMethod } from 'wix-web-module';
58import { reservations } from 'wix-table-reservations.v2';
59import { elevate } from 'wix-auth';
60
61export const queryReservationsInDateRange = webMethod(Permissions.Anyone, async (startDate, endDate) => {
62 const elevatedQueryReservations = elevate(reservations.queryReservations);
63
64 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 error
73 }
74});
75
76/*************
77 * Page code *
78 *************/
79
80import { reservationLocations } from 'wix-table-reservations.v2';
81import { reservations } from 'wix-table-reservations.v2';
82
83import { getRestaurantLocation } from 'backend/reservationLocations.web';
84import { getRestautantDetails } from 'backend/reservationLocations.web';
85import { updateRestaurantDetails } from 'backend/reservationLocations.web';
86import { queryReservationsInDateRange } from 'backend/reservations.web';
87
88$w.onReady(async function () {
89 $w('#datePicker').hide();
90 $w('#confirmButton').hide();
91 $w('#cancellationNotice').hide();
92
93 // Set the earliest selectable date to the day after the current date
94 var tomorrowsDate = new Date();
95 tomorrowsDate.setDate(tomorrowsDate.getDate() + 1);
96 $w('#datePicker').minDate = tomorrowsDate;
97 $w('#datePicker').value = tomorrowsDate;
98
99 //Get the list of reservation locations
100 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 error
107 }
108 })();
109
110 //Create an array to hold names and values for our dropdown list
111 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` service
114 const locationObject = await getRestaurantLocation(myReservationLocationList[object].location._id);
115 dropdownOptions.push({
116 "label": locationObject.name,
117 "value": myReservationLocationList[object]._id
118 })
119 }
120 //Populate our dropdown list with the array
121 $w("#locationDropdown").options = dropdownOptions;
122
123 $w('#locationDropdown').onChange((event) => {
124 if ($w('#locationDropdown').value) {
125 $w('#datePicker').show();
126 $w('#confirmButton').show();
127 }
128 });
129
130 $w('#confirmButton').onClick(async (event) => {
131 const options = {
132 "fieldsets": "FULL"
133 }
134 const selectedLocation = await getRestautantDetails($w('#locationDropdown').value, options)
135
136 //Create a new special hour period to push to the list in our reservation location's business schedule
137 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": true
144 }
145
146 if (!("businessSchedule" in selectedLocation.configuration.onlineReservations)) {
147 //Handle business schedule configuration
148 }
149 selectedLocation.configuration.onlineReservations.businessSchedule.specialHourPeriod.push(mySpecialHourPeriod);
150
151 await updateRestaurantDetails(selectedLocation._id, selectedLocation);
152
153 //Create a variable to hold of names and phone numbers of canceled reservations.
154 let canceledReservationsNote = "Notify of cancellation:\n";
155
156 //Query for reservations on the selected date and cancel any at our selected reservation location
157 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.phone
162 }
163
164 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 error
170 return false;
171 }
172 })();
173
174 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 }
181
182 $w('#cancellationNotice').text = canceledReservationsNote;
183 $w('#cancellationNotice').show();
184 });
185});
186