From 14bca391ac2bf3a2d9b27a252abfbe8f7ab1a672 Mon Sep 17 00:00:00 2001
From: Bastien DUMONT <bdumont@grandlyon.com>
Date: Wed, 23 Nov 2022 12:35:17 +0100
Subject: [PATCH] fix: convert puissance into energie

---
 src/core/contractVerification.js |  4 ++++
 src/helpers/parsing.js           | 27 +++++++++++++++++++++------
 src/index.js                     | 11 +++++------
 3 files changed, 30 insertions(+), 12 deletions(-)

diff --git a/src/core/contractVerification.js b/src/core/contractVerification.js
index 684c3aa..7b559ce 100644
--- a/src/core/contractVerification.js
+++ b/src/core/contractVerification.js
@@ -69,6 +69,10 @@ async function verifyContract(url, apiAuthKey, appLogin, contractId, pointId) {
         `Enedis issue ${parsedReply.Envelope.Body.Fault.detail.erreur.resultat.$.code}: ${parsedReply.Envelope.Body.Fault.faultstring}`
       )
     }
+    log(
+      'error',
+      'if an erorr is thrown here, it probably means that the contract has already been open today and that enedis cannot open a second one. Wait until tomorow to try again'
+    )
     throw errors.LOGIN_FAILED
   }
 }
diff --git a/src/helpers/parsing.js b/src/helpers/parsing.js
index 5141eb9..26d13c8 100644
--- a/src/helpers/parsing.js
+++ b/src/helpers/parsing.js
@@ -139,6 +139,20 @@ function parseValue(value, name) {
   return value
 }
 
+/**
+ *
+ * @param {string} value
+ * @param {string} name
+ * @returns {string|number} value
+ */
+function parseValueHalfHour(value, name) {
+  // W * 1/2h => KW * h
+  if (name === 'v') {
+    return parseFloat((parseInt(value) / 2000).toFixed(2))
+  }
+  return value
+}
+
 /**
  * Remove SGE useless multiple white spaces
  * @param {string} str
@@ -158,16 +172,17 @@ function removeAddressnumber(str) {
 }
 
 module.exports = {
-  parseSgeXmlData,
+  checkContractExists,
   formateDataForDoctype,
-  parseTags,
-  parseValue,
-  parseUserPdl,
   parseContracts,
   parseContractStartDate,
   parseServiceId,
+  parseSgeXmlData,
+  parseTags,
   parseUserAddress,
-  checkContractExists,
-  removeMultipleSpaces,
+  parseUserPdl,
+  parseValue,
+  parseValueHalfHour,
   removeAddressnumber,
+  removeMultipleSpaces,
 }
diff --git a/src/index.js b/src/index.js
index cd15b06..a3a93d5 100644
--- a/src/index.js
+++ b/src/index.js
@@ -16,6 +16,7 @@ const {
   formateDataForDoctype,
   parseTags,
   parseValue,
+  parseValueHalfHour,
 } = require('./helpers/parsing')
 const {
   consultationMesuresDetailleesMaxPower,
@@ -419,11 +420,9 @@ async function getDataHalfHour(url, apiAuthKey, userLogin, pointId) {
     apikey: apiAuthKey,
   }
 
-  let MAX_HISTO = 4
-  // If manual execution, retrieve only 1 week
-  if (manualExecution) {
-    MAX_HISTO = 1
-  }
+  // If manual execution, retrieve only 1 week otherwise retrieve 4 weeks
+  const MAX_HISTO = manualExecution ? 1 : 4
+
   for (var i = 0; i < MAX_HISTO; i++) {
     log('info', 'launch process with history')
     const increamentedStartDateString = moment(startLoadDate)
@@ -454,7 +453,7 @@ async function getDataHalfHour(url, apiAuthKey, userLogin, pointId) {
       response.body,
       {
         tagNameProcessors: [parseTags],
-        valueProcessors: [parseValue],
+        valueProcessors: [parseValueHalfHour],
         explicitArray: false,
       },
       processData('com.grandlyon.enedis.minute')
-- 
GitLab