From 453329821f8e357193452152d538d12d4051c718 Mon Sep 17 00:00:00 2001
From: git-directory-deploy <>
Date: Wed, 27 Jan 2021 11:55:00 +0100
Subject: [PATCH] publish: fix: set usage point id when refresh token

generated from commit 6f45a986836a8a7af4507a1135138dbf3b69e18b
---
 index.js | 128 +++++++++++++++++++++++++++++++------------------------
 1 file changed, 72 insertions(+), 56 deletions(-)

diff --git a/index.js b/index.js
index b2325be..b9d5383 100644
--- a/index.js
+++ b/index.js
@@ -94,7 +94,7 @@ const {
   addData,
   hydrateAndFilter,
   errors,
-  cozyClient
+  cozyClient,
 } = __webpack_require__(1)
 
 const getAccountId = __webpack_require__(1244)
@@ -107,13 +107,18 @@ moment.locale('fr') // set the language
 moment.tz.setDefault('Europe/Paris') // set the timezone
 
 /*** Connector Constants ***/
-const startDailyDate = moment().subtract(32, 'month')
+const manualExecution = process.env.COZY_JOB_MANUAL_EXECUTION
+const startDailyDate = manualExecution
+  ? moment().subtract(12, 'month')
+  : moment().subtract(32, 'month')
 const startDailyDateString = startDailyDate.format('YYYY-MM-DD')
 const startLoadDate = moment().subtract(7, 'day')
 const startLoadDateString = startLoadDate.format('YYYY-MM-DD')
 const endDate = moment()
 const endDateString = endDate.format('YYYY-MM-DD')
 const baseUrl = 'https://gw.prd.api.enedis.fr'
