Skip to content

Commit

Permalink
fix: static json vers une l'api adresse data gouv
Browse files Browse the repository at this point in the history
  • Loading branch information
jenovateurs committed Mar 3, 2025
1 parent 35bd968 commit 6cd5a16
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 39 deletions.
20 changes: 0 additions & 20 deletions backend/controllers/outils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import communes from "@etalab/decoupage-administratif/data/communes.json" assert { type: "json" }
import epci from "@etalab/decoupage-administratif/data/epci.json" assert { type: "json" }
import communesLonLat from "communes-lonlat"
import Sentry from "@sentry/node"

const communesActuelles = communes.filter((c) => c.type === "commune-actuelle")
const communeMap = {}
Expand Down Expand Up @@ -48,22 +46,4 @@ export default {
communes: function (req, res) {
res.send(find(req.params.codePostal))
},
centerCoordinatesFromPostalCode: function (req, res) {
try {
const postalCode = find(req.params.codePostal)[0].code
if (!postalCode) {
throw new Error("Postal code not found")
}

const commune = communesLonLat.find((c) => c.code === postalCode)
if (!commune?.centre?.coordinates) {
throw new Error("Commune coordinates not found")
}

res.send(commune.centre.coordinates)
} catch (e) {
Sentry.captureException(e)
res.status(404).send("Coordinates not found")
}
},
}
3 changes: 0 additions & 3 deletions backend/routes/outils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,4 @@ import { Express } from "express"

export default function (api: Express) {
api.route("/outils/communes/:codePostal").get(outils.communes)
api
.route("/outils/codePostal/:codePostal/centerCoordinates")
.get(outils.centerCoordinatesFromPostalCode)
}
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@
"@vitejs/plugin-legacy": "^5.1.5",
"@vitejs/plugin-vue": "^5.2.0",
"axios": "^1.7.7",
"communes-lonlat": "^1.1.0",
"consolidate": "^0.16.0",
"cookie-parser": "^1.4.7",
"core-js": "^3.39.0",
Expand Down
52 changes: 37 additions & 15 deletions src/composables/use-voluntary-organisations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,53 +5,75 @@ import axios from "axios"

type Coordinates = [number, number]

interface GeoAPIResponse {
type: string
version: string
features: Array<{
type: string
geometry: {
type: string
coordinates: [number, number]
}
properties: {
label: string
score: number
type: string
postcode: string
citycode: string
city: string
}
}>
}

export function useVolontaryOrganisations() {
const store = useStore()

const volontaryOrganisationsLink = ref<string>("")
const updating = ref<boolean>(true)

const fetchPostalCodeFromStore = async (): Promise<string> => {
const fetchPostalCodeFromStore = async (): Promise<[string, string]> => {
let postalCode = store.situation.menage._codePostal
let depcom = store.situation.menage.depcom

const simulationId = Simulations.getLatestId()
if (!store.hasResults && !postalCode && simulationId) {
await store.fetch(simulationId)
postalCode = store.situation.menage._codePostal
depcom = store.situation.menage.depcom
}
return postalCode
return [postalCode!, depcom!]
}

const fetchCenterCoordinatesFromPostalCode = async (
postalCode
postalCode: string,
depcom
): Promise<Coordinates> => {
const response = await axios.get(
`/api/outils/codePostal/${postalCode}/centerCoordinates`
const response = await axios.get<GeoAPIResponse>(
`https://api-adresse.data.gouv.fr/search/?citycode=${depcom}&q=${postalCode}&limit=1`
)
const centerCoordinates: Coordinates = response.data

const hasValidCoordinates =
centerCoordinates && centerCoordinates.length === 2

if (!hasValidCoordinates) {
const features = response.data.features
if (!features || features.length === 0) {
throw new Error(
`Can't find center coordinates for this postal code ${postalCode}`
)
}
return centerCoordinates

const coordinates = features[0].geometry.coordinates
return coordinates
}

async function buildVolontaryOrganisationsLink() {
const baseUrl = "https://www.jeveuxaider.gouv.fr/organisations"
try {
const postalCode: string = await fetchPostalCodeFromStore()
const [postalCode, depcom] = await fetchPostalCodeFromStore()

if (!postalCode) {
throw new Error("Can't find postal code")
if (!postalCode || !depcom) {
throw new Error("Can't find postal code or depcom")
}

const centerCoordinates: Coordinates =
await fetchCenterCoordinatesFromPostalCode(postalCode)
await fetchCenterCoordinatesFromPostalCode(postalCode, depcom)

volontaryOrganisationsLink.value = `${baseUrl}?city=${postalCode}&aroundLatLng=${centerCoordinates[1]},${centerCoordinates[0]}`

Expand Down

0 comments on commit 6cd5a16

Please sign in to comment.