feat: implement customer registration flow and business hours system

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
This commit is contained in:
Marco Gallegos
2026-01-17 00:29:49 -06:00
parent fb60178c86
commit 583a25a6f6
56 changed files with 2676 additions and 491 deletions

View File

@@ -1,5 +1,5 @@
import { NextRequest, NextResponse } from 'next/server'
import { supabaseAdmin } from '@/lib/supabase/client'
import { supabaseAdmin } from '@/lib/supabase/admin'
async function validateAdmin(request: NextRequest) {
const authHeader = request.headers.get('authorization')

View File

@@ -1,5 +1,5 @@
import { NextRequest, NextResponse } from 'next/server'
import { supabaseAdmin } from '@/lib/supabase/client'
import { supabaseAdmin } from '@/lib/supabase/admin'
async function validateAdmin(request: NextRequest) {
const authHeader = request.headers.get('authorization')

View File

@@ -1,5 +1,5 @@
import { NextRequest, NextResponse } from 'next/server'
import { supabaseAdmin } from '@/lib/supabase/client'
import { supabaseAdmin } from '@/lib/supabase/admin'
async function validateAdmin(request: NextRequest) {
const authHeader = request.headers.get('authorization')

View File

@@ -1,5 +1,5 @@
import { NextRequest, NextResponse } from 'next/server'
import { supabaseAdmin } from '@/lib/supabase/client'
import { supabaseAdmin } from '@/lib/supabase/admin'
/**
* @description Fetches bookings with filters for dashboard view

View File

@@ -1,5 +1,5 @@
import { NextRequest, NextResponse } from 'next/server'
import { supabaseAdmin } from '@/lib/supabase/client'
import { supabaseAdmin } from '@/lib/supabase/admin'
/**
* @description Fetches recent payments report

View File

@@ -1,5 +1,5 @@
import { NextRequest, NextResponse } from 'next/server'
import { supabaseAdmin } from '@/lib/supabase/client'
import { supabaseAdmin } from '@/lib/supabase/admin'
/**
* @description Fetches payroll report for staff based on recent bookings

View File

@@ -1,5 +1,5 @@
import { NextRequest, NextResponse } from 'next/server'
import { supabaseAdmin } from '@/lib/supabase/client'
import { supabaseAdmin } from '@/lib/supabase/admin'
/**
* @description Fetches sales report including total sales, completed bookings, average service price, and sales by service

View File

@@ -1,5 +1,5 @@
import { NextRequest, NextResponse } from 'next/server'
import { supabaseAdmin } from '@/lib/supabase/client'
import { supabaseAdmin } from '@/lib/supabase/admin'
/**
* @description Retrieves active resources, optionally filtered by location

View File

@@ -1,5 +1,5 @@
import { NextRequest, NextResponse } from 'next/server'
import { supabaseAdmin } from '@/lib/supabase/client'
import { supabaseAdmin } from '@/lib/supabase/admin'
/**
* @description Gets available staff for a location and date

View File

@@ -1,5 +1,5 @@
import { NextRequest, NextResponse } from 'next/server'
import { supabaseAdmin } from '@/lib/supabase/client'
import { supabaseAdmin } from '@/lib/supabase/admin'
/**
* @description Retrieves staff availability schedule with optional filters

View File

@@ -1,5 +1,5 @@
import { NextRequest, NextResponse } from 'next/server'
import { supabaseAdmin } from '@/lib/supabase/client'
import { supabaseAdmin } from '@/lib/supabase/admin'
async function validateAdmin(request: NextRequest) {
const authHeader = request.headers.get('authorization')

View File

@@ -1,5 +1,5 @@
import { NextRequest, NextResponse } from 'next/server'
import { supabaseAdmin } from '@/lib/supabase/client'
import { supabaseAdmin } from '@/lib/supabase/admin'
async function validateAdminOrStaff(request: NextRequest) {
const authHeader = request.headers.get('authorization')

View File

@@ -1,5 +1,5 @@
import { NextRequest, NextResponse } from 'next/server'
import { supabaseAdmin } from '@/lib/supabase/client'
import { supabaseAdmin } from '@/lib/supabase/admin'
/**
* @description Retrieves available staff for a time range

View File

@@ -1,5 +1,5 @@
import { NextRequest, NextResponse } from 'next/server'
import { supabaseAdmin } from '@/lib/supabase/client'
import { supabaseAdmin } from '@/lib/supabase/admin'
/**
* @description Retrieves detailed availability time slots for a date

View File

@@ -1,5 +1,5 @@
import { NextRequest, NextResponse } from 'next/server'
import { supabaseAdmin } from '@/lib/supabase/client'
import { supabaseAdmin } from '@/lib/supabase/admin'
/**
* @description Updates the status of a specific booking

View File

@@ -1,5 +1,5 @@
import { NextRequest, NextResponse } from 'next/server'
import { supabaseAdmin } from '@/lib/supabase/client'
import { supabaseAdmin } from '@/lib/supabase/admin'
import { generateShortId } from '@/lib/utils/short-id'
/**
@@ -9,6 +9,7 @@ export async function POST(request: NextRequest) {
try {
const body = await request.json()
const {
customer_id,
customer_email,
customer_phone,
customer_first_name,
@@ -19,9 +20,16 @@ export async function POST(request: NextRequest) {
notes
} = body
if (!customer_email || !service_id || !location_id || !start_time_utc) {
if (!customer_id && (!customer_email || !customer_first_name || !customer_last_name)) {
return NextResponse.json(
{ error: 'Missing required fields: customer_email, service_id, location_id, start_time_utc' },
{ error: 'Missing required fields: customer_id OR (customer_email, customer_first_name, customer_last_name)' },
{ status: 400 }
)
}
if (!service_id || !location_id || !start_time_utc) {
return NextResponse.json(
{ error: 'Missing required fields: service_id, location_id, start_time_utc' },
{ status: 400 }
)
}
@@ -122,25 +130,39 @@ export async function POST(request: NextRequest) {
const assignedResource = availableResources[0]
// Create or find customer based on email
const { data: customer, error: customerError } = await supabaseAdmin
.from('customers')
.upsert({
email: customer_email,
phone: customer_phone || null,
first_name: customer_first_name || null,
last_name: customer_last_name || null
}, {
onConflict: 'email',
ignoreDuplicates: false
})
.select()
.single()
let customer
let customerError
if (customer_id) {
const result = await supabaseAdmin
.from('customers')
.select('*')
.eq('id', customer_id)
.single()
customer = result.data
customerError = result.error
} else {
const result = await supabaseAdmin
.from('customers')
.upsert({
email: customer_email,
phone: customer_phone || null,
first_name: customer_first_name || null,
last_name: customer_last_name || null
}, {
onConflict: 'email',
ignoreDuplicates: false
})
.select()
.single()
customer = result.data
customerError = result.error
}
if (customerError || !customer) {
console.error('Error creating customer:', customerError)
console.error('Error handling customer:', customerError)
return NextResponse.json(
{ error: 'Failed to create customer' },
{ error: 'Failed to handle customer' },
{ status: 500 }
)
}
@@ -248,8 +270,7 @@ export async function GET(request: NextRequest) {
id,
name,
duration_minutes,
base_price,
category
base_price
),
resource (
id,

View File

@@ -1,6 +1,6 @@
import { NextRequest, NextResponse } from 'next/server'
import Stripe from 'stripe'
import { supabaseAdmin } from '@/lib/supabase/client'
import { supabaseAdmin } from '@/lib/supabase/admin'
const stripe = new Stripe(process.env.STRIPE_SECRET_KEY!)

120
app/api/customers/route.ts Normal file
View File

@@ -0,0 +1,120 @@
import { NextRequest, NextResponse } from 'next/server'
import { supabaseAdmin } from '@/lib/supabase/admin'
export async function GET(request: NextRequest) {
try {
const { searchParams } = new URL(request.url)
const email = searchParams.get('email')
const phone = searchParams.get('phone')
if (!email && !phone) {
return NextResponse.json(
{ error: 'Se requiere email o teléfono' },
{ status: 400 }
)
}
let query = supabaseAdmin.from('customers').select('*').eq('is_active', true)
if (email) {
query = query.ilike('email', email)
} else if (phone) {
query = query.ilike('phone', phone)
}
const { data: customers, error } = await query.limit(1)
if (error) {
console.error('Error buscando cliente:', error)
return NextResponse.json(
{ error: 'Error al buscar cliente' },
{ status: 500 }
)
}
return NextResponse.json({
exists: customers && customers.length > 0,
customer: customers && customers.length > 0 ? customers[0] : null
})
} catch (error) {
console.error('Error en GET /api/customers:', error)
return NextResponse.json(
{ error: 'Error interno del servidor' },
{ status: 500 }
)
}
}
export async function POST(request: NextRequest) {
try {
const body = await request.json()
const { email, phone, first_name, last_name, birthday, occupation } = body
if (!email || !phone || !first_name || !last_name) {
return NextResponse.json(
{ error: 'Faltan campos requeridos: email, phone, first_name, last_name' },
{ status: 400 }
)
}
const { data: existingCustomer, error: checkError } = await supabaseAdmin
.from('customers')
.select('*')
.or(`email.ilike.${email},phone.ilike.${phone}`)
.eq('is_active', true)
.limit(1)
.single()
if (checkError && checkError.code !== 'PGRST116') {
console.error('Error verificando cliente existente:', checkError)
return NextResponse.json(
{ error: 'Error al verificar cliente existente' },
{ status: 500 }
)
}
if (existingCustomer) {
return NextResponse.json({
success: false,
message: 'El cliente ya existe',
customer: existingCustomer
})
}
const { data: newCustomer, error: insertError } = await supabaseAdmin
.from('customers')
.insert({
email,
phone,
first_name,
last_name,
tier: 'free',
total_spent: 0,
total_visits: 0,
is_active: true,
notes: birthday || occupation ? JSON.stringify({ birthday, occupation }) : null
})
.select()
.single()
if (insertError) {
console.error('Error creando cliente:', insertError)
return NextResponse.json(
{ error: 'Error al crear cliente' },
{ status: 500 }
)
}
return NextResponse.json({
success: true,
message: 'Cliente registrado exitosamente',
customer: newCustomer
})
} catch (error) {
console.error('Error en POST /api/customers:', error)
return NextResponse.json(
{ error: 'Error interno del servidor' },
{ status: 500 }
)
}
}

View File

@@ -1,5 +1,5 @@
import { NextRequest, NextResponse } from 'next/server'
import { supabaseAdmin } from '@/lib/supabase/client'
import { supabaseAdmin } from '@/lib/supabase/admin'
import { Kiosk } from '@/lib/db/types'
/**

View File

@@ -1,5 +1,5 @@
import { NextRequest, NextResponse } from 'next/server'
import { supabaseAdmin } from '@/lib/supabase/client'
import { supabaseAdmin } from '@/lib/supabase/admin'
async function validateKiosk(request: NextRequest) {
const apiKey = request.headers.get('x-kiosk-api-key')

View File

@@ -1,5 +1,5 @@
import { NextRequest, NextResponse } from 'next/server'
import { supabaseAdmin } from '@/lib/supabase/client'
import { supabaseAdmin } from '@/lib/supabase/admin'
async function validateKiosk(request: NextRequest) {
const apiKey = request.headers.get('x-kiosk-api-key')

View File

@@ -1,5 +1,5 @@
import { NextRequest, NextResponse } from 'next/server'
import { supabaseAdmin } from '@/lib/supabase/client'
import { supabaseAdmin } from '@/lib/supabase/admin'
async function validateKiosk(request: NextRequest) {
const apiKey = request.headers.get('x-kiosk-api-key')

View File

@@ -1,5 +1,5 @@
import { NextRequest, NextResponse } from 'next/server'
import { supabaseAdmin } from '@/lib/supabase/client'
import { supabaseAdmin } from '@/lib/supabase/admin'
/**
* Validates kiosk API key and returns kiosk info if valid

View File

@@ -1,5 +1,5 @@
import { NextRequest, NextResponse } from 'next/server'
import { supabaseAdmin } from '@/lib/supabase/client'
import { supabaseAdmin } from '@/lib/supabase/admin'
/**
* @description Retrieves all active locations

View File

@@ -1,5 +1,5 @@
import { NextRequest, NextResponse } from 'next/server'
import { supabaseAdmin } from '@/lib/supabase/client'
import { supabaseAdmin } from '@/lib/supabase/admin'
/**
* @description Retrieves active services, optionally filtered by location
@@ -11,9 +11,8 @@ export async function GET(request: NextRequest) {
let query = supabaseAdmin
.from('services')
.select('*')
.select('id, name, description, duration_minutes, base_price, requires_dual_artist, premium_fee_enabled, category, is_active, created_at, updated_at')
.eq('is_active', true)
.order('category', { ascending: true })
.order('name', { ascending: true })
if (locationId) {