mirror of
https://github.com/marcogll/AnchorOS.git
synced 2026-03-15 19:24:32 +00:00
Major changes: - Add customer registration with email/phone lookup (app/booking/registro) - Add customers API endpoint (app/api/customers/route) - Implement business hours for locations (mon-fri 10-7, sat 10-6, sun closed) - Fix availability function type casting issues - Add business hours utilities (lib/utils/business-hours.ts) - Update Location type to include business_hours JSONB - Add mock payment component for testing - Remove Supabase auth from booking flow - Fix /cita redirect path in booking flow Database migrations: - Add category column to services table - Add business_hours JSONB column to locations table - Fix availability functions with proper type casting - Update get_detailed_availability to use business_hours Features: - Customer lookup by email or phone - Auto-redirect to registration if customer not found - Pre-fill customer data if exists - Business hours per day of week - Location-specific opening/closing times
205 lines
5.2 KiB
TypeScript
205 lines
5.2 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server'
|
|
import { supabaseAdmin } from '@/lib/supabase/admin'
|
|
|
|
async function validateKiosk(request: NextRequest) {
|
|
const apiKey = request.headers.get('x-kiosk-api-key')
|
|
|
|
if (!apiKey) {
|
|
return null
|
|
}
|
|
|
|
const { data: kiosk } = await supabaseAdmin
|
|
.from('kiosks')
|
|
.select('id, location_id, is_active')
|
|
.eq('api_key', apiKey)
|
|
.eq('is_active', true)
|
|
.single()
|
|
|
|
return kiosk
|
|
}
|
|
|
|
/**
|
|
* @description Retrieves pending/confirmed bookings for kiosk
|
|
*/
|
|
export async function GET(request: NextRequest) {
|
|
try {
|
|
const kiosk = await validateKiosk(request)
|
|
|
|
if (!kiosk) {
|
|
return NextResponse.json(
|
|
{ error: 'Unauthorized' },
|
|
{ status: 401 }
|
|
)
|
|
}
|
|
|
|
const { searchParams } = new URL(request.url)
|
|
const short_id = searchParams.get('short_id')
|
|
const date = searchParams.get('date')
|
|
|
|
let query = supabaseAdmin
|
|
.from('bookings')
|
|
.select()
|
|
.eq('location_id', kiosk.location_id)
|
|
.in('status', ['pending', 'confirmed'])
|
|
|
|
if (short_id) {
|
|
query = query.eq('short_id', short_id)
|
|
}
|
|
|
|
if (date) {
|
|
const startDate = new Date(date)
|
|
const endDate = new Date(startDate)
|
|
endDate.setDate(endDate.getDate() + 1)
|
|
|
|
query = query
|
|
.gte('start_time_utc', startDate.toISOString())
|
|
.lt('start_time_utc', endDate.toISOString())
|
|
}
|
|
|
|
const { data: bookings, error } = await query.order('start_time_utc', { ascending: true })
|
|
|
|
if (error) {
|
|
return NextResponse.json(
|
|
{ error: error.message },
|
|
{ status: 400 }
|
|
)
|
|
}
|
|
|
|
return NextResponse.json({ bookings })
|
|
} catch (error) {
|
|
console.error('Kiosk bookings GET error:', error)
|
|
return NextResponse.json(
|
|
{ error: 'Internal server error' },
|
|
{ status: 500 }
|
|
)
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @description Creates a new booking for kiosk
|
|
*/
|
|
export async function POST(request: NextRequest) {
|
|
try {
|
|
const kiosk = await validateKiosk(request)
|
|
|
|
if (!kiosk) {
|
|
return NextResponse.json(
|
|
{ error: 'Unauthorized' },
|
|
{ status: 401 }
|
|
)
|
|
}
|
|
|
|
const body = await request.json()
|
|
const {
|
|
customer_email,
|
|
customer_phone,
|
|
customer_name,
|
|
service_id,
|
|
staff_id,
|
|
start_time_utc,
|
|
notes
|
|
} = body
|
|
|
|
if (!customer_email || !service_id || !staff_id || !start_time_utc) {
|
|
return NextResponse.json(
|
|
{ error: 'Missing required fields: customer_email, service_id, staff_id, start_time_utc' },
|
|
{ status: 400 }
|
|
)
|
|
}
|
|
|
|
const { data: service, error: serviceError } = await supabaseAdmin
|
|
.from('services')
|
|
.select('*')
|
|
.eq('id', service_id)
|
|
.eq('is_active', true)
|
|
.single()
|
|
|
|
if (serviceError || !service) {
|
|
return NextResponse.json(
|
|
{ error: 'Invalid service_id' },
|
|
{ status: 400 }
|
|
)
|
|
}
|
|
|
|
const startTime = new Date(start_time_utc)
|
|
const endTime = new Date(startTime)
|
|
endTime.setMinutes(endTime.getMinutes() + service.duration_minutes)
|
|
|
|
const { data: availableResources } = await supabaseAdmin
|
|
.rpc('get_available_resources_with_priority', {
|
|
p_location_id: kiosk.location_id,
|
|
p_start_time: startTime.toISOString(),
|
|
p_end_time: endTime.toISOString()
|
|
})
|
|
|
|
if (!availableResources || availableResources.length === 0) {
|
|
return NextResponse.json(
|
|
{ error: 'No resources available for the selected time' },
|
|
{ status: 400 }
|
|
)
|
|
}
|
|
|
|
const assignedResource = availableResources[0]
|
|
|
|
const { data: customer, error: customerError } = await supabaseAdmin
|
|
.from('customers')
|
|
.upsert({
|
|
email: customer_email,
|
|
first_name: customer_name?.split(' ')[0] || 'Cliente',
|
|
last_name: customer_name?.split(' ').slice(1).join(' ') || 'Kiosko',
|
|
phone: customer_phone,
|
|
tier: 'free',
|
|
is_active: true
|
|
})
|
|
.select()
|
|
.single()
|
|
|
|
if (customerError || !customer) {
|
|
return NextResponse.json(
|
|
{ error: 'Failed to create/find customer' },
|
|
{ status: 400 }
|
|
)
|
|
}
|
|
|
|
const { data: booking, error: bookingError } = await supabaseAdmin
|
|
.from('bookings')
|
|
.insert({
|
|
customer_id: customer.id,
|
|
staff_id,
|
|
location_id: kiosk.location_id,
|
|
resource_id: assignedResource.resource_id,
|
|
service_id,
|
|
start_time_utc: startTime.toISOString(),
|
|
end_time_utc: endTime.toISOString(),
|
|
status: 'pending',
|
|
deposit_amount: 0,
|
|
total_amount: service.base_price,
|
|
is_paid: false,
|
|
notes
|
|
})
|
|
.select('id, short_id, status, start_time_utc, end_time_utc')
|
|
.single()
|
|
|
|
if (bookingError || !booking) {
|
|
return NextResponse.json(
|
|
{ error: bookingError?.message || 'Failed to create booking' },
|
|
{ status: 400 }
|
|
)
|
|
}
|
|
|
|
return NextResponse.json({
|
|
success: true,
|
|
booking,
|
|
service_name: service.name,
|
|
resource_name: assignedResource.resource_name,
|
|
resource_type: assignedResource.resource_type
|
|
}, { status: 201 })
|
|
} catch (error) {
|
|
console.error('Kiosk bookings POST error:', error)
|
|
return NextResponse.json(
|
|
{ error: 'Internal server error' },
|
|
{ status: 500 }
|
|
)
|
|
}
|
|
}
|