Setting the file. One moment.
Reservations · Wix Headless Templates · wix/skills · Skills Docs
ContentsBack to the top of the page restaurants/rest/ reservations.ts
TypeScript · 102 lines · 6 KB
12
import
{ wixRequest }
from
"../../shared/rest/client.js"
;
13 import { availableSlots, reservationBody, reserveeBody, toConfirmation, toHold, toLocations } from "../app/wix/restaurants/reservations-core.js" ;
14 import type { Raw } from "../app/wix/restaurants/menu-core.js" ;
15 import type {
16 ReservationConfirmation,
17 ReservationFormConfig,
18 ReservationHold,
19 ReservationLocationInfo,
20 ReservationReservee,
21 ReservationSlot,
22 } from "../app/wix/restaurants/types.js" ;
23
24 const RESERVATIONS = "/table-reservations/reservations/v1" ;
25
26 /**
27 * The locations to offer: those with online reservations ENABLED (default first), or only the
28 * default one when none is enabled (then `onlineReservationsEnabled: false` → render an honest
29 * notice); [] when Table Reservations isn't set up. Each carries name, address, timezone, approval
30 * rule, and the owner's form configuration.
31 * GET /table-reservations/reservation-locations/v1/reservation-locations
32 */
33 export async function fetchReservationLocations () : Promise < ReservationLocationInfo []> {
34 const res = await wixRequest < Raw >( "/table-reservations/reservation-locations/v1/reservation-locations" , { method: "GET" });
35 return toLocations (res?.reservationLocations ?? []);
36 }
37
38 /**
39 * AVAILABLE slots around a moment for a party size (UNAVAILABLE / NON_WORKING_HOURS dropped);
40 * `aroundIso` is built in the location's zone (`zonedIso`), labels and day keys use `timeZone`.
41 * POST /table-reservations/reservations/v1/time-slots { reservationLocationId, date, partySize, slotsBefore, slotsAfter }
42 */
43 export async function fetchReservationSlots (
44 locationId : string ,
45 aroundIso : string ,
46 partySize : number ,
47 { timeZone = "" , slotsBefore = 6 , slotsAfter = 6 } : { timeZone ?: string ; slotsBefore ?: number ; slotsAfter ?: number } = {},
48 ) : Promise < ReservationSlot []> {
49 const res = await wixRequest < Raw >( `${ RESERVATIONS }/time-slots` , {
50 body: { reservationLocationId: locationId, date: new Date (aroundIso). toISOString (), partySize, slotsBefore, slotsAfter },
51 });
52 return availableSlots (res?.timeSlots ?? [], timeZone);
53 }
54
55 /**
56 * Hold a slot for 10 minutes while the visitor enters their details (AUTOMATIC approval); the hold's
57 * { reservationId, revision } feed completeReservation, `expiresAtIso` a countdown.
58 * POST /table-reservations/reservations/v1/reservations/hold { reservationDetails: { reservationLocationId, startDate, partySize } }
59 */
60 export async function holdReservation ( locationId : string , startIso : string , partySize : number ) : Promise < ReservationHold > {
61 const res = await wixRequest < Raw >( `${ RESERVATIONS }/reservations/hold` , {
62 body: { reservationDetails: { reservationLocationId: locationId, startDate: new Date (startIso). toISOString (), partySize } },
63 });
64 return toHold (res, startIso, partySize);
65 }
66
67 /**
68 * Complete a held reservation with the visitor's details, validated against the location's `form`
69 * (firstName + phone always; lastName/email/custom fields as configured); an expired hold can't be
70 * reserved — start a fresh hold. The outcome carries the API's real status.
71 * POST /table-reservations/reservations/v1/reservations/{reservationId}/reserve
72 * { revision, reservee: { firstName, phone, lastName?, email?, marketingConsent?, customFields? } }
73 */
74 export async function completeReservation (
75 hold : Pick < ReservationHold , "reservationId" | "revision" >,
76 reservee : ReservationReservee ,
77 form : ReservationFormConfig ,
78 ) : Promise < ReservationConfirmation > {
79 const body = reserveeBody (reservee, form);
80 const res = await wixRequest < Raw >( `${ RESERVATIONS }/reservations/${ encodeURIComponent ( hold . reservationId ) }/reserve` , {
81 body: { revision: hold.revision, reservee: body },
82 });
83 return toConfirmation (res);
84 }
85
86 /**
87 * Request a reservation that needs the restaurant's approval (MANUAL, or a large party under
88 * MANUAL_FOR_LARGE_PARTIES): ONE create with details + reservee, no hold. Normally ends REQUESTED (pending).
89 * POST /table-reservations/reservations/v1/reservations
90 * { reservation: { details: { reservationLocationId, startDate, partySize }, reservee: { … } } }
91 */
92 export async function requestReservation (
93 locationId : string ,
94 startIso : string ,
95 partySize : number ,
96 reservee : ReservationReservee ,
97 form : ReservationFormConfig ,
98 ) : Promise < ReservationConfirmation > {
99 const reservation = reservationBody (locationId, new Date (startIso). toISOString (), partySize, reservee, form);
100 const res = await wixRequest < Raw >( `${ RESERVATIONS }/reservations` , { body: { reservation } });
101 return toConfirmation (res);
102 }