diff --git a/manifest.konnector b/manifest.konnector index b9e2ba231ca70d9e21c33b9da6227d5761068a89..ea10c3b916b828333c16faf85c3d8ef685e318e3 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 9f62628a11f49c40ca1f59ab268955a34e2ba4ac..b4a61b85f8d700f2f041178a93f71a8ebefd4cc8 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 0000000000000000000000000000000000000000..5608da18e98dfc01de97cbef7246735cdf211032 --- /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 2b71ee5505ddf0f8acc6cd06bf43a88bbb457f79..91b051476a3fe76f9c4e3af9dfbb2bab1c12ab2b 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(