From 3365060f431d1e068017e52460a7dc4356809775 Mon Sep 17 00:00:00 2001
From: Hugo <hnouts.dev@gmail.com>
Date: Fri, 19 Feb 2021 11:12:31 +0100
Subject: [PATCH] filter map

---
 src/index.js | 35 +++++++++++++++++++++++++++--------
 1 file changed, 27 insertions(+), 8 deletions(-)

diff --git a/src/index.js b/src/index.js
index aa807d2..c8a4205 100644
--- a/src/index.js
+++ b/src/index.js
@@ -47,8 +47,6 @@ async function start(fields, cozyParameters) {
     // await resetData()
     const baseUrl = cozyParameters.secret.eglBaseURL
     const apiAuthKey = cozyParameters.secret.eglAPIAuthKey
-    log('info', 'Start fields are : ' + fields)
-
     log('info', 'Authenticating ...')
     const response = await authenticate(
       fields.login,
@@ -150,12 +148,23 @@ async function getData(response, baseUrl, apiAuthKey) {
 }
 
 function format(response) {
-  const data = response.resultatRetour.slice(1).map((value, index) => {
+  const data = response.resultatRetour.slice(1).filter(value => {
+    value.TypeAgregat === "R"
+  }).map((value, index) => {
     const time = moment(value.DateReleve, moment.ISO_8601)
-    log('info', 'SUBSTRACTING : ' + value.ValeurIndex + ' - ' + response.resultatRetour[index].ValeurIndex + '\n')
+    log(
+      'info',
+      'date -> ' + value.DateReleve + ' SUBSTRACTING : ' +
+        value.ValeurIndex +
+        ' - ' +
+        response.resultatRetour[index].ValeurIndex +
+        '\n'
+    )
     return {
-      // Add if/else to not substract null value (typeagregat T)
-      load: value.TypeAgregat === 'T' ? null : value.ValeurIndex - response.resultatRetour[index].ValeurIndex,
+      load:
+        value.TypeAgregat === 'T'
+          ? null
+          : value.ValeurIndex - response.resultatRetour[index].ValeurIndex,
       year: parseInt(time.format('YYYY')),
       month: parseInt(time.format('M')),
       day: parseInt(time.format('D')),
@@ -165,8 +174,18 @@ function format(response) {
     }
   })
   log('info', 'DATALOAD LIST\n')
-  data.map((value) => {
-    log('info', value.month + '-' + value.day + '-' + value.year + ' : ' + value.load + '\n')
+  data.map(value => {
+    log(
+      'info',
+      value.month +
+        '-' +
+        value.day +
+        '-' +
+        value.year +
+        ' : ' +
+        value.load +
+        '\n'
+    )
   })
   return data
 }
-- 
GitLab