mirror of
https://github.com/marcogll/AnchorOS.git
synced 2026-03-15 16:24:30 +00:00
- Add KiosksManagement component with full CRUD for kiosks - Add ScheduleManagement for staff schedules with break reminders - Update booking flow to allow artist selection by customers - Add staff_services API for assigning services to artists - Update staff management UI with service assignment dialog - Add auto-break reminder when schedule >= 8 hours - Update availability API to filter artists by service - Add kiosk management to Aperture dashboard - Clean up ralphy artifacts and logs
71 lines
2.7 KiB
TypeScript
71 lines
2.7 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server'
|
|
import { supabaseAdmin } from '@/lib/supabase/admin'
|
|
|
|
/**
|
|
* @description Generates sales report with metrics: total revenue, completed bookings, average price, and sales breakdown by service
|
|
* @returns {NextResponse} JSON with success status and comprehensive sales metrics
|
|
* @example GET /api/aperture/reports/sales
|
|
* @audit BUSINESS RULE: Only completed bookings (status='completed') counted in sales metrics
|
|
* @audit SECURITY: Sales data restricted to admin/manager roles for financial confidentiality
|
|
* @audit Validate: No query parameters required - returns all-time sales data
|
|
* @audit PERFORMANCE: Uses reduce operations on client side for aggregation (suitable for small-medium datasets)
|
|
* @audit PERFORMANCE: Consider adding date filters for larger datasets (current implementation scans all bookings)
|
|
* @audit AUDIT: Sales reports generated logged for financial compliance and auditing
|
|
*/
|
|
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 })
|
|
}
|
|
} |