From 78e782c3a7ef4846794ca5a3b988f13eecd0fc74 Mon Sep 17 00:00:00 2001
From: Bastien DUMONT <bdumont@grandlyon.com>
Date: Mon, 19 Sep 2022 11:44:56 +0200
Subject: [PATCH] feat: reduce fetching data to 1y on manual exec

---
 src/index.js | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/src/index.js b/src/index.js
index 899af77..bec5320 100644
--- a/src/index.js
+++ b/src/index.js
@@ -107,6 +107,7 @@ async function start(fields, cozyParameters) {
   log('info', 'User Logging...')
 
   if (isFirstStart(await getAccount(ACCOUNT_ID))) {
+    log('info', 'First start...')
     const user = await verifyUserIdentity(fields, baseUrl, apiAuthKey, sgeLogin)
 
     let consent = await createBoConsent(
@@ -371,7 +372,8 @@ async function getMaxPowerData(url, apiAuthKey, userLogin, pointId) {
 
 /**
  * If start date exceed the maximum amount of data we can get with one query
- * get only 36 month
+ * get only 36 month.
+ * On manual execution, set the start date to one year ago.
  */
 function setStartDate() {
   if (moment(endDate).diff(startDailyDate, 'months', true) > 36) {
@@ -382,6 +384,10 @@ function setStartDate() {
     startDailyDate = moment(endDate).subtract(36, 'month')
     startDailyDateString = startDailyDate.format('YYYY-MM-DD')
   }
+  if (manualExecution) {
+    startDailyDate = moment(endDate).subtract(12, 'month')
+    startDailyDateString = startDailyDate.format('YYYY-MM-DD')
+  }
 }
 
 /**
-- 
GitLab