51 lines
1.7 KiB
TypeScript
51 lines
1.7 KiB
TypeScript
import { readFileSync } from 'fs'
|
|
import { resolve } from 'path'
|
|
|
|
export default defineEventHandler(async (event) => {
|
|
const config = useRuntimeConfig()
|
|
|
|
// Paramètre optionnel ?format=map pour retourner uniquement les champs carte
|
|
const query = getQuery(event)
|
|
|
|
try {
|
|
// NocoDB V2 — table organisations, filtre approved + published
|
|
const url = `${config.nocodbUrl}/api/v2/tables/${config.orgTableId}/records?where=(moderation_status,eq,approved)&limit=300&sort=-Id`
|
|
|
|
const data: any = await $fetch(url, {
|
|
headers: { 'xc-token': config.nocodbToken },
|
|
timeout: 8000,
|
|
})
|
|
|
|
return { list: data?.list ?? [], source: 'nocodb' }
|
|
} catch (err) {
|
|
// Fallback seed JSON pour dev local si NocoDB inaccessible
|
|
console.warn('[NAV API] NocoDB inaccessible, fallback seed JSON:', err)
|
|
|
|
try {
|
|
const seedPath = resolve(process.cwd(), 'V2-cadrage/seed-94-fiches-v2.json')
|
|
const raw = readFileSync(seedPath, 'utf-8')
|
|
const seed: any[] = JSON.parse(raw)
|
|
|
|
// Normaliser le seed au format NocoDB (ajouter Id fictif)
|
|
const list = seed.map((item: any, i: number) => ({
|
|
Id: 1000 + i,
|
|
nom: item.nom,
|
|
url: item.url,
|
|
description: item.description,
|
|
echelle: item.echelle,
|
|
tags_fonction: item.fonctions?.join(',') ?? '',
|
|
territoire: item.territoire,
|
|
localisation_ville: item.ville,
|
|
latitude: item.lat,
|
|
longitude: item.lon,
|
|
moderation_status: 'approved',
|
|
prioritaire: false,
|
|
}))
|
|
|
|
return { list, source: 'seed' }
|
|
} catch (seedErr) {
|
|
throw createError({ statusCode: 503, message: 'Service indisponible — NocoDB et seed inaccessibles' })
|
|
}
|
|
}
|
|
})
|