@wix/agent-skills 1.20.0 → 1.21.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/skills/wix-app/SKILL.md +14 -112
- package/skills/wix-app/references/APP_TOOLS.md +2 -1
- package/skills/wix-app/references/BACKEND_EVENT.md +3 -3
- package/skills/wix-app/references/CUSTOM_ELEMENT_WIDGET.md +41 -152
- package/skills/wix-app/references/DASHBOARD_MODAL.md +4 -4
- package/skills/wix-app/references/SDK_MODULE_MAP.md +44 -0
- package/skills/wix-app/references/SITE_PLUGIN.md +36 -190
- package/skills/wix-app/references/app-tools/TOOLS.md +3 -1
- package/skills/wix-app/references/custom-element-widget/SETTINGS_PANEL.md +21 -0
- package/skills/wix-app/references/dashboard-page/ECOM_NAVIGATION.md +17 -0
- package/skills/wix-app/references/dashboard-plugin/SLOTS.md +4 -18
- package/skills/wix-app/references/site-plugin/EXAMPLES.md +1 -1
- package/skills/wix-headless-fast/SKILL.md +302 -51
- package/skills/wix-headless-fast/entry/skill.md +11 -2
- package/skills/wix-headless-fast/install/AGENTS.md +32 -0
- package/skills/wix-headless-fast/install/agents-md.mjs +39 -0
- package/skills/wix-headless-fast/install/attach.mjs +360 -0
- package/skills/wix-headless-fast/install/deploy.mjs +109 -9
- package/skills/wix-headless-fast/install/setup.mjs +239 -0
- package/skills/wix-headless-fast/references/blog/INSTRUCTIONS.md +226 -60
- package/skills/wix-headless-fast/references/blog/app/components/blog/BlogFeedView.tsx +2 -3
- package/skills/wix-headless-fast/references/blog/app/components/blog/PostView.tsx +2 -3
- package/skills/wix-headless-fast/references/blog/app/hooks/blog/useBlogFeed.ts +24 -116
- package/skills/wix-headless-fast/references/blog/app/hooks/blog/usePost.ts +16 -64
- package/skills/wix-headless-fast/references/blog/app/wix/blog/blog-feed-store.ts +137 -0
- package/skills/wix-headless-fast/references/blog/app/wix/blog/post-store.ts +78 -0
- package/skills/wix-headless-fast/references/blog/app/wix/blog/posts-core.ts +117 -0
- package/skills/wix-headless-fast/references/blog/app/wix/blog/posts.ts +13 -60
- package/skills/wix-headless-fast/references/blog/app/wix/blog/taxonomy-core.ts +47 -0
- package/skills/wix-headless-fast/references/blog/app/wix/blog/taxonomy.ts +10 -28
- package/skills/wix-headless-fast/references/blog/app-astro/pages/blog/[...slug].astro +2 -4
- package/skills/wix-headless-fast/references/blog/rest/posts.ts +42 -0
- package/skills/wix-headless-fast/references/blog/rest/taxonomy.ts +62 -0
- package/skills/wix-headless-fast/references/blog/seed/SEED.md +9 -0
- package/skills/wix-headless-fast/references/blog/seed/read-site.mjs +22 -0
- package/skills/wix-headless-fast/references/bookings/INSTRUCTIONS.md +181 -33
- package/skills/wix-headless-fast/references/bookings/app/components/bookings/ServiceBookingView.tsx +2 -1
- package/skills/wix-headless-fast/references/bookings/app/hooks/bookings/useBookingFlow.ts +26 -132
- package/skills/wix-headless-fast/references/bookings/app/hooks/bookings/useServices.ts +18 -49
- package/skills/wix-headless-fast/references/bookings/app/wix/bookings/booking-core.ts +218 -0
- package/skills/wix-headless-fast/references/bookings/app/wix/bookings/booking-flow-store.ts +169 -0
- package/skills/wix-headless-fast/references/bookings/app/wix/bookings/booking.ts +57 -142
- package/skills/wix-headless-fast/references/bookings/app/wix/bookings/services-core.ts +92 -0
- package/skills/wix-headless-fast/references/bookings/app/wix/bookings/services-store.ts +90 -0
- package/skills/wix-headless-fast/references/bookings/app/wix/bookings/services.ts +22 -61
- package/skills/wix-headless-fast/references/bookings/app/wix/bookings/types.ts +3 -1
- package/skills/wix-headless-fast/references/bookings/rest/booking.ts +113 -0
- package/skills/wix-headless-fast/references/bookings/rest/services.ts +55 -0
- package/skills/wix-headless-fast/references/bookings/seed/SEED.md +10 -0
- package/skills/wix-headless-fast/references/bookings/seed/read-site.mjs +28 -0
- package/skills/wix-headless-fast/references/cms/INSTRUCTIONS.md +238 -62
- package/skills/wix-headless-fast/references/cms/app/hooks/cms/useCollection.ts +19 -52
- package/skills/wix-headless-fast/references/cms/app/hooks/cms/useItem.ts +16 -36
- package/skills/wix-headless-fast/references/cms/app/wix/cms/collection-store.ts +158 -0
- package/skills/wix-headless-fast/references/cms/app/wix/cms/item-store.ts +93 -0
- package/skills/wix-headless-fast/references/cms/app/wix/cms/items-core.ts +177 -0
- package/skills/wix-headless-fast/references/cms/app/wix/cms/items.ts +13 -41
- package/skills/wix-headless-fast/references/cms/rest/items.ts +125 -0
- package/skills/wix-headless-fast/references/cms/seed/SEED.md +9 -0
- package/skills/wix-headless-fast/references/cms/seed/read-site.mjs +20 -0
- package/skills/wix-headless-fast/references/events/INSTRUCTIONS.md +241 -83
- package/skills/wix-headless-fast/references/events/app/components/events/EventConfirmationView.tsx +1 -1
- package/skills/wix-headless-fast/references/events/app/components/events/EventRegistrationView.tsx +7 -5
- package/skills/wix-headless-fast/references/events/app/components/events/EventsView.tsx +2 -2
- package/skills/wix-headless-fast/references/events/app/hooks/events/useEventRegistration.ts +28 -110
- package/skills/wix-headless-fast/references/events/app/hooks/events/useEvents.ts +19 -54
- package/skills/wix-headless-fast/references/events/app/wix/events/events-core.ts +130 -0
- package/skills/wix-headless-fast/references/events/app/wix/events/events-store.ts +97 -0
- package/skills/wix-headless-fast/references/events/app/wix/events/events.ts +13 -94
- package/skills/wix-headless-fast/references/events/app/wix/events/registration-core.ts +113 -0
- package/skills/wix-headless-fast/references/events/app/wix/events/registration-store.ts +156 -0
- package/skills/wix-headless-fast/references/events/app/wix/events/registration.ts +38 -69
- package/skills/wix-headless-fast/references/events/rest/events.ts +57 -0
- package/skills/wix-headless-fast/references/events/rest/registration.ts +83 -0
- package/skills/wix-headless-fast/references/events/seed/SEED.md +9 -0
- package/skills/wix-headless-fast/references/events/seed/read-site.mjs +26 -0
- package/skills/wix-headless-fast/references/forms/INSTRUCTIONS.md +216 -26
- package/skills/wix-headless-fast/references/forms/app/hooks/forms/useWixForm.ts +35 -267
- package/skills/wix-headless-fast/references/forms/app/wix/forms/form-store.ts +332 -0
- package/skills/wix-headless-fast/references/forms/app/wix/forms/forms-core.ts +238 -0
- package/skills/wix-headless-fast/references/forms/app/wix/forms/forms.ts +28 -243
- package/skills/wix-headless-fast/references/forms/app/wix/forms/submissions-core.ts +216 -0
- package/skills/wix-headless-fast/references/forms/app/wix/forms/submissions.ts +32 -204
- package/skills/wix-headless-fast/references/forms/rest/forms.ts +34 -0
- package/skills/wix-headless-fast/references/forms/rest/submissions.ts +56 -0
- package/skills/wix-headless-fast/references/forms/seed/SEED.md +8 -0
- package/skills/wix-headless-fast/references/forms/seed/read-site.mjs +20 -0
- package/skills/wix-headless-fast/references/forms/seed/seed-forms.mjs +15 -8
- package/skills/wix-headless-fast/references/members/INSTRUCTIONS.md +316 -63
- package/skills/wix-headless-fast/references/members/app/components/members/LoginForm.tsx +1 -1
- package/skills/wix-headless-fast/references/members/app/wix/members/auth-core.ts +105 -0
- package/skills/wix-headless-fast/references/members/app/wix/members/auth.ts +36 -65
- package/skills/wix-headless-fast/references/members/app/wix/members/client.ts +1 -1
- package/skills/wix-headless-fast/references/members/app/wix/members/members-core.ts +43 -0
- package/skills/wix-headless-fast/references/members/app/wix/members/members.ts +11 -32
- package/skills/wix-headless-fast/references/members/rest/auth.ts +215 -0
- package/skills/wix-headless-fast/references/members/rest/members.ts +26 -0
- package/skills/wix-headless-fast/references/members/seed/read-site.mjs +15 -0
- package/skills/wix-headless-fast/references/portfolio/INSTRUCTIONS.md +240 -59
- package/skills/wix-headless-fast/references/portfolio/app/components/portfolio/CollectionProjectsView.tsx +2 -2
- package/skills/wix-headless-fast/references/portfolio/app/components/portfolio/CollectionsView.tsx +2 -2
- package/skills/wix-headless-fast/references/portfolio/app/components/portfolio/GalleryMedia.tsx +35 -0
- package/skills/wix-headless-fast/references/portfolio/app/components/portfolio/ProjectDetailView.tsx +8 -38
- package/skills/wix-headless-fast/references/portfolio/app/hooks/portfolio/useCollectionProjects.ts +23 -50
- package/skills/wix-headless-fast/references/portfolio/app/hooks/portfolio/useCollections.ts +16 -34
- package/skills/wix-headless-fast/references/portfolio/app/hooks/portfolio/useProjectDetail.ts +22 -49
- package/skills/wix-headless-fast/references/portfolio/app/wix/portfolio/collection-projects-store.ts +82 -0
- package/skills/wix-headless-fast/references/portfolio/app/wix/portfolio/collections-store.ts +58 -0
- package/skills/wix-headless-fast/references/portfolio/app/wix/portfolio/portfolio-core.ts +177 -0
- package/skills/wix-headless-fast/references/portfolio/app/wix/portfolio/portfolio.ts +31 -118
- package/skills/wix-headless-fast/references/portfolio/app/wix/portfolio/project-detail-store.ts +81 -0
- package/skills/wix-headless-fast/references/portfolio/rest/portfolio.ts +75 -0
- package/skills/wix-headless-fast/references/portfolio/seed/SEED.md +8 -0
- package/skills/wix-headless-fast/references/portfolio/seed/read-site.mjs +20 -0
- package/skills/wix-headless-fast/references/portfolio/seed/seed-portfolio.mjs +19 -21
- package/skills/wix-headless-fast/references/pricing-plans/INSTRUCTIONS.md +214 -51
- package/skills/wix-headless-fast/references/pricing-plans/app/components/pricing-plans/PlanDetailView.tsx +9 -20
- package/skills/wix-headless-fast/references/pricing-plans/app/components/pricing-plans/PlansView.tsx +7 -32
- package/skills/wix-headless-fast/references/pricing-plans/app/components/pricing-plans/SubscribeButton.tsx +41 -0
- package/skills/wix-headless-fast/references/pricing-plans/app/hooks/pricing-plans/usePlanPurchase.ts +12 -33
- package/skills/wix-headless-fast/references/pricing-plans/app/hooks/pricing-plans/usePlans.ts +17 -34
- package/skills/wix-headless-fast/references/pricing-plans/app/wix/pricing-plans/plan-purchase-store.ts +54 -0
- package/skills/wix-headless-fast/references/pricing-plans/app/wix/pricing-plans/plans-core.ts +106 -0
- package/skills/wix-headless-fast/references/pricing-plans/app/wix/pricing-plans/plans-store.ts +64 -0
- package/skills/wix-headless-fast/references/pricing-plans/app/wix/pricing-plans/plans.ts +7 -55
- package/skills/wix-headless-fast/references/pricing-plans/app/wix/pricing-plans/purchase-core.ts +43 -0
- package/skills/wix-headless-fast/references/pricing-plans/app/wix/pricing-plans/purchase.ts +11 -34
- package/skills/wix-headless-fast/references/pricing-plans/rest/plans.ts +31 -0
- package/skills/wix-headless-fast/references/pricing-plans/rest/purchase.ts +20 -0
- package/skills/wix-headless-fast/references/pricing-plans/seed/SEED.md +8 -0
- package/skills/wix-headless-fast/references/pricing-plans/seed/read-site.mjs +21 -0
- package/skills/wix-headless-fast/references/restaurants/INSTRUCTIONS.md +242 -47
- package/skills/wix-headless-fast/references/restaurants/app/components/restaurants/MenuView.tsx +2 -2
- package/skills/wix-headless-fast/references/restaurants/app/components/restaurants/OrderCartDrawer.tsx +2 -2
- package/skills/wix-headless-fast/references/restaurants/app/hooks/restaurants/useMenus.ts +14 -35
- package/skills/wix-headless-fast/references/restaurants/app/hooks/restaurants/useReservation.ts +28 -171
- package/skills/wix-headless-fast/references/restaurants/app/wix/restaurants/menu-core.ts +155 -0
- package/skills/wix-headless-fast/references/restaurants/app/wix/restaurants/menu-store.ts +89 -0
- package/skills/wix-headless-fast/references/restaurants/app/wix/restaurants/menu.ts +23 -117
- package/skills/wix-headless-fast/references/restaurants/app/wix/restaurants/ordering-core.ts +124 -0
- package/skills/wix-headless-fast/references/restaurants/app/wix/restaurants/ordering.ts +45 -114
- package/skills/wix-headless-fast/references/restaurants/app/wix/restaurants/reservation-store.ts +193 -0
- package/skills/wix-headless-fast/references/restaurants/app/wix/restaurants/reservations-core.ts +94 -0
- package/skills/wix-headless-fast/references/restaurants/app/wix/restaurants/reservations.ts +15 -87
- package/skills/wix-headless-fast/references/restaurants/rest/menu.ts +60 -0
- package/skills/wix-headless-fast/references/restaurants/rest/ordering.ts +169 -0
- package/skills/wix-headless-fast/references/restaurants/rest/reservations.ts +68 -0
- package/skills/wix-headless-fast/references/restaurants/seed/SEED.md +8 -0
- package/skills/wix-headless-fast/references/restaurants/seed/read-site.mjs +25 -0
- package/skills/wix-headless-fast/references/restaurants/seed/seed-restaurants.mjs +25 -5
- package/skills/wix-headless-fast/references/shared/SUPPLIED-CONTENT.md +23 -0
- package/skills/wix-headless-fast/references/shared/app/wix/media.ts +45 -4
- package/skills/wix-headless-fast/references/shared/app/wix/sdk.ts +13 -0
- package/skills/wix-headless-fast/references/shared/rest/client.ts +155 -0
- package/skills/wix-headless-fast/references/shared/rest/config.ts +3 -0
- package/skills/wix-headless-fast/references/shared/rest/media.ts +65 -0
- package/skills/wix-headless-fast/references/shared/seed/read-site.mjs +99 -0
- package/skills/wix-headless-fast/references/storefront/INSTRUCTIONS.md +186 -63
- package/skills/wix-headless-fast/references/storefront/app/components/storefront/OptionPicker.tsx +95 -0
- package/skills/wix-headless-fast/references/storefront/app/components/storefront/QuickAdd.tsx +29 -58
- package/skills/wix-headless-fast/references/storefront/app/hooks/storefront/useProductDetail.ts +29 -163
- package/skills/wix-headless-fast/references/storefront/app/hooks/storefront/useShop.ts +26 -300
- package/skills/wix-headless-fast/references/storefront/app/wix/storefront/cart-core.ts +121 -0
- package/skills/wix-headless-fast/references/storefront/app/wix/storefront/cart.ts +49 -162
- package/skills/wix-headless-fast/references/storefront/app/wix/storefront/catalog-core.ts +314 -0
- package/skills/wix-headless-fast/references/storefront/app/wix/storefront/catalog.ts +47 -370
- package/skills/wix-headless-fast/references/storefront/app/wix/storefront/product-detail-store.ts +147 -0
- package/skills/wix-headless-fast/references/storefront/app/wix/storefront/shop-store.ts +289 -0
- package/skills/wix-headless-fast/references/storefront/app/wix/storefront/types.ts +2 -0
- package/skills/wix-headless-fast/references/storefront/rest/cart.ts +132 -0
- package/skills/wix-headless-fast/references/storefront/rest/catalog.ts +123 -0
- package/skills/wix-headless-fast/references/storefront/seed/SEED.md +31 -9
- package/skills/wix-headless-fast/references/storefront/seed/read-site.mjs +38 -0
- package/skills/wix-headless-fast/references/storefront/seed/seed-store.mjs +57 -50
- package/skills/wix-manage/references/ecommerce/ecom-shipping.md +1 -1
- package/skills/wix-manage/references/ecommerce/pricing-promotions/ecom-pricing-create-coupon.md +10 -19
- package/skills/wix-manage/references/events/create-wix-event.md +5 -1
- package/skills/wix-vibe-headless/references/storefront/INSTRUCTIONS.md +0 -40
- package/skills/wix-vibe-headless/references/storefront/seed/SEED.md +3 -0
- package/skills/wix-vibe-headless/references/storefront/seed/seed-store.cjs +6 -0
- package/skills/wix-headless-fast/install/fast-path.mjs +0 -186
- package/skills/wix-headless-fast/install/pin-agents-md.mjs +0 -57
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
// Booking rules, request builders, and DTO mapping — transport-agnostic, imported by BOTH transports:
|
|
2
|
+
// ./booking.ts (the SDK) and the REST twin in references/bookings/rest/booking.ts (fetch). The
|
|
3
|
+
// availability request bodies, the slot mapper, the form-field rules, the createBooking body, the
|
|
4
|
+
// cart body, and the checkout-or-place decision live HERE, once — these are the payloads that are
|
|
5
|
+
// exact and easy to get subtly wrong. Raw entities may carry `_id` (SDK) or `id` (REST). Imports are
|
|
6
|
+
// type-only so a strip to JS emits no imports.
|
|
7
|
+
import type { BookingFormField, BookingResult, ServiceDetail, Slot } from "./types";
|
|
8
|
+
|
|
9
|
+
export type Raw = Record<string, any>;
|
|
10
|
+
|
|
11
|
+
export const rawId = (raw: Raw | undefined | null): string => raw?._id ?? raw?.id ?? "";
|
|
12
|
+
|
|
13
|
+
// Copies of services-core's ids (same values) so this file stands alone when stripped.
|
|
14
|
+
/** The Wix Bookings app id — the cart line's catalogReference.appId. */
|
|
15
|
+
export const BOOKINGS_APP_ID = "13d21c63-b5ec-5912-8397-c3a5ddb27a97";
|
|
16
|
+
/** Staff-member resource type id (ANY_RESOURCE fallback + staff filtering). */
|
|
17
|
+
export const STAFF_RESOURCE_TYPE_ID = "1cd44cf8-756f-41c3-bd90-3e2ffcaf1155";
|
|
18
|
+
|
|
19
|
+
const pad = (n: number) => String(n).padStart(2, "0");
|
|
20
|
+
/** Local wall-clock "YYYY-MM-DDThh:mm:ss" (NO Z) — the format the availability APIs require. */
|
|
21
|
+
export function toLocalDateString(d: Date): string {
|
|
22
|
+
return `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())}T${pad(d.getHours())}:${pad(d.getMinutes())}:${pad(d.getSeconds())}`;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** The visitor's IANA time zone — availability and the booking are computed in it. */
|
|
26
|
+
export const defaultTimeZone = (): string => Intl.DateTimeFormat().resolvedOptions().timeZone;
|
|
27
|
+
|
|
28
|
+
// ---- availability ----------------------------------------------------------------------------------
|
|
29
|
+
|
|
30
|
+
export interface SlotsWindow {
|
|
31
|
+
from?: Date;
|
|
32
|
+
days?: number;
|
|
33
|
+
timeZone?: string;
|
|
34
|
+
staffId?: string;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** [from, from + days) as the local strings both time-slot APIs take. */
|
|
38
|
+
export function slotsRange({ from = new Date(), days = 7 }: SlotsWindow): { fromLocalDate: string; toLocalDate: string } {
|
|
39
|
+
const to = new Date(from.getTime() + days * 24 * 3600 * 1000);
|
|
40
|
+
return { fromLocalDate: toLocalDateString(from), toLocalDate: toLocalDateString(to) };
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* The List Availability Time Slots body for an APPOINTMENT service: bookable slots only, the staff
|
|
45
|
+
* resources included on each slot, optionally narrowed to one staff member.
|
|
46
|
+
*/
|
|
47
|
+
export function appointmentSlotsRequest(serviceId: string, { timeZone = defaultTimeZone(), staffId, ...window }: SlotsWindow): Raw {
|
|
48
|
+
return {
|
|
49
|
+
serviceId,
|
|
50
|
+
...slotsRange(window),
|
|
51
|
+
timeZone,
|
|
52
|
+
bookable: true,
|
|
53
|
+
cursorPaging: { limit: 100 },
|
|
54
|
+
includeResourceTypeIds: [STAFF_RESOURCE_TYPE_ID],
|
|
55
|
+
...(staffId ? { resourceTypes: [{ resourceTypeId: STAFF_RESOURCE_TYPE_ID, resourceIds: [staffId] }] } : {}),
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** The List Event Time Slots body for a CLASS service: its sessions, bookable only, optionally one staff member's. */
|
|
60
|
+
export function classSlotsRequest(serviceId: string, { timeZone = defaultTimeZone(), staffId, ...window }: SlotsWindow): Raw {
|
|
61
|
+
return {
|
|
62
|
+
serviceIds: [serviceId],
|
|
63
|
+
...slotsRange(window),
|
|
64
|
+
timeZone,
|
|
65
|
+
includeNonBookable: false,
|
|
66
|
+
...(staffId ? { eventFilter: { "resources.id": { $hasSome: [staffId] } } } : {}),
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export function toSlot(raw: Raw): Slot {
|
|
71
|
+
const start: string = raw.localStartDate ?? "";
|
|
72
|
+
const staff = ((raw.availableResources ?? []) as Raw[])
|
|
73
|
+
.flatMap((ar) => (ar.resources ?? []) as Raw[])
|
|
74
|
+
.map((r) => ({ id: rawId(r), name: r.name ?? "" }))
|
|
75
|
+
.filter((r) => r.id);
|
|
76
|
+
let label = start.slice(11, 16);
|
|
77
|
+
try {
|
|
78
|
+
label = new Date(start).toLocaleTimeString(undefined, { hour: "numeric", minute: "2-digit" });
|
|
79
|
+
} catch {
|
|
80
|
+
/* keep the hh:mm fallback */
|
|
81
|
+
}
|
|
82
|
+
return {
|
|
83
|
+
startLocal: start,
|
|
84
|
+
endLocal: raw.localEndDate ?? "",
|
|
85
|
+
dayKey: start.slice(0, 10),
|
|
86
|
+
label,
|
|
87
|
+
scheduleId: raw.scheduleId ?? null,
|
|
88
|
+
eventId: raw.eventInfo?.eventId ?? null,
|
|
89
|
+
staff,
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export interface BookingDay {
|
|
94
|
+
dayKey: string;
|
|
95
|
+
dayLabel: string;
|
|
96
|
+
slots: Slot[];
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export const dayLabel = (dayKey: string): string => {
|
|
100
|
+
try {
|
|
101
|
+
return new Date(`${dayKey}T12:00:00`).toLocaleDateString(undefined, { weekday: "short", month: "short", day: "numeric" });
|
|
102
|
+
} catch {
|
|
103
|
+
return dayKey;
|
|
104
|
+
}
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
/** Slots grouped by day, days in order — what a slot picker renders. */
|
|
108
|
+
export function groupSlotsByDay(slots: Slot[]): BookingDay[] {
|
|
109
|
+
const byDay = new Map<string, Slot[]>();
|
|
110
|
+
for (const s of slots) byDay.set(s.dayKey, [...(byDay.get(s.dayKey) ?? []), s]);
|
|
111
|
+
return [...byDay.entries()]
|
|
112
|
+
.sort(([a], [b]) => a.localeCompare(b))
|
|
113
|
+
.map(([dayKey, daySlots]) => ({ dayKey, dayLabel: dayLabel(dayKey), slots: daySlots }));
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// ---- the booking form -------------------------------------------------------------------------------
|
|
117
|
+
|
|
118
|
+
export const FALLBACK_FIELDS: BookingFormField[] = [
|
|
119
|
+
{ target: "first_name", label: "First Name", type: "STRING", required: true },
|
|
120
|
+
{ target: "last_name", label: "Last Name", type: "STRING", required: true },
|
|
121
|
+
{ target: "email", label: "Email", type: "EMAIL", required: true },
|
|
122
|
+
];
|
|
123
|
+
|
|
124
|
+
const FIELD_TYPES = ["STRING", "EMAIL", "PHONE", "NUMBER", "URL"];
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Flat, render-ready fields from a form summary — deleted fields and non-text types dropped, values
|
|
128
|
+
* keyed by `target`. `required` comes from the full form schema (`fields[].validation.required`,
|
|
129
|
+
* keyed by target); the summary doesn't carry it. Without the schema every field counts as required.
|
|
130
|
+
* ALWAYS non-empty: contact basics when the schema is missing or unusable.
|
|
131
|
+
*/
|
|
132
|
+
export function toFormFields(summary: Raw | null | undefined, form?: Raw | null): BookingFormField[] {
|
|
133
|
+
const requiredByTarget = new Map<string, boolean>(
|
|
134
|
+
((form?.fields ?? []) as Raw[]).filter((f) => f.target).map((f) => [f.target as string, f.validation?.required === true]),
|
|
135
|
+
);
|
|
136
|
+
const fields = ((summary?.fields ?? []) as Raw[])
|
|
137
|
+
.filter((f) => !f.deleted)
|
|
138
|
+
.filter((f) => f.type && FIELD_TYPES.includes(f.type))
|
|
139
|
+
.map((f) => ({
|
|
140
|
+
target: f.target ?? "",
|
|
141
|
+
label: f.label ?? f.target ?? "",
|
|
142
|
+
type: f.type as BookingFormField["type"],
|
|
143
|
+
...(Array.isArray(f.options) && f.options.length ? { options: f.options as string[] } : {}),
|
|
144
|
+
required: requiredByTarget.get(f.target ?? "") ?? true,
|
|
145
|
+
}))
|
|
146
|
+
.filter((f) => f.target);
|
|
147
|
+
return fields.length ? fields : FALLBACK_FIELDS;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// ---- createBooking → cart → checkout-or-place --------------------------------------------------------
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* The Create Booking `booking` object. APPOINTMENT: the SLOT's scheduleId (never service.schedule.id)
|
|
154
|
+
* plus start/end as local wall-clock with the time zone; CLASS: the eventId. A chosen staff member
|
|
155
|
+
* is the slot's `resource`; otherwise ANY_RESOURCE of the staff type. `idKey` spells the resource's
|
|
156
|
+
* id the way the transport wants it: `_id` on the SDK, `id` on REST.
|
|
157
|
+
*/
|
|
158
|
+
export function bookingRequest(
|
|
159
|
+
service: Pick<ServiceDetail, "id" | "paymentOption">,
|
|
160
|
+
slot: Slot,
|
|
161
|
+
{ staffId, timeZone = defaultTimeZone(), idKey = "id" }: { staffId?: string; timeZone?: string; idKey?: "_id" | "id" } = {},
|
|
162
|
+
): Raw {
|
|
163
|
+
const staff = staffId ? slot.staff.find((s) => s.id === staffId) : undefined;
|
|
164
|
+
return {
|
|
165
|
+
selectedPaymentOption: service.paymentOption,
|
|
166
|
+
totalParticipants: 1,
|
|
167
|
+
bookedEntity: {
|
|
168
|
+
slot: {
|
|
169
|
+
serviceId: service.id,
|
|
170
|
+
scheduleId: slot.scheduleId ?? undefined,
|
|
171
|
+
eventId: slot.eventId ?? undefined,
|
|
172
|
+
startDate: slot.startLocal,
|
|
173
|
+
endDate: slot.endLocal,
|
|
174
|
+
timezone: timeZone,
|
|
175
|
+
...(staff
|
|
176
|
+
? { resource: { [idKey]: staff.id, name: staff.name } }
|
|
177
|
+
: { resourceSelections: [{ resourceTypeId: STAFF_RESOURCE_TYPE_ID, selectionMethod: "ANY_RESOURCE" }] }),
|
|
178
|
+
location: { locationType: "OWNER_BUSINESS" },
|
|
179
|
+
},
|
|
180
|
+
},
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/** The Create Cart body that holds the booked seat: one line whose catalogItemId is the BOOKING id. */
|
|
185
|
+
export function bookingCartRequest(bookingId: string): Raw {
|
|
186
|
+
return {
|
|
187
|
+
catalogItems: [{ quantity: 1, catalogReference: { catalogItemId: bookingId, appId: BOOKINGS_APP_ID } }],
|
|
188
|
+
cart: { source: { channelType: "WEB" } },
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/** The booking id out of a Create Booking response (wrapped { booking } on both transports). */
|
|
193
|
+
export const bookingIdOf = (res: Raw | null | undefined): string => rawId(res?.booking);
|
|
194
|
+
|
|
195
|
+
/** The cart id out of a Create Cart response — the SDK may unwrap the cart, REST wraps it. */
|
|
196
|
+
export const cartIdOf = (res: Raw | null | undefined): string => rawId(res) || rawId(res?.cart);
|
|
197
|
+
|
|
198
|
+
/** The cart's total from a Calculate Cart response (`summary.priceSummary.total`); 0 when absent. */
|
|
199
|
+
export const cartTotal = (calc: Raw | null | undefined): number => Number(calc?.summary?.priceSummary?.total?.amount ?? 0);
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* Hosted checkout is required for anything that costs money paid online, and always when the
|
|
203
|
+
* service carries a cancellation fee (a card must be on file). Otherwise the order is placed
|
|
204
|
+
* directly — free and pay-in-person bookings confirm without a checkout.
|
|
205
|
+
*/
|
|
206
|
+
export function checkoutRequired(service: Pick<ServiceDetail, "paymentOption" | "cancellationFeeEnabled">, total: number): boolean {
|
|
207
|
+
return service.cancellationFeeEnabled || (total > 0 && service.paymentOption !== "OFFLINE");
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/** The Create Redirect Session body for a cart's checkout; the cart id IS the checkout id. */
|
|
211
|
+
export function checkoutRedirectRequest(cartId: string, origin: string): Raw {
|
|
212
|
+
return { ecomCheckout: { checkoutId: cartId }, callbacks: origin ? { postFlowUrl: `${origin}/` } : {} };
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/** A placed order → the confirmed result; the order id is wherever the transport put it. */
|
|
216
|
+
export function confirmedResult(bookingId: string, order: Raw | null | undefined): BookingResult {
|
|
217
|
+
return { kind: "confirmed", bookingId, orderId: order?.orderId ?? (rawId(order?.order) || null) };
|
|
218
|
+
}
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
// The whole booking state machine for one service as a framework-free store — the logic behind
|
|
2
|
+
// useBookingFlow, usable from React (useBookingFlow wraps it), from a static page's booking surface,
|
|
3
|
+
// from Vue/Svelte, or as the specification for a port: the availability window (day-grouped slots,
|
|
4
|
+
// week paging, optional staff filter), the schema-driven form, and book(). All correctness (slot
|
|
5
|
+
// scheduleId vs eventId, ANY_RESOURCE, formSubmission, payment option, checkout-or-place) lives in
|
|
6
|
+
// the data layer; this store orchestrates. A late response from a superseded window is dropped.
|
|
7
|
+
// One store per booking surface: createBookingFlowStore(service), not a singleton.
|
|
8
|
+
import { bookService, fetchBookingForm, fetchSlots } from "./booking";
|
|
9
|
+
import { groupSlotsByDay, type BookingDay } from "./booking-core";
|
|
10
|
+
import type { BookingFormField, BookingResult, ServiceDetail, Slot } from "./types";
|
|
11
|
+
|
|
12
|
+
export type { BookingDay };
|
|
13
|
+
|
|
14
|
+
const WEEK = 7 * 24 * 3600 * 1000;
|
|
15
|
+
|
|
16
|
+
/** Everything a booking surface renders from. Read it with getState() or through a subscription. */
|
|
17
|
+
export interface BookingFlowState {
|
|
18
|
+
/** Slots grouped by day, in order — [{ dayKey, dayLabel, slots }]. null while loading. */
|
|
19
|
+
days: BookingDay[] | null;
|
|
20
|
+
/** The 7-day window start; page with nextWeek/prevWeek (prev clamps to today). */
|
|
21
|
+
windowStart: Date;
|
|
22
|
+
/** Staff filter — render a picker only when service.staff.length > 1. */
|
|
23
|
+
staffId: string | undefined;
|
|
24
|
+
selectedSlot: Slot | null;
|
|
25
|
+
/** Schema-driven form fields (never empty — contact basics fallback). */
|
|
26
|
+
formFields: BookingFormField[];
|
|
27
|
+
values: Record<string, string>;
|
|
28
|
+
/** True when a slot is selected and every field has a value. */
|
|
29
|
+
canBook: boolean;
|
|
30
|
+
booking: boolean;
|
|
31
|
+
/** Set after a free/offline booking completes — the only REAL success signal. */
|
|
32
|
+
confirmed: BookingResult | null;
|
|
33
|
+
error: string | null;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export interface BookingFlowStore {
|
|
37
|
+
getState(): BookingFlowState;
|
|
38
|
+
subscribe(listener: () => void): () => void;
|
|
39
|
+
/** Load the first window and the form. Call once when mounted (a browser — availability is timezone-specific). */
|
|
40
|
+
start(): void;
|
|
41
|
+
/** Stop reacting; drop late responses. */
|
|
42
|
+
stop(): void;
|
|
43
|
+
nextWeek(): void;
|
|
44
|
+
prevWeek(): void;
|
|
45
|
+
setStaffId(id: string | undefined): void;
|
|
46
|
+
setSelectedSlot(slot: Slot | null): void;
|
|
47
|
+
setValue(target: string, value: string): void;
|
|
48
|
+
/** Books the selected slot. On "redirect" the browser is already navigating; otherwise `confirmed` is set. Rejects with the refusal (also in .error). */
|
|
49
|
+
book(): Promise<BookingResult>;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export function createBookingFlowStore(service: ServiceDetail): BookingFlowStore {
|
|
53
|
+
let windowStart = new Date();
|
|
54
|
+
let staffId: string | undefined;
|
|
55
|
+
let slots: Slot[] | null = null;
|
|
56
|
+
let selectedSlot: Slot | null = null;
|
|
57
|
+
let formFields: BookingFormField[] = [];
|
|
58
|
+
let values: Record<string, string> = {};
|
|
59
|
+
let booking = false;
|
|
60
|
+
let confirmed: BookingResult | null = null;
|
|
61
|
+
let error: string | null = null;
|
|
62
|
+
let started = false;
|
|
63
|
+
let generation = 0;
|
|
64
|
+
const listeners = new Set<() => void>();
|
|
65
|
+
let snapshot: BookingFlowState | null = null;
|
|
66
|
+
const emit = () => {
|
|
67
|
+
snapshot = null;
|
|
68
|
+
for (const fn of listeners) fn();
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
function getState(): BookingFlowState {
|
|
72
|
+
if (snapshot) return snapshot;
|
|
73
|
+
snapshot = {
|
|
74
|
+
days: slots === null ? null : groupSlotsByDay(slots),
|
|
75
|
+
windowStart,
|
|
76
|
+
staffId,
|
|
77
|
+
selectedSlot,
|
|
78
|
+
formFields,
|
|
79
|
+
values,
|
|
80
|
+
canBook: !!selectedSlot && formFields.every((f) => !f.required || (values[f.target] ?? "").trim().length > 0),
|
|
81
|
+
booking,
|
|
82
|
+
confirmed,
|
|
83
|
+
error,
|
|
84
|
+
};
|
|
85
|
+
return snapshot;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// The window or the staff filter changed: clear the picker and load the slots for the new selection.
|
|
89
|
+
function loadSlots(): void {
|
|
90
|
+
if (!started) return;
|
|
91
|
+
const id = ++generation;
|
|
92
|
+
slots = null;
|
|
93
|
+
selectedSlot = null;
|
|
94
|
+
emit();
|
|
95
|
+
fetchSlots(service, { from: windowStart, days: 7, staffId })
|
|
96
|
+
.then((s) => {
|
|
97
|
+
if (!started || generation !== id) return; // superseded — drop it
|
|
98
|
+
slots = s;
|
|
99
|
+
emit();
|
|
100
|
+
})
|
|
101
|
+
.catch((e) => {
|
|
102
|
+
if (!started || generation !== id) return;
|
|
103
|
+
slots = [];
|
|
104
|
+
error = e instanceof Error ? e.message : String(e);
|
|
105
|
+
emit();
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
return {
|
|
110
|
+
getState,
|
|
111
|
+
subscribe(fn) {
|
|
112
|
+
listeners.add(fn);
|
|
113
|
+
return () => listeners.delete(fn);
|
|
114
|
+
},
|
|
115
|
+
start() {
|
|
116
|
+
if (started) return;
|
|
117
|
+
started = true;
|
|
118
|
+
loadSlots();
|
|
119
|
+
fetchBookingForm(service.formId).then((f) => { if (started) { formFields = f; emit(); } });
|
|
120
|
+
},
|
|
121
|
+
stop() {
|
|
122
|
+
started = false;
|
|
123
|
+
generation++;
|
|
124
|
+
},
|
|
125
|
+
nextWeek() {
|
|
126
|
+
windowStart = new Date(windowStart.getTime() + WEEK);
|
|
127
|
+
loadSlots();
|
|
128
|
+
},
|
|
129
|
+
prevWeek() {
|
|
130
|
+
const prev = new Date(windowStart.getTime() - WEEK);
|
|
131
|
+
windowStart = prev < new Date() ? new Date() : prev;
|
|
132
|
+
loadSlots();
|
|
133
|
+
},
|
|
134
|
+
setStaffId(id) {
|
|
135
|
+
if (id === staffId) return;
|
|
136
|
+
staffId = id;
|
|
137
|
+
loadSlots();
|
|
138
|
+
},
|
|
139
|
+
setSelectedSlot(slot) {
|
|
140
|
+
selectedSlot = slot;
|
|
141
|
+
emit();
|
|
142
|
+
},
|
|
143
|
+
setValue(target, value) {
|
|
144
|
+
values = { ...values, [target]: value };
|
|
145
|
+
emit();
|
|
146
|
+
},
|
|
147
|
+
async book() {
|
|
148
|
+
if (!selectedSlot) throw new Error("Pick a time first.");
|
|
149
|
+
booking = true;
|
|
150
|
+
error = null;
|
|
151
|
+
emit();
|
|
152
|
+
try {
|
|
153
|
+
const result = await bookService(service, selectedSlot, values, { staffId });
|
|
154
|
+
if (result.kind === "redirect") {
|
|
155
|
+
window.location.href = result.url;
|
|
156
|
+
} else {
|
|
157
|
+
confirmed = result;
|
|
158
|
+
}
|
|
159
|
+
return result;
|
|
160
|
+
} catch (e) {
|
|
161
|
+
error = e instanceof Error ? e.message : String(e);
|
|
162
|
+
throw e;
|
|
163
|
+
} finally {
|
|
164
|
+
booking = false;
|
|
165
|
+
emit();
|
|
166
|
+
}
|
|
167
|
+
},
|
|
168
|
+
};
|
|
169
|
+
}
|
|
@@ -1,6 +1,8 @@
|
|
|
1
|
-
// Availability, the booking form, and the createBooking → Cart V2 → checkout-or-place
|
|
2
|
-
//
|
|
3
|
-
//
|
|
1
|
+
// Availability, the booking form, and the createBooking → Cart V2 → checkout-or-place sequence
|
|
2
|
+
// over the SDK. The payload shapes are exact and easy to get subtly wrong — they are built in
|
|
3
|
+
// ./booking-core (shared with the REST twin in references/bookings/rest/); this file is the
|
|
4
|
+
// transport only. Copy as-is; extend by calling these exports, never by editing them. Failures
|
|
5
|
+
// are loud.
|
|
4
6
|
// docs: https://dev.wix.com/docs/api-reference/business-solutions/bookings/time-slots/time-slots-v2/list-availability-time-slots.md
|
|
5
7
|
// docs: https://dev.wix.com/docs/api-reference/business-solutions/bookings/time-slots/time-slots-v2/list-event-time-slots.md
|
|
6
8
|
// docs: https://dev.wix.com/docs/api-reference/business-solutions/bookings/bookings/bookings-writer-v2/create-booking.md
|
|
@@ -9,106 +11,51 @@
|
|
|
9
11
|
// docs: https://dev.wix.com/docs/api-reference/business-solutions/e-commerce/purchase-flow/cart-v2/place-order.md
|
|
10
12
|
// docs: https://dev.wix.com/docs/api-reference/business-management/headless/redirects/create-redirect-session.md
|
|
11
13
|
// docs: https://dev.wix.com/docs/api-reference/crm/forms/form-schemas/get-form-summary.md
|
|
12
|
-
import {
|
|
13
|
-
availabilityTimeSlots,
|
|
14
|
-
eventTimeSlots,
|
|
15
|
-
bookings as bookingsModule,
|
|
16
|
-
} from "@wix/bookings";
|
|
14
|
+
import { availabilityTimeSlots, eventTimeSlots, bookings as bookingsModule } from "@wix/bookings";
|
|
17
15
|
import { createCart, calculateCart, placeOrder } from "@wix/auto_sdk_ecom_cart-v-2";
|
|
18
16
|
import { redirects as redirectsModule } from "@wix/redirects";
|
|
19
|
-
import {
|
|
20
|
-
import {
|
|
21
|
-
|
|
17
|
+
import { wixFetch, wixModule } from "../sdk";
|
|
18
|
+
import {
|
|
19
|
+
FALLBACK_FIELDS,
|
|
20
|
+
appointmentSlotsRequest,
|
|
21
|
+
bookingCartRequest,
|
|
22
|
+
bookingIdOf,
|
|
23
|
+
bookingRequest,
|
|
24
|
+
cartIdOf,
|
|
25
|
+
cartTotal,
|
|
26
|
+
checkoutRedirectRequest,
|
|
27
|
+
checkoutRequired,
|
|
28
|
+
classSlotsRequest,
|
|
29
|
+
confirmedResult,
|
|
30
|
+
defaultTimeZone,
|
|
31
|
+
toFormFields,
|
|
32
|
+
toLocalDateString,
|
|
33
|
+
toSlot,
|
|
34
|
+
type Raw,
|
|
35
|
+
type SlotsWindow,
|
|
36
|
+
} from "./booking-core";
|
|
22
37
|
import type { BookingFormField, BookingResult, ServiceDetail, Slot } from "./types";
|
|
23
38
|
|
|
39
|
+
export { toLocalDateString };
|
|
40
|
+
|
|
24
41
|
const apptSlots = wixModule(availabilityTimeSlots);
|
|
25
42
|
const classSlots = wixModule(eventTimeSlots);
|
|
26
43
|
const bookings = wixModule(bookingsModule);
|
|
27
44
|
const cart = wixModule({ createCart, calculateCart, placeOrder });
|
|
28
45
|
const redirects = wixModule(redirectsModule);
|
|
29
|
-
const forms = wixModule(formsModule);
|
|
30
|
-
|
|
31
|
-
type Raw = Record<string, any>;
|
|
32
|
-
|
|
33
|
-
const pad = (n: number) => String(n).padStart(2, "0");
|
|
34
|
-
/** Local wall-clock "YYYY-MM-DDThh:mm:ss" (NO Z) — the format the availability APIs require. */
|
|
35
|
-
export function toLocalDateString(d: Date): string {
|
|
36
|
-
return `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())}T${pad(d.getHours())}:${pad(d.getMinutes())}:${pad(d.getSeconds())}`;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
function toSlot(raw: Raw): Slot {
|
|
40
|
-
const start: string = raw.localStartDate ?? "";
|
|
41
|
-
const staff = (raw.availableResources ?? [])
|
|
42
|
-
.flatMap((ar: Raw) => ar.resources ?? [])
|
|
43
|
-
.map((r: Raw) => ({ id: r._id ?? "", name: r.name ?? "" }))
|
|
44
|
-
.filter((r: { id: string }) => r.id);
|
|
45
|
-
let label = start.slice(11, 16);
|
|
46
|
-
try {
|
|
47
|
-
label = new Date(start).toLocaleTimeString(undefined, { hour: "numeric", minute: "2-digit" });
|
|
48
|
-
} catch {
|
|
49
|
-
/* keep the hh:mm fallback */
|
|
50
|
-
}
|
|
51
|
-
return {
|
|
52
|
-
startLocal: start,
|
|
53
|
-
endLocal: raw.localEndDate ?? "",
|
|
54
|
-
dayKey: start.slice(0, 10),
|
|
55
|
-
label,
|
|
56
|
-
scheduleId: raw.scheduleId ?? null,
|
|
57
|
-
eventId: raw.eventInfo?.eventId ?? null,
|
|
58
|
-
staff,
|
|
59
|
-
};
|
|
60
|
-
}
|
|
61
46
|
|
|
62
47
|
/**
|
|
63
|
-
* Bookable slots for a service in [from,
|
|
48
|
+
* Bookable slots for a service in [from, from + days) — APPOINTMENT and CLASS use different APIs;
|
|
64
49
|
* this branches for you. Dates are local wall-clock; timeZone defaults to the visitor's.
|
|
65
50
|
*/
|
|
66
|
-
export async function fetchSlots(
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
staffId,
|
|
73
|
-
}: { from?: Date; days?: number; timeZone?: string; staffId?: string } = {},
|
|
74
|
-
): Promise<Slot[]> {
|
|
75
|
-
const to = new Date(from.getTime() + days * 24 * 3600 * 1000);
|
|
76
|
-
const fromLocalDate = toLocalDateString(from);
|
|
77
|
-
const toLocalDate = toLocalDateString(to);
|
|
78
|
-
|
|
79
|
-
if (service.type === "CLASS") {
|
|
80
|
-
const res: Raw = await classSlots.listEventTimeSlots({
|
|
81
|
-
serviceIds: [service.id],
|
|
82
|
-
fromLocalDate,
|
|
83
|
-
toLocalDate,
|
|
84
|
-
timeZone,
|
|
85
|
-
includeNonBookable: false,
|
|
86
|
-
...(staffId ? { eventFilter: { "resources.id": { $hasSome: [staffId] } } } : {}),
|
|
87
|
-
} as any);
|
|
88
|
-
return (res.timeSlots ?? []).map((t: Raw) => toSlot(t));
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
const res: Raw = await apptSlots.listAvailabilityTimeSlots({
|
|
92
|
-
serviceId: service.id,
|
|
93
|
-
fromLocalDate,
|
|
94
|
-
toLocalDate,
|
|
95
|
-
timeZone,
|
|
96
|
-
bookable: true,
|
|
97
|
-
cursorPaging: { limit: 100 },
|
|
98
|
-
includeResourceTypeIds: [STAFF_RESOURCE_TYPE_ID],
|
|
99
|
-
...(staffId
|
|
100
|
-
? { resourceTypes: [{ resourceTypeId: STAFF_RESOURCE_TYPE_ID, resourceIds: [staffId] }] }
|
|
101
|
-
: {}),
|
|
102
|
-
} as any);
|
|
103
|
-
return (res.timeSlots ?? []).map((t: Raw) => toSlot(t));
|
|
51
|
+
export async function fetchSlots(service: Pick<ServiceDetail, "id" | "type">, window: SlotsWindow = {}): Promise<Slot[]> {
|
|
52
|
+
const res: Raw =
|
|
53
|
+
service.type === "CLASS"
|
|
54
|
+
? await classSlots.listEventTimeSlots(classSlotsRequest(service.id, window) as any)
|
|
55
|
+
: await apptSlots.listAvailabilityTimeSlots(appointmentSlotsRequest(service.id, window) as any);
|
|
56
|
+
return ((res.timeSlots ?? []) as Raw[]).map(toSlot);
|
|
104
57
|
}
|
|
105
58
|
|
|
106
|
-
const FALLBACK_FIELDS: BookingFormField[] = [
|
|
107
|
-
{ target: "first_name", label: "First Name", type: "STRING" },
|
|
108
|
-
{ target: "last_name", label: "Last Name", type: "STRING" },
|
|
109
|
-
{ target: "email", label: "Email", type: "EMAIL" },
|
|
110
|
-
];
|
|
111
|
-
|
|
112
59
|
/**
|
|
113
60
|
* The service's booking-form fields, flat and render-ready (values are keyed by `target`).
|
|
114
61
|
* ALWAYS returns a non-empty list — contact basics when the schema is missing/unusable —
|
|
@@ -117,18 +64,20 @@ const FALLBACK_FIELDS: BookingFormField[] = [
|
|
|
117
64
|
export async function fetchBookingForm(formId: string | null): Promise<BookingFormField[]> {
|
|
118
65
|
if (!formId) return FALLBACK_FIELDS;
|
|
119
66
|
try {
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
})
|
|
130
|
-
.
|
|
131
|
-
|
|
67
|
+
// Over wixFetch, not the @wix/forms `forms` module: that generated module is 15 MB and would
|
|
68
|
+
// ride into the booking island's client chunk (11 MB per visitor). The summary has labels and
|
|
69
|
+
// types; only the full schema says which fields are required.
|
|
70
|
+
const id = encodeURIComponent(formId);
|
|
71
|
+
const json = async (path: string): Promise<Raw | null> => {
|
|
72
|
+
const r = await wixFetch(path);
|
|
73
|
+
return r.ok ? ((await r.json()) as Raw) : null;
|
|
74
|
+
};
|
|
75
|
+
const [res, form] = await Promise.all([
|
|
76
|
+
json(`/form-schema-service/v4/forms/${id}/summary`),
|
|
77
|
+
json(`/form-schema-service/v4/forms/${id}`).catch(() => null),
|
|
78
|
+
]);
|
|
79
|
+
if (!res) return FALLBACK_FIELDS;
|
|
80
|
+
return toFormFields(res.formSummary, form?.form);
|
|
132
81
|
} catch {
|
|
133
82
|
return FALLBACK_FIELDS;
|
|
134
83
|
}
|
|
@@ -145,62 +94,28 @@ export async function bookService(
|
|
|
145
94
|
service: ServiceDetail,
|
|
146
95
|
slot: Slot,
|
|
147
96
|
formValues: Record<string, unknown>,
|
|
148
|
-
{ staffId, timeZone =
|
|
97
|
+
{ staffId, timeZone = defaultTimeZone() }: { staffId?: string; timeZone?: string } = {},
|
|
149
98
|
): Promise<BookingResult> {
|
|
150
|
-
const staff = staffId ? slot.staff.find((s) => s.id === staffId) : undefined;
|
|
151
99
|
const created: Raw = await bookings.createBooking(
|
|
152
|
-
{
|
|
153
|
-
selectedPaymentOption: service.paymentOption,
|
|
154
|
-
totalParticipants: 1,
|
|
155
|
-
bookedEntity: {
|
|
156
|
-
slot: {
|
|
157
|
-
serviceId: service.id,
|
|
158
|
-
scheduleId: slot.scheduleId ?? undefined, // APPOINTMENT: the SLOT's scheduleId (never service.schedule.id)
|
|
159
|
-
eventId: slot.eventId ?? undefined, // CLASS
|
|
160
|
-
startDate: slot.startLocal,
|
|
161
|
-
endDate: slot.endLocal,
|
|
162
|
-
timezone: timeZone,
|
|
163
|
-
...(staff
|
|
164
|
-
? { resource: { _id: staff.id, name: staff.name } }
|
|
165
|
-
: {
|
|
166
|
-
resourceSelections: [
|
|
167
|
-
{ resourceTypeId: STAFF_RESOURCE_TYPE_ID, selectionMethod: "ANY_RESOURCE" },
|
|
168
|
-
],
|
|
169
|
-
}),
|
|
170
|
-
location: { locationType: "OWNER_BUSINESS" },
|
|
171
|
-
},
|
|
172
|
-
},
|
|
173
|
-
} as any,
|
|
100
|
+
bookingRequest(service, slot, { staffId, timeZone, idKey: "_id" }) as any,
|
|
174
101
|
{ formSubmission: formValues } as any,
|
|
175
102
|
);
|
|
176
|
-
const bookingId
|
|
103
|
+
const bookingId = bookingIdOf(created);
|
|
177
104
|
if (!bookingId) throw new Error("The booking couldn't be created — the slot may have just been taken.");
|
|
178
105
|
|
|
179
|
-
const newCart: Raw = await cart.createCart(
|
|
180
|
-
|
|
181
|
-
{ quantity: 1, catalogReference: { catalogItemId: bookingId, appId: BOOKINGS_APP_ID } },
|
|
182
|
-
],
|
|
183
|
-
cart: { source: { channelType: "WEB" } },
|
|
184
|
-
} as any);
|
|
185
|
-
const cartId: string | undefined = newCart._id ?? newCart.cart?._id;
|
|
106
|
+
const newCart: Raw = await cart.createCart(bookingCartRequest(bookingId) as any);
|
|
107
|
+
const cartId = cartIdOf(newCart);
|
|
186
108
|
if (!cartId) throw new Error("The booking couldn't be reserved — please try again.");
|
|
187
109
|
|
|
188
110
|
const calc: Raw = await cart.calculateCart(cartId);
|
|
189
|
-
|
|
190
|
-
const checkoutRequired =
|
|
191
|
-
service.cancellationFeeEnabled || (total > 0 && service.paymentOption !== "OFFLINE");
|
|
192
|
-
|
|
193
|
-
if (checkoutRequired) {
|
|
111
|
+
if (checkoutRequired(service, cartTotal(calc))) {
|
|
194
112
|
const origin = typeof window !== "undefined" ? window.location.origin : "";
|
|
195
|
-
const session: Raw = await redirects.createRedirectSession(
|
|
196
|
-
ecomCheckout: { checkoutId: cartId },
|
|
197
|
-
callbacks: { postFlowUrl: origin ? `${origin}/` : undefined },
|
|
198
|
-
});
|
|
113
|
+
const session: Raw = await redirects.createRedirectSession(checkoutRedirectRequest(cartId, origin));
|
|
199
114
|
const url = session?.redirectSession?.fullUrl;
|
|
200
115
|
if (!url) throw new Error("Checkout couldn't start — please try again.");
|
|
201
116
|
return { kind: "redirect", url };
|
|
202
117
|
}
|
|
203
118
|
|
|
204
119
|
const order: Raw = await cart.placeOrder(cartId);
|
|
205
|
-
return
|
|
120
|
+
return confirmedResult(bookingId, order);
|
|
206
121
|
}
|