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

Merge branch '21-contains-sub-name' into 'main'

fix(insee): city name included in some communes

See merge request !40
parents 53cb38fa 03b094c3
No related branches found
No related tags found
1 merge request!40fix(insee): city name included in some communes
Pipeline #61396 passed
......@@ -184,5 +184,19 @@ describe('getInseeCode', () => {
expect(await getInseeCode(95640, 'Breancon')).toEqual('95102')
})
})
describe('should return correct insee code when other communes contains the city', () => {
it('should return insee code for: Cluses', async () => {
expect(await getInseeCode(74300, 'Cluses')).toEqual('74081')
})
it('should return insee code for: Chatillon sur cluses', async () => {
expect(await getInseeCode(74300, 'Chatillon sur cluses')).toEqual(
'74064'
)
})
it('should return insee code for: Nancy sur cluses', async () => {
expect(await getInseeCode(74300, 'Nancy sur cluses')).toEqual('74196')
})
})
})
})
......@@ -27,13 +27,20 @@ async function getInseeCode(postalCode, city) {
sanitizeCity(commune.nomCommune).includes(parsedCity)
)
if (filteredResponse.length === 1) {
return filteredResponse[0].codeCommune
}
if (filteredResponse.length > 1) {
throw new Error(
'Input city is not precise enough, more than one city was found'
)
// Try to get the same length of the city input
for (const commune of filteredResponse) {
if (commune.nomCommune.length === city.length) {
return commune.codeCommune
}
}
}
return filteredResponse[0].codeCommune
throw new Error('No match could be found')
}
} catch (error) {
const errorMessage = `Query getInseeCode failed for postalCode ${postalCode} / ${city}`
......
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