+const dailyDataURL = `${baseUrl}/v4/metering_data/daily_consumption`
+const loadCurveURL = `${baseUrl}/v4/metering_data/consumption_load_curve`
 
 /**
  * The start function is run by the BaseKonnector instance only when it got all the account
@@ -129,7 +134,7 @@ const baseUrl = 'https://gw.prd.api.enedis.fr'
  */
 async function start(fields, cozyParameters, doRetry = true) {
   log('info', 'Starting the enedis konnector')
-  log("info", "COZY_JOB_MANUAL_EXECUTION: " + process.env.COZY_JOB_MANUAL_EXECUTION)
+  log('info', `Manual execution: ${manualExecution}` )
   const accountId = getAccountId()
   let usage_point_id = ''
   try {
@@ -205,8 +210,8 @@ async function getDailyData(token, usagePointID) {
   const dataRequest = {
     method: 'GET',
     uri:
-      baseUrl +
-      '/v4/metering_data/daily_consumption?start=' +
+      dailyDataURL +
+      '?start=' +
       startDailyDateString +
       '&end=' +
       endDateString +
@@ -214,8 +219,8 @@ async function getDailyData(token, usagePointID) {
       usagePointID,
     headers: {
       Accept: 'application/json',
-      Authorization: 'Bearer ' + token
-    }
+      Authorization: 'Bearer ' + token,
+    },
   }
   const response = await rp(dataRequest)
   return response
@@ -228,11 +233,13 @@ async function getDailyData(token, usagePointID) {
  */
 async function startLoadDataProcess(token, usagePointID) {
   log('info', 'Check consent for user')
-  const isConsent = await checkConsentForLoadCurve(token,
+  const isConsent = await checkConsentForLoadCurve(
+    token,
     usagePointID,
     startLoadDateString,
-    endDateString)
-  if(isConsent) {
+    endDateString
+  )
+  if (isConsent) {
     log('info', 'Check history')
     const isHistory = await isHistoryLoaded('com.grandlyon.enedis.minute')
     if (isHistory) {
@@ -266,45 +273,20 @@ async function startLoadDataProcess(token, usagePointID) {
 }
 
 /**
- * Launch process to handle load data
+ * Request API and check return code
+ * Return true or false
  */
-async function launchLoadDataProcess(
+async function checkConsentForLoadCurve(
   token,
   usagePointID,
-  _startLoadDate,
+  _startDate,
   _endDate
 ) {
-  log('info', 'Fetching enedis load data')
-  const fetchedLoadData = await getLoadData(
-    token,
-    usagePointID,
-    _startLoadDate,
-    _endDate
-  )
-  if (fetchedLoadData && fetchedLoadData.length > 0) {
-    log('info', 'Process enedis load data')
-    const processedLoadData = await processData(
-      fetchedLoadData,
-      'com.grandlyon.enedis.minute',
-      ['year', 'month', 'day', 'hour', 'minute']
-    )
-    log('info', 'Agregate enedis load data for hour')
-    await agregateHourlyData(processedLoadData)
-  } else {
-    log('info', 'No consent or data for load curve')
-  }
-}
-
-/**
- * Request API and check return code
- * Return true or false
- */
-async function checkConsentForLoadCurve(token, usagePointID, _startDate, _endDate) {
   const dataRequest = {
     method: 'GET',
     uri:
-      baseUrl +
-      '/v4/metering_data/consumption_load_curve?start=' +
+      loadCurveURL +
+      '?start=' +
       _startDate +
       '&end=' +
       _endDate +
@@ -312,18 +294,22 @@ async function checkConsentForLoadCurve(token, usagePointID, _startDate, _endDat
       usagePointID,
     headers: {
       Accept: 'application/json',
-      Authorization: 'Bearer ' + token
-    }
+      Authorization: 'Bearer ' + token,
+    },
   }
   try {
     await rp(dataRequest)
+    log('info', 'Consent found for load curve')
     return true
   } catch (err) {
-    if ((err.statusCode === 400 || err.code === 400) && err.message.search("ADAM-ERR0075") > 0) {
-      log('warning', 'No consent for load curve')
+    if (
+      (err.statusCode === 400 || err.code === 400) &&
+      err.message.search('ADAM-ERR0075') > 0
+    ) {
+      log('info', 'No consent for load curve')
       return false
     } else if (err.statusCode === 403 || err.code === 403) {
-      log('warning', 'No consent for load curve')
+      log('info', 'No consent for load curve')
       return false
     } else {
       throw err
@@ -331,6 +317,36 @@ async function checkConsentForLoadCurve(token, usagePointID, _startDate, _endDat
   }
 }
 
+/**
+ * Launch process to handle load data
+ */
+async function launchLoadDataProcess(
+  token,
+  usagePointID,
+  _startLoadDate,
+  _endDate
+) {
+  log('info', 'Fetching enedis load data')
+  const fetchedLoadData = await getLoadData(
+    token,
+    usagePointID,
+    _startLoadDate,
+    _endDate
+  )
+  if (fetchedLoadData && fetchedLoadData.length > 0) {
+    log('info', 'Process enedis load data')
+    const processedLoadData = await processData(
+      fetchedLoadData,
+      'com.grandlyon.enedis.minute',
+      ['year', 'month', 'day', 'hour', 'minute']
+    )
+    log('info', 'Agregate enedis load data for hour')
+    await agregateHourlyData(processedLoadData)
+  } else {
+    log('info', 'No consent or data for load curve')
+  }
+}
+
 /**
  * Retrieve data from the API
  * Format: { value: "W", "date": "YYYY-MM-DD hh:mm:ss" }
@@ -339,8 +355,8 @@ async function getLoadData(token, usagePointID, _startDate, _endDate) {
   const dataRequest = {
     method: 'GET',
     uri:
-      baseUrl +
-      '/v4/metering_data/consumption_load_curve?start=' +
+      loadCurveURL +
+      '?start=' +
       _startDate +
       '&end=' +
       _endDate +
@@ -348,8 +364,8 @@ async function getLoadData(token, usagePointID, _startDate, _endDate) {
       usagePointID,
     headers: {
       Accept: 'application/json',
-      Authorization: 'Bearer ' + token
-    }
+      Authorization: 'Bearer ' + token,
+    },
   }
   const response = await rp(dataRequest)
   return response
@@ -367,7 +383,7 @@ async function processData(data, doctype, filterKeys) {
   const formatedData = await formateData(intervalData, doctype)
   // Remove data for existing days into the DB
   const filteredData = await hydrateAndFilter(formatedData, doctype, {
-    keys: filterKeys
+    keys: filterKeys,
   })
   // Store new day data
   await storeData(filteredData, doctype, filterKeys)
@@ -397,7 +413,7 @@ async function agregateMonthAndYearData(data) {
     )
     await storeData(agregatedMonthData, 'com.grandlyon.enedis.month', [
       'year',
-      'month'
+      'month',
     ])
     // Agregation for Year data
     const agregatedYearData = await buildAgregatedData(
@@ -437,7 +453,7 @@ async function agregateHourlyData(data) {
       'year',
       'month',
       'day',
-      'hour'
+      'hour',
     ])
   }
 }
@@ -448,7 +464,7 @@ async function agregateHourlyData(data) {
 async function storeData(data, doctype, filterKeys) {
   log('debug', doctype, 'Store into')
   const filteredDocuments = await hydrateAndFilter(data, doctype, {
-    keys: filterKeys
+    keys: filterKeys,
   })
   return await addData(filteredDocuments, doctype)
 }
@@ -475,7 +491,7 @@ async function formateData(data, doctype) {
         month: parseInt(date.format('M')),
         day: parseInt(date.format('D')),
         hour: parseInt(date.format('H')),
-        minute: parseInt(date.format('m'))
+        minute: parseInt(date.format('m')),
       }
     }
   })
@@ -528,7 +544,7 @@ async function buildDataFromKey(doctype, key, value) {
     month: parseInt(month),
     day: parseInt(day),
     hour: parseInt(hour),
-    minute: 0
+    minute: 0,
   }
 }
 
-- 
GitLab