@ticketboothapp/booking 0.1.10 → 0.1.11

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ticketboothapp/booking",
3
- "version": "0.1.10",
3
+ "version": "0.1.11",
4
4
  "private": false,
5
5
  "sideEffects": false,
6
6
  "publishConfig": {
@@ -1,6 +1,13 @@
1
1
  'use client';
2
2
 
3
- import { useMemo, type CSSProperties } from 'react';
3
+ import { useState, useEffect, useLayoutEffect } from 'react';
4
+ import { formatBookingRefForDisplay } from '@/lib/booking-ref';
5
+ import { BookingDetails, type BookingData } from '@/components/BookingDetails';
6
+ import { type Currency } from '@/components/CurrencySwitcher';
7
+ import { ENV } from '@/lib/env';
8
+ import { formatCurrencyAmount } from '@/lib/currency';
9
+
10
+ const API_URL = ENV.API_URL;
4
11
 
5
12
  /** Provider-only fields merged after public manage lookup (e.g. embedded in provider dashboard). */
6
13
  export interface StaffBookingAttribution {
@@ -18,58 +25,369 @@ export interface StaffBookingAttribution {
18
25
  }
19
26
 
20
27
  export interface ManageBookingViewProps {
28
+ /** Initial booking reference (ref or bookingReference from URL) */
21
29
  initialRef?: string;
30
+ /** Initial last name (from URL or success callback) */
22
31
  initialLastName?: string;
32
+ /** Reservation reference (e.g. after Stripe redirect; triggers poll until booking exists) */
23
33
  initialReservationRef?: string;
34
+ /** True when returning from balance payment success (Stripe redirect); triggers polling to catch webhook update */
24
35
  initialPaymentSuccess?: boolean;
36
+ /** When provided (e.g. manage page), called to replace URL when poll finds booking; when undefined (e.g. dialog), no navigation */
25
37
  replaceUrl?: (url: string) => void;
38
+ /** When provided (e.g. dialog), show compact layout and optionally a close button. Set showCloseButton false when the host provides its own close (e.g. dialog X). */
26
39
  onClose?: () => void;
40
+ /** When false, do not render the in-content close button (e.g. host dialog already has an X). Default true when onClose is set. */
27
41
  showCloseButton?: boolean;
42
+ /**
43
+ * When embedded with staff auth, fetch attribution (partner portal vs main site, partner/agent, commission)
44
+ * after the public booking loads. Uses provider GET /1/bookings/:ref — implement in the host app.
45
+ */
28
46
  fetchStaffAttribution?: (bookingReference: string) => Promise<StaffBookingAttribution | null>;
29
47
  }
30
48
 
31
- const getBookingHost = (): string =>
32
- process.env.NEXT_PUBLIC_BOOKING_WIDGET_URL || 'https://viaviamorainelake.com';
49
+ function staffChannelLabel(source: string): string {
50
+ const labels: Record<string, string> = {
51
+ DASHBOARD: 'Dashboard',
52
+ GYG: 'GetYourGuide',
53
+ WEBSITE: 'Website (main site)',
54
+ PARTNER_PORTAL: 'Website (main site)',
55
+ WEBSITE_PARTNER_PORTAL: 'Partner booking portal',
56
+ AFFILIATE: 'Affiliate',
57
+ VIATOR: 'Viator',
58
+ };
59
+ return labels[source] ?? source;
60
+ }
33
61
 
34
62
  export function ManageBookingView({
35
- initialRef = '',
36
- initialLastName = '',
37
- initialReservationRef = '',
63
+ initialRef: initialRefProp = '',
64
+ initialLastName: initialLastNameProp = '',
65
+ initialReservationRef: initialReservationRefProp = '',
38
66
  initialPaymentSuccess = false,
67
+ replaceUrl,
39
68
  onClose,
40
69
  showCloseButton = true,
70
+ fetchStaffAttribution,
41
71
  }: ManageBookingViewProps) {
42
- const src = useMemo(() => {
43
- const url = new URL('/manage', getBookingHost());
44
- if (initialRef) url.searchParams.set('ref', initialRef);
45
- if (initialLastName) url.searchParams.set('lastName', initialLastName);
46
- if (initialReservationRef) url.searchParams.set('reservationRef', initialReservationRef);
47
- if (initialPaymentSuccess) url.searchParams.set('payment', 'balance_success');
48
- return url.toString();
49
- }, [initialLastName, initialPaymentSuccess, initialRef, initialReservationRef]);
50
-
51
- const frameStyle: CSSProperties = {
52
- width: '100%',
53
- minHeight: '700px',
54
- border: 0,
55
- display: 'block',
56
- backgroundColor: 'transparent',
57
- };
72
+ const [bookingReference, setBookingReference] = useState(initialRefProp);
73
+ const [lastName, setLastName] = useState(initialLastNameProp);
74
+ const [booking, setBooking] = useState<BookingData | null>(null);
75
+ const [staffAttribution, setStaffAttribution] = useState<StaffBookingAttribution | null>(null);
76
+ const [loading, setLoading] = useState(false);
77
+ const [error, setError] = useState('');
78
+
79
+ useLayoutEffect(() => {
80
+ setBookingReference(initialRefProp ? formatBookingRefForDisplay(initialRefProp) || initialRefProp : initialRefProp);
81
+ setLastName(initialLastNameProp);
82
+ }, [initialRefProp, initialLastNameProp]);
83
+
84
+ const initialRef = initialRefProp.trim();
85
+ const initialLastName = initialLastNameProp.trim();
86
+ const initialReservationRef = (initialReservationRefProp || '').trim();
87
+
88
+ useEffect(() => {
89
+ if (!booking?.bookingReference || !fetchStaffAttribution) {
90
+ setStaffAttribution(null);
91
+ return;
92
+ }
93
+ let cancelled = false;
94
+ const ref = formatBookingRefForDisplay(booking.bookingReference) || booking.bookingReference;
95
+ fetchStaffAttribution(ref)
96
+ .then((a) => {
97
+ if (!cancelled) setStaffAttribution(a);
98
+ })
99
+ .catch(() => {
100
+ if (!cancelled) setStaffAttribution(null);
101
+ });
102
+ return () => {
103
+ cancelled = true;
104
+ };
105
+ }, [booking?.bookingReference, fetchStaffAttribution]);
106
+
107
+ useEffect(() => {
108
+ if (!initialRef || !initialLastName) return;
109
+ let cancelled = false;
110
+ const POLL_DELAY_MS = 2000;
111
+ const MAX_POLL_ATTEMPTS = 3;
112
+
113
+ const fetchBooking = async (): Promise<BookingData | null> => {
114
+ const response = await fetch(
115
+ `${API_URL}/1/public/bookings/${encodeURIComponent(initialRef)}?lastName=${encodeURIComponent(initialLastName)}`,
116
+ { method: 'GET', headers: { 'Content-Type': 'application/json' } }
117
+ );
118
+ if (!response.ok) {
119
+ if (response.status === 404) throw new Error('Booking not found or last name does not match');
120
+ const err = await response.json();
121
+ throw new Error(err.errorMessage || err.error || 'Failed to lookup booking');
122
+ }
123
+ const data = await response.json();
124
+ return data.data as BookingData;
125
+ };
126
+
127
+ const doLookup = async () => {
128
+ setLoading(true);
129
+ setError('');
130
+ setBooking(null);
131
+ try {
132
+ let b = await fetchBooking();
133
+ if (cancelled) return;
134
+ setBooking(b);
135
+
136
+ const hasPaymentOwing = (booking: BookingData) => {
137
+ const status = booking?.payment?.status;
138
+ const balanceAmount = booking?.payment?.plan?.balanceAmount ?? 0;
139
+ const depositAmount = booking?.payment?.plan?.depositAmount ?? 0;
140
+ return (status === 'DEPOSIT_PAID' && balanceAmount > 0) ||
141
+ (status === 'AWAITING_PAYMENT' && (depositAmount > 0 || balanceAmount > 0));
142
+ };
143
+
144
+ if (initialPaymentSuccess && b && hasPaymentOwing(b)) {
145
+ for (let attempt = 1; attempt < MAX_POLL_ATTEMPTS && !cancelled; attempt++) {
146
+ await new Promise((r) => setTimeout(r, POLL_DELAY_MS));
147
+ if (cancelled) return;
148
+ b = await fetchBooking();
149
+ if (cancelled) return;
150
+ setBooking(b);
151
+ if (!b || !hasPaymentOwing(b)) break;
152
+ }
153
+ replaceUrl?.(`/manage?ref=${encodeURIComponent(initialRef)}&lastName=${encodeURIComponent(initialLastName)}`);
154
+ }
155
+ } catch (err) {
156
+ if (!cancelled) setError(err instanceof Error ? err.message : 'Failed to lookup booking');
157
+ } finally {
158
+ if (!cancelled) setLoading(false);
159
+ }
160
+ };
161
+ doLookup();
162
+ return () => { cancelled = true; };
163
+ }, [initialRef, initialLastName, initialPaymentSuccess, replaceUrl]);
164
+
165
+ useEffect(() => {
166
+ if (!initialReservationRef || !initialLastName || initialRef) return;
167
+ let cancelled = false;
168
+ const POLL_MS = 1500;
169
+ const MAX_MS = 30000;
170
+ let attempt = 0;
171
+ const doLookup = async () => {
172
+ setLoading(true);
173
+ setError('');
174
+ setBooking(null);
175
+ const poll = async (): Promise<void> => {
176
+ if (cancelled) return;
177
+ try {
178
+ const response = await fetch(
179
+ `${API_URL}/1/public/bookings/by-reservation?reservationRef=${encodeURIComponent(initialReservationRef)}&lastName=${encodeURIComponent(initialLastName)}`,
180
+ { method: 'GET', headers: { 'Content-Type': 'application/json' } }
181
+ );
182
+ if (cancelled) return;
183
+ if (response.ok) {
184
+ const data = await response.json();
185
+ const b = data.data as BookingData;
186
+ if (b?.bookingReference && !cancelled) {
187
+ const shortRef = formatBookingRefForDisplay(b.bookingReference);
188
+ replaceUrl?.(`/manage?ref=${encodeURIComponent(shortRef)}&lastName=${encodeURIComponent(initialLastName)}`);
189
+ setBookingReference(shortRef);
190
+ setBooking(b);
191
+ setLoading(false);
192
+ return;
193
+ }
194
+ }
195
+ } catch { }
196
+ if (cancelled) return;
197
+ attempt += 1;
198
+ if (attempt * POLL_MS < MAX_MS) {
199
+ setTimeout(poll, POLL_MS);
200
+ } else {
201
+ if (!cancelled) setError('Booking not found. Enter your booking reference from the confirmation email when it arrives.');
202
+ setLoading(false);
203
+ }
204
+ };
205
+ await poll();
206
+ };
207
+ doLookup();
208
+ return () => { cancelled = true; };
209
+ }, [initialReservationRef, initialLastName, initialRef, replaceUrl]);
210
+
211
+ async function handleLookup() {
212
+ if (!bookingReference.trim() || !lastName.trim()) {
213
+ setError('Please enter both booking reference and last name');
214
+ return;
215
+ }
216
+
217
+ setLoading(true);
218
+ setError('');
219
+ setBooking(null);
220
+
221
+ try {
222
+ const response = await fetch(
223
+ `${API_URL}/1/public/bookings/${encodeURIComponent(bookingReference.trim())}?lastName=${encodeURIComponent(lastName.trim())}`,
224
+ { method: 'GET', headers: { 'Content-Type': 'application/json' } }
225
+ );
226
+
227
+ if (!response.ok) {
228
+ if (response.status === 404) throw new Error('Booking not found or last name does not match');
229
+ const err = await response.json();
230
+ throw new Error(err.errorMessage || err.error || 'Failed to lookup booking');
231
+ }
232
+
233
+ const data = await response.json();
234
+ setBooking(data.data);
235
+ } catch (err) {
236
+ setError(err instanceof Error ? err.message : 'Failed to lookup booking');
237
+ } finally {
238
+ setLoading(false);
239
+ }
240
+ }
241
+
242
+ async function handleRefetch() {
243
+ if (!bookingReference.trim() || !lastName.trim()) return;
244
+ try {
245
+ const response = await fetch(
246
+ `${API_URL}/1/public/bookings/${encodeURIComponent(bookingReference.trim())}?lastName=${encodeURIComponent(lastName.trim())}`,
247
+ { method: 'GET', headers: { 'Content-Type': 'application/json' } }
248
+ );
249
+ if (response.ok) {
250
+ const data = await response.json();
251
+ setBooking(data.data);
252
+ }
253
+ } catch {
254
+ // ignore
255
+ }
256
+ }
257
+
258
+ const isEmbed = !!onClose;
259
+ const showClose = isEmbed && showCloseButton;
260
+ const containerClass = isEmbed ? 'flex flex-col p-4' : 'min-h-screen bg-gradient-to-b from-stone-100 to-stone-200 flex flex-col items-center justify-center gap-4 p-4';
261
+ const formOuterClass = isEmbed ? '' : 'min-h-screen bg-gradient-to-b from-stone-100 to-stone-200 flex items-center justify-center p-4';
262
+
263
+ if (booking) {
264
+ return (
265
+ <div className={isEmbed ? 'w-full max-w-2xl' : 'min-h-screen bg-gradient-to-b from-stone-100 to-stone-200'}>
266
+ {showClose && (
267
+ <div className="flex justify-end mb-2">
268
+ <button
269
+ type="button"
270
+ onClick={onClose}
271
+ className="text-sm hover:opacity-80"
272
+ style={{ color: 'var(--booking-text-muted)' }}
273
+ >
274
+ Close
275
+ </button>
276
+ </div>
277
+ )}
278
+ {staffAttribution ? (
279
+ <div
280
+ className="mb-4 rounded-xl border border-stone-200 bg-stone-50 px-4 py-3 text-sm text-stone-800"
281
+ role="region"
282
+ aria-label="Staff booking attribution"
283
+ >
284
+ <p className="font-semibold text-stone-900 mb-2">Booking attribution</p>
285
+ <p>
286
+ <span className="text-stone-600">Channel:</span> {staffChannelLabel(staffAttribution.reportingSource)}
287
+ </p>
288
+ {staffAttribution.partnerId ? (
289
+ <p>
290
+ <span className="text-stone-600">Partner:</span>{' '}
291
+ {staffAttribution.partnerName
292
+ ? `${staffAttribution.partnerName} (${staffAttribution.partnerId})`
293
+ : staffAttribution.partnerId}
294
+ </p>
295
+ ) : null}
296
+ {staffAttribution.agentDisplay || staffAttribution.agentId ? (
297
+ <p>
298
+ <span className="text-stone-600">Agent:</span>{' '}
299
+ {staffAttribution.agentDisplay ?? staffAttribution.agentId}
300
+ </p>
301
+ ) : null}
302
+ {staffAttribution.commissionAmount != null && staffAttribution.commissionAmount > 0 ? (
303
+ <p>
304
+ <span className="text-stone-600">Commission:</span>{' '}
305
+ {formatCurrencyAmount(staffAttribution.commissionAmount, staffAttribution.bookingCurrency as Currency)}
306
+ {staffAttribution.commissionRate != null && staffAttribution.commissionRate > 0 ? (
307
+ <>
308
+ {' '}
309
+ ({Math.round(staffAttribution.commissionRate * 100)}% of{' '}
310
+ {staffAttribution.commissionUsesPartnerRate && staffAttribution.commissionBaseAmount != null
311
+ ? `${formatCurrencyAmount(staffAttribution.commissionBaseAmount, staffAttribution.bookingCurrency as Currency)} pre-tax`
312
+ : 'booking total'}
313
+ )
314
+ </>
315
+ ) : null}
316
+ </p>
317
+ ) : null}
318
+ </div>
319
+ ) : null}
320
+ <BookingDetails booking={booking} currency={booking.receipt.currency as Currency} onRefetch={handleRefetch} />
321
+ </div>
322
+ );
323
+ }
58
324
 
59
325
  return (
60
- <div className="w-full">
61
- {onClose && showCloseButton ? (
62
- <div className="mb-2 flex justify-end">
326
+ <div className={formOuterClass || undefined}>
327
+ <div className="p-8 max-w-md w-full shadow-xl" style={{ backgroundColor: 'var(--booking-surface)', borderRadius: 'var(--booking-radius)' }}>
328
+ {isEmbed && (
329
+ <div className={`flex justify-between items-center mb-4 ${showClose ? '' : 'justify-start'}`}>
330
+ <h1 className="text-2xl font-bold" style={{ color: 'var(--booking-text)' }}>Manage Your Booking</h1>
331
+ {showClose && (
332
+ <button type="button" onClick={onClose} className="text-sm hover:opacity-80" style={{ color: 'var(--booking-text-muted)' }}>
333
+ Close
334
+ </button>
335
+ )}
336
+ </div>
337
+ )}
338
+ {!isEmbed && <h1 className="text-2xl font-bold mb-6" style={{ color: 'var(--booking-text)' }}>Manage Your Booking</h1>}
339
+
340
+ <form onSubmit={(e) => { e.preventDefault(); handleLookup(); }} className="space-y-4">
341
+ <div>
342
+ <label htmlFor="manage-booking-ref" className="block text-sm font-medium mb-1" style={{ color: 'var(--booking-text)' }}>
343
+ Booking Reference
344
+ </label>
345
+ <input
346
+ id="manage-booking-ref"
347
+ type="text"
348
+ value={bookingReference}
349
+ onChange={(e) => setBookingReference(formatBookingRefForDisplay(e.target.value) || e.target.value)}
350
+ className="w-full px-4 py-2 rounded-lg focus:ring-2"
351
+ style={{ borderColor: 'var(--booking-border-input)', borderWidth: '1px', borderStyle: 'solid' }}
352
+ placeholder="e.g., ABC12345"
353
+ required
354
+ />
355
+ </div>
356
+ <div>
357
+ <label htmlFor="manage-booking-lastname" className="block text-sm font-medium mb-1" style={{ color: 'var(--booking-text)' }}>
358
+ Last Name
359
+ </label>
360
+ <input
361
+ id="manage-booking-lastname"
362
+ type="text"
363
+ value={lastName}
364
+ onChange={(e) => setLastName(e.target.value)}
365
+ className="w-full px-4 py-2 rounded-lg focus:ring-2"
366
+ style={{ borderColor: 'var(--booking-border-input)', borderWidth: '1px', borderStyle: 'solid' }}
367
+ placeholder="As entered at booking"
368
+ required
369
+ />
370
+ </div>
371
+ {error && (
372
+ <div className="rounded-lg p-3 text-sm" style={{ backgroundColor: 'var(--booking-error-bg)', border: '1px solid var(--booking-error-border)', color: 'var(--booking-error-text)' }}>{error}</div>
373
+ )}
63
374
  <button
64
- type="button"
65
- onClick={onClose}
66
- className="rounded border border-stone-300 px-3 py-1 text-sm hover:bg-stone-100"
375
+ type="submit"
376
+ disabled={loading}
377
+ className="w-full py-3 text-white font-semibold rounded-lg transition-colors disabled:cursor-not-allowed"
378
+ style={{ backgroundColor: loading ? 'var(--booking-border-input)' : 'var(--booking-primary)' }}
67
379
  >
68
- Close
380
+ {loading ? 'Looking up...' : 'View Booking'}
69
381
  </button>
70
- </div>
71
- ) : null}
72
- <iframe title="Manage booking" src={src} style={frameStyle} loading="lazy" />
382
+ </form>
383
+
384
+ {!isEmbed && (
385
+ <div className="mt-6 pt-6 text-center text-sm" style={{ borderColor: 'var(--booking-border)', borderTopWidth: '1px', borderTopStyle: 'solid', color: 'var(--booking-text-muted)' }}>
386
+ <p>Enter your booking reference and last name to view your booking details.</p>
387
+ <p className="mt-1" style={{ color: 'var(--booking-text-muted)' }}>You can also share a link: /manage?ref=ABC12345&amp;lastName=Smith</p>
388
+ </div>
389
+ )}
390
+ </div>
73
391
  </div>
74
392
  );
75
393
  }