mirror of
https://github.com/marcogll/AnchorOS.git
synced 2026-03-15 22:24:34 +00:00
Add detailed logging to API endpoints for debugging 500 errors
This commit is contained in:
@@ -22,7 +22,9 @@ async function validateKiosk(request: NextRequest) {
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Creates a walk-in booking for kiosk
|
||||
* @description FASE 2.2: Creates walk-in booking with dual artist + premium fee support
|
||||
* @sprint 2.2 Dual Artists: Auto-assigns primary/secondary artists & room if service.requires_dual_artist
|
||||
* @sprint 2.2: total_amount = calculate_service_total(service_id) incl premium
|
||||
*/
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
@@ -66,43 +68,69 @@ export async function POST(request: NextRequest) {
|
||||
)
|
||||
}
|
||||
|
||||
const { data: availableStaff } = await supabaseAdmin
|
||||
.from('staff')
|
||||
.select('id, display_name, role')
|
||||
.eq('location_id', kiosk.location_id)
|
||||
.eq('is_active', true)
|
||||
.in('role', ['artist', 'staff', 'manager'])
|
||||
|
||||
if (!availableStaff || availableStaff.length === 0) {
|
||||
return NextResponse.json(
|
||||
{ error: 'No staff available' },
|
||||
{ status: 400 }
|
||||
)
|
||||
}
|
||||
|
||||
const assignedStaff = availableStaff[0]
|
||||
|
||||
// For walk-ins, booking starts immediately
|
||||
const startTime = new Date()
|
||||
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()
|
||||
})
|
||||
let staff_id: string
|
||||
let secondary_artist_id: string | null = null
|
||||
let resource_id: string
|
||||
|
||||
if (!availableResources || availableResources.length === 0) {
|
||||
return NextResponse.json(
|
||||
{ error: 'No resources available for immediate booking' },
|
||||
{ status: 400 }
|
||||
)
|
||||
if (service.requires_dual_artist) {
|
||||
const { data: assignment } = await supabaseAdmin
|
||||
.rpc('assign_dual_artists', {
|
||||
p_location_id: kiosk.location_id,
|
||||
p_start_time_utc: startTime.toISOString(),
|
||||
p_end_time_utc: endTime.toISOString(),
|
||||
p_service_id: service.id
|
||||
})
|
||||
|
||||
if (!assignment || !assignment.success) {
|
||||
return NextResponse.json(
|
||||
{ error: assignment?.error || 'No dual artists or room available' },
|
||||
{ status: 400 }
|
||||
)
|
||||
}
|
||||
|
||||
staff_id = assignment.primary_artist
|
||||
secondary_artist_id = assignment.secondary_artist
|
||||
resource_id = assignment.room_resource
|
||||
} else {
|
||||
const { data: availableStaff } = await supabaseAdmin
|
||||
.from('staff')
|
||||
.select('id')
|
||||
.eq('location_id', kiosk.location_id)
|
||||
.eq('is_active', true)
|
||||
.in('role', ['artist', 'staff', 'manager'])
|
||||
.limit(1)
|
||||
|
||||
if (!availableStaff || availableStaff.length === 0) {
|
||||
return NextResponse.json(
|
||||
{ error: 'No staff available' },
|
||||
{ status: 400 }
|
||||
)
|
||||
}
|
||||
|
||||
staff_id = availableStaff[0].id
|
||||
|
||||
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 immediate booking' },
|
||||
{ status: 400 }
|
||||
)
|
||||
}
|
||||
|
||||
resource_id = availableResources[0].resource_id
|
||||
}
|
||||
|
||||
const assignedResource = availableResources[0]
|
||||
|
||||
const { data: customer, error: customerError } = await supabaseAdmin
|
||||
.from('customers')
|
||||
.upsert({
|
||||
@@ -123,19 +151,22 @@ export async function POST(request: NextRequest) {
|
||||
)
|
||||
}
|
||||
|
||||
const { data: total } = await supabaseAdmin.rpc('calculate_service_total', { p_service_id: service.id })
|
||||
|
||||
const { data: booking, error: bookingError } = await supabaseAdmin
|
||||
.from('bookings')
|
||||
.insert({
|
||||
customer_id: customer.id,
|
||||
staff_id: assignedStaff.id,
|
||||
staff_id,
|
||||
secondary_artist_id,
|
||||
location_id: kiosk.location_id,
|
||||
resource_id: assignedResource.resource_id,
|
||||
resource_id,
|
||||
service_id,
|
||||
start_time_utc: startTime.toISOString(),
|
||||
end_time_utc: endTime.toISOString(),
|
||||
status: 'confirmed',
|
||||
deposit_amount: 0,
|
||||
total_amount: service.base_price,
|
||||
total_amount: total ?? service.base_price,
|
||||
is_paid: false,
|
||||
notes: notes ? `${notes} [Walk-in]` : '[Walk-in]'
|
||||
})
|
||||
@@ -161,15 +192,38 @@ export async function POST(request: NextRequest) {
|
||||
console.error('Failed to send receipt email:', emailError)
|
||||
}
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
booking,
|
||||
service_name: service.name,
|
||||
resource_name: assignedResource.resource_name,
|
||||
resource_type: assignedResource.resource_type,
|
||||
staff_name: assignedStaff.display_name,
|
||||
message: 'Walk-in booking created successfully'
|
||||
}, { status: 201 })
|
||||
const { data: staffData } = await supabaseAdmin
|
||||
.from('staff')
|
||||
.select('display_name')
|
||||
.eq('id', staff_id)
|
||||
.single()
|
||||
|
||||
const { data: resourceData } = await supabaseAdmin
|
||||
.from('resources')
|
||||
.select('name, type')
|
||||
.eq('id', resource_id)
|
||||
.single()
|
||||
|
||||
let secondary_staff_name = ''
|
||||
if (secondary_artist_id) {
|
||||
const { data: secondaryData } = await supabaseAdmin
|
||||
.from('staff')
|
||||
.select('display_name')
|
||||
.eq('id', secondary_artist_id)
|
||||
.single()
|
||||
secondary_staff_name = secondaryData?.display_name || ''
|
||||
}
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
booking,
|
||||
service_name: service.name,
|
||||
resource_name: resourceData?.name || '',
|
||||
resource_type: resourceData?.type || '',
|
||||
staff_name: staffData?.display_name || '',
|
||||
secondary_staff_name,
|
||||
message: 'Walk-in booking created successfully'
|
||||
}, { status: 201 })
|
||||
} catch (error) {
|
||||
console.error('Kiosk walk-in error:', error)
|
||||
return NextResponse.json(
|
||||
|
||||
Reference in New Issue
Block a user