mirror of
https://github.com/marcogll/AnchorOS.git
synced 2026-03-16 10:24:49 +00:00
- Add JSDoc comments to API routes and business logic functions - Update README.md with Phase 2 status and deployment/production notes - Enhance TASKS.md with estimated timelines and dependencies - Create docs/STAFF_TRAINING.md for team onboarding - Create docs/CLIENT_ONBOARDING.md for customer experience - Create docs/OPERATIONAL_PROCEDURES.md for daily operations - Create docs/TROUBLESHOOTING.md for common setup issues - Fix TypeScript errors in hq/page.tsx
63 lines
2.0 KiB
TypeScript
63 lines
2.0 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server'
|
|
import { supabaseAdmin } from '@/lib/supabase/client'
|
|
|
|
/**
|
|
* @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 })
|
|
}
|
|
} |