Files
vuelato/server/api/sync/locations.post.ts
Alejandro Martinez b8906efc80
Some checks failed
ci / ci (22, ubuntu-latest) (push) Has been cancelled
Initial commit: Vuelato - buscador de vuelos
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>
2026-04-10 23:37:06 +02:00

60 lines
1.8 KiB
TypeScript

import { serverSupabaseServiceRole } from '#supabase/server'
export default defineEventHandler(async (event) => {
const supabase = serverSupabaseServiceRole(event)
// Fetch from Flightics
const [locData, countryData] = await Promise.all([
getLocations(),
getCountries()
])
// Build lookup maps for city names and countries
const cityMap = new Map(locData.cities.map(c => [c.id, c]))
const locCountryMap = new Map(locData.countries.map(c => [c.id, c]))
// Upsert airports enriched with city and country names
const airports = locData.airports.map(a => {
const city = cityMap.get(a.cityId)
const country = city ? locCountryMap.get(city.countryId) : undefined
return {
iata: a.iata,
icao: a.icao,
name: a.nameEng,
lat: a.lat,
lon: a.lon,
city_id: a.cityId,
city_name: city?.nameEng || '',
country_code: country?.isoCode2 || '',
country_name: country?.nameEng || '',
updated_at: new Date().toISOString()
}
})
if (airports.length > 0) {
const { error: airportErr } = await supabase
.from('airports')
.upsert(airports as never, { onConflict: 'iata' })
if (airportErr) throw createError({ statusCode: 500, message: airportErr.message })
}
// Upsert countries from dedicated endpoint
const countries = countryData.countries.map(c => ({
iso_code2: c.isoCode2,
iso_code3: c.isoCode3,
name_eng: c.nameEng,
name_native: c.nameNative,
phone_prefix: c.phonePreselection,
updated_at: new Date().toISOString()
}))
if (countries.length > 0) {
const { error: countryErr } = await supabase
.from('countries')
.upsert(countries as never, { onConflict: 'iso_code2' })
if (countryErr) throw createError({ statusCode: 500, message: countryErr.message })
}
return { count: airports.length, countries: countries.length }
})