Files
AnchorOS/app/api/aperture/reports/sales/route.ts
Marco Gallegos 583a25a6f6 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
2026-01-17 00:29:49 -06:00

63 lines
2.0 KiB
TypeScript

import { NextRequest, NextResponse } from 'next/server'
import { supabaseAdmin } from '@/lib/supabase/admin'
/**
* @description Fetches sales report including total sales, completed bookings, average service price, and sales by service
*/
export async function GET() {
try {
// Get total sales
const { data: bookings, error: bookingsError } = await supabaseAdmin
.from('bookings')
.select('services(base_price)')
.eq('status', 'completed')
if (bookingsError) throw bookingsError
const totalSales = bookings.reduce((sum, booking) => sum + (booking.services?.[0]?.base_price || 0), 0)
// Get completed bookings count
const completedBookings = bookings.length
// Get average service price
const { data: services, error: servicesError } = await supabaseAdmin
.from('services')
.select('base_price')
if (servicesError) throw servicesError
const avgServicePrice = services.length > 0
? Math.round(services.reduce((sum, s) => sum + s.base_price, 0) / services.length)
: 0
// Sales by service
const { data: salesByService, error: salesError } = await supabaseAdmin
.from('bookings')
.select('services(name, base_price)')
.eq('status', 'completed')
if (salesError) throw salesError
const serviceTotals: { [key: string]: number } = {}
salesByService.forEach(booking => {
const serviceName = booking.services?.[0]?.name || 'Unknown'
serviceTotals[serviceName] = (serviceTotals[serviceName] || 0) + (booking.services?.[0]?.base_price || 0)
})
const salesByServiceArray = Object.entries(serviceTotals).map(([service, total]) => ({
service,
total
}))
return NextResponse.json({
success: true,
totalSales,
completedBookings,
avgServicePrice,
salesByService: salesByServiceArray
})
} catch (error) {
console.error('Error fetching sales report:', error)
return NextResponse.json({ success: false, error: 'Failed to fetch sales report' }, { status: 500 })
}
}