From 9b8a491f63cbf8a359a1e64c419d34a8b4bb36f7 Mon Sep 17 00:00:00 2001
From: Bastien DUMONT <bdumont@grandlyon.com>
Date: Tue, 2 Aug 2022 15:47:39 +0200
Subject: [PATCH] fix: insee now throws errors

---
 __tests__/requests/insee.spec.js | 17 +++++++++++++----
 src/index.js                     |  4 ++--
 src/requests/insee.js            |  8 ++++----
 3 files changed, 19 insertions(+), 10 deletions(-)

diff --git a/__tests__/requests/insee.spec.js b/__tests__/requests/insee.spec.js
index 2be3245..c8154a3 100644
--- a/__tests__/requests/insee.spec.js
+++ b/__tests__/requests/insee.spec.js
@@ -1,15 +1,24 @@
+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 () => {
diff --git a/src/index.js b/src/index.js
index a8158f0..d9354dd 100644
--- a/src/index.js
+++ b/src/index.js
@@ -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`,
diff --git a/src/requests/insee.js b/src/requests/insee.js
index b32fef4..a616d9d 100644
--- a/src/requests/insee.js
+++ b/src/requests/insee.js
@@ -1,6 +1,6 @@
 // @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
   }
 }
 
-- 
GitLab