mirror of
https://github.com/marcogll/AnchorOS.git
synced 2026-03-15 11:24:26 +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
185 lines
4.2 KiB
TypeScript
185 lines
4.2 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server'
|
|
import { supabaseAdmin } from '@/lib/supabase/admin'
|
|
|
|
async function validateAdminOrStaff(request: NextRequest) {
|
|
const authHeader = request.headers.get('authorization')
|
|
|
|
if (!authHeader) {
|
|
return null
|
|
}
|
|
|
|
const token = authHeader.replace('Bearer ', '')
|
|
|
|
if (token !== process.env.ADMIN_ENROLLMENT_KEY) {
|
|
return null
|
|
}
|
|
|
|
return true
|
|
}
|
|
|
|
/**
|
|
* @description Marks staff as unavailable for a time period
|
|
*/
|
|
export async function POST(request: NextRequest) {
|
|
try {
|
|
const hasAccess = await validateAdminOrStaff(request)
|
|
|
|
if (!hasAccess) {
|
|
return NextResponse.json(
|
|
{ error: 'Unauthorized' },
|
|
{ status: 401 }
|
|
)
|
|
}
|
|
|
|
const body = await request.json()
|
|
const {
|
|
staff_id,
|
|
date,
|
|
start_time,
|
|
end_time,
|
|
reason,
|
|
location_id
|
|
} = body
|
|
|
|
if (!staff_id || !date || !start_time || !end_time) {
|
|
return NextResponse.json(
|
|
{ error: 'Missing required fields: staff_id, date, start_time, end_time' },
|
|
{ status: 400 }
|
|
)
|
|
}
|
|
|
|
const { data: staff, error: staffError } = await supabaseAdmin
|
|
.from('staff')
|
|
.select('id, location_id')
|
|
.eq('id', staff_id)
|
|
.single()
|
|
|
|
if (staffError || !staff) {
|
|
return NextResponse.json(
|
|
{ error: staffError?.message || 'Staff not found' },
|
|
{ status: 400 }
|
|
)
|
|
}
|
|
|
|
const { data: availability, error: availabilityError } = await supabaseAdmin.rpc('check_staff_availability', {
|
|
p_staff_id: staff_id,
|
|
p_start_time_utc: `${date}T${start_time}Z`,
|
|
p_end_time_utc: `${date}T${end_time}Z`
|
|
})
|
|
|
|
if (availabilityError) {
|
|
return NextResponse.json(
|
|
{ error: availabilityError.message },
|
|
{ status: 400 }
|
|
)
|
|
}
|
|
|
|
const { data: existingAvailability } = await supabaseAdmin
|
|
.from('staff_availability')
|
|
.select('*')
|
|
.eq('staff_id', staff_id)
|
|
.eq('date', date)
|
|
.single()
|
|
|
|
if (existingAvailability) {
|
|
return NextResponse.json(
|
|
{ error: 'Availability already exists for this staff and date' },
|
|
{ status: 400 }
|
|
)
|
|
}
|
|
|
|
const { data: newAvailability, error: createError } = await supabaseAdmin
|
|
.from('staff_availability')
|
|
.insert({
|
|
staff_id,
|
|
date,
|
|
start_time,
|
|
end_time,
|
|
is_available: false,
|
|
reason,
|
|
created_by: staff_id
|
|
})
|
|
.select()
|
|
.single()
|
|
|
|
if (createError) {
|
|
return NextResponse.json(
|
|
{ error: createError.message },
|
|
{ status: 400 }
|
|
)
|
|
}
|
|
|
|
return NextResponse.json({
|
|
success: true,
|
|
availability: newAvailability
|
|
})
|
|
} catch (error) {
|
|
console.error('Staff unavailable POST error:', error)
|
|
return NextResponse.json(
|
|
{ error: 'Internal server error' },
|
|
{ status: 500 }
|
|
)
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @description Retrieves staff unavailability records
|
|
*/
|
|
export async function GET(request: NextRequest) {
|
|
try {
|
|
const hasAccess = await validateAdminOrStaff(request)
|
|
|
|
if (!hasAccess) {
|
|
return NextResponse.json(
|
|
{ error: 'Unauthorized' },
|
|
{ status: 401 }
|
|
)
|
|
}
|
|
|
|
const { searchParams } = new URL(request.url)
|
|
const staffId = searchParams.get('staff_id')
|
|
const startDate = searchParams.get('start_date')
|
|
const endDate = searchParams.get('end_date')
|
|
|
|
if (!staffId) {
|
|
return NextResponse.json(
|
|
{ error: 'Missing required parameter: staff_id' },
|
|
{ status: 400 }
|
|
)
|
|
}
|
|
|
|
let query = supabaseAdmin
|
|
.from('staff_availability')
|
|
.select('*')
|
|
.eq('staff_id', staffId)
|
|
|
|
if (startDate) {
|
|
query = query.gte('date', startDate)
|
|
}
|
|
|
|
if (endDate) {
|
|
query = query.lte('date', endDate)
|
|
}
|
|
|
|
const { data: availabilityList, error } = await query.order('date', { ascending: true })
|
|
|
|
if (error) {
|
|
return NextResponse.json(
|
|
{ error: error.message },
|
|
{ status: 400 }
|
|
)
|
|
}
|
|
|
|
return NextResponse.json({
|
|
success: true,
|
|
availability: availabilityList || []
|
|
})
|
|
} catch (error) {
|
|
console.error('Staff unavailable GET error:', error)
|
|
return NextResponse.json(
|
|
{ error: 'Internal server error' },
|
|
{ status: 500 }
|
|
)
|
|
}
|
|
}
|