From 6f45a986836a8a7af4507a1135138dbf3b69e18b Mon Sep 17 00:00:00 2001
From: git-directory-deploy <>
Date: Mon, 4 Jan 2021 11:57:59 +0100
Subject: [PATCH] fix: set usage point id when refresh token

---
 manifest.konnector | 2 +-
 package.json       | 2 +-
 src/index.js       | 6 +++++-
 3 files changed, 7 insertions(+), 3 deletions(-)

diff --git a/manifest.konnector b/manifest.konnector
index bb79464..093a713 100644
--- a/manifest.konnector
+++ b/manifest.konnector
@@ -1,5 +1,5 @@
 {
-  "version": "0.2.0",
+  "version": "0.2.1",
   "name": "Enedis",
   "type": "konnector",
   "language": "node",
diff --git a/package.json b/package.json
index af6fd52..e01d79e 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
 {
   "name": "enedis",
-  "version": "0.2.0",
+  "version": "0.2.1",
   "description": "",
   "repository": {
     "type": "git",
diff --git a/src/index.js b/src/index.js
index d34fbb2..b5ed7a1 100644
--- a/src/index.js
+++ b/src/index.js
@@ -40,9 +40,9 @@ const baseUrl = 'https://gw.prd.api.enedis.fr'
 async function start(fields, cozyParameters, doRetry = true) {
   log('info', 'Starting the enedis konnector')
   const accountId = getAccountId()
+  let usage_point_id = ''
   try {
     const { access_token } = fields
-    let usage_point_id = ''
     if (
       this._account &&
       this._account.oauth_callback_results &&
@@ -52,6 +52,9 @@ async function start(fields, cozyParameters, doRetry = true) {
         ','
       )
       usage_point_id = usage_points_id[0]
+    } else if (fields.usage_point_id) {
+      // In case of refresh token, we retrieve the usage point id from the fields
+      usage_point_id = fields.usage_point_id
     } else {
       log('error', 'no usage_point_id found')
       throw errors.USER_ACTION_NEEDED_OAUTH_OUTDATED
@@ -91,6 +94,7 @@ async function start(fields, cozyParameters, doRetry = true) {
         log('info', 'refresh response')
         log('info', JSON.stringify(body))
         fields.access_token = body.attributes.oauth.access_token
+        fields.usage_point_id = usage_point_id
         return start(fields, cozyParameters, false)
       }
       log('error', `Error during authentication: ${err.message}`)
-- 
GitLab