Skip to content
Snippets Groups Projects
Commit abb720e9 authored by Bastien DUMONT's avatar Bastien DUMONT :angel:
Browse files

feat(insee code): working function

parent be461e09
No related branches found
No related tags found
1 merge request!9Feat/insee code
Pipeline #39576 passed
{}
\ No newline at end of file
......@@ -40,6 +40,7 @@
"travisDeployKey": "./bin/generate_travis_deploy_key"
},
"dependencies": {
"axios": "^0.27.2",
"cozy-konnector-libs": "4.52.1",
"easy-soap-request": "^4.7.0",
"jest": "^28.1.3",
......
// @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 = {
......
......@@ -2356,6 +2356,14 @@ axios@^0.26.1:
dependencies:
follow-redirects "^1.14.8"
axios@^0.27.2:
version "0.27.2"
resolved "https://registry.yarnpkg.com/axios/-/axios-0.27.2.tgz#207658cc8621606e586c85db4b41a750e756d972"
integrity sha512-t+yRIyySRTp/wua5xEr+z1q60QmLq8ABsS5O9Me1AsE5dfKqgnCFzwiCZZ/cGNd1lq4/7akDWMxdhVlucjmnOQ==
dependencies:
follow-redirects "^1.14.9"
form-data "^4.0.0"
babel-eslint@10.0.1:
version "10.0.1"
resolved "https://registry.yarnpkg.com/babel-eslint/-/babel-eslint-10.0.1.tgz#919681dc099614cd7d31d45c8908695092a1faed"
......@@ -2871,7 +2879,7 @@ colors@^1.1.2:
resolved "https://registry.yarnpkg.com/colors/-/colors-1.4.0.tgz#c50491479d4c1bdaed2c9ced32cf7c7dc2360f78"
integrity sha512-a+UqTh4kgZg/SlGvfbzDHpgRu7AAQOmmqRHJnxhRZICKFUT91brVhNNt58CMWU9PsBbv3PDCZUHbVxuDiH2mtA==
combined-stream@^1.0.6, combined-stream@~1.0.6:
combined-stream@^1.0.6, combined-stream@^1.0.8, combined-stream@~1.0.6:
version "1.0.8"
resolved "https://registry.yarnpkg.com/combined-stream/-/combined-stream-1.0.8.tgz#c3d45a8b34fd730631a110a8a2520682b31d5a7f"
integrity sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==
......@@ -4314,11 +4322,25 @@ follow-redirects@^1.14.8:
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.14.9.tgz#dd4ea157de7bfaf9ea9b3fbd85aa16951f78d8d7"
integrity sha512-MQDfihBQYMcyy5dhRDJUHcw7lb2Pv/TuE6xP1vyraLukNDHKbDxDNaOE3NbCAdKQApno+GPRyo1YAp89yCjK4w==
follow-redirects@^1.14.9:
version "1.15.1"
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.15.1.tgz#0ca6a452306c9b276e4d3127483e29575e207ad5"
integrity sha512-yLAMQs+k0b2m7cVxpS1VKJVvoz7SS9Td1zss3XRwXj+ZDH00RJgnuLx7E44wx02kQLrdM3aOOy+FpzS7+8OizA==
forever-agent@~0.6.1:
version "0.6.1"
resolved "https://registry.yarnpkg.com/forever-agent/-/forever-agent-0.6.1.tgz#fbc71f0c41adeb37f96c577ad1ed42d8fdacca91"
integrity sha1-+8cfDEGt6zf5bFd60e1C2P2sypE=
form-data@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/form-data/-/form-data-4.0.0.tgz#93919daeaf361ee529584b9b31664dc12c9fa452"
integrity sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==
dependencies:
asynckit "^0.4.0"
combined-stream "^1.0.8"
mime-types "^2.1.12"
form-data@~2.3.2:
version "2.3.3"
resolved "https://registry.yarnpkg.com/form-data/-/form-data-2.3.3.tgz#dcce52c05f644f298c6a7ab936bd724ceffbf3a6"
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment