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

Merge branch '9-ameliorer-la-tolerance-de-l-api-insee' into 'main'

Resolve "[?] Améliorer la tolérance de l'api insee"

See merge request !28
parents ce526b6a ca75b1d9
No related branches found
No related tags found
1 merge request!28Resolve "[?] Améliorer la tolérance de l'api insee"
Pipeline #49734 passed
......@@ -12,6 +12,7 @@ default:
stages:
- test
- build
- deploy
- publish
sast:
......@@ -71,6 +72,30 @@ build:
only:
- main
deploy_dev:
stage: deploy
tags:
- deploy-alpha
script:
- cd /root/ecolyo-infra-scripts/cicid_scripts
- './update_sge_dev.sh'
environment:
name: dev
url: https://dev.cozy.self-data.alpha.grandlyon.com/
deploy_demo:
stage: deploy
tags:
- deploy-alpha
script:
- cd /root/ecolyo-infra-scripts/cicid_scripts
- './update_all_sge_dev.sh'
only:
- main
environment:
name: ecolyodemo
url: https://ecolyodemo.cozy.self-data.alpha.grandlyon.com/
publish:
stage: publish
before_script:
......
const { errors } = require('cozy-konnector-libs')
const { getInseeCode } = require('../../src/requests/insee')
describe('getInseeCode', () => {
it('should return a valid insee code for Lyon 7', async () => {
expect(await getInseeCode(69007)).toEqual('69387')
......@@ -21,11 +22,60 @@ describe('getInseeCode', () => {
}
})
it('should return Craponne insee code for post code 69290', async () => {
expect(await getInseeCode(69290, 'CRAPONNE')).toEqual('69069')
})
describe('should handle communes with multiple insee code', () => {
it('should return Craponne insee code for post code 69290', async () => {
expect(await getInseeCode(69290, 'CRAPONNE')).toEqual('69069')
})
it('should return Pollionnay insee code for post code 69290', async () => {
expect(await getInseeCode(69290, 'POLLIONNAY')).toEqual('69154')
})
it('should return insee code for: Saint-bernard', async () => {
expect(await getInseeCode('01600', 'SAINT-BERNARD')).toEqual('01339')
})
it('should return insee code for: Neuville sur Saône', async () => {
expect(await getInseeCode(69250, 'Neuville sur Saône')).toEqual('69143')
})
it("should return insee code for: L'isle d'abeau", async () => {
expect(await getInseeCode(38080, "L'isle d'abeau")).toEqual('38193')
})
describe("should return correct insee code for Couzon-au-Mont-d'Or", () => {
it("should return insee code for: Couzon au mont d'or", async () => {
expect(await getInseeCode(69270, "Couzon au mont d'or")).toEqual(
'69068'
)
})
it('should return insee code for: Couzon au mont dʼor', async () => {
expect(await getInseeCode(69270, 'Couzon au mont dʼor')).toEqual(
'69068'
)
})
it("should return insee code for: Couzon-au-mont-d'or", async () => {
expect(await getInseeCode(69270, "Couzon-au-mont-d'or")).toEqual(
'69068'
)
})
it('should return insee code for: Couzon au mont d or', async () => {
expect(await getInseeCode(69270, 'Couzon au mont d or')).toEqual(
'69068'
)
})
})
it('should return Pollionnay insee code for post code 69290', async () => {
expect(await getInseeCode(69290, 'POLLIONNAY')).toEqual('69154')
describe('should return correct insee code for Fontaines-sur-Saône', () => {
it('should return insee code for: Fontaines-sur-Saône', async () => {
expect(await getInseeCode(69270, 'Fontaines-sur-Saône')).toEqual(
'69088'
)
})
it('should return insee code for: Fontaines-sur-Saone', async () => {
expect(await getInseeCode(69270, 'Fontaines-sur-Saone')).toEqual(
'69088'
)
})
})
})
})
......@@ -21,8 +21,10 @@ async function getInseeCode(postalCode, city) {
} else {
if (!city) throw new Error('No city')
const parsedCity = sanitizeCity(city)
const filteredResponse = response.data.filter(
town => town.nomCommune.toLowerCase() === city.toLowerCase()
town => sanitizeCity(town.nomCommune) === parsedCity
)
return filteredResponse[0].codeCommune
}
......@@ -34,6 +36,38 @@ async function getInseeCode(postalCode, city) {
}
}
/**
* Clean city input and remove all characters such as (^, ¨, ʼ, ', -, é, è)
* @param {string} city
* @return {string} parsedCity
*/
function sanitizeCity(city) {
return city
.toLowerCase()
.replace(/[âêîôûäëïü-\sʼ'éèç]/g, match => REPLACE_CHARS[match])
.trim()
}
const REPLACE_CHARS = {
â: 'a',
ê: 'e',
î: 'i',
ô: 'o',
û: 'u',
ä: 'a',
ë: 'e',
ï: 'i',
ö: 'o',
ü: 'u',
'-': '',
' ': '',
ʼ: '',
"'": '',
é: 'e',
è: 'e',
ç: 'c',
}
module.exports = {
getInseeCode,
}
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