Some checks failed
ci / ci (22, ubuntu-latest) (push) Has been cancelled
Nuxt 4 + Supabase + Flightics API. Incluye búsqueda de vuelos, inspiraciones, watchlist, tracking de precios y mapa interactivo. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
34 lines
1.1 KiB
TypeScript
34 lines
1.1 KiB
TypeScript
import { serverSupabaseServiceRole, serverSupabaseClient } from '#supabase/server'
|
|
|
|
export default defineEventHandler(async (event) => {
|
|
const client = await serverSupabaseClient(event)
|
|
const { data: { user } } = await client.auth.getUser()
|
|
if (!user) throw createError({ statusCode: 401, message: 'No autenticado' })
|
|
|
|
const supabase = serverSupabaseServiceRole(event)
|
|
|
|
const { data: searches, error } = await supabase
|
|
.from('tracked_searches')
|
|
.select('*')
|
|
.eq('user_id', user.id)
|
|
.order('created_at', { ascending: false })
|
|
|
|
if (error) throw createError({ statusCode: 500, message: error.message })
|
|
|
|
const searchesWithSnapshot = await Promise.all(
|
|
((searches as Record<string, unknown>[]) || []).map(async (s) => {
|
|
const { data: snapshot } = await supabase
|
|
.from('price_snapshots')
|
|
.select('cheapest_price, avg_price, median_price, total_results, recorded_at')
|
|
.eq('tracked_search_id', s.id as string)
|
|
.order('recorded_at', { ascending: false })
|
|
.limit(1)
|
|
.single()
|
|
|
|
return { ...s, latest_snapshot: snapshot || null }
|
|
})
|
|
)
|
|
|
|
return searchesWithSnapshot
|
|
})
|