@profitstudio/booking-engine 0.0.0-stage → 2.0.0-beta.2
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/CHANGELOG.md +578 -0
- package/LICENSE +21 -0
- package/README.md +18 -2
- package/THIRD_PARTY_NOTICES.md +41 -0
- package/bin/booking-engine.mjs +575 -0
- package/bin/connect.mjs +408 -0
- package/bin/photos.mjs +82 -0
- package/dist/admin/BookingAdmin.d.ts +29 -0
- package/dist/admin/BookingAdmin.js +338 -0
- package/dist/admin/admin.css +1423 -0
- package/dist/admin/alerts.d.ts +10 -0
- package/dist/admin/alerts.js +11 -0
- package/dist/admin/booking-detail.d.ts +47 -0
- package/dist/admin/booking-detail.js +159 -0
- package/dist/admin/content-inputs.d.ts +15 -0
- package/dist/admin/content-inputs.js +105 -0
- package/dist/admin/context.d.ts +41 -0
- package/dist/admin/context.js +8 -0
- package/dist/admin/drawer.d.ts +14 -0
- package/dist/admin/drawer.js +56 -0
- package/dist/admin/feedback.d.ts +32 -0
- package/dist/admin/feedback.js +70 -0
- package/dist/admin/fields.d.ts +27 -0
- package/dist/admin/fields.js +146 -0
- package/dist/admin/fonts/OFL-Geist-Mono.txt +93 -0
- package/dist/admin/fonts/OFL-Geist.txt +93 -0
- package/dist/admin/fonts/geist-latin-wght-normal.woff2 +0 -0
- package/dist/admin/fonts/geist-mono-latin-wght-normal.woff2 +0 -0
- package/dist/admin/icons.d.ts +66 -0
- package/dist/admin/icons.js +72 -0
- package/dist/admin/image-upload.d.ts +58 -0
- package/dist/admin/image-upload.js +111 -0
- package/dist/admin/index.d.ts +7 -0
- package/dist/admin/index.js +6 -0
- package/dist/admin/lucide.generated.d.ts +521 -0
- package/dist/admin/lucide.generated.js +76 -0
- package/dist/admin/sections/BookingsSection.d.ts +1 -0
- package/dist/admin/sections/BookingsSection.js +137 -0
- package/dist/admin/sections/EmailSection.d.ts +8 -0
- package/dist/admin/sections/EmailSection.js +184 -0
- package/dist/admin/sections/HoursSection.d.ts +1 -0
- package/dist/admin/sections/HoursSection.js +110 -0
- package/dist/admin/sections/OverviewSection.d.ts +1 -0
- package/dist/admin/sections/OverviewSection.js +427 -0
- package/dist/admin/sections/ServicesSection.d.ts +1 -0
- package/dist/admin/sections/ServicesSection.js +236 -0
- package/dist/admin/sections/SettingsSection.d.ts +10 -0
- package/dist/admin/sections/SettingsSection.js +96 -0
- package/dist/admin/sections/WebsiteSection.d.ts +1 -0
- package/dist/admin/sections/WebsiteSection.js +228 -0
- package/dist/admin/sections.d.ts +22 -0
- package/dist/admin/sections.js +29 -0
- package/dist/admin/setup.d.ts +1 -0
- package/dist/admin/setup.js +45 -0
- package/dist/admin/today.d.ts +43 -0
- package/dist/admin/today.js +115 -0
- package/dist/admin/ui.d.ts +134 -0
- package/dist/admin/ui.js +220 -0
- package/dist/calendar.d.ts +64 -0
- package/dist/calendar.js +139 -0
- package/dist/client.d.ts +147 -0
- package/dist/client.js +507 -0
- package/dist/config.d.ts +25 -0
- package/dist/config.js +120 -0
- package/dist/content/image.d.ts +20 -0
- package/dist/content/image.js +90 -0
- package/dist/content/resolve.d.ts +12 -0
- package/dist/content/resolve.js +73 -0
- package/dist/content/schema.d.ts +119 -0
- package/dist/content/schema.js +53 -0
- package/dist/content/store.d.ts +48 -0
- package/dist/content/store.js +123 -0
- package/dist/content/validate.d.ts +33 -0
- package/dist/content/validate.js +177 -0
- package/dist/errors.d.ts +19 -0
- package/dist/errors.js +94 -0
- package/dist/generated/manifest.d.ts +6 -0
- package/dist/generated/manifest.js +7 -0
- package/dist/index.d.ts +20 -0
- package/dist/index.js +20 -0
- package/dist/presets/hair-salon.d.ts +8 -0
- package/dist/presets/hair-salon.js +94 -0
- package/dist/presets/index.d.ts +5 -0
- package/dist/presets/index.js +6 -0
- package/dist/preview.d.ts +18 -0
- package/dist/preview.js +134 -0
- package/dist/semver.d.ts +16 -0
- package/dist/semver.js +55 -0
- package/dist/time.d.ts +31 -0
- package/dist/time.js +138 -0
- package/dist/types.d.ts +477 -0
- package/dist/types.js +3 -0
- package/dist/vite.d.ts +52 -0
- package/dist/vite.js +150 -0
- package/docs/BUILD-A-SITE.md +203 -0
- package/docs/EMAIL-SETUP.md +224 -0
- package/docs/INTEGRATION.md +1112 -0
- package/engine.manifest.json +90 -0
- package/functions/booking-mailer/deno.json +6 -0
- package/functions/booking-mailer/index.ts +44 -0
- package/functions/booking-mailer/mailer.js +156 -0
- package/functions/booking-mailer/providers/index.js +42 -0
- package/functions/booking-mailer/providers/resend.js +75 -0
- package/functions/booking-mailer/templates.js +345 -0
- package/migrations/0001_core.sql +1673 -0
- package/migrations/0002_security_hardening.sql +711 -0
- package/migrations/0003_private_definers_and_holds.sql +808 -0
- package/migrations/0004_verified_requests.sql +1702 -0
- package/migrations/0005_scoped_mailer_credential.sql +347 -0
- package/migrations/0006_trusted_client_ip.sql +56 -0
- package/migrations/0007_site_content.sql +380 -0
- package/migrations/0008_service_images_opening_times.sql +328 -0
- package/package.json +85 -4
- package/templates/BOOKING_ENGINE.md +61 -0
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,477 @@
|
|
|
1
|
+
/** How the engine is connected. */
|
|
2
|
+
export type EngineMode =
|
|
3
|
+
/** Connected to a Supabase project. Bookings are real. */
|
|
4
|
+
'live'
|
|
5
|
+
/** No backend; fictional preset data; nothing is ever saved or emailed. Development only. */
|
|
6
|
+
| 'preview'
|
|
7
|
+
/** No backend and preview not allowed (e.g. a production build without env vars). */
|
|
8
|
+
| 'unconfigured';
|
|
9
|
+
export type EngineErrorCode = 'INVALID_INPUT' | 'SERVICE_UNAVAILABLE' | 'SLOT_UNAVAILABLE' | 'RATE_LIMITED' | 'NOT_AUTHORIZED' | 'NOT_FOUND' | 'INVALID_TRANSITION' | 'CONFLICT' | 'PREVIEW_MODE' | 'NOT_CONFIGURED' | 'SCHEMA_MISSING' | 'SCHEMA_INCOMPATIBLE' | 'NETWORK' | 'UNKNOWN'
|
|
10
|
+
/** The owner has not switched online booking on (it needs working email first). */
|
|
11
|
+
| 'BOOKING_DISABLED'
|
|
12
|
+
/** createBooking (unverified) is closed since 2.0.0-alpha.4: use requestBooking + verifyBooking. */
|
|
13
|
+
| 'VERIFICATION_REQUIRED'
|
|
14
|
+
/** The code is wrong; `attemptsLeft` says how many tries remain. */
|
|
15
|
+
| 'INVALID_CODE'
|
|
16
|
+
/** The code's time ran out (or a newer request replaced it); the time is free again. */
|
|
17
|
+
| 'CODE_EXPIRED'
|
|
18
|
+
/** Five wrong codes: the request is closed and its time released. */
|
|
19
|
+
| 'TOO_MANY_ATTEMPTS'
|
|
20
|
+
/** The verified address already has the maximum number of upcoming bookings. */
|
|
21
|
+
| 'TOO_MANY_BOOKINGS'
|
|
22
|
+
/** A new code can be sent after `retryAfterSeconds`. */
|
|
23
|
+
| 'RESEND_TOO_SOON'
|
|
24
|
+
/** No more codes for this request (3 re-sends). */
|
|
25
|
+
| 'RESEND_LIMIT';
|
|
26
|
+
export interface EngineError {
|
|
27
|
+
code: EngineErrorCode;
|
|
28
|
+
/** Safe, human-readable English. Sites may map `code` (and `field`) to their own copy. */
|
|
29
|
+
message: string;
|
|
30
|
+
/** The input that was rejected, e.g. "customer.email". */
|
|
31
|
+
field?: string;
|
|
32
|
+
/** INVALID_CODE: tries left for this code. */
|
|
33
|
+
attemptsLeft?: number;
|
|
34
|
+
/** RESEND_TOO_SOON: seconds until a new code can be requested. */
|
|
35
|
+
retryAfterSeconds?: number;
|
|
36
|
+
}
|
|
37
|
+
/** Every engine call resolves to a Result. Engine calls never throw for expected failures. */
|
|
38
|
+
export type Result<T> = {
|
|
39
|
+
ok: true;
|
|
40
|
+
data: T;
|
|
41
|
+
} | {
|
|
42
|
+
ok: false;
|
|
43
|
+
error: EngineError;
|
|
44
|
+
};
|
|
45
|
+
/** ISO weekday: 1 = Monday … 7 = Sunday. */
|
|
46
|
+
export type Weekday = 1 | 2 | 3 | 4 | 5 | 6 | 7;
|
|
47
|
+
/** Opening hours in the business time zone, "HH:MM" (end may be "24:00"). */
|
|
48
|
+
export interface OpeningHoursInterval {
|
|
49
|
+
weekday: Weekday;
|
|
50
|
+
start: string;
|
|
51
|
+
end: string;
|
|
52
|
+
}
|
|
53
|
+
export interface BookingPolicies {
|
|
54
|
+
slotIntervalMinutes: number;
|
|
55
|
+
minNoticeMinutes: number;
|
|
56
|
+
maxAdvanceDays: number;
|
|
57
|
+
/** Verified requests are confirmed at once; otherwise the owner confirms each one. */
|
|
58
|
+
autoConfirm: boolean;
|
|
59
|
+
requirePhone: boolean;
|
|
60
|
+
/** How long a verified request holds its time while the owner decides. */
|
|
61
|
+
pendingHoldMinutes: number;
|
|
62
|
+
/** False until the owner switches online booking on: show a "contact us" message instead of the form. */
|
|
63
|
+
onlineBookingEnabled: boolean;
|
|
64
|
+
/** How long the emailed code works (and the time is held for the customer). */
|
|
65
|
+
verificationWindowMinutes: number;
|
|
66
|
+
}
|
|
67
|
+
export interface Business {
|
|
68
|
+
name: string;
|
|
69
|
+
/** IANA time zone, e.g. "Europe/Paris". All dates and times are in this zone. */
|
|
70
|
+
timezone: string;
|
|
71
|
+
/** ISO 4217, e.g. "EUR". */
|
|
72
|
+
currency: string;
|
|
73
|
+
/** BCP 47, e.g. "fr-FR". */
|
|
74
|
+
locale: string;
|
|
75
|
+
contact: {
|
|
76
|
+
email: string | null;
|
|
77
|
+
phone: string | null;
|
|
78
|
+
address: string | null;
|
|
79
|
+
};
|
|
80
|
+
policies: BookingPolicies;
|
|
81
|
+
openingHours: OpeningHoursInterval[];
|
|
82
|
+
}
|
|
83
|
+
export interface Service {
|
|
84
|
+
id: string;
|
|
85
|
+
name: string;
|
|
86
|
+
description: string | null;
|
|
87
|
+
durationMinutes: number;
|
|
88
|
+
/** Minor units (cents). null = price on request. */
|
|
89
|
+
priceCents: number | null;
|
|
90
|
+
/** The price is a starting price ("from …"). */
|
|
91
|
+
priceFrom: boolean;
|
|
92
|
+
currency: string;
|
|
93
|
+
/**
|
|
94
|
+
* The owner's optional photo of the service (null: none — never required).
|
|
95
|
+
* Render it in your site's own style; the engine imposes no layout.
|
|
96
|
+
* Since 2.0.0-alpha.10 (schema 2.0.0-alpha.10); always null on older databases.
|
|
97
|
+
*/
|
|
98
|
+
image?: ServiceImage | null;
|
|
99
|
+
}
|
|
100
|
+
/** A service photo the owner uploaded in the dashboard. */
|
|
101
|
+
export interface ServiceImage {
|
|
102
|
+
/** Public address of the file (long-cached; a replaced photo gets a new URL). */
|
|
103
|
+
url: string;
|
|
104
|
+
/** The file inside the media bucket, "services/…". */
|
|
105
|
+
path: string;
|
|
106
|
+
/** What the photo shows, written by the owner; "" when empty — then treat the photo as decorative (alt=""). */
|
|
107
|
+
alt: string;
|
|
108
|
+
/** Pixel size when known: use it for the aspect-ratio box, never to decide whether to show the photo. */
|
|
109
|
+
width: number | null;
|
|
110
|
+
height: number | null;
|
|
111
|
+
}
|
|
112
|
+
/** What saveService stores as a service's photo: an uploadServiceImage result, plus an optional description. */
|
|
113
|
+
export interface ServiceImageInput {
|
|
114
|
+
path: string;
|
|
115
|
+
/** Up to 300 characters, one line. */
|
|
116
|
+
alt?: string;
|
|
117
|
+
width?: number;
|
|
118
|
+
height?: number;
|
|
119
|
+
}
|
|
120
|
+
export interface OpeningTimesQuery {
|
|
121
|
+
/** First business-local date, "YYYY-MM-DD". */
|
|
122
|
+
from: string;
|
|
123
|
+
/** Last business-local date (inclusive). Defaults to `from`. At most 42 days. */
|
|
124
|
+
to?: string;
|
|
125
|
+
}
|
|
126
|
+
/** One business-local date's opening hours after closures. */
|
|
127
|
+
export interface OpeningTimesDay {
|
|
128
|
+
date: string;
|
|
129
|
+
/** Business-local "HH:MM" ranges in order (end may be "24:00"). Empty: closed that day. */
|
|
130
|
+
hours: Array<{
|
|
131
|
+
start: string;
|
|
132
|
+
end: string;
|
|
133
|
+
}>;
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* When the business is open on each date: weekly hours minus closures (whole
|
|
137
|
+
* days and partial days). For "Open now" lines — never for bookable times,
|
|
138
|
+
* which only getAvailability gives.
|
|
139
|
+
*/
|
|
140
|
+
export interface OpeningTimes {
|
|
141
|
+
timezone: string;
|
|
142
|
+
from: string;
|
|
143
|
+
to: string;
|
|
144
|
+
days: OpeningTimesDay[];
|
|
145
|
+
}
|
|
146
|
+
export interface AvailabilityQuery {
|
|
147
|
+
serviceId: string;
|
|
148
|
+
/** First business-local date, "YYYY-MM-DD". */
|
|
149
|
+
from: string;
|
|
150
|
+
/** Last business-local date (inclusive), "YYYY-MM-DD". Defaults to `from`. At most 42 days. */
|
|
151
|
+
to?: string;
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* available — bookable times exist
|
|
155
|
+
* full — open, but every time is taken or too soon
|
|
156
|
+
* closed — no opening hours that day, or closed all day
|
|
157
|
+
* unavailable — in the past or beyond the booking window
|
|
158
|
+
*/
|
|
159
|
+
export type DayStatus = 'available' | 'full' | 'closed' | 'unavailable';
|
|
160
|
+
export interface Slot {
|
|
161
|
+
/** Exact instant, ISO 8601 UTC. Send this back unchanged to requestBooking. */
|
|
162
|
+
startsAt: string;
|
|
163
|
+
endsAt: string;
|
|
164
|
+
/** Business-local start time, "HH:MM". On a DST change two slots can share a label. */
|
|
165
|
+
time: string;
|
|
166
|
+
}
|
|
167
|
+
export interface AvailabilityDay {
|
|
168
|
+
date: string;
|
|
169
|
+
status: DayStatus;
|
|
170
|
+
slots: Slot[];
|
|
171
|
+
}
|
|
172
|
+
export interface Availability {
|
|
173
|
+
serviceId: string;
|
|
174
|
+
timezone: string;
|
|
175
|
+
from: string;
|
|
176
|
+
to: string;
|
|
177
|
+
days: AvailabilityDay[];
|
|
178
|
+
}
|
|
179
|
+
export interface BookingRequest {
|
|
180
|
+
serviceId: string;
|
|
181
|
+
/** A `Slot.startsAt` value from getAvailability. */
|
|
182
|
+
startsAt: string;
|
|
183
|
+
customer: {
|
|
184
|
+
name: string;
|
|
185
|
+
email: string;
|
|
186
|
+
phone?: string | null;
|
|
187
|
+
};
|
|
188
|
+
notes?: string | null;
|
|
189
|
+
}
|
|
190
|
+
/** Step 1 done: the time is held while the customer enters the code emailed to them. */
|
|
191
|
+
export interface PendingVerification {
|
|
192
|
+
/** Pass to verifyBooking / resendVerificationCode. Keep it in memory only. */
|
|
193
|
+
requestId: string;
|
|
194
|
+
/** Until when the code works and the time is held (ISO UTC). */
|
|
195
|
+
expiresAt: string;
|
|
196
|
+
/** Where the code was sent, masked: "a***@example.com". */
|
|
197
|
+
email: string;
|
|
198
|
+
/** Digits in the code: always 6 in 2.x. */
|
|
199
|
+
codeLength: number;
|
|
200
|
+
/** Offer "Send a new code" after this many seconds. */
|
|
201
|
+
resendAfterSeconds: number;
|
|
202
|
+
/** New codes that can still be requested for this request (3 at first). */
|
|
203
|
+
resendsLeft: number;
|
|
204
|
+
/** 'email' — a real email was sent. 'preview' — preview mode: nothing was sent and nothing will be saved. */
|
|
205
|
+
delivery: 'email' | 'preview';
|
|
206
|
+
}
|
|
207
|
+
export interface VerifyBookingInput {
|
|
208
|
+
requestId: string;
|
|
209
|
+
/** The code from the email; spaces and dashes are ignored. */
|
|
210
|
+
code: string;
|
|
211
|
+
}
|
|
212
|
+
export interface RequestReleased {
|
|
213
|
+
/** true: the hold was released now; false: it had already ended (expired or replaced). */
|
|
214
|
+
released: boolean;
|
|
215
|
+
}
|
|
216
|
+
export interface CodeResent {
|
|
217
|
+
/** Unchanged: a new code never extends the time the request is held. */
|
|
218
|
+
expiresAt: string;
|
|
219
|
+
resendsLeft: number;
|
|
220
|
+
resendAfterSeconds: number;
|
|
221
|
+
delivery: 'email' | 'preview';
|
|
222
|
+
}
|
|
223
|
+
export interface BookingConfirmation {
|
|
224
|
+
reference: string;
|
|
225
|
+
/** pending = a verified request the business will confirm or decline; confirmed = booked. */
|
|
226
|
+
status: 'pending' | 'confirmed';
|
|
227
|
+
startsAt: string;
|
|
228
|
+
endsAt: string;
|
|
229
|
+
/** Business-local date and time. */
|
|
230
|
+
date: string;
|
|
231
|
+
time: string;
|
|
232
|
+
serviceName: string;
|
|
233
|
+
timezone: string;
|
|
234
|
+
/** Until when the request holds its time (ISO UTC). The owner confirms before then. */
|
|
235
|
+
holdExpiresAt: string | null;
|
|
236
|
+
}
|
|
237
|
+
export interface EngineInfo {
|
|
238
|
+
engine: string;
|
|
239
|
+
packageVersion: string;
|
|
240
|
+
/** Schema version of the connected database (or of this package in preview). */
|
|
241
|
+
schemaVersion: string;
|
|
242
|
+
requiredSchemaVersion: string;
|
|
243
|
+
mode: EngineMode;
|
|
244
|
+
compatible: boolean;
|
|
245
|
+
}
|
|
246
|
+
/**
|
|
247
|
+
* expired = a request whose hold lapsed before the owner confirmed it (still confirmable if the time is free).
|
|
248
|
+
* Requests still waiting for their email code are never listed.
|
|
249
|
+
*/
|
|
250
|
+
export type BookingStatus = 'pending' | 'confirmed' | 'completed' | 'cancelled' | 'no_show' | 'expired';
|
|
251
|
+
export interface AdminIdentity {
|
|
252
|
+
userId: string | null;
|
|
253
|
+
email: string | null;
|
|
254
|
+
isAdmin: boolean;
|
|
255
|
+
}
|
|
256
|
+
export interface AdminBooking {
|
|
257
|
+
id: string;
|
|
258
|
+
reference: string;
|
|
259
|
+
status: BookingStatus;
|
|
260
|
+
/** Pending/expired requests only: when the hold on the time ends (ISO UTC). */
|
|
261
|
+
holdExpiresAt: string | null;
|
|
262
|
+
/** The customer proved they own the email address (false for bookings made before 2.0.0-alpha.4 and by hand). */
|
|
263
|
+
emailVerified: boolean;
|
|
264
|
+
startsAt: string;
|
|
265
|
+
endsAt: string;
|
|
266
|
+
date: string;
|
|
267
|
+
time: string;
|
|
268
|
+
endTime: string;
|
|
269
|
+
serviceId: string;
|
|
270
|
+
serviceName: string;
|
|
271
|
+
priceCents: number | null;
|
|
272
|
+
currency: string;
|
|
273
|
+
customer: {
|
|
274
|
+
name: string;
|
|
275
|
+
email: string;
|
|
276
|
+
phone: string | null;
|
|
277
|
+
};
|
|
278
|
+
notes: string | null;
|
|
279
|
+
source: 'website' | 'admin';
|
|
280
|
+
createdAt: string;
|
|
281
|
+
statusChangedAt: string;
|
|
282
|
+
}
|
|
283
|
+
export interface BookingListQuery {
|
|
284
|
+
/** Business-local dates, inclusive. */
|
|
285
|
+
from: string;
|
|
286
|
+
to: string;
|
|
287
|
+
statuses?: BookingStatus[];
|
|
288
|
+
}
|
|
289
|
+
export interface AdminService {
|
|
290
|
+
id: string;
|
|
291
|
+
name: string;
|
|
292
|
+
description: string | null;
|
|
293
|
+
durationMinutes: number;
|
|
294
|
+
bufferAfterMinutes: number;
|
|
295
|
+
priceCents: number | null;
|
|
296
|
+
priceFrom: boolean;
|
|
297
|
+
isActive: boolean;
|
|
298
|
+
sortOrder: number;
|
|
299
|
+
/** Optional photo (null: none). Always null on a database older than schema 2.0.0-alpha.10. */
|
|
300
|
+
image: ServiceImage | null;
|
|
301
|
+
createdAt: string;
|
|
302
|
+
updatedAt: string;
|
|
303
|
+
}
|
|
304
|
+
/** Without `id`: create. With `id`: update only the given fields. */
|
|
305
|
+
export interface ServiceInput {
|
|
306
|
+
id?: string;
|
|
307
|
+
name?: string;
|
|
308
|
+
description?: string | null;
|
|
309
|
+
durationMinutes?: number;
|
|
310
|
+
bufferAfterMinutes?: number;
|
|
311
|
+
priceCents?: number | null;
|
|
312
|
+
priceFrom?: boolean;
|
|
313
|
+
isActive?: boolean;
|
|
314
|
+
sortOrder?: number;
|
|
315
|
+
/** A photo (from uploadServiceImage) sets it, null removes it, leaving it out keeps it. Needs schema 2.0.0-alpha.10. */
|
|
316
|
+
image?: ServiceImageInput | ServiceImage | null;
|
|
317
|
+
}
|
|
318
|
+
export interface AvailabilityException {
|
|
319
|
+
id: string;
|
|
320
|
+
kind: 'closed';
|
|
321
|
+
startsOn: string;
|
|
322
|
+
endsOn: string;
|
|
323
|
+
/** null = closed all day. */
|
|
324
|
+
startTime: string | null;
|
|
325
|
+
endTime: string | null;
|
|
326
|
+
reason: string | null;
|
|
327
|
+
}
|
|
328
|
+
export interface ExceptionInput {
|
|
329
|
+
startsOn: string;
|
|
330
|
+
endsOn?: string;
|
|
331
|
+
startTime?: string | null;
|
|
332
|
+
endTime?: string | null;
|
|
333
|
+
reason?: string | null;
|
|
334
|
+
}
|
|
335
|
+
export interface AdminSettings {
|
|
336
|
+
businessName: string;
|
|
337
|
+
timezone: string;
|
|
338
|
+
currency: string;
|
|
339
|
+
locale: string;
|
|
340
|
+
publicEmail: string | null;
|
|
341
|
+
publicPhone: string | null;
|
|
342
|
+
address: string | null;
|
|
343
|
+
slotIntervalMinutes: number;
|
|
344
|
+
minNoticeMinutes: number;
|
|
345
|
+
maxAdvanceDays: number;
|
|
346
|
+
autoConfirm: boolean;
|
|
347
|
+
requirePhone: boolean;
|
|
348
|
+
maxActiveBookingsPerEmail: number;
|
|
349
|
+
/** Site-wide cap on new website booking requests per rolling hour (spam circuit breaker). */
|
|
350
|
+
maxWebsiteBookingsPerHour: number;
|
|
351
|
+
pendingHoldMinutes: number;
|
|
352
|
+
/** Where new-request alerts go; null = the public business email. */
|
|
353
|
+
ownerNotificationEmail: string | null;
|
|
354
|
+
/** 5–30 minutes. */
|
|
355
|
+
verificationWindowMinutes: number;
|
|
356
|
+
/** Site-wide ceiling on code emails per 24 hours (cost and abuse control). */
|
|
357
|
+
maxVerificationEmailsPerDay: number;
|
|
358
|
+
/** Can only be switched on after a test email was delivered. */
|
|
359
|
+
onlineBookingEnabled: boolean;
|
|
360
|
+
updatedAt: string;
|
|
361
|
+
}
|
|
362
|
+
export type SettingsPatch = Partial<Omit<AdminSettings, 'updatedAt'>>;
|
|
363
|
+
export type EmailKind = 'verification_code' | 'request_received' | 'booking_confirmed' | 'booking_declined' | 'booking_cancelled' | 'request_expired' | 'owner_new_request' | 'test';
|
|
364
|
+
export type EmailDeliveryStatus = 'pending' | 'sending' | 'sent' | 'failed' | 'cancelled';
|
|
365
|
+
/** Email health for the dashboard. Never contains email contents or full addresses. */
|
|
366
|
+
export interface EmailStatus {
|
|
367
|
+
mailer: {
|
|
368
|
+
lastAttemptAt: string | null;
|
|
369
|
+
/** null until the first email was delivered — online booking stays locked until then. */
|
|
370
|
+
lastSuccessAt: string | null;
|
|
371
|
+
lastFailureAt: string | null;
|
|
372
|
+
lastError: string | null;
|
|
373
|
+
};
|
|
374
|
+
/** Is automatic sending set up (booleans only), and how did the last start of the mailer go? */
|
|
375
|
+
wiring: {
|
|
376
|
+
pgNet: boolean;
|
|
377
|
+
/** `booking.configure_mailer()` was run: the mailer's address and token are stored. */
|
|
378
|
+
configured: boolean;
|
|
379
|
+
schedule: boolean;
|
|
380
|
+
/** The last time the database started the mailer; `status` is its HTTP answer (null while waiting). */
|
|
381
|
+
lastStart: {
|
|
382
|
+
at: string;
|
|
383
|
+
status: number | null;
|
|
384
|
+
error: string | null;
|
|
385
|
+
} | null;
|
|
386
|
+
};
|
|
387
|
+
counts: {
|
|
388
|
+
queued: number;
|
|
389
|
+
/** When the oldest email still waiting was queued (null when none are waiting). */
|
|
390
|
+
oldestQueuedAt: string | null;
|
|
391
|
+
failedLast7Days: number;
|
|
392
|
+
/** Failed emails from the last 23 hours that `retryFailedEmails()` would send again. */
|
|
393
|
+
retryable: number;
|
|
394
|
+
sentLast24h: number;
|
|
395
|
+
codesLast24h: number;
|
|
396
|
+
};
|
|
397
|
+
recent: Array<{
|
|
398
|
+
id: string;
|
|
399
|
+
kind: EmailKind;
|
|
400
|
+
status: EmailDeliveryStatus;
|
|
401
|
+
/** Masked: "a***@example.com". */
|
|
402
|
+
recipient: string;
|
|
403
|
+
attempts: number;
|
|
404
|
+
createdAt: string;
|
|
405
|
+
sentAt: string | null;
|
|
406
|
+
lastError: string | null;
|
|
407
|
+
}>;
|
|
408
|
+
}
|
|
409
|
+
/** What the database sees of the caller's own request (admin only; never credentials). */
|
|
410
|
+
export interface RequestDiagnostics {
|
|
411
|
+
headerNames: string[];
|
|
412
|
+
cfConnectingIp: string | null;
|
|
413
|
+
xRealIp: string | null;
|
|
414
|
+
xForwardedFor: string | null;
|
|
415
|
+
/** The normalised identity rate limits use: "ip4:…", "ip6:…/64" or "unknown". */
|
|
416
|
+
clientKey: string;
|
|
417
|
+
}
|
|
418
|
+
export interface PresetService {
|
|
419
|
+
name: string;
|
|
420
|
+
description?: string;
|
|
421
|
+
durationMinutes: number;
|
|
422
|
+
bufferAfterMinutes?: number;
|
|
423
|
+
priceCents?: number | null;
|
|
424
|
+
priceFrom?: boolean;
|
|
425
|
+
}
|
|
426
|
+
/** Operational starter data for a niche. Pure data: the engine core never branches on it. */
|
|
427
|
+
export interface Preset {
|
|
428
|
+
id: string;
|
|
429
|
+
label: string;
|
|
430
|
+
/** Words the website should use for its customers and appointments. */
|
|
431
|
+
terminology: {
|
|
432
|
+
customer: string;
|
|
433
|
+
customers: string;
|
|
434
|
+
booking: string;
|
|
435
|
+
bookings: string;
|
|
436
|
+
};
|
|
437
|
+
/** Fictional business used only in preview mode. */
|
|
438
|
+
previewBusiness: {
|
|
439
|
+
name: string;
|
|
440
|
+
currency: string;
|
|
441
|
+
locale: string;
|
|
442
|
+
contact: {
|
|
443
|
+
email: string;
|
|
444
|
+
phone: string;
|
|
445
|
+
address: string;
|
|
446
|
+
};
|
|
447
|
+
};
|
|
448
|
+
/** Starter policies. Online booking is never switched on by a preset (it needs working email). */
|
|
449
|
+
policies: Omit<BookingPolicies, 'onlineBookingEnabled'>;
|
|
450
|
+
workingHours: OpeningHoursInterval[];
|
|
451
|
+
services: PresetService[];
|
|
452
|
+
}
|
|
453
|
+
/** What visitors read: the owner's published website content. */
|
|
454
|
+
export interface PublishedSiteContent {
|
|
455
|
+
/** Increases with every publish; 0 = nothing published yet (the website shows its defaults). */
|
|
456
|
+
version: number;
|
|
457
|
+
publishedAt: string | null;
|
|
458
|
+
/** Field id → value, as stored (resolve with resolveSiteContent or a SiteContentStore). */
|
|
459
|
+
values: Record<string, unknown>;
|
|
460
|
+
}
|
|
461
|
+
/** What the dashboard works on: the draft and the published copy. */
|
|
462
|
+
export interface AdminSiteContent {
|
|
463
|
+
draft: Record<string, unknown>;
|
|
464
|
+
published: Record<string, unknown>;
|
|
465
|
+
/** Increases with every save; send it back so another admin's newer save is never overwritten. */
|
|
466
|
+
draftRevision: number;
|
|
467
|
+
version: number;
|
|
468
|
+
draftUpdatedAt: string | null;
|
|
469
|
+
publishedAt: string | null;
|
|
470
|
+
hasUnpublishedChanges: boolean;
|
|
471
|
+
}
|
|
472
|
+
/** An image the owner uploaded (the value to store in an image field). */
|
|
473
|
+
export interface UploadedSiteImage {
|
|
474
|
+
path: string;
|
|
475
|
+
width: number;
|
|
476
|
+
height: number;
|
|
477
|
+
}
|
package/dist/types.js
ADDED
package/dist/vite.d.ts
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import type { SiteContentSchema, SiteContentValues } from './content/schema.js';
|
|
2
|
+
import { type PublishedSnapshot } from './content/store.js';
|
|
3
|
+
export interface PublishedSiteContentOptions<S extends SiteContentSchema> {
|
|
4
|
+
/** The site's content schema (src/content/site.ts). */
|
|
5
|
+
schema: S;
|
|
6
|
+
/** Must contain VITE_SUPABASE_URL and VITE_SUPABASE_PUBLISHABLE_KEY: pass `loadEnv(mode, process.cwd(), 'VITE_')`. */
|
|
7
|
+
env: Record<string, string | undefined>;
|
|
8
|
+
/** Title and description for search engines and link previews, from the resolved content. */
|
|
9
|
+
meta?: (values: SiteContentValues<S>) => {
|
|
10
|
+
title?: string;
|
|
11
|
+
description?: string;
|
|
12
|
+
};
|
|
13
|
+
/** The built pages that get the snapshot and metadata. Default: the public page only (`/index.html`), never /admin/. */
|
|
14
|
+
pages?: string[];
|
|
15
|
+
/** Element id of the snapshot (match readSiteContentSnapshot). Default 'site-content'. */
|
|
16
|
+
id?: string;
|
|
17
|
+
}
|
|
18
|
+
/** The part of a Vite plugin this uses (no dependency on Vite's own types). */
|
|
19
|
+
export interface SiteContentVitePlugin {
|
|
20
|
+
name: string;
|
|
21
|
+
apply: 'build';
|
|
22
|
+
buildStart(): Promise<void>;
|
|
23
|
+
transformIndexHtml(html: string, ctx: {
|
|
24
|
+
path: string;
|
|
25
|
+
}): string;
|
|
26
|
+
}
|
|
27
|
+
export type ContentLoad = {
|
|
28
|
+
kind: 'published';
|
|
29
|
+
snapshot: PublishedSnapshot;
|
|
30
|
+
}
|
|
31
|
+
/** No backend configured, or a database without website content (nothing can have been published). */
|
|
32
|
+
| {
|
|
33
|
+
kind: 'defaults';
|
|
34
|
+
reason: string;
|
|
35
|
+
};
|
|
36
|
+
/**
|
|
37
|
+
* Fetches the published content for a build. Resolves with the snapshot, or
|
|
38
|
+
* with "defaults" when there is no backend / no content yet; THROWS when a
|
|
39
|
+
* configured backend cannot be read (after three attempts).
|
|
40
|
+
*/
|
|
41
|
+
export declare function loadPublishedSiteContent(options: {
|
|
42
|
+
url?: string;
|
|
43
|
+
key?: string;
|
|
44
|
+
fetch?: typeof fetch;
|
|
45
|
+
/** Waits between attempts (default 1 s, then 3 s). */
|
|
46
|
+
retryDelaysMs?: number[];
|
|
47
|
+
timeoutMs?: number;
|
|
48
|
+
}): Promise<ContentLoad>;
|
|
49
|
+
/** Why the build stopped, with what to do — names only, never values. */
|
|
50
|
+
export declare function backendEnvError(problems: string[]): Error;
|
|
51
|
+
/** Vite plugin: published content → snapshot + title/description in the built HTML. */
|
|
52
|
+
export declare function publishedSiteContent<S extends SiteContentSchema>(options: PublishedSiteContentOptions<S>): SiteContentVitePlugin;
|
package/dist/vite.js
ADDED
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
// Build step for static websites: bakes the owner's PUBLISHED content into the
|
|
2
|
+
// built HTML — the title and description search engines and link previews read,
|
|
3
|
+
// and a snapshot the site shows on its first paint (it then checks for a newer
|
|
4
|
+
// one). Use it from vite.config.ts:
|
|
5
|
+
//
|
|
6
|
+
// publishedSiteContent({ schema: siteContent, env: loadEnv(mode, process.cwd(), 'VITE_'),
|
|
7
|
+
// meta: (v) => ({ title: v['seo.title'], description: v['seo.description'] }) })
|
|
8
|
+
//
|
|
9
|
+
// When a backend is configured but cannot be reached, the build FAILS: the host
|
|
10
|
+
// then keeps the site it already serves, with its real published text, instead
|
|
11
|
+
// of replacing it with the defaults. SITE_CONTENT_OPTIONAL=1 builds with the
|
|
12
|
+
// defaults on purpose (for example to ship a fix during a Supabase outage).
|
|
13
|
+
//
|
|
14
|
+
// It also checks the two connection variables first (checkBackendEnv): a
|
|
15
|
+
// half-set pair, a misnamed variable, a dashboard address or a secret key stops
|
|
16
|
+
// the build — none of them can be bypassed — and a build with no backend at all
|
|
17
|
+
// says plainly that online booking will be off on the live site.
|
|
18
|
+
import { BACKEND_ENV, BACKEND_ENV_ALIASES, checkBackendEnv } from './config.js';
|
|
19
|
+
import { resolveSiteContent } from './content/resolve.js';
|
|
20
|
+
import { siteContentSnapshotTag } from './content/store.js';
|
|
21
|
+
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
|
|
22
|
+
const escapeHtml = (s) => s.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"');
|
|
23
|
+
function isSnapshot(v) {
|
|
24
|
+
const s = v;
|
|
25
|
+
return Boolean(s && typeof s.version === 'number' && s.values && typeof s.values === 'object' && !Array.isArray(s.values));
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Fetches the published content for a build. Resolves with the snapshot, or
|
|
29
|
+
* with "defaults" when there is no backend / no content yet; THROWS when a
|
|
30
|
+
* configured backend cannot be read (after three attempts).
|
|
31
|
+
*/
|
|
32
|
+
export async function loadPublishedSiteContent(options) {
|
|
33
|
+
const url = options.url?.trim().replace(/\/+$/, '');
|
|
34
|
+
const key = options.key?.trim();
|
|
35
|
+
if (!url || !key)
|
|
36
|
+
return { kind: 'defaults', reason: 'no backend configured (VITE_SUPABASE_URL / VITE_SUPABASE_PUBLISHABLE_KEY)' };
|
|
37
|
+
const doFetch = options.fetch ?? fetch;
|
|
38
|
+
const delays = options.retryDelaysMs ?? [1000, 3000];
|
|
39
|
+
let problem = '';
|
|
40
|
+
for (let attempt = 0; attempt <= delays.length; attempt++) {
|
|
41
|
+
if (attempt > 0)
|
|
42
|
+
await sleep(delays[attempt - 1] ?? 0);
|
|
43
|
+
try {
|
|
44
|
+
const res = await doFetch(`${url}/rest/v1/rpc/booking_get_site_content`, {
|
|
45
|
+
method: 'POST',
|
|
46
|
+
headers: { apikey: key, Authorization: `Bearer ${key}`, 'Content-Type': 'application/json' },
|
|
47
|
+
body: '{}',
|
|
48
|
+
signal: AbortSignal.timeout(options.timeoutMs ?? 10_000),
|
|
49
|
+
});
|
|
50
|
+
const text = await res.text();
|
|
51
|
+
if (res.ok) {
|
|
52
|
+
const body = JSON.parse(text);
|
|
53
|
+
if (isSnapshot(body))
|
|
54
|
+
return { kind: 'published', snapshot: body };
|
|
55
|
+
problem = 'unexpected answer';
|
|
56
|
+
continue;
|
|
57
|
+
}
|
|
58
|
+
// The database has no website content yet (engine schema before 2.0.0-alpha.7): nothing was ever published.
|
|
59
|
+
if (res.status === 404 && text.includes('PGRST202')) {
|
|
60
|
+
return { kind: 'defaults', reason: 'the database has no website content yet (engine schema before 2.0.0-alpha.7)' };
|
|
61
|
+
}
|
|
62
|
+
problem = `HTTP ${res.status}`;
|
|
63
|
+
if (res.status === 401 || res.status === 403)
|
|
64
|
+
break; // a wrong key does not fix itself
|
|
65
|
+
}
|
|
66
|
+
catch (error) {
|
|
67
|
+
problem = error.name === 'TimeoutError' ? 'timed out' : error.message;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
throw new Error(`Could not load the published website content from ${url} (${problem}). The build stops so the live site keeps ` +
|
|
71
|
+
'its current text and search details. Fix the connection and build again — or set SITE_CONTENT_OPTIONAL=1 to build ' +
|
|
72
|
+
"with the website's default content on purpose.");
|
|
73
|
+
}
|
|
74
|
+
/** Why the build stopped, with what to do — names only, never values. */
|
|
75
|
+
export function backendEnvError(problems) {
|
|
76
|
+
return new Error(`The website's Supabase connection is not usable:\n${problems.map((p) => ` - ${p}`).join('\n')}\n` +
|
|
77
|
+
'Fix the environment variables (locally: .env.local; on Hostinger: the website\'s environment variables) and build again. ' +
|
|
78
|
+
'The build stops so a broken or unsafe site is never published.');
|
|
79
|
+
}
|
|
80
|
+
/** Vite plugin: published content → snapshot + title/description in the built HTML. */
|
|
81
|
+
export function publishedSiteContent(options) {
|
|
82
|
+
const pages = options.pages ?? ['/index.html'];
|
|
83
|
+
let snapshot = null;
|
|
84
|
+
const processEnv = () => globalThis.process?.env ?? {};
|
|
85
|
+
const optional = () => processEnv().SITE_CONTENT_OPTIONAL === '1';
|
|
86
|
+
return {
|
|
87
|
+
name: 'booking-engine:published-site-content',
|
|
88
|
+
apply: 'build',
|
|
89
|
+
async buildStart() {
|
|
90
|
+
// Misnamed variables (e.g. without VITE_) are only visible in the build's own environment.
|
|
91
|
+
const aliases = Object.fromEntries(Object.keys(BACKEND_ENV_ALIASES).map((name) => [name, processEnv()[name]]));
|
|
92
|
+
const env = checkBackendEnv({ ...aliases, ...options.env });
|
|
93
|
+
if (env.status === 'invalid')
|
|
94
|
+
throw backendEnvError(env.problems);
|
|
95
|
+
if (env.status === 'none') {
|
|
96
|
+
console.warn(`[booking-engine] No backend in this build (${BACKEND_ENV.url} and ${BACKEND_ENV.key} are not set): the live site ` +
|
|
97
|
+
'will say “Online booking is not available yet” and the dashboard will ask to connect a backend. Set both ' +
|
|
98
|
+
'variables in your host (on Hostinger: the website\'s environment variables) and build again to go live.');
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
try {
|
|
102
|
+
const loaded = await loadPublishedSiteContent({ url: options.env.VITE_SUPABASE_URL, key: options.env.VITE_SUPABASE_PUBLISHABLE_KEY });
|
|
103
|
+
if (loaded.kind === 'published') {
|
|
104
|
+
snapshot = loaded.snapshot;
|
|
105
|
+
console.info(`[booking-engine] published website content: version ${snapshot.version}`);
|
|
106
|
+
}
|
|
107
|
+
else {
|
|
108
|
+
console.warn(`[booking-engine] website content: building with the defaults — ${loaded.reason}`);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
catch (error) {
|
|
112
|
+
if (!optional())
|
|
113
|
+
throw error;
|
|
114
|
+
console.warn(`[booking-engine] SITE_CONTENT_OPTIONAL=1: building with the defaults. ${error.message}`);
|
|
115
|
+
}
|
|
116
|
+
},
|
|
117
|
+
transformIndexHtml(html, ctx) {
|
|
118
|
+
if (!pages.includes(ctx.path))
|
|
119
|
+
return html;
|
|
120
|
+
const values = resolveSiteContent(options.schema, snapshot?.values ?? null, () => null);
|
|
121
|
+
const meta = options.meta?.(values) ?? {};
|
|
122
|
+
let out = html;
|
|
123
|
+
// Function replacements: owner text such as "from $25" must never be read as "$2" (a regex group).
|
|
124
|
+
const setMeta = (attr, key, value, insert) => {
|
|
125
|
+
const re = new RegExp(`(<meta\\s+${attr}="${key}"\\s+content=")[^"]*(")`);
|
|
126
|
+
if (re.test(out))
|
|
127
|
+
out = out.replace(re, (_m, open, close) => `${open}${value}${close}`);
|
|
128
|
+
else if (insert)
|
|
129
|
+
out = out.replace('</head>', () => ` <meta ${attr}="${key}" content="${value}" />\n </head>`);
|
|
130
|
+
};
|
|
131
|
+
if (meta.title) {
|
|
132
|
+
const title = escapeHtml(meta.title);
|
|
133
|
+
out = /<title>[\s\S]*?<\/title>/.test(out)
|
|
134
|
+
? out.replace(/<title>[\s\S]*?<\/title>/, () => `<title>${title}</title>`)
|
|
135
|
+
: out.replace('</head>', () => ` <title>${title}</title>\n </head>`);
|
|
136
|
+
setMeta('property', 'og:title', title, false);
|
|
137
|
+
}
|
|
138
|
+
if (meta.description) {
|
|
139
|
+
const description = escapeHtml(meta.description);
|
|
140
|
+
setMeta('name', 'description', description, true);
|
|
141
|
+
setMeta('property', 'og:description', description, false);
|
|
142
|
+
}
|
|
143
|
+
if (snapshot) {
|
|
144
|
+
const tag = siteContentSnapshotTag(snapshot, options.id);
|
|
145
|
+
out = out.replace('</head>', () => ` ${tag}\n </head>`);
|
|
146
|
+
}
|
|
147
|
+
return out;
|
|
148
|
+
},
|
|
149
|
+
};
|
|
150
|
+
}
|