Skip to content
Snippets Groups Projects

Feat/insee code

Merged Hugo SUBTIL requested to merge feat/insee-code into main
4 files
+ 49
5
Compare changes
  • Side-by-side
  • Inline
Files
4
+ 24
4
// @ts-check
const { default: axios } = require('axios')
const { log } = require('cozy-konnector-libs')
const API_URL = 'https://apicarto.ign.fr/api'
/**
* Return inseeCode given a postalCode
* @param {string} postalCode
* @return {Promise<string>} inseeCode
* @param {string} [city]
* @return {Promise<string | null>} inseeCode
*/
async function getInseeCode(postalCode, city) {
//TODO: Implement
log('info', `Query getInseeCode for postalCode ${postalCode} / ${city}`)
throw new Error('Function not implemented.')
try {
const response = await axios.get(
`${API_URL}/codes-postaux/communes/${postalCode}`
)
log('info', `Query getInseeCode for postalCode ${postalCode} / ${city}`)
if (response.data.length === 1) {
return response.data[0].codeCommune
} else {
if (!city) return null
const filteredResponse = response.data.filter(
town => town.nomCommune.toLowerCase() === city.toLowerCase()
)
return filteredResponse[0].codeCommune
}
} catch (error) {
return null
}
}
module.exports = {
Loading