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 } })