From 6df0a654bce869d5959553eae460fefb29e550a8 Mon Sep 17 00:00:00 2001
From: Yoan Vallet <ext.sopra.yvallet@grandlyon.com>
Date: Fri, 5 Jun 2020 10:48:57 +0200
Subject: [PATCH] feat: use usage point id retrieve by oauth

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

diff --git a/manifest.konnector b/manifest.konnector
index d84958b..5c8db4a 100644
--- a/manifest.konnector
+++ b/manifest.konnector
@@ -1,5 +1,5 @@
 {
-  "version": "0.1.0",
+  "version": "0.1.1",
   "name": "Enedis",
   "type": "konnector",
   "language": "node",
diff --git a/package.json b/package.json
index 8047365..fc5a265 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
 {
   "name": "enedis",
-  "version": "0.1.0",
+  "version": "0.1.1",
   "description": "",
   "repository": {
     "type": "git",
diff --git a/src/index.js b/src/index.js
index bafdd95..1964943 100644
--- a/src/index.js
+++ b/src/index.js
@@ -24,7 +24,7 @@ const startLoadDate = moment()
   .subtract(7, 'day')
   .format('YYYY-MM-DD')
 const endDate = moment().format('YYYY-MM-DD')
-const baseUrl = 'https://gw.hml.api.enedis.fr'
+const baseUrl = 'https://gw.prd.api.enedis.fr'
 
 /**
  * The start function is run by the BaseKonnector instance only when it got all the account
@@ -43,9 +43,21 @@ async function start(fields, cozyParameters, doRetry = true) {
   const accountId = getAccountId()
   try {
     const { access_token } = fields
-    // const { oauth_callback_results } = fields
-    // const usage_point_id = oauth_callback_results.usage_point_id
-    const usage_point_id = 22516914714270
+    let usage_point_id = ''
+    if (
+      this._account &&
+      this._account.oauth_callback_results &&
+      this._account.oauth_callback_results.usage_points_id
+    ) {
+      const usage_points_id = this._account.oauth_callback_results.usage_points_id.split(
+        ','
+      )
+      usage_point_id = usage_points_id[0]
+    } else {
+      log('error', 'no usage_point_id found')
+      throw errors.USER_ACTION_NEEDED_OAUTH_OUTDATED
+    }
+
     log('info', 'Fetching enedis daily data')
     const fetchedDailyData = await getDailyData(access_token, usage_point_id)
     log('info', 'Process enedis daily data')
-- 
GitLab