Skip to content

Commit

Permalink
Added new DILA api route and result formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
antoineludeau committed Apr 10, 2024
1 parent 3aafb34 commit 86fa703
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .env.default
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ NEXT_PUBLIC_ADRESSE_URL=http://localhost:3000
NEXT_PUBLIC_API_GEO_URL=https://geo.api.gouv.fr/2023
NEXT_PUBLIC_API_BAN_URL=https://plateforme.adresse.data.gouv.fr
NEXT_PUBLIC_API_ADRESSE=https://api-adresse.data.gouv.fr
NEXT_PUBLIC_API_ETABLISSEMENTS_PUBLIC=https://etablissements-publics.api.gouv.fr/v3
NEXT_PUBLIC_API_ETABLISSEMENTS_PUBLIC=https://api-lannuaire.service-public.fr/api/explore/v2.1
NEXT_PUBLIC_MATOMO_URL=
NEXT_PUBLIC_MATOMO_SITE_ID=
MATOMO_TOKEN_AUTH=
Expand Down
5 changes: 2 additions & 3 deletions components/bases-locales/charte/partner.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,11 @@ function Partner({partnerInfos, isCommune}) {
const getMairieInfos = useCallback(async () => {
if (isCommune && codeCommune) {
const mairie = await getMairie(codeCommune)
const {properties} = mairie.features[0]

setMairieContact(
{
mail: properties.email,
phone: properties.telephone
mail: mairie.email,
phone: mairie.telephone
}
)
}
Expand Down
5 changes: 2 additions & 3 deletions components/search-commune-contact/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,9 @@ function SearchCommuneContact() {
setLoading(true)
setMairie(null)

const results = await getMairie(commune.code)
const mairie = results.features[0]
const mairie = await getMairie(commune.code)
if (mairie) {
setMairie(results.features[0].properties)
setMairie(mairie)
} else {
const error = new Error(`Aucune information disponible pour la mairie de ${commune.nom}.`)
setError(error)
Expand Down
49 changes: 43 additions & 6 deletions lib/api-etablissements-public.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import getConfig from 'next/config'

import {isCOM} from '@/lib/ban'

import HttpError from './http-error'

const {NEXT_PUBLIC_API_ETABLISSEMENTS_PUBLIC: API_ETABLISSEMENTS_PUBLIC} = getConfig().publicRuntimeConfig
Expand Down Expand Up @@ -30,9 +28,48 @@ export async function _fetch(url) {
throw new Error('Une erreur est survenue')
}

export function getMairie(codeCommune) {
const type = isCOM(codeCommune) && 'mairie_com'
const url = `${API_ETABLISSEMENTS_PUBLIC}/communes/${codeCommune}/${type ? type : 'mairie'}`
const formatMairie = rawMairie => {
return {
nom: rawMairie.nom,
telephone: rawMairie.telephone ? JSON.parse(rawMairie.telephone)[0]?.valeur : undefined,
email: rawMairie.adresse_courriel,
horaires: rawMairie.plage_ouverture ? JSON.parse(rawMairie.plage_ouverture).map(openingRange => formatOpeningRange(openingRange)) : undefined
}
}

const formatOpeningRange = openingRange => {
return {
du: openingRange.nom_jour_debut,
au: openingRange.nom_jour_fin,
heures: formatOpeningHourRange(openingRange)
}
}

const formatOpeningHourRange = openingRange => {
const totalTimeRange = Object.entries(openingRange).filter(([key, value]) => key.includes('valeur_heure_debut') && value).length
const openingTimeRanges = []
for (let i = 1; i <= totalTimeRange; i++) {
openingTimeRanges.push({
de: openingRange[`valeur_heure_debut_${i}`],
a: openingRange[`valeur_heure_fin_${i}`]
})
}

return openingTimeRanges
}

export async function getMairie(codeCommune) {
// Documentation : https://api-lannuaire.service-public.fr/explore/dataset/api-lannuaire-administration/information/
const route = 'catalog/datasets/api-lannuaire-administration/records'
const query = `select=plage_ouverture,nom,telephone,adresse_courriel&where=code_insee_commune="${codeCommune}" and pivot LIKE "mairie"`
const url = `${API_ETABLISSEMENTS_PUBLIC}/${route}?${query}`

const response = await _fetch(url)
const rawMairie = response?.results?.[0]
if (!rawMairie) {
return null
}

return _fetch(url)
const mairie = formatMairie(rawMairie)
return mairie
}
2 changes: 1 addition & 1 deletion pages/commune/[codeCommune].js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ Commune.getInitialProps = async ({query}) => {
return {
codeCommune,
communeInfos: commune,
mairieInfos: mairie.features[0]?.properties,
mairieInfos: mairie,
revisions,
currentRevision,
typeCompositionAdresses
Expand Down

0 comments on commit 86fa703

Please sign in to comment.