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

fix: insee now throws errors

parent 8cc7e810
No related branches found
No related tags found
2 merge requests!12Feat/donnes tech debut contrat,!10Feat/rechercher point
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')
})
it('should return null for a unexisting post code', async () => {
expect(await getInseeCode(69069)).toEqual(null)
it('should throw USER_ACTION_NEEDED for a unexisting post code', async () => {
try {
await getInseeCode(69069)
} catch (error) {
expect(error).toEqual(errors.USER_ACTION_NEEDED)
}
})
it('should return null for post code 69290 when city is not provided', async () => {
expect(await getInseeCode(69290)).toEqual(null)
it('should throw USER_ACTION_NEEDED for post code 69290 when city is not provided', async () => {
try {
await getInseeCode(69290)
} catch (error) {
expect(error).toEqual(errors.USER_ACTION_NEEDED)
}
})
it('should return Craponne insee code for post code 69290', async () => {
......
......@@ -97,6 +97,7 @@ async function start(fields, cozyParameters) {
await commanderCollectePublicationMesures()
await updateBoConsent()
} else {
//AlternateStart
await getBoConsent()
if (!(await verifyUserIdentity(fields))) {
await deleteBoConsent()
......@@ -116,14 +117,13 @@ async function start(fields, cozyParameters) {
* @param {string} apiAuthKey
* @param {string} loginUtilisateur
*/
async function verifyUserIdentity(
export async function verifyUserIdentity(
fields,
baseUrl,
apiAuthKey,
loginUtilisateur
) {
const inseeCode = await getInseeCode(fields.postalCode)
if (!inseeCode) throw errors.USER_ACTION_NEEDED
const pdl = await findUserPdl(
`${baseUrl}/enedis_SDE_recherche-point/1.0`,
......
// @ts-check
const { default: axios } = require('axios')
const { log } = require('cozy-konnector-libs')
const { log, errors } = require('cozy-konnector-libs')
const API_URL = 'https://apicarto.ign.fr/api/codes-postaux/communes'
......@@ -8,7 +8,7 @@ const API_URL = 'https://apicarto.ign.fr/api/codes-postaux/communes'
* Return inseeCode given a postalCode
* @param {string} postalCode
* @param {string} [city]
* @return {Promise<string | null>} inseeCode
* @return {Promise<string>} inseeCode
*/
async function getInseeCode(postalCode, city) {
try {
......@@ -18,7 +18,7 @@ async function getInseeCode(postalCode, city) {
if (response.data.length === 1) {
return response.data[0].codeCommune
} else {
if (!city) return null
if (!city) throw errors.USER_ACTION_NEEDED
const filteredResponse = response.data.filter(
town => town.nomCommune.toLowerCase() === city.toLowerCase()
......@@ -30,7 +30,7 @@ async function getInseeCode(postalCode, city) {
'error',
`Query getInseeCode failed for postalCode ${postalCode} / ${city}`
)
return null
throw errors.USER_ACTION_NEEDED
}
}
......
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