fix: Fetch completo de negocios/shuttles e integracion de cupones en analisis

This commit is contained in:
2026-03-04 20:59:35 -05:00
parent 5a220310af
commit 52cc20e81b

View File

@ -421,11 +421,15 @@ onMounted(async () => {
try {
const [
{ count: userCount },
{ data: events }
{ data: events },
{ data: allBusinesses },
{ data: allShuttles }
] = await Promise.all([
supabase.from('users').select('*', { count: 'exact', head: true }).eq('is_active', true),
// In a production app with >1M rows we might use a group-by RPC
supabase.from('analytics_events').select('*')
supabase.from('analytics_events').select('*'),
supabase.from('businesses').select('name'),
supabase.from('shuttles').select('company_name, vehicle_type')
])
const shuttleStats: any = {}
@ -434,6 +438,25 @@ onMounted(async () => {
let total_shuttle_contacts = 0
let total_biz_views = 0
// PRE-FILL all businesses so they always show even with 0 clicks
if (allBusinesses) {
for (const b of allBusinesses) {
if (b.name) {
bizStats[b.name] = { views: 0, promos: 0, favs: 0, social: 0, location: 0, calls: 0 }
}
}
}
// PRE-FILL all shuttles so they always show even with 0 clicks
if (allShuttles) {
for (const s of allShuttles) {
const name = s.company_name || s.vehicle_type || 'shuttle'
if (!shuttleStats[name]) {
shuttleStats[name] = { views: 0, contacts: 0, calls: 0, whatsapp: 0 }
}
}
}
const safeRows = events || []
for (const ev of safeRows) {
@ -473,6 +496,19 @@ onMounted(async () => {
bizStats[nameKey].promos++
total_promo_clicks++
}
} else if (ev.entity_type === 'coupon') {
// Los cupones se suman a la parte del negocio correspondiente
const bizName = ev.properties?.business || nameKey
if (!bizStats[bizName]) {
bizStats[bizName] = { views: 0, promos: 0, favs: 0, social: 0, location: 0, calls: 0 }
}
if (ev.event_name === 'promo_view') {
bizStats[bizName].promos++
total_promo_clicks++
} else if (ev.event_name === 'location_click') {
bizStats[bizName].location++
}
}
}