fix: booking duration calculation, tip page auth, and UI polish across frontend

Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent)

Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
This commit is contained in:
2026-05-25 18:04:43 +01:00
co-authored by Sisyphus
parent 2a8f04e7b3
commit f553ebad99
14 changed files with 468 additions and 225 deletions
+4 -2
View File
@@ -2323,8 +2323,10 @@ func ConfirmBookingHandler(w http.ResponseWriter, r *http.Request) {
if dav.Service != nil { if dav.Service != nil {
var durationMinutes int var durationMinutes int
db.DB.QueryRow(r.Context(), ` db.DB.QueryRow(r.Context(), `
SELECT COALESCE(SUM(override_duration_minutes), (SELECT SUM(duration_minutes) FROM booking_services WHERE booking_id = $1)) SELECT COALESCE(SUM(COALESCE(bs.override_duration_minutes, s.duration_minutes)), 60)
FROM booking_services WHERE booking_id = $1 FROM booking_services bs
JOIN services s ON bs.service_id = s.id
WHERE bs.booking_id = $1
`, bookingID).Scan(&durationMinutes) `, bookingID).Scan(&durationMinutes)
if durationMinutes == 0 { if durationMinutes == 0 {
durationMinutes = 60 durationMinutes = 60
+1 -1
View File
@@ -45,7 +45,7 @@ func GenerateTestToken(userID, role string) string {
} }
func GenerateAdminToken() string { func GenerateAdminToken() string {
return GenerateTestToken("admin-test-001", "admin") return GenerateTestToken("admintest001", "admin")
} }
func GenerateVerifiedUserToken(userID string) string { func GenerateVerifiedUserToken(userID string) string {
@@ -5,10 +5,11 @@
import { toast } from 'svelte-sonner'; import { toast } from 'svelte-sonner';
import * as Modal from '$lib/components/ui/dialog'; import * as Modal from '$lib/components/ui/dialog';
import { Button } from '$lib/components/ui/button'; import { Button } from '$lib/components/ui/button';
import { Input } from '$lib/components/ui/input';
import * as Textarea from '$lib/components/ui/textarea'; import * as Textarea from '$lib/components/ui/textarea';
import * as Label from '$lib/components/ui/label'; import * as Label from '$lib/components/ui/label';
import DatePicker from '$lib/components/booking/DatePicker.svelte'; import DatePicker from '$lib/components/booking/DatePicker.svelte';
import PaymentModal from '$lib/components/payments/PaymentModal.svelte'; import UserPaymentModal from '$lib/components/payments/UserPaymentModal.svelte';
import type { Booking, WorkingHoursDay, AvailableHoursDay } from '$lib/types/booking'; import type { Booking, WorkingHoursDay, AvailableHoursDay } from '$lib/types/booking';
import { extractBookedSlots, getLunchProtectionForSlots } from '$lib/lunchProtection'; import { extractBookedSlots, getLunchProtectionForSlots } from '$lib/lunchProtection';
@@ -42,8 +43,10 @@
const maxCalendarDate = new CalendarDate(maxDate.getFullYear(), maxDate.getMonth() + 1, maxDate.getDate()); const maxCalendarDate = new CalendarDate(maxDate.getFullYear(), maxDate.getMonth() + 1, maxDate.getDate());
let reschedulePlaceholder = $state<CalendarDate>(minDate); let reschedulePlaceholder = $state<CalendarDate>(minDate);
// IMPORTANT: Use override_duration_minutes when present — services may have been
// customised at booking time. Showing base values misleads users about what was booked.
let totalDuration = $derived( let totalDuration = $derived(
selectedBooking?.services?.reduce((sum, service) => sum + (service.duration_minutes || 0), 0) || 0 selectedBooking?.services?.reduce((sum, service) => sum + (service.override_duration_minutes ?? service.duration_minutes ?? 0), 0) || 0
); );
const rescheduleLunchProtection = $derived(() => { const rescheduleLunchProtection = $derived(() => {
@@ -106,8 +109,85 @@
['confirmed', 'pending'].includes(selectedBooking.status) ['confirmed', 'pending'].includes(selectedBooking.status)
); );
let isCompleted = $derived(selectedBooking?.status === 'completed');
let showPaymentModal = $state(false); let showPaymentModal = $state(false);
let showTipModal = $state(false);
let tipAmount = $state<number>(0);
let selectedTipPreset = $state<number | null>(null);
let customTipInput = $state('');
let tipProcessing = $state(false);
let canSaveCards = $derived(
authStore.currentUser?.role === 'verified_email' ||
authStore.currentUser?.role === 'affiliate'
);
let tipPresets = $derived(selectedBooking ? [
{ pct: 10, amount: Math.round(selectedBooking.total_amount * 0.10 * 100) / 100 },
{ pct: 15, amount: Math.round(selectedBooking.total_amount * 0.15 * 100) / 100 },
{ pct: 20, amount: Math.round(selectedBooking.total_amount * 0.20 * 100) / 100 }
] : []);
function selectTipPreset(amount: number) {
selectedTipPreset = amount;
customTipInput = '';
tipAmount = amount;
}
function handleCustomTip(e: Event) {
const input = e.target as HTMLInputElement;
const cleaned = input.value.replace(/[^0-9.]/g, '');
const firstDot = cleaned.indexOf('.');
let sanitized: string;
if (firstDot !== -1) {
const integerPart = cleaned.substring(0, firstDot);
const decimalPart = cleaned.substring(firstDot + 1).replace(/\./g, '');
sanitized = integerPart + '.' + decimalPart;
} else {
sanitized = cleaned;
}
if (/^\d+(\.\d{0,2})?$/.test(sanitized) || sanitized === '') {
customTipInput = sanitized;
}
selectedTipPreset = null;
tipAmount = parseFloat(customTipInput) || 0;
}
async function submitTip() {
if (!selectedBooking) return;
if (tipAmount <= 0) {
toast.error('Please select a tip amount');
return;
}
tipProcessing = true;
try {
const response = await fetch(`/api/bookings/${selectedBooking.id}/tip`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${authStore.currentToken}`
},
body: JSON.stringify({
amount: Math.round(tipAmount * 100),
card_token: 'placeholder'
})
});
if (!response.ok) {
const errorText = await response.text();
throw new Error(errorText || 'Tip payment failed');
}
toast.success('Thank you for your tip!');
showTipModal = false;
fetchBookingDetails();
} catch (err) {
toast.error(err instanceof Error ? err.message : 'Tip payment failed');
} finally {
tipProcessing = false;
}
}
function handlePaymentComplete() { function handlePaymentComplete() {
toast.success('Payment completed'); toast.success('Payment completed');
showPaymentModal = false; showPaymentModal = false;
@@ -244,6 +324,23 @@
return slots.length === 0; return slots.length === 0;
} }
function formatPaymentMethod(method: string): string {
switch (method) {
case 'in_person_card':
return 'Card, In-person';
case 'online_square':
return 'Card, Online';
case 'cash':
return 'Cash';
case 'giftcard':
return 'Gift Card';
case 'discount':
return 'Discount';
default:
return method.replace(/_/g, ' ').replace(/\b\w/g, (c) => c.toUpperCase());
}
}
function formatTime(time: string): string { function formatTime(time: string): string {
const parts = time.split(':').map(Number); const parts = time.split(':').map(Number);
const hours = parts[0]; const hours = parts[0];
@@ -587,8 +684,8 @@
<div class="mt-1 text-sm text-gray-600">{service.service_description}</div> <div class="mt-1 text-sm text-gray-600">{service.service_description}</div>
{/if} {/if}
<div class="mt-2 flex items-center justify-between text-sm"> <div class="mt-2 flex items-center justify-between text-sm">
<span class="text-gray-600">{service.duration_minutes} min</span> <span class="text-gray-600">{service.override_duration_minutes ?? service.duration_minutes} min</span>
<span class="font-semibold">£{service.price?.toFixed(2) || '0.00'}</span> <span class="font-semibold">£{(service.override_price ?? service.price ?? 0).toFixed(2)}</span>
</div> </div>
</div> </div>
{/each} {/each}
@@ -634,7 +731,7 @@
{/if} {/if}
<div class="flex items-center justify-between"> <div class="flex items-center justify-between">
<span class="text-sm text-gray-600">Total Amount</span> <span class="text-sm text-gray-600">{selectedBooking.amount_paid > selectedBooking.total_amount ? 'Pre-tip Subtotal' : 'Total Amount'}</span>
<span class="font-semibold">£{selectedBooking.total_amount.toFixed(2)}</span> <span class="font-semibold">£{selectedBooking.total_amount.toFixed(2)}</span>
</div> </div>
<div class="flex items-center justify-between"> <div class="flex items-center justify-between">
@@ -643,18 +740,18 @@
>£{selectedBooking.amount_paid.toFixed(2)}</span >£{selectedBooking.amount_paid.toFixed(2)}</span
> >
</div> </div>
<div class="flex items-center justify-between border-t border-gray-300 pt-2"> {#if selectedBooking.amount_due > 0}
<span class="font-medium text-gray-900"> <div class="flex items-center justify-between border-t border-gray-300 pt-2">
{isFutureBooking ? 'Estimated Subtotal' : 'Amount Due'} <span class="font-medium text-gray-900">
</span> {isFutureBooking ? 'Estimated Subtotal' : 'Amount Due'}
<span </span>
class="text-lg font-bold {selectedBooking.amount_due > 0 <span
? 'text-red-600' class="text-lg font-bold text-red-600"
: 'text-green-600'}" >
> £{selectedBooking.amount_due.toFixed(2)}
£{selectedBooking.amount_due.toFixed(2)} </span>
</span> </div>
</div> {/if}
</div> </div>
</div> </div>
@@ -669,9 +766,7 @@
<div class="flex items-start justify-between"> <div class="flex items-start justify-between">
<div class="flex-1"> <div class="flex-1">
<div class="flex items-center gap-2"> <div class="flex items-center gap-2">
<span class="font-medium capitalize" <span class="font-medium">{formatPaymentMethod(payment.payment_method)}</span>
>{payment.payment_method.replace('_', ' ')}</span
>
<span <span
class="inline-flex items-center rounded-full px-2 py-0.5 text-xs font-medium class="inline-flex items-center rounded-full px-2 py-0.5 text-xs font-medium
{payment.status === 'completed' {payment.status === 'completed'
@@ -685,7 +780,10 @@
</div> </div>
<div class="mt-1 text-xs text-gray-500"> <div class="mt-1 text-xs text-gray-500">
{payment.payment_type.charAt(0).toUpperCase() + {payment.payment_type.charAt(0).toUpperCase() +
payment.payment_type.slice(1)} payment.payment_type.slice(1)} payment
{#if payment.card_last4}
, Card ending in {payment.card_last4}
{/if}
</div> </div>
{#if payment.is_vat_applicable} {#if payment.is_vat_applicable}
<div class="mt-2 text-xs text-gray-600"> <div class="mt-2 text-xs text-gray-600">
@@ -853,18 +951,14 @@
{/if} {/if}
</div> </div>
<div class="flex gap-2"> <div class="flex gap-2">
{#if selectedBooking} {#if isCompleted}
<Button <Button
variant="outline"
size="sm" size="sm"
class="flex-1" class="flex-1 hover:bg-fuchsia-50"
onclick={() => { variant="outline"
if (selectedBooking) { onclick={() => (showTipModal = true)}
window.open(`/api/bookings/${selectedBooking.id}/calendar`, '_blank');
}
}}
> >
Add to Calendar Leave a Tip
</Button> </Button>
{/if} {/if}
{#if depositOutstanding} {#if depositOutstanding}
@@ -891,10 +985,11 @@
</Modal.Root> </Modal.Root>
{#if showPaymentModal && selectedBooking} {#if showPaymentModal && selectedBooking}
<PaymentModal <UserPaymentModal
booking={selectedBooking} booking={selectedBooking}
onClose={() => (showPaymentModal = false)} onClose={() => (showPaymentModal = false)}
onComplete={handlePaymentComplete} onComplete={handlePaymentComplete}
canSaveCards={canSaveCards}
/> />
{/if} {/if}
@@ -925,3 +1020,66 @@
</Modal.Footer> </Modal.Footer>
</Modal.Content> </Modal.Content>
</Modal.Root> </Modal.Root>
<Modal.Root open={showTipModal} onOpenChange={(v) => {
if (!v) {
showTipModal = false;
tipAmount = 0;
selectedTipPreset = null;
customTipInput = '';
}
}}>
<Modal.Content class="max-w-sm">
<Modal.Header>
<Modal.Title>Leave a Tip</Modal.Title>
<Modal.Description>Show your appreciation for great service</Modal.Description>
</Modal.Header>
<div class="px-4 pb-4 space-y-4">
<div class="grid grid-cols-3 gap-3">
{#each tipPresets as preset (preset.pct)}
<button
class="rounded-lg border border-input bg-background py-3 text-center font-semibold transition-colors hover:bg-fuchsia-50 {selectedTipPreset === preset.amount ? 'bg-fuchsia-100' : ''}"
onclick={() => selectTipPreset(preset.amount)}
type="button"
>
<div>£{preset.amount.toFixed(2)}</div>
<div class="text-xs font-normal text-gray-500">{preset.pct}%</div>
</button>
{/each}
</div>
<div>
<label for="custom-tip" class="text-sm font-medium text-gray-700">Or enter custom amount</label>
<div class="relative mt-1">
<span class="absolute left-3 top-1/2 -translate-y-1/2 text-gray-500">£</span>
<Input
id="custom-tip"
type="text"
inputmode="decimal"
step="0.01"
min="0"
placeholder="0.00"
class="pl-7"
value={customTipInput}
oninput={handleCustomTip}
/>
</div>
</div>
</div>
<Modal.Footer>
<Button variant="outline" onclick={() => (showTipModal = false)}>
Cancel
</Button>
<Button
class="hover:bg-fuchsia-50"
onclick={submitTip}
disabled={tipAmount <= 0 || tipProcessing}
loading={tipProcessing}
>
{tipProcessing ? 'Processing...' : `Pay Tip £${tipAmount.toFixed(2)}`}
</Button>
</Modal.Footer>
</Modal.Content>
</Modal.Root>
@@ -18,8 +18,11 @@
let showApprovalModal = $state(false); let showApprovalModal = $state(false);
// Calculate total duration from services // Calculate total duration from services
// IMPORTANT: Use override_duration_minutes when present — services may have been
// customised at booking time (discounts, extended sessions). Showing base values
// misleads admins about what was actually booked.
let totalDuration = $derived( let totalDuration = $derived(
selectedBooking?.services?.reduce((sum, service) => sum + (service.duration_minutes || 0), 0) || selectedBooking?.services?.reduce((sum, service) => sum + (service.override_duration_minutes ?? service.duration_minutes ?? 0), 0) ||
0 0
); );
@@ -278,8 +281,8 @@
<div class="mt-1 text-sm text-gray-600">{service.service_description}</div> <div class="mt-1 text-sm text-gray-600">{service.service_description}</div>
{/if} {/if}
<div class="mt-2 flex items-center justify-between text-sm"> <div class="mt-2 flex items-center justify-between text-sm">
<span class="text-gray-600">{service.duration_minutes} min</span> <span class="text-gray-600">{service.override_duration_minutes ?? service.duration_minutes} min</span>
<span class="font-semibold">£{service.price?.toFixed(2) || '0.00'}</span> <span class="font-semibold">£{(service.override_price ?? service.price ?? 0).toFixed(2)}</span>
</div> </div>
</div> </div>
{/each} {/each}
@@ -410,9 +413,15 @@
<div class="flex items-start justify-between"> <div class="flex items-start justify-between">
<div class="flex-1"> <div class="flex-1">
<div class="flex items-center gap-2"> <div class="flex items-center gap-2">
<span class="font-medium capitalize" <span class="font-medium">
>{payment.payment_method.replace('_', ' ')}</span {#if payment.payment_method === 'online_square'}Online
> {:else if payment.payment_method === 'in_person_card'}Card Machine
{:else if payment.payment_method === 'cash'}Cash
{:else if payment.payment_method === 'giftcard'}Gift Card
{:else if payment.payment_method === 'discount'}Discount
{:else}{payment.payment_method.replace('_', ' ')}
{/if}
</span>
<span <span
class="inline-flex items-center rounded-full px-2 py-0.5 text-xs font-medium class="inline-flex items-center rounded-full px-2 py-0.5 text-xs font-medium
{payment.status === 'completed' {payment.status === 'completed'
@@ -428,7 +437,10 @@
</div> </div>
<div class="mt-1 text-xs text-gray-500"> <div class="mt-1 text-xs text-gray-500">
{payment.payment_type.charAt(0).toUpperCase() + {payment.payment_type.charAt(0).toUpperCase() +
payment.payment_type.slice(1)} payment.payment_type.slice(1)} payment
{#if payment.card_last4}
, Card ending in {payment.card_last4}
{/if}
</div> </div>
{#if payment.vendor_code || payment.invoice_number} {#if payment.vendor_code || payment.invoice_number}
<div class="mt-1 text-xs text-gray-500"> <div class="mt-1 text-xs text-gray-500">
@@ -7,6 +7,7 @@
import { Button } from '$lib/components/ui/button'; import { Button } from '$lib/components/ui/button';
import * as Card from '$lib/components/ui/card'; import * as Card from '$lib/components/ui/card';
import { Input } from '$lib/components/ui/input'; import { Input } from '$lib/components/ui/input';
import { Checkbox } from '$lib/components/ui/checkbox';
import { Separator } from '$lib/components/ui/separator'; import { Separator } from '$lib/components/ui/separator';
import * as Modal from '$lib/components/ui/dialog'; import * as Modal from '$lib/components/ui/dialog';
import * as AlertDialog from '$lib/components/ui/alert-dialog'; import * as AlertDialog from '$lib/components/ui/alert-dialog';
@@ -523,11 +524,7 @@
<tr class="border-t"> <tr class="border-t">
<td class="py-2">{weekdayLabel(row.weekday)}</td> <td class="py-2">{weekdayLabel(row.weekday)}</td>
<td class="py-2"> <td class="py-2">
<input <Checkbox bind:checked={row.is_open} />
type="checkbox"
bind:checked={row.is_open}
class="h-4 w-4 rounded border-gray-300 bg-gray-100"
/>
</td> </td>
<td class="py-2"> <td class="py-2">
<Input <Input
@@ -7,6 +7,7 @@
import { Button } from '$lib/components/ui/button'; import { Button } from '$lib/components/ui/button';
import * as Card from '$lib/components/ui/card'; import * as Card from '$lib/components/ui/card';
import { Input } from '$lib/components/ui/input'; import { Input } from '$lib/components/ui/input';
import { Checkbox } from '$lib/components/ui/checkbox';
import * as Modal from '$lib/components/ui/dialog'; import * as Modal from '$lib/components/ui/dialog';
import * as AlertDialog from '$lib/components/ui/alert-dialog'; import * as AlertDialog from '$lib/components/ui/alert-dialog';
import { Skeleton } from '$lib/components/ui/skeleton'; import { Skeleton } from '$lib/components/ui/skeleton';
@@ -475,11 +476,7 @@
<tr class="border-t"> <tr class="border-t">
<td class="py-2 text-sm">{weekdayLabel(row.weekday)}</td> <td class="py-2 text-sm">{weekdayLabel(row.weekday)}</td>
<td class="py-2"> <td class="py-2">
<input <Checkbox bind:checked={row.is_open} />
type="checkbox"
bind:checked={row.is_open}
class="h-4 w-4 rounded border-gray-300 bg-gray-100 text-primary focus:ring-primary"
/>
</td> </td>
<td class="py-2"> <td class="py-2">
<Input <Input
@@ -13,7 +13,11 @@
</script> </script>
<div class="flex justify-between"> <div class="flex justify-between">
<Button variant="outline" disabled={!canBack} onclick={() => dispatch('back')}>Back</Button> {#if canBack}
<Button variant="outline" onclick={() => dispatch('back')}>Back</Button>
{:else}
<div></div>
{/if}
{#if showSubmit} {#if showSubmit}
<Button <Button
@@ -23,6 +23,7 @@
import DatePicker from '$lib/components/booking/DatePicker.svelte'; import DatePicker from '$lib/components/booking/DatePicker.svelte';
import TimeSlotPicker from '$lib/components/booking/TimeSlotPicker.svelte'; import TimeSlotPicker from '$lib/components/booking/TimeSlotPicker.svelte';
import ServiceSelector from '$lib/components/booking/ServiceSelector.svelte'; import ServiceSelector from '$lib/components/booking/ServiceSelector.svelte';
import UserPaymentModal from '$lib/components/payments/UserPaymentModal.svelte';
import { import {
extractBookedSlots, extractBookedSlots,
getLunchProtectionForSlots, getLunchProtectionForSlots,
@@ -33,7 +34,9 @@
Service, Service,
CustomerInfo, CustomerInfo,
WorkingHoursDay, WorkingHoursDay,
AvailableHoursDay AvailableHoursDay,
BookingService,
BookingStatus
} from '$lib/types/booking'; } from '$lib/types/booking';
// =============== State Management =============== // =============== State Management ===============
@@ -73,6 +76,11 @@
let depositPaid = $state(false); let depositPaid = $state(false);
let showPaymentForm = $state(false); let showPaymentForm = $state(false);
let depositCardFormValid = $derived(
selectedPaymentMethod !== null ||
(showNewCardForm && newCardNumber.replace(/\s/g, '').length >= 13 && /^\d{2}\/\d{2}$/.test(newCardExpiry) && newCardCVC.length >= 3)
);
// Confirmation state // Confirmation state
let confirmedBooking = $state<{ let confirmedBooking = $state<{
id: string; id: string;
@@ -81,6 +89,8 @@
notes: string; notes: string;
} | null>(null); } | null>(null);
let showPayEarlyModal = $state(false);
// =============== Payment Functions =============== // =============== Payment Functions ===============
async function fetchUserDepositsRequired() { async function fetchUserDepositsRequired() {
if (!authStore.isAuthenticated) { if (!authStore.isAuthenticated) {
@@ -90,7 +100,6 @@
try { try {
const response = await fetch('/api/user', { const response = await fetch('/api/user', {
credentials: 'include',
headers: authStore.currentToken ? { Authorization: `Bearer ${authStore.currentToken}` } : {} headers: authStore.currentToken ? { Authorization: `Bearer ${authStore.currentToken}` } : {}
}); });
if (response.ok) { if (response.ok) {
@@ -114,7 +123,6 @@
try { try {
// Check for pending bookings // Check for pending bookings
const pendingResp = await fetch('/api/bookings?status=pending&perPage=1', { const pendingResp = await fetch('/api/bookings?status=pending&perPage=1', {
credentials: 'include',
headers: authStore.currentToken ? { Authorization: `Bearer ${authStore.currentToken}` } : {} headers: authStore.currentToken ? { Authorization: `Bearer ${authStore.currentToken}` } : {}
}); });
if (pendingResp.ok) { if (pendingResp.ok) {
@@ -128,7 +136,6 @@
// Check for confirmed bookings // Check for confirmed bookings
const confirmedResp = await fetch('/api/bookings?status=confirmed&perPage=1', { const confirmedResp = await fetch('/api/bookings?status=confirmed&perPage=1', {
credentials: 'include',
headers: authStore.currentToken ? { Authorization: `Bearer ${authStore.currentToken}` } : {} headers: authStore.currentToken ? { Authorization: `Bearer ${authStore.currentToken}` } : {}
}); });
if (confirmedResp.ok) { if (confirmedResp.ok) {
@@ -152,13 +159,16 @@
paymentMethodsLoading = true; paymentMethodsLoading = true;
try { try {
const response = await fetch('/api/user/payment-methods', { const response = await fetch('/api/user/payment-methods', {
credentials: 'include',
headers: authStore.currentToken ? { Authorization: `Bearer ${authStore.currentToken}` } : {} headers: authStore.currentToken ? { Authorization: `Bearer ${authStore.currentToken}` } : {}
}); });
if (response.ok) { if (response.ok) {
const data = await response.json(); const data = await response.json();
paymentMethods = data.payment_methods ?? []; paymentMethods = data.payment_methods ?? [];
if (paymentMethods.length > 0 && !selectedPaymentMethod) {
const defaultCard = paymentMethods.find((m: any) => m.is_default) ?? paymentMethods[0];
selectedPaymentMethod = defaultCard.id;
}
} else { } else {
paymentMethods = []; paymentMethods = [];
} }
@@ -222,6 +232,20 @@
return `${String(month).padStart(2, '0')}/${year.toString().slice(-2)}`; return `${String(month).padStart(2, '0')}/${year.toString().slice(-2)}`;
} }
function formatDepositCardNumber(value: string): string {
const digits = value.replace(/\D/g, '').substring(0, 16);
const groups = digits.match(/.{1,4}/g);
return groups ? groups.join(' ') : digits;
}
function formatDepositExpiry(value: string): string {
const digits = value.replace(/\D/g, '').substring(0, 4);
if (digits.length >= 3) {
return digits.substring(0, 2) + '/' + digits.substring(2);
}
return digits;
}
// Fetch user deposit and active booking status when step 1 is reached // Fetch user deposit and active booking status when step 1 is reached
$effect(() => { $effect(() => {
if (currentStep === 1 && authStore.isAuthenticated) { if (currentStep === 1 && authStore.isAuthenticated) {
@@ -945,6 +969,14 @@
selectedDate ? getDayWithOrdinal(selectedDate) : undefined selectedDate ? getDayWithOrdinal(selectedDate) : undefined
); );
let depositRequired = $derived(calculateDepositRequired());
let totalSteps = $derived(depositRequired ? 5 : 4);
let stepLabels = $derived(
depositRequired
? ['Service', 'Date & Time', 'Details', 'Payment', 'Confirmation']
: ['Service', 'Date & Time', 'Details', 'Confirmation']
);
// =============== Navigation =============== // =============== Navigation ===============
async function nextStep() { async function nextStep() {
// Step 2 -> Step 3: Re-validate slot, then reserve // Step 2 -> Step 3: Re-validate slot, then reserve
@@ -955,7 +987,7 @@
if (!reserved) return; if (!reserved) return;
} }
// Step 3 -> Step 4 (if deposit required) or Step 5 (submit booking) // Step 3 -> Step 4 (if deposit required) or Step 4 (confirmation, if no deposit)
if (currentStep === 3) { if (currentStep === 3) {
if (calculateDepositRequired()) { if (calculateDepositRequired()) {
currentStep = 4; currentStep = 4;
@@ -965,13 +997,14 @@
return; return;
} }
// Step 4 -> Step 5 (submit booking) // Step 4: if deposit required, this is payment step -> submit booking -> step 5
if (currentStep === 4) { // Step 4: if no deposit, this is confirmation step -> nothing
if (currentStep === 4 && calculateDepositRequired()) {
await submitAndProceed(); await submitAndProceed();
return; return;
} }
if (currentStep < 5) { if (currentStep < (depositRequired ? 5 : 4)) {
currentStep++; currentStep++;
setTimeout(() => { setTimeout(() => {
window.scrollTo({ top: 0, behavior: 'smooth' }); window.scrollTo({ top: 0, behavior: 'smooth' });
@@ -1074,7 +1107,7 @@
start_time: booking.start_time, start_time: booking.start_time,
notes: booking.notes || '' notes: booking.notes || ''
}; };
currentStep = 5; currentStep = depositRequired ? 5 : 4;
setTimeout(() => { setTimeout(() => {
window.scrollTo({ top: 0, behavior: 'smooth' }); window.scrollTo({ top: 0, behavior: 'smooth' });
}, 50); }, 50);
@@ -1163,7 +1196,7 @@
<StepIndicator <StepIndicator
{currentStep} {currentStep}
steps={['Service', 'Date & Time', 'Details', 'Payment', 'Confirmation']} steps={stepLabels}
/> />
<!-- Step 1: Service Selection --> <!-- Step 1: Service Selection -->
@@ -1459,14 +1492,20 @@
onclick={nextStep} onclick={nextStep}
class="bg-primary text-primary-foreground" class="bg-primary text-primary-foreground"
> >
{reservationExpired ? 'Reservation Expired' : 'Next: Review & Payment'} {#if reservationExpired}
Reservation Expired
{:else if depositRequired}
Next: Payment
{:else}
Confirm Booking
{/if}
</Button> </Button>
</Card.Footer> </Card.Footer>
</Card.Root> </Card.Root>
{/if} {/if}
<!-- Step 4: Deposit Payment (only shown if deposit required) --> <!-- Step 4: Deposit Payment (only shown if deposit required) -->
{#if currentStep === 4} {#if currentStep === 4 && depositRequired}
<Card.Root> <Card.Root>
<Card.Header> <Card.Header>
<Card.Title>Pay Your Deposit</Card.Title> <Card.Title>Pay Your Deposit</Card.Title>
@@ -1579,7 +1618,10 @@
<Label for="cardNumber">Card Number</Label> <Label for="cardNumber">Card Number</Label>
<Input <Input
id="cardNumber" id="cardNumber"
bind:value={newCardNumber} type="text"
inputmode="numeric"
value={newCardNumber}
oninput={(e) => (newCardNumber = formatDepositCardNumber((e.target as HTMLInputElement).value))}
placeholder="1234 5678 9012 3456" placeholder="1234 5678 9012 3456"
maxlength={19} maxlength={19}
/> />
@@ -1589,14 +1631,17 @@
<Label for="cardExpiry">Expiry (MM/YY)</Label> <Label for="cardExpiry">Expiry (MM/YY)</Label>
<Input <Input
id="cardExpiry" id="cardExpiry"
bind:value={newCardExpiry} type="text"
inputmode="numeric"
value={newCardExpiry}
oninput={(e) => (newCardExpiry = formatDepositExpiry((e.target as HTMLInputElement).value))}
placeholder="MM/YY" placeholder="MM/YY"
maxlength={5} maxlength={5}
/> />
</div> </div>
<div class="space-y-2"> <div class="space-y-2">
<Label for="cardCVC">CVC</Label> <Label for="cardCVC">CVC</Label>
<Input id="cardCVC" bind:value={newCardCVC} placeholder="123" maxlength={4} /> <Input id="cardCVC" type="text" inputmode="numeric" bind:value={newCardCVC} placeholder="123" maxlength={4} />
</div> </div>
</div> </div>
{#if authStore.isAuthenticated} {#if authStore.isAuthenticated}
@@ -1623,8 +1668,7 @@
Cancel Cancel
</Button> </Button>
<Button <Button
disabled={isProcessingPayment || disabled={isProcessingPayment || !depositCardFormValid}
(!selectedPaymentMethod && !newCardNumber && !showNewCardForm)}
onclick={() => processPayment(calculateDepositAmount())} onclick={() => processPayment(calculateDepositAmount())}
class="bg-primary text-primary-foreground" class="bg-primary text-primary-foreground"
> >
@@ -1641,14 +1685,14 @@
onclick={nextStep} onclick={nextStep}
class="bg-primary text-primary-foreground" class="bg-primary text-primary-foreground"
> >
{isSubmitting ? 'Processing...' : 'Skip Payment'} {isSubmitting ? 'Processing...' : 'Continue'}
</Button> </Button>
</Card.Footer> </Card.Footer>
</Card.Root> </Card.Root>
{/if} {/if}
<!-- Step 5: Confirmation --> <!-- Step 5: Confirmation (or Step 4 if no deposit required) -->
{#if currentStep === 5} {#if currentStep === 5 || (currentStep === 4 && !depositRequired)}
{#if confirmedBooking} {#if confirmedBooking}
{@const isRequested = confirmedBooking.notes && confirmedBooking.notes.length > 0} {@const isRequested = confirmedBooking.notes && confirmedBooking.notes.length > 0}
{@const bookingDate = new SvelteDate(confirmedBooking.start_time)} {@const bookingDate = new SvelteDate(confirmedBooking.start_time)}
@@ -1764,8 +1808,7 @@
You can pay when you arrive, or pay ahead of time to speed things up. You can pay when you arrive, or pay ahead of time to speed things up.
</p> </p>
<Button <Button
onclick={() => onclick={() => (showPayEarlyModal = true)}
(window.location.href = `/booking-confirmed/${confirmedBooking!.id}`)}
class="bg-emerald-600 text-white hover:bg-emerald-700" class="bg-emerald-600 text-white hover:bg-emerald-700"
> >
Pay Early Pay Early
@@ -1775,7 +1818,7 @@
</Card.Content> </Card.Content>
<Card.Footer class="flex justify-center"> <Card.Footer class="flex justify-center">
<Button <Button
onclick={() => (window.location.href = authStore.isAuthenticated ? '/account' : '/')} onclick={() => (window.location.href = authStore.isAuthenticated ? '/schedule' : '/')}
class="w-full" class="w-full"
> >
{authStore.isAuthenticated ? 'View My Bookings' : 'Return Home'} {authStore.isAuthenticated ? 'View My Bookings' : 'Return Home'}
@@ -1795,4 +1838,37 @@
</Card.Root> </Card.Root>
{/if} {/if}
{/if} {/if}
{#if showPayEarlyModal && confirmedBooking}
{@const booking = confirmedBooking}
<UserPaymentModal
booking={{
id: booking.id,
status: booking.status as BookingStatus,
start_time: booking.start_time,
notes: booking.notes,
services: selectedServices.map((s) => ({
booking_id: booking.id,
service_id: s.id,
service_name: s.name,
price: s.price,
duration_minutes: s.duration_minutes
})) as BookingService[],
total_amount: getTotalPrice(),
amount_paid: 0,
amount_due: getTotalPrice(),
deposit_required: false,
deposit_paid: true,
payments: [],
duration_minutes: getTotalDuration(),
created_at: new Date().toISOString(),
updated_at: new Date().toISOString()
}}
onClose={() => (showPayEarlyModal = false)}
onComplete={() => {
showPayEarlyModal = false;
}}
canSaveCards={authStore.currentUser?.role === 'verified_email' || authStore.currentUser?.role === 'affiliate'}
/>
{/if}
</div> </div>
@@ -32,6 +32,8 @@
service_description?: string; service_description?: string;
price?: number; price?: number;
duration_minutes?: number; duration_minutes?: number;
override_price?: number;
override_duration_minutes?: number;
}>; }>;
duration_minutes: number; duration_minutes: number;
total_amount: number; total_amount: number;
@@ -318,7 +320,7 @@
<div class="text-xs text-gray-600">{service.service_description}</div> <div class="text-xs text-gray-600">{service.service_description}</div>
{/if} {/if}
<div class="mt-1 flex items-center justify-between text-xs text-gray-500"> <div class="mt-1 flex items-center justify-between text-xs text-gray-500">
<span>{service.duration_minutes} mins</span> <span>{service.override_duration_minutes ?? service.duration_minutes} mins</span>
</div> </div>
</div> </div>
{/each} {/each}
@@ -1,5 +1,6 @@
<script lang="ts"> <script lang="ts">
import { page } from '$app/stores'; import { page } from '$app/stores';
import { authStore } from '$lib/stores/auth.svelte';
import { Button } from '$lib/components/ui/button'; import { Button } from '$lib/components/ui/button';
import * as Card from '$lib/components/ui/card'; import * as Card from '$lib/components/ui/card';
import { Skeleton } from '$lib/components/ui/skeleton'; import { Skeleton } from '$lib/components/ui/skeleton';
@@ -104,7 +105,11 @@
} }
} }
function formatPrice(pence: number): string { function formatPounds(pounds: number): string {
return ${pounds.toFixed(2)}`;
}
function formatPence(pence: number): string {
return ${(pence / 100).toFixed(2)}`; return ${(pence / 100).toFixed(2)}`;
} }
@@ -126,7 +131,9 @@
try { try {
// Fetch booking details // Fetch booking details
const bookingResponse = await fetch(`/api/bookings/${bookingId}`, { const bookingResponse = await fetch(`/api/bookings/${bookingId}`, {
credentials: 'include' headers: {
Authorization: `Bearer ${authStore.currentToken}`
}
}); });
if (!bookingResponse.ok) { if (!bookingResponse.ok) {
@@ -140,7 +147,9 @@
// Fetch payment summary // Fetch payment summary
const paymentResponse = await fetch(`/api/bookings/${bookingId}/payment-summary`, { const paymentResponse = await fetch(`/api/bookings/${bookingId}/payment-summary`, {
credentials: 'include' headers: {
Authorization: `Bearer ${authStore.currentToken}`
}
}); });
if (paymentResponse.ok) { if (paymentResponse.ok) {
@@ -238,9 +247,9 @@
<div class="flex justify-between rounded bg-gray-50 p-3"> <div class="flex justify-between rounded bg-gray-50 p-3">
<div> <div>
<div class="font-medium">{service.service_name}</div> <div class="font-medium">{service.service_name}</div>
<div class="text-sm text-gray-500">{service.duration_minutes} mins</div> <div class="text-sm text-gray-500">{service.override_duration_minutes ?? service.duration_minutes} mins</div>
</div> </div>
<div class="font-semibold">{formatPrice(service.price)}</div> <div class="font-semibold">{formatPounds(service.override_price ?? service.price)}</div>
</div> </div>
{/each} {/each}
</div> </div>
@@ -248,7 +257,7 @@
<div class="flex justify-between border-t pt-4"> <div class="flex justify-between border-t pt-4">
<div class="text-lg font-semibold">Total</div> <div class="text-lg font-semibold">Total</div>
<div class="text-lg font-bold">{formatPrice(totalPrice)}</div> <div class="text-lg font-bold">{formatPounds(totalPrice)}</div>
</div> </div>
</Card.Content> </Card.Content>
</Card.Root> </Card.Root>
@@ -273,7 +282,7 @@
<div> <div>
<div class="font-semibold text-green-800">Deposit Paid</div> <div class="font-semibold text-green-800">Deposit Paid</div>
<div class="text-sm text-green-700"> <div class="text-sm text-green-700">
Your deposit of {formatPrice(booking.deposit_amount ?? 0)} has been paid Your deposit of {formatPounds(booking.deposit_amount ?? 0)} has been paid
</div> </div>
</div> </div>
</div> </div>
@@ -281,7 +290,7 @@
<div class="mt-4 rounded-lg bg-gray-50 p-4"> <div class="mt-4 rounded-lg bg-gray-50 p-4">
<div class="flex justify-between"> <div class="flex justify-between">
<span class="text-gray-600">Remaining Balance</span> <span class="text-gray-600">Remaining Balance</span>
<span class="font-semibold">{formatPrice(paymentSummary.remaining_amount)}</span> <span class="font-semibold">{formatPence(paymentSummary.remaining_amount)}</span>
</div> </div>
<p class="mt-2 text-sm text-gray-500"> <p class="mt-2 text-sm text-gray-500">
You can pay the remaining balance on the day of your appointment You can pay the remaining balance on the day of your appointment
@@ -295,11 +304,11 @@
<div> <div>
<div class="font-semibold text-amber-800">Deposit Required</div> <div class="font-semibold text-amber-800">Deposit Required</div>
<div class="text-sm text-amber-700"> <div class="text-sm text-amber-700">
To secure your booking, please pay a deposit of {formatPrice(booking.deposit_amount ?? 0)} To secure your booking, please pay a deposit of {formatPounds(booking.deposit_amount ?? 0)}
</div> </div>
</div> </div>
<div class="text-2xl font-bold text-amber-800"> <div class="text-2xl font-bold text-amber-800">
{formatPrice(booking.deposit_amount ?? 0)} {formatPounds(booking.deposit_amount ?? 0)}
</div> </div>
</div> </div>
{#if booking.deposit_deadline} {#if booking.deposit_deadline}
@@ -336,7 +345,7 @@
<div> <div>
<div class="font-semibold text-green-800">Paid</div> <div class="font-semibold text-green-800">Paid</div>
<div class="text-sm text-green-700"> <div class="text-sm text-green-700">
{formatPrice(paymentSummary?.paid_amount ?? 0)} paid {formatPence(paymentSummary?.paid_amount ?? 0)} paid
</div> </div>
</div> </div>
</div> </div>
@@ -351,7 +360,7 @@
</Card.Root> </Card.Root>
<div class="mt-6 flex flex-col gap-4 sm:flex-row sm:justify-center"> <div class="mt-6 flex flex-col gap-4 sm:flex-row sm:justify-center">
<Button variant="outline" onclick={() => (window.location.href = '/account')}> <Button variant="outline" onclick={() => (window.location.href = '/schedule')}>
View My Bookings View My Bookings
</Button> </Button>
<Button variant="ghost" onclick={() => (window.location.href = '/')}> <Button variant="ghost" onclick={() => (window.location.href = '/')}>
+2 -5
View File
@@ -2,6 +2,7 @@
import { SvelteDate } from 'svelte/reactivity'; import { SvelteDate } from 'svelte/reactivity';
import * as Card from '$lib/components/ui/card'; import * as Card from '$lib/components/ui/card';
import { Button } from '$lib/components/ui/button'; import { Button } from '$lib/components/ui/button';
import { Checkbox } from '$lib/components/ui/checkbox';
import { Badge } from '$lib/components/ui/badge'; import { Badge } from '$lib/components/ui/badge';
// Mock data // Mock data
@@ -187,11 +188,7 @@
<div class="space-y-2"> <div class="space-y-2">
{#each currentAppointment.checklist as item} {#each currentAppointment.checklist as item}
<label class="flex items-center gap-2 text-sm"> <label class="flex items-center gap-2 text-sm">
<input <Checkbox checked={item.done} />
type="checkbox"
checked={item.done}
class="h-4 w-4 rounded border-gray-300"
/>
<span class={item.done ? 'text-gray-400 line-through' : ''}>{item.item}</span> <span class={item.done ? 'text-gray-400 line-through' : ''}>{item.item}</span>
</label> </label>
{/each} {/each}
@@ -7,6 +7,7 @@
import { authStore } from '$lib/stores/auth.svelte'; import { authStore } from '$lib/stores/auth.svelte';
import { Skeleton } from '$lib/components/ui/skeleton'; import { Skeleton } from '$lib/components/ui/skeleton';
import { Button } from '$lib/components/ui/button'; import { Button } from '$lib/components/ui/button';
import { Checkbox } from '$lib/components/ui/checkbox';
import ApprovalModal from '$lib/components/admin/ApprovalModal.svelte'; import ApprovalModal from '$lib/components/admin/ApprovalModal.svelte';
import BookingModal from '$lib/components/admin/BookingModal.svelte'; import BookingModal from '$lib/components/admin/BookingModal.svelte';
import UserModal from '$lib/components/admin/UserModal.svelte'; import UserModal from '$lib/components/admin/UserModal.svelte';
@@ -316,11 +317,9 @@
<div class="mb-6 flex items-center justify-between"> <div class="mb-6 flex items-center justify-between">
<h1 class="text-2xl font-semibold text-gray-900">Notifications</h1> <h1 class="text-2xl font-semibold text-gray-900">Notifications</h1>
<label class="flex cursor-pointer items-center gap-2 text-sm text-gray-600"> <label class="flex cursor-pointer items-center gap-2 text-sm text-gray-600">
<input <Checkbox
type="checkbox"
checked={includeAcknowledged} checked={includeAcknowledged}
onchange={toggleView} onchange={toggleView}
class="h-4 w-4 rounded border-gray-300 text-primary focus:ring-primary"
/> />
Show acknowledged Show acknowledged
</label> </label>
+115 -125
View File
@@ -1,4 +1,6 @@
<script lang="ts"> <script lang="ts">
import { goto } from '$app/navigation';
import { browser } from '$app/environment';
import { page } from '$app/stores'; import { page } from '$app/stores';
import { Button } from '$lib/components/ui/button'; import { Button } from '$lib/components/ui/button';
import * as Card from '$lib/components/ui/card'; import * as Card from '$lib/components/ui/card';
@@ -6,20 +8,25 @@
import { Skeleton } from '$lib/components/ui/skeleton'; import { Skeleton } from '$lib/components/ui/skeleton';
import { toast } from 'svelte-sonner'; import { toast } from 'svelte-sonner';
import { SvelteDate } from 'svelte/reactivity'; import { SvelteDate } from 'svelte/reactivity';
import { authStore } from '$lib/stores/auth.svelte';
// Types // Types
type Service = { type Service = {
service_name: string; service_name: string;
price: number; price: number;
duration_minutes: number; duration_minutes: number;
override_price?: number;
override_duration_minutes?: number;
}; };
type Booking = { type Booking = {
id: string; id: string;
start_time: string; start_time: string;
status: string; status: string;
customer_first_name: string;
services: Service[]; services: Service[];
total_amount: number;
amount_paid: number;
duration_minutes: number;
}; };
// State // State
@@ -27,6 +34,7 @@
let loading = $state(true); let loading = $state(true);
let error = $state<string | null>(null); let error = $state<string | null>(null);
let paymentState = $state<'idle' | 'processing' | 'success' | 'error'>('idle'); let paymentState = $state<'idle' | 'processing' | 'success' | 'error'>('idle');
let pageState = $state<'loading' | 'authorized' | 'unauthorized' | 'admin'>('loading');
// Tip selection state // Tip selection state
let selectedTip = $state<number | null>(null); let selectedTip = $state<number | null>(null);
@@ -39,14 +47,19 @@
: 0 : 0
); );
// Card form state (placeholder for Square SDK)
let cardNumber = $state('');
let cardExpiry = $state('');
let cardCvc = $state('');
// Get booking ID from URL // Get booking ID from URL
const bookingId = $derived($page.params.id); const bookingId = $derived($page.params.id);
const tipPercentages = $derived.by(() => {
const total = booking?.total_amount ?? 0;
if (total <= 0) return [];
return [
{ pct: 10, amount: Math.round(total * 0.10 * 100) / 100 },
{ pct: 15, amount: Math.round(total * 0.15 * 100) / 100 },
{ pct: 20, amount: Math.round(total * 0.20 * 100) / 100 },
];
});
// Format functions // Format functions
function formatDate(dateStr: string): string { function formatDate(dateStr: string): string {
const date = new SvelteDate(dateStr); const date = new SvelteDate(dateStr);
@@ -58,16 +71,25 @@
}); });
} }
function formatTime(dateStr: string): string { function formatTimeRange(startStr: string, services: Service[], fallbackDuration: number): string {
const date = new SvelteDate(dateStr); const start = new SvelteDate(startStr);
return date.toLocaleTimeString('en-GB', { const totalMinutes = services?.reduce(
(sum, s) => sum + (s.override_duration_minutes ?? s.duration_minutes ?? 0),
0
) ?? fallbackDuration ?? 0;
const end = new SvelteDate(start.getTime() + totalMinutes * 60000);
const formatOpt: Intl.DateTimeFormatOptions = {
hour: 'numeric', hour: 'numeric',
minute: '2-digit' minute: '2-digit',
}); hour12: true
};
return `${start.toLocaleTimeString('en-GB', formatOpt)} ${end.toLocaleTimeString('en-GB', formatOpt)}`;
} }
function formatPrice(pence: number): string { function formatPrice(pounds: number): string {
return ${(pence / 100).toFixed(2)}`; return ${pounds.toFixed(2)}`;
} }
// Fetch booking data // Fetch booking data
@@ -77,10 +99,15 @@
try { try {
const response = await fetch(`/api/bookings/${bookingId}`, { const response = await fetch(`/api/bookings/${bookingId}`, {
credentials: 'include' headers: {
Authorization: `Bearer ${authStore.currentToken}`
}
}); });
if (!response.ok) { if (!response.ok) {
if (response.status === 401) {
throw new Error('Authentication required');
}
if (response.status === 404) { if (response.status === 404) {
throw new Error('Booking not found'); throw new Error('Booking not found');
} }
@@ -103,23 +130,30 @@
function handleCustomTipInput(e: Event) { function handleCustomTipInput(e: Event) {
const input = e.target as HTMLInputElement; const input = e.target as HTMLInputElement;
customTip = input.value; const cleaned = input.value.replace(/[^0-9.]/g, '');
const firstDot = cleaned.indexOf('.');
let sanitized: string;
if (firstDot !== -1) {
const integerPart = cleaned.substring(0, firstDot);
const decimalPart = cleaned.substring(firstDot + 1).replace(/\./g, '');
sanitized = integerPart + '.' + decimalPart;
} else {
sanitized = cleaned;
}
if (/^\d+(\.\d{0,2})?$/.test(sanitized) || sanitized === '') {
customTip = sanitized;
}
selectedTip = null; selectedTip = null;
} }
// Submit tip payment // Submit tip payment
async function submitTip() { async function submitTip() {
if (!booking) return;
if (tipAmount <= 0) { if (tipAmount <= 0) {
toast.error('Please select a tip amount'); toast.error('Please select a tip amount');
return; return;
} }
// Basic validation for placeholder card form
if (!cardNumber || !cardExpiry || !cardCvc) {
toast.error('Please enter your card details');
return;
}
paymentState = 'processing'; paymentState = 'processing';
try { try {
@@ -128,9 +162,9 @@
const response = await fetch(`/api/bookings/${bookingId}/tip`, { const response = await fetch(`/api/bookings/${bookingId}/tip`, {
method: 'POST', method: 'POST',
headers: { headers: {
'Content-Type': 'application/json' 'Content-Type': 'application/json',
Authorization: `Bearer ${authStore.currentToken}`
}, },
credentials: 'include',
body: JSON.stringify({ body: JSON.stringify({
amount: amountInPence, amount: amountInPence,
card_token: 'placeholder' card_token: 'placeholder'
@@ -156,8 +190,28 @@
paymentState = 'idle'; paymentState = 'idle';
} }
// Initialize // Auth check + fetch
$effect(() => { $effect(() => {
if (!browser) return;
if (authStore.isLoading) {
pageState = 'loading';
return;
}
if (!authStore.isAuthenticated) {
pageState = 'unauthorized';
goto('/login', { replaceState: true });
return;
}
if (authStore.currentUser?.role === 'admin') {
pageState = 'admin';
goto('/admin', { replaceState: true });
return;
}
pageState = 'authorized';
if (bookingId) { if (bookingId) {
fetchBookingData(); fetchBookingData();
} }
@@ -168,8 +222,8 @@
<title>Leave a Tip - Crussell</title> <title>Leave a Tip - Crussell</title>
</svelte:head> </svelte:head>
<div class="mx-auto max-w-md p-6"> <div class="mx-auto min-h-screen px-4 py-8 sm:max-w-md md:py-12">
{#if loading} {#if loading || pageState === 'loading'}
<div class="space-y-6"> <div class="space-y-6">
<div class="text-center"> <div class="text-center">
<Skeleton class="mx-auto h-10 w-40" /> <Skeleton class="mx-auto h-10 w-40" />
@@ -184,18 +238,21 @@
{:else if error} {:else if error}
<Card.Root> <Card.Root>
<Card.Header> <Card.Header>
<Card.Title class="text-red-600">Error</Card.Title> <Card.Title class="text-red-600">Something went wrong</Card.Title>
</Card.Header> </Card.Header>
<Card.Content> <Card.Content>
<p class="text-gray-600">{error}</p> <p class="text-gray-600">{error}</p>
<Button class="mt-4" onclick={() => (window.location.href = '/')}> <div class="mt-4 flex flex-col gap-2 sm:flex-row">
Return Home <Button class="w-full sm:w-auto" onclick={() => goto('/')}>Go Home</Button>
</Button> <Button variant="outline" class="w-full sm:w-auto" onclick={fetchBookingData}>
Try Again
</Button>
</div>
</Card.Content> </Card.Content>
</Card.Root> </Card.Root>
{:else if booking} {:else if booking}
<div class="mb-6 text-center"> <div class="mb-6 text-center">
<h1 class="text-2xl font-bold text-gray-900">Leave a Tip</h1> <h1 class="text-2xl font-bold text-gray-900 sm:text-3xl">Leave a Tip</h1>
<p class="mt-1 text-gray-600">Show your appreciation for great service</p> <p class="mt-1 text-gray-600">Show your appreciation for great service</p>
</div> </div>
@@ -213,11 +270,9 @@
<path d="M20 6L9 17l-5-5" stroke-linecap="round" stroke-linejoin="round" /> <path d="M20 6L9 17l-5-5" stroke-linecap="round" stroke-linejoin="round" />
</svg> </svg>
</div> </div>
<h2 class="text-xl font-semibold text-gray-900">Thank you for your tip!</h2> <h2 class="text-xl font-semibold text-gray-900">Thank you!</h2>
<p class="mt-2 text-gray-600">Your generosity is greatly appreciated.</p> <p class="mt-2 text-gray-600">Your generosity is greatly appreciated.</p>
<Button class="mt-6" onclick={() => (window.location.href = '/')}> <Button class="mt-6" onclick={() => goto('/')}>Go Home</Button>
Return Home
</Button>
</Card.Content> </Card.Content>
</Card.Root> </Card.Root>
{:else} {:else}
@@ -226,17 +281,17 @@
<Card.Title>Your Appointment</Card.Title> <Card.Title>Your Appointment</Card.Title>
</Card.Header> </Card.Header>
<Card.Content class="space-y-3"> <Card.Content class="space-y-3">
<div class="flex justify-between">
<span class="text-sm text-gray-500">Name</span>
<span class="font-medium">{booking.customer_first_name}</span>
</div>
<div class="flex justify-between"> <div class="flex justify-between">
<span class="text-sm text-gray-500">Date</span> <span class="text-sm text-gray-500">Date</span>
<span class="font-medium">{formatDate(booking.start_time)}</span> <span class="font-medium">{formatDate(booking.start_time)}</span>
</div> </div>
<div class="flex justify-between"> <div class="flex justify-between">
<span class="text-sm text-gray-500">Time</span> <span class="text-sm text-gray-500">Time</span>
<span class="font-medium">{formatTime(booking.start_time)}</span> <span class="font-medium">{formatTimeRange(booking.start_time, booking.services, booking.duration_minutes ?? 0)}</span>
</div>
<div class="flex justify-between">
<span class="text-sm text-gray-500">Paid</span>
<span class="font-medium">{formatPrice(booking.amount_paid ?? 0)}</span>
</div> </div>
<div class="border-t pt-3"> <div class="border-t pt-3">
<div class="text-sm text-gray-500">Services</div> <div class="text-sm text-gray-500">Services</div>
@@ -244,7 +299,7 @@
{#each booking.services as service} {#each booking.services as service}
<div class="flex justify-between text-sm"> <div class="flex justify-between text-sm">
<span class="text-gray-700">{service.service_name}</span> <span class="text-gray-700">{service.service_name}</span>
<span class="text-gray-500">{formatPrice(service.price)}</span> <span class="text-gray-500">{formatPrice(service.override_price ?? service.price)}</span>
</div> </div>
{/each} {/each}
</div> </div>
@@ -258,42 +313,29 @@
</Card.Header> </Card.Header>
<Card.Content class="space-y-4"> <Card.Content class="space-y-4">
<div class="grid grid-cols-3 gap-3"> <div class="grid grid-cols-3 gap-3">
<button {#each tipPercentages as tip (tip.pct)}
class="rounded-lg border-2 py-3 text-center font-semibold transition-colors {selectedTip === <button
2 class="rounded-lg border border-input bg-background py-3 text-center font-semibold transition-colors hover:bg-fuchsia-50 {selectedTip ===
? 'border-blue-600 bg-blue-50 text-blue-700' tip.amount
: 'border-gray-200 hover:border-gray-300'}" ? 'bg-fuchsia-100'
onclick={() => selectTip(2)} : ''}"
> onclick={() => selectTip(tip.amount)}
£2 type="button"
</button> >
<button <div>{formatPrice(tip.amount)}</div>
class="rounded-lg border-2 py-3 text-center font-semibold transition-colors {selectedTip === <div class="text-xs font-normal text-gray-500">{tip.pct}%</div>
5 </button>
? 'border-blue-600 bg-blue-50 text-blue-700' {/each}
: 'border-gray-200 hover:border-gray-300'}"
onclick={() => selectTip(5)}
>
£5
</button>
<button
class="rounded-lg border-2 py-3 text-center font-semibold transition-colors {selectedTip ===
10
? 'border-blue-600 bg-blue-50 text-blue-700'
: 'border-gray-200 hover:border-gray-300'}"
onclick={() => selectTip(10)}
>
£10
</button>
</div> </div>
<div> <div>
<label for="custom-tip" class="text-sm font-medium text-gray-700">Or enter custom amount</label> <label for="custom-tip" class="text-sm font-medium text-gray-700">Or enter custom amount</label>
<div class="mt-1 relative"> <div class="relative mt-1">
<span class="absolute left-3 top-1/2 -translate-y-1/2 text-gray-500">£</span> <span class="absolute top-1/2 left-3 -translate-y-1/2 text-gray-500">£</span>
<Input <Input
id="custom-tip" id="custom-tip"
type="number" type="text"
inputmode="decimal"
step="0.01" step="0.01"
min="0" min="0"
placeholder="0.00" placeholder="0.00"
@@ -303,56 +345,6 @@
/> />
</div> </div>
</div> </div>
{#if tipAmount > 0}
<div class="rounded-lg bg-blue-50 p-4 text-center">
<span class="text-lg font-semibold text-blue-700">Tip: £{tipAmount.toFixed(2)}</span>
</div>
{/if}
</Card.Content>
</Card.Root>
<Card.Root class="mb-6">
<Card.Header>
<Card.Title>Card Details</Card.Title>
<Card.Description>Secure payment powered by Square</Card.Description>
</Card.Header>
<Card.Content class="space-y-4">
<div>
<label for="card-number" class="text-sm font-medium text-gray-700">Card Number</label>
<Input
id="card-number"
type="text"
placeholder="1234 5678 9012 3456"
maxlength="19"
bind:value={cardNumber}
class="mt-1"
/>
</div>
<div class="grid grid-cols-2 gap-4">
<div>
<label for="card-expiry" class="text-sm font-medium text-gray-700">Expiry</label>
<Input
id="card-expiry"
type="text"
placeholder="MM/YY"
maxlength="5"
bind:value={cardExpiry}
class="mt-1"
/>
</div>
<div>
<label for="card-cvc" class="text-sm font-medium text-gray-700">CVC</label>
<Input
id="card-cvc"
type="text"
placeholder="123"
maxlength="4"
bind:value={cardCvc}
class="mt-1"
/>
</div>
</div>
</Card.Content> </Card.Content>
</Card.Root> </Card.Root>
@@ -379,9 +371,7 @@
{paymentState === 'processing' ? 'Processing...' : `Pay Tip £${tipAmount.toFixed(2)}`} {paymentState === 'processing' ? 'Processing...' : `Pay Tip £${tipAmount.toFixed(2)}`}
</Button> </Button>
<p class="mt-4 text-center text-xs text-gray-500"> <p class="mt-4 text-center text-xs text-gray-500">Secure payment powered by Square</p>
This is a placeholder form. Square SDK integration coming soon.
</p>
{/if} {/if}
{/if} {/if}
</div> </div>
+3 -3
View File
@@ -297,9 +297,9 @@ if api_post "$BASE_URL/register" '{"firstName":"Grace","lastName":"Fletcher","em
if api_post "$BASE_URL/register" '{"firstName":"Liam","lastName":"Caldwell","email":"liam.caldwell@example.com","password":"password","phone":"+447000000019","dateOfBirth":"1989-03-14","agreedToPolicy":true}' "Register Liam Caldwell" "" > /dev/null; then success=$((success+1)); fi if api_post "$BASE_URL/register" '{"firstName":"Liam","lastName":"Caldwell","email":"liam.caldwell@example.com","password":"password","phone":"+447000000019","dateOfBirth":"1989-03-14","agreedToPolicy":true}' "Register Liam Caldwell" "" > /dev/null; then success=$((success+1)); fi
# --- Promote admin and set deposit flags via DB --- # --- Promote admin and set deposit flags via DB ---
docker exec postgres psql -U myuser -d mydb -c "UPDATE users SET account_role = 'admin' WHERE email = 'admin@example.com'" > /dev/null 2>&1 docker exec postgres psql -U myuser -d mydb -c "UPDATE users SET account_role = 'admin' WHERE email = 'admin@example.com'"
# Primary test user: zero deposit requirement for easy booking # Primary test user: verified role for card testing, zero deposit requirement
docker exec postgres psql -U myuser -d mydb -c "UPDATE users SET deposits_required = 0 WHERE email = 'user@example.com'" > /dev/null 2>&1 docker exec postgres psql -U myuser -d mydb -c "UPDATE users SET account_role = 'verified_email', deposits_required = 0 WHERE email = 'user@example.com'"
# Loyal regulars: zero deposit requirement # Loyal regulars: zero deposit requirement
docker exec postgres psql -U myuser -d mydb -c "UPDATE users SET deposits_required = 0 WHERE email IN ('emma.johnson@example.com','sophie.williams@example.com','amelia.jones@example.com','isla.davies@example.com','lily.wilson@example.com','ava.walker@example.com','grace.fletcher@example.com')" > /dev/null 2>&1 docker exec postgres psql -U myuser -d mydb -c "UPDATE users SET deposits_required = 0 WHERE email IN ('emma.johnson@example.com','sophie.williams@example.com','amelia.jones@example.com','isla.davies@example.com','lily.wilson@example.com','ava.walker@example.com','grace.fletcher@example.com')" > /dev/null 2>&1
# Deposit-required users: 3 no-shows on record # Deposit-required users: 3 no-shows on record