From 962f1c7895e8cbed78332310e75be5707af9af5c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?R=C3=A9mi=20PAILHAREY?= <rpailharey@grandlyon.com>
Date: Wed, 17 Apr 2024 14:34:37 +0000
Subject: [PATCH] fix: throw CHALLENGE_ASKED after creating grdf consent

---
 src/core/core.js     |  5 ++---
 src/index.js         | 11 +----------
 src/requests/grdf.js |  1 -
 3 files changed, 3 insertions(+), 14 deletions(-)

diff --git a/src/core/core.js b/src/core/core.js
index a056afc..ba7b22b 100644
--- a/src/core/core.js
+++ b/src/core/core.js
@@ -72,7 +72,7 @@ async function handleConsents(consents, boUrlGRDF, boToken) {
  * Creates consents
  * - first in our back-office
  * - then at GRDF
- * @returns {Promise<boolean>} isWaitingForConsentValidation Boolean that indicates that the consent was created and needs a user validation.
+ * @returns {Promise<void>}
  */
 async function createConsent({
   bearerToken,
@@ -126,8 +126,7 @@ async function createConsent({
     })
     throw err
   })
-
-  return true
+  throw errors.CHALLENGE_ASKED
 }
 
 module.exports = { handleConsents, createConsent }
diff --git a/src/index.js b/src/index.js
index d2faeaf..b4c147e 100755
--- a/src/index.js
+++ b/src/index.js
@@ -93,9 +93,8 @@ async function start(fields, cozyParameters) {
       process.exit()
     }
 
-    let isWaitingForConsentValidation = false
     if (noValidConsent) {
-      isWaitingForConsentValidation = await createConsent({
+      await createConsent({
         bearerToken: access_token,
         pce,
         firstname,
@@ -109,14 +108,6 @@ async function start(fields, cozyParameters) {
       })
     }
 
-    if (isWaitingForConsentValidation) {
-      log(
-        'info',
-        'Need to validate consent before getting data, stopping konnector here'
-      )
-      return
-    }
-
     const grdfData = await getData(
       access_token,
       pce,
diff --git a/src/requests/grdf.js b/src/requests/grdf.js
index 1895664..bbcf009 100644
--- a/src/requests/grdf.js
+++ b/src/requests/grdf.js
@@ -116,7 +116,6 @@ async function createGRDFConsent({
       }
     })
     log('info', response.data.message_retour_traitement)
-    return true
   } catch (error) {
     log('error', `Failed to create GRDF consent`)
     log('error', error.response.data)
-- 
GitLab