From 7a9ecb677963d5cab69eec5c4e769372d95db9d1 Mon Sep 17 00:00:00 2001
From: Hugo SUBTIL <ext.sopra.husubtil@grandlyon.com>
Date: Wed, 10 Aug 2022 11:17:36 +0200
Subject: [PATCH] fix: env variable + add city field

---
 manifest.konnector          |  3 +++
 src/helpers/env.js          |  4 +++-
 src/helpers/getAccountId.js | 12 ++++++++++++
 src/index.js                |  9 ++++-----
 4 files changed, 22 insertions(+), 6 deletions(-)
 create mode 100644 src/helpers/getAccountId.js

diff --git a/manifest.konnector b/manifest.konnector
index b9e2ba2..ea10c3b 100644
--- a/manifest.konnector
+++ b/manifest.konnector
@@ -23,6 +23,9 @@
     "postalCode": {
       "type": "string"
     },
+    "city": {
+      "type": "string"
+    },
     "pointId": {
       "type": "number"
     }
diff --git a/src/helpers/env.js b/src/helpers/env.js
index 9f62628..b4a61b8 100644
--- a/src/helpers/env.js
+++ b/src/helpers/env.js
@@ -1,5 +1,7 @@
 function iSLocal() {
-  return process.env.NODE_ENV === 'development' || process.env.NODE_ENV
+  return (
+    process.env.NODE_ENV === 'development' || process.env.NODE_ENV === 'local'
+  )
 }
 
 module.exports = { iSLocal }
diff --git a/src/helpers/getAccountId.js b/src/helpers/getAccountId.js
new file mode 100644
index 0000000..5608da1
--- /dev/null
+++ b/src/helpers/getAccountId.js
@@ -0,0 +1,12 @@
+function getAccountId() {
+  try {
+    return process.env.NODE_ENV === 'development' ||
+      process.env.NODE_ENV === 'test'
+      ? 'fakeAccountId'
+      : JSON.parse(process.env.COZY_FIELDS).account
+  } catch (err) {
+    throw new Error(`You must provide 'account' in COZY_FIELDS: ${err.message}`)
+  }
+}
+
+module.exports = { getAccountId }
diff --git a/src/index.js b/src/index.js
index 2b71ee5..91b0514 100644
--- a/src/index.js
+++ b/src/index.js
@@ -33,7 +33,7 @@ const { verifyContract } = require('./core/contractVerification')
 const { terminateContract } = require('./core/contractTermination')
 const { getContractStartDate } = require('./core/contractStartDate')
 const { getAccount, saveAccountData } = require('./requests/cozy')
-const { iSLocal } = require('./helpers/env')
+const { getAccountId } = require('./helpers/getAccountId')
 
 moment.locale('fr') // set the language
 moment.tz.setDefault('Europe/Paris') // set the timezone
@@ -48,7 +48,6 @@ let startDailyDateString = startDailyDate.format('YYYY-MM-DD')
 const startLoadDate = moment().subtract(7, 'day')
 const endDate = moment()
 const endDateString = endDate.format('YYYY-MM-DD')
-const ACCOUNT_ID = iSLocal() ? 'default_account_id' : 'enedis-sge-grandlyon'
 
 module.exports = new BaseKonnector(start)
 
@@ -62,14 +61,14 @@ module.exports = new BaseKonnector(start)
  * @param {{secret: fields}} cozyParameters
  */
 async function start(fields, cozyParameters) {
-  log('info', 'Gathering data ...')
+  log('info', 'Konnector configuration ...')
+  const ACCOUNT_ID = await getAccountId()
   const pointId = fields.pointId
   let baseUrl = fields.wso2BaseUrl
   let apiAuthKey = fields.apiToken
   let contractId = fields.contractId
   //TODO switch variable to english
   let sgeLogin = fields.sgeLogin
-  log('info', 'Authenticating ...')
   //TODO: Verify if condition is working in local and on build version
   if (cozyParameters && Object.keys(cozyParameters).length !== 0) {
     log('debug', 'Found COZY_PARAMETERS')
@@ -95,7 +94,7 @@ async function start(fields, cozyParameters) {
    */
   log('info', 'User Logging...')
 
-  if (await isFirstStart(await getAccount('default_account_id'))) {
+  if (await isFirstStart(await getAccount(ACCOUNT_ID))) {
     const user = await verifyUserIdentity(fields, baseUrl, apiAuthKey, sgeLogin)
 
     let consent = await createBoConsent(
-- 
GitLab