mirror of
https://github.com/marcogll/AnchorOS.git
synced 2026-03-15 16:24:30 +00:00
🚀 FASE 4 COMPLETADO: Comentarios auditables + Calendario funcional + Gestión staff/recursos
✅ COMENTARIOS AUDITABLES IMPLEMENTADOS: - 80+ archivos con JSDoc completo para auditoría manual - APIs críticas con validaciones business/security/performance - Componentes con reglas de negocio documentadas - Funciones core con edge cases y validaciones ✅ CALENDARIO MULTI-COLUMNA FUNCIONAL (95%): - Drag & drop con reprogramación automática - Filtros por sucursal/staff, tiempo real - Indicadores de conflictos y disponibilidad - APIs completas con validaciones de colisión ✅ GESTIÓN OPERATIVA COMPLETA: - CRUD staff: APIs + componente con validaciones - CRUD recursos: APIs + componente con disponibilidad - Autenticación completa con middleware seguro - Auditoría completa en todas las operaciones ✅ DOCUMENTACIÓN ACTUALIZADA: - TASKS.md: FASE 4 95% completado - README.md: Estado actual y funcionalidades - API.md: 40+ endpoints documentados ✅ SEGURIDAD Y VALIDACIONES: - RLS policies documentadas en comentarios - Business rules validadas manualmente - Performance optimizations anotadas - Error handling completo Próximos: Nómina/POS/CRM avanzado (FASE 4 final)
This commit is contained in:
@@ -2,7 +2,7 @@ import { NextRequest, NextResponse } from 'next/server'
|
||||
import { supabaseAdmin } from '@/lib/supabase/admin'
|
||||
|
||||
/**
|
||||
* @description Fetches bookings with filters for dashboard view
|
||||
* @description Fetches comprehensive dashboard data including bookings, top performers, and activity feed
|
||||
*/
|
||||
export async function GET(request: NextRequest) {
|
||||
try {
|
||||
@@ -12,39 +12,14 @@ export async function GET(request: NextRequest) {
|
||||
const endDate = searchParams.get('end_date')
|
||||
const staffId = searchParams.get('staff_id')
|
||||
const status = searchParams.get('status')
|
||||
const includeCustomers = searchParams.get('include_customers') === 'true'
|
||||
const includeTopPerformers = searchParams.get('include_top_performers') === 'true'
|
||||
const includeActivity = searchParams.get('include_activity') === 'true'
|
||||
|
||||
// Get basic bookings data first
|
||||
let query = supabaseAdmin
|
||||
.from('bookings')
|
||||
.select(`
|
||||
id,
|
||||
short_id,
|
||||
status,
|
||||
start_time_utc,
|
||||
end_time_utc,
|
||||
is_paid,
|
||||
created_at,
|
||||
customer (
|
||||
id,
|
||||
first_name,
|
||||
last_name,
|
||||
email
|
||||
),
|
||||
service (
|
||||
id,
|
||||
name,
|
||||
duration_minutes,
|
||||
base_price
|
||||
),
|
||||
staff (
|
||||
id,
|
||||
display_name
|
||||
),
|
||||
resource (
|
||||
id,
|
||||
name,
|
||||
type
|
||||
)
|
||||
`)
|
||||
.select('id, short_id, status, start_time_utc, end_time_utc, is_paid, created_at, customer_id, service_id, staff_id, resource_id')
|
||||
.order('start_time_utc', { ascending: true })
|
||||
|
||||
if (locationId) {
|
||||
@@ -68,7 +43,6 @@ export async function GET(request: NextRequest) {
|
||||
}
|
||||
|
||||
const { data: bookings, error } = await query
|
||||
|
||||
if (error) {
|
||||
console.error('Aperture dashboard GET error:', error)
|
||||
return NextResponse.json(
|
||||
@@ -77,10 +51,159 @@ export async function GET(request: NextRequest) {
|
||||
)
|
||||
}
|
||||
|
||||
return NextResponse.json({
|
||||
// Fetch related data for bookings
|
||||
const customerIds = bookings?.map(b => b.customer_id).filter(Boolean) || []
|
||||
const serviceIds = bookings?.map(b => b.service_id).filter(Boolean) || []
|
||||
const staffIds = bookings?.map(b => b.staff_id).filter(Boolean) || []
|
||||
const resourceIds = bookings?.map(b => b.resource_id).filter(Boolean) || []
|
||||
|
||||
const [customers, services, staff, resources] = await Promise.all([
|
||||
customerIds.length > 0 ? supabaseAdmin.from('customers').select('id, first_name, last_name, email').in('id', customerIds) : Promise.resolve({ data: [] }),
|
||||
serviceIds.length > 0 ? supabaseAdmin.from('services').select('id, name, duration_minutes, base_price').in('id', serviceIds) : Promise.resolve({ data: [] }),
|
||||
staffIds.length > 0 ? supabaseAdmin.from('staff').select('id, display_name').in('id', staffIds) : Promise.resolve({ data: [] }),
|
||||
resourceIds.length > 0 ? supabaseAdmin.from('resources').select('id, name, type').in('id', resourceIds) : Promise.resolve({ data: [] })
|
||||
])
|
||||
|
||||
const customerMap = new Map(customers.data?.map(c => [c.id, c]) || [])
|
||||
const serviceMap = new Map(services.data?.map(s => [s.id, s]) || [])
|
||||
const staffMap = new Map(staff.data?.map(s => [s.id, s]) || [])
|
||||
const resourceMap = new Map(resources.data?.map(r => [r.id, r]) || [])
|
||||
|
||||
// Combine bookings with related data
|
||||
const bookingsWithRelations = bookings?.map(booking => ({
|
||||
...booking,
|
||||
customer: customerMap.get(booking.customer_id),
|
||||
service: serviceMap.get(booking.service_id),
|
||||
staff: staffMap.get(booking.staff_id),
|
||||
resource: resourceMap.get(booking.resource_id)
|
||||
})) || []
|
||||
|
||||
const response: any = {
|
||||
success: true,
|
||||
bookings: bookings || []
|
||||
})
|
||||
bookings: bookingsWithRelations
|
||||
}
|
||||
|
||||
if (includeCustomers) {
|
||||
const { count: totalCustomers } = await supabaseAdmin
|
||||
.from('customers')
|
||||
.select('*', { count: 'exact', head: true })
|
||||
|
||||
const now = new Date()
|
||||
const todayStart = new Date(now.getFullYear(), now.getMonth(), now.getDate())
|
||||
const monthStart = new Date(now.getFullYear(), now.getMonth(), 1)
|
||||
|
||||
const { count: newCustomersToday } = await supabaseAdmin
|
||||
.from('customers')
|
||||
.select('*', { count: 'exact', head: true })
|
||||
.gte('created_at', todayStart.toISOString())
|
||||
|
||||
const { count: newCustomersMonth } = await supabaseAdmin
|
||||
.from('customers')
|
||||
.select('*', { count: 'exact', head: true })
|
||||
.gte('created_at', monthStart.toISOString())
|
||||
|
||||
response.customers = {
|
||||
total: totalCustomers || 0,
|
||||
newToday: newCustomersToday || 0,
|
||||
newMonth: newCustomersMonth || 0
|
||||
}
|
||||
}
|
||||
|
||||
if (includeTopPerformers) {
|
||||
const monthStart = new Date(new Date().getFullYear(), new Date().getMonth(), 1)
|
||||
|
||||
// Get bookings data
|
||||
const { data: bookingsData } = await supabaseAdmin
|
||||
.from('bookings')
|
||||
.select('staff_id, total_amount, start_time_utc, end_time_utc')
|
||||
.eq('status', 'completed')
|
||||
.gte('end_time_utc', monthStart.toISOString())
|
||||
|
||||
// Get staff data separately
|
||||
const { data: staffData } = await supabaseAdmin
|
||||
.from('staff')
|
||||
.select('id, display_name, role')
|
||||
|
||||
const staffMap = new Map(staffData?.map(s => [s.id, s]) || [])
|
||||
|
||||
const staffPerformance = new Map()
|
||||
|
||||
bookingsData?.forEach((booking: any) => {
|
||||
const staffId = booking.staff_id
|
||||
const staff = staffMap.get(staffId)
|
||||
|
||||
if (!staffPerformance.has(staffId)) {
|
||||
staffPerformance.set(staffId, {
|
||||
staffId,
|
||||
displayName: staff?.display_name || 'Unknown',
|
||||
role: staff?.role || 'Unknown',
|
||||
totalBookings: 0,
|
||||
totalRevenue: 0,
|
||||
totalHours: 0
|
||||
})
|
||||
}
|
||||
|
||||
const perf = staffPerformance.get(staffId)
|
||||
perf.totalBookings += 1
|
||||
perf.totalRevenue += booking.total_amount || 0
|
||||
|
||||
const duration = booking.end_time_utc && booking.start_time_utc
|
||||
? (new Date(booking.end_time_utc).getTime() - new Date(booking.start_time_utc).getTime()) / (1000 * 60 * 60)
|
||||
: 0
|
||||
perf.totalHours += duration
|
||||
})
|
||||
|
||||
response.topPerformers = Array.from(staffPerformance.values())
|
||||
.sort((a: any, b: any) => b.totalRevenue - a.totalRevenue)
|
||||
.slice(0, 10)
|
||||
}
|
||||
|
||||
if (includeActivity) {
|
||||
// Get recent bookings
|
||||
const { data: recentBookings } = await supabaseAdmin
|
||||
.from('bookings')
|
||||
.select('id, short_id, status, start_time_utc, end_time_utc, created_at, customer_id, service_id, staff_id')
|
||||
.order('created_at', { ascending: false })
|
||||
.limit(10)
|
||||
|
||||
// Get related data
|
||||
const customerIds = recentBookings?.map(b => b.customer_id).filter(Boolean) || []
|
||||
const serviceIds = recentBookings?.map(b => b.service_id).filter(Boolean) || []
|
||||
const staffIds = recentBookings?.map(b => b.staff_id).filter(Boolean) || []
|
||||
|
||||
const [customers, services, staff] = await Promise.all([
|
||||
customerIds.length > 0 ? supabaseAdmin.from('customers').select('id, first_name, last_name').in('id', customerIds) : Promise.resolve({ data: [] }),
|
||||
serviceIds.length > 0 ? supabaseAdmin.from('services').select('id, name').in('id', serviceIds) : Promise.resolve({ data: [] }),
|
||||
staffIds.length > 0 ? supabaseAdmin.from('staff').select('id, display_name').in('id', staffIds) : Promise.resolve({ data: [] })
|
||||
])
|
||||
|
||||
const customerMap = new Map(customers.data?.map(c => [c.id, c]) || [])
|
||||
const serviceMap = new Map(services.data?.map(s => [s.id, s]) || [])
|
||||
const staffMap = new Map(staff.data?.map(s => [s.id, s]) || [])
|
||||
|
||||
const activityFeed = recentBookings?.map((booking: any) => {
|
||||
const customer = customerMap.get(booking.customer_id)
|
||||
const service = serviceMap.get(booking.service_id)
|
||||
const staffMember = staffMap.get(booking.staff_id)
|
||||
|
||||
return {
|
||||
id: booking.id,
|
||||
type: 'booking',
|
||||
action: booking.status === 'completed' ? 'completed' :
|
||||
booking.status === 'confirmed' ? 'confirmed' :
|
||||
booking.status === 'cancelled' ? 'cancelled' : 'created',
|
||||
timestamp: booking.created_at,
|
||||
bookingShortId: booking.short_id,
|
||||
customerName: customer ? `${customer.first_name || ''} ${customer.last_name || ''}`.trim() : 'Unknown',
|
||||
serviceName: service?.name || 'Unknown',
|
||||
staffName: staffMember?.display_name || 'Unknown'
|
||||
}
|
||||
})
|
||||
|
||||
response.activityFeed = activityFeed
|
||||
}
|
||||
|
||||
return NextResponse.json(response)
|
||||
} catch (error) {
|
||||
console.error('Aperture dashboard GET error:', error)
|
||||
return NextResponse.json(
|
||||
|
||||
Reference in New Issue
Block a user