Search.../

cancelReservation( )

Developer Preview

Cancels a reservation.

Description

Sets the reservation status to CANCELED.

Syntax

function cancelReservation(reservationId: string, revision: string, options: CancelReservationOptions): Promise<CancelReservationResponse>

cancelReservation Parameters

NAME
TYPE
DESCRIPTION
reservationId
string

Reservation ID.

revision
string

Revision number.

Include the existing revision to prevent conflicting updates to reservations.

options
Optional
CancelReservationOptions

Options for canceling the reservation.

Returns

Return Type:

Promise<
CancelReservationResponse
>
NAME
TYPE
DESCRIPTION
reservation
Reservation

Reservation.

Was this helpful?

Cancel a WALK_IN reservation that has no phone number (page code)

Copy Code
1import { reservations } from 'wix-table-reservations.v2';
2
3/* Sample reservationId value: "36b144c4-9cb5-4c92-96c1-5027301cac05"
4 * Sample revision value: 1
5 */
6
7reservations.cancelReservation(reservationId, revision)
8 .then((canceledReservation) => {
9 const updatedRevision = canceledReservation.revision;
10 const status = canceledReservation.status;
11
12 console.log('Success! Canceled the reservation:', canceledReservation);
13 return canceledReservation;
14 })
15 .catch((error) => {
16 console.error(error);
17 // Handle the error
18 });
19
20/* Promise resolves to:
21 * {
22 * "reservation": {
23 * "status": "CANCELED",
24 * "source": "UNKNOWN",
25 * "details": {
26 * "reservationLocationId": "fab8cc1f-31cf-462f-b5bb-392594624bf2",
27 * "tableIds": [],
28 * "startDate": "2024-12-06T14:30:00.000Z",
29 * "endDate": "2024-12-06T16:00:00.000Z",
30 * "partySize": 2
31 * },
32 * "revision": "2",
33 * "migrationNotes": [],
34 * "tablesWithReservationConflicts": [],
35 * "_id": "36b144c4-9cb5-4c92-96c1-5027301cac05",
36 * "_createdDate": "2024-01-22T11:20:02.781Z"
37 * }
38 * }
39 */
Cancel an ONLINE reservation with a phone number (backend)

Copy Code
1import { Permissions, webMethod } from 'wix-web-module';
2import { reservations } from 'wix-table-reservations.v2';
3
4/* Sample reservationId value: "26cdab39-9fb5-45b8-867e-5f6d85541468"
5 * Sample revision value: 1
6 * Sample phone value: "+972555555555"
7 */
8
9export const myCancelReservationFunction = webMethod(Permissions.Anyone, async (reservationId, revision, phone) => {
10 try {
11 const canceledReservation = await reservations.cancelReservation(reservationId, revision, phone);
12
13 const updatedRevision = canceledReservation.revision;
14 const status = canceledReservation.status;
15
16 console.log('Success! Canceled the reservation:', canceledReservation);
17 return canceledReservation;
18 } catch (error) {
19 console.error(error);
20 // Handle the error
21 }
22
23});
24
25/* Promise resolves to:
26 * {
27 * "reservation": {
28 * "status": "CANCELED",
29 * "source": "UNKNOWN",
30 * "details": {
31 * "reservationLocationId": "fab8cc1f-31cf-462f-b5bb-392594624bf2",
32 * "tableIds": [],
33 * "startDate": "2024-12-06T14:30:00.000Z",
34 * "endDate": "2024-12-06T16:00:00.000Z",
35 * "partySize": 2
36 * },
37 * "revision": "2",
38 * "migrationNotes": [],
39 * "tablesWithReservationConflicts": [],
40 * "_id": "26cdab39-9fb5-45b8-867e-5f6d85541468",
41 * "_createdDate": "2024-01-22T11:24:56.658Z"
42 * }
43 * }
44 */
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