diff --git a/index.js b/index.js index 74dfbcf50bd5b140f2ca21c02fd3a60682fb70a0..e9997e38fd05665e172668db4b94d3503718461a 100644 --- a/index.js +++ b/index.js @@ -21,10 +21,8 @@ const { formateDataForDoctype, parseTags, parseValue, - parseContractStartDate, } = __webpack_require__(1555) const { - consulterDonneesTechniquesContractuelles, consultationMesuresDetailleesMaxPower, consultationMesuresDetaillees, } = __webpack_require__(1556) @@ -38,8 +36,9 @@ const { verifyUserIdentity } = __webpack_require__(1558) const { activateContract } = __webpack_require__(1598) const { verifyContract } = __webpack_require__(1599) const { terminateContract } = __webpack_require__(1601) -const { getAccount, saveAccountData } = __webpack_require__(1602) -const { iSLocal } = __webpack_require__(1603) +const { getContractStartDate } = __webpack_require__(1602) +const { getAccount, saveAccountData } = __webpack_require__(1603) +const { iSLocal } = __webpack_require__(1604) moment.locale('fr') // set the language moment.tz.setDefault('Europe/Paris') // set the timezone @@ -57,7 +56,6 @@ const endDateString = endDate.format('YYYY-MM-DD') const ACCOUNT_ID = iSLocal() ? 'default_account_id' : 'enedis-sge-grandlyon' module.exports = new BaseKonnector(start) -module.exports = { getContractStartDate } /** * The start function is run by the BaseKonnector instance only when it got all the account @@ -107,12 +105,20 @@ async function start(fields, cozyParameters) { user.postalCode, user.inseeCode ) - await getContractStartDate(baseUrl, apiAuthKey, sgeLogin, pointId) + + // handle user contract start date in order to preperly request data + const userContractstartDate = await getContractStartDate( + baseUrl, + apiAuthKey, + sgeLogin, + pointId + ) + startDailyDate = moment(userContractstartDate, 'YYYY-MM-DD') + startDailyDateString = startDailyDate.format('YYYY-MM-DD') const contractStartDate = moment().format('YYYY-MM-DD') - //TODO: consent time ? 5 years? const contractEndDate = moment() - .add(1, 'year') + .add(1, 'year') // SGE force 1 year duration .format('YYYY-MM-DD') let serviceId = await verifyContract( @@ -203,41 +209,6 @@ async function gatherData(baseUrl, apiAuthKey, sgeLogin, pointId) { log('info', 'Querying data: done') } -/** - * //TODO: Move - * @param {string} url - * @param {string} apiAuthKey - * @param {string} userLogin - * @param {number} pointId - * @returns {Promise<void>} - */ -async function getContractStartDate(url, apiAuthKey, userLogin, pointId) { - log('info', 'Fetching data start date') - const sgeHeaders = { - 'Content-Type': 'text/xml;charset=UTF-8', - apikey: apiAuthKey, - } - - const { response } = await soapRequest({ - url: `${url}/enedis_SGE_ConsultationDonneesTechniquesContractuelles/1.0`, - headers: sgeHeaders, - xml: consulterDonneesTechniquesContractuelles(pointId, userLogin), - }).catch(err => { - log('error', 'Error while fetching contract start date : ' + err) - throw errors.VENDOR_DOWN - }) - - xml2js.parseString( - response.body, - { - tagNameProcessors: [parseTags], - valueProcessors: [parseValue], - explicitArray: false, - }, - processStartDate() - ) -} - /** * Get hour data * @param {string} url @@ -422,26 +393,6 @@ function processData(doctype = 'com.grandlyon.enedis.day') { } } -/** - * Store an accurate start date based on contrat start - */ -function processStartDate() { - return async (err, result) => { - if (err) { - log('error', err) - throw err - } - // update start Date with contract openning date - try { - startDailyDate = moment(parseContractStartDate(result), 'YYYY-MM-DD') - startDailyDateString = startDailyDate.format('YYYY-MM-DD') - } catch (error) { - log('error', 'Error while processing contract start date: ' + error) - throw errors.NOT_EXISTING_DIRECTORY - } - } -} - /** * Save data in the right doctype db and prevent duplicated keys * @param {EnedisKonnectorData[]} data @@ -223875,11 +223826,12 @@ module.exports = { /***/ ((module, __unused_webpack_exports, __webpack_require__) => { // @ts-check -const { log, errors } = __webpack_require__(1) +const { log } = __webpack_require__(1) /** * @param {number} pointId - * @param {string} name + * @param {string} lastname + * @param {string} firstname * @param {string} address * @param {string} postalCode * @param {string} inseeCode @@ -223926,7 +223878,7 @@ function updateBoConsent(consent, serviceId) { */ function getBoConsent(boId) { //TODO: Implement - log('info', `Query getBoConsent`) + log('info', `Query getBoConsent ${boId}`) return { pointId: 1234, lastname: 'SUBTIL', @@ -228242,8 +228194,63 @@ module.exports = { terminateContract } /* 1602 */ /***/ ((module, __unused_webpack_exports, __webpack_require__) => { +// @ts-check +const { log, errors } = __webpack_require__(1) +const soapRequest = __webpack_require__(1331) +const { + parseTags, + parseValue, + parseContractStartDate, +} = __webpack_require__(1555) +const xml2js = __webpack_require__(1513) +const { consulterDonneesTechniquesContractuelles } = __webpack_require__(1556) + +/** + * Get user contract start date + * @param {string} url + * @param {string} apiAuthKey + * @param {string} userLogin + * @param {number} pointId + * @returns {Promise<string>} + */ +async function getContractStartDate(url, apiAuthKey, userLogin, pointId) { + log('info', 'Fetching data start date') + const sgeHeaders = { + 'Content-Type': 'text/xml;charset=UTF-8', + apikey: apiAuthKey, + } + + const { response } = await soapRequest({ + url: `${url}/enedis_SGE_ConsultationDonneesTechniquesContractuelles/1.0`, + headers: sgeHeaders, + xml: consulterDonneesTechniquesContractuelles(pointId, userLogin), + }).catch(err => { + log('error', 'Error while fetching contract start date : ' + err) + throw errors.VENDOR_DOWN + }) + + const result = await xml2js.parseStringPromise(response.body, { + tagNameProcessors: [parseTags], + valueProcessors: [parseValue], + explicitArray: false, + }) + try { + return parseContractStartDate(result) + } catch (error) { + log('error', 'Error while processing contract start date: ' + error) + throw errors.NOT_EXISTING_DIRECTORY + } +} + +module.exports = { getContractStartDate } + + +/***/ }), +/* 1603 */ +/***/ ((module, __unused_webpack_exports, __webpack_require__) => { + const { log, updateOrCreate } = __webpack_require__(1) -const { iSLocal } = __webpack_require__(1603) +const { iSLocal } = __webpack_require__(1604) const cozyClient = __webpack_require__(485) async function saveAccountData(accountId, accountData) { @@ -228271,7 +228278,7 @@ module.exports = { getAccount, saveAccountData } /***/ }), -/* 1603 */ +/* 1604 */ /***/ ((module) => { function iSLocal() { diff --git a/package.json b/package.json index 87eab8c4ab9a44615f57bef7762ab4a9456bd7d2..eb3c5f85ada17a90a7928cd8d91bd1490381b56a 100644 --- a/package.json +++ b/package.json @@ -19,6 +19,11 @@ "build", "data" ], + "husky": { + "hooks": { + "pre-commit": "yarn lint" + } + }, "scripts": { "start": "node ./src/index.js", "dev": "cozy-konnector-dev",