139 lines
3.8 KiB
TypeScript
139 lines
3.8 KiB
TypeScript
import { defineHook } from "@directus/extensions-sdk"
|
|
import { writeFile } from "fs/promises"
|
|
import { join } from "path"
|
|
import { formatDate } from "./utils"
|
|
|
|
export default defineHook(({ action }, { env }) => {
|
|
console.log("!!! extension loaded with collection:", env.COLLECTION)
|
|
|
|
const mapsGraphQLQuery = `
|
|
query getMaps {
|
|
maps {
|
|
id
|
|
property_name
|
|
description
|
|
date_created
|
|
photos {
|
|
photo {
|
|
id
|
|
filename_disk
|
|
}
|
|
}
|
|
property_code_type
|
|
geoFeatures
|
|
property_rooms
|
|
property_surface
|
|
property_sell
|
|
property_fees
|
|
property_price
|
|
property_dpe_conso
|
|
property_dpe_ges
|
|
property_dpe_date
|
|
property_depenses_min
|
|
property_depenses_max
|
|
property_depenses_est
|
|
property_dpe_date_price
|
|
property_PMR
|
|
property_furnished
|
|
property_floor
|
|
}
|
|
}
|
|
`
|
|
|
|
const handleMapsEvent = async () => {
|
|
const response = await fetch(`${env.LOCAL_URL}/graphql`, {
|
|
method: "POST",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
Authorization: `Bearer ${env.API_TOKEN}`,
|
|
},
|
|
body: JSON.stringify({ query: mapsGraphQLQuery }),
|
|
})
|
|
|
|
const json = await response.json()
|
|
const allMaps = json.data?.maps ?? []
|
|
|
|
const transformMapData = (maps: any[]): {} => {
|
|
const annonces: any[] = maps.map((map) => ({
|
|
// ANNONCE
|
|
reference: map.id,
|
|
titre: map.property_name ?? undefined,
|
|
texte: map?.description ?? "Pas encore de description.",
|
|
date_saisie: formatDate(map.date_created),
|
|
photos: map?.photos?.map(
|
|
(img: any) => `${env.PUBLIC_URL}/assets/${img.photo.filename_disk}`
|
|
),
|
|
|
|
// BIEN
|
|
code_type: map.property_code_type ?? undefined,
|
|
code_postal: map.geoFeatures.properties.postcode,
|
|
ville: map.geoFeatures.properties.city,
|
|
nb_pieces_logement: map.property_rooms ?? undefined,
|
|
surface: map.property_surface ?? undefined,
|
|
adresse: map.geoFeatures.properties.name ?? undefined,
|
|
meuble: map.property_furnished,
|
|
acces_handicapes: map.property_PMR,
|
|
etage: map.property_floor ?? undefined,
|
|
charges_copropriete: map.property_utilities ?? undefined,
|
|
|
|
// PRESTATION
|
|
type: map.property_sell,
|
|
frais_agence: map.property_fees ?? undefined,
|
|
// si vente
|
|
prix: map.property_sell !== "L" ? map.property_price : undefined,
|
|
// si location
|
|
loyer_mensuel:
|
|
map.property_sell === "L" ? map.property_price : undefined,
|
|
|
|
// DPE
|
|
dpe_valeur_conso: map.property_dpe_conso,
|
|
dpe_valeur_ges: map.property_dpe_ges,
|
|
dpe_date_realisation: formatDate(map.property_dpe_date),
|
|
montant_depenses_energies_min: map.property_depenses_min,
|
|
montant_depenses_energies_max: map.property_depenses_max,
|
|
montant_depenses_energies_estime: map.property_depenses_est,
|
|
date_indice_prix_energies: formatDate(map.property_dpe_date_price),
|
|
|
|
// Complex nested objects
|
|
// settings: {
|
|
// zoom: map?.settings?.zoom ?? 10,
|
|
// theme: map?.settings?.theme ?? "default",
|
|
// markers: map?.settings?.markers?.enabled ?? false,
|
|
// },
|
|
|
|
// Conditional assignment using optional chaining
|
|
// ...(map?.location && {
|
|
// latitude: map.location?.lat,
|
|
// longitude: map.location?.lng,
|
|
// }),
|
|
|
|
// Keep some original structure if it exists
|
|
// ...(map?.metadata && { originalMetadata: map.metadata }),
|
|
}))
|
|
|
|
return annonces
|
|
}
|
|
|
|
// apply key remapping
|
|
const transformedMaps = Array.isArray(allMaps)
|
|
? transformMapData(allMaps)
|
|
: []
|
|
|
|
const jsonData = JSON.stringify(transformedMaps, null, 2)
|
|
|
|
// write to existing file
|
|
const filePath = join(env.OUTPUT_DIR, env.OUTPUT_FILENAME)
|
|
await writeFile(filePath, jsonData, "utf8")
|
|
}
|
|
|
|
// Register hook for operations on maps
|
|
const mapsActions = [
|
|
`${env.COLLECTION}.items.create`,
|
|
`${env.COLLECTION}.items.update`,
|
|
`${env.COLLECTION}.items.delete`,
|
|
]
|
|
|
|
mapsActions.forEach((actionName) => {
|
|
action(actionName, handleMapsEvent)
|
|
})
|
|
})
|