diff --git a/src/helpers/parsing.js b/src/helpers/parsing.js index 2b529b527e21748c47e5e79dd8d92787cfaca926..af11ce21e28ce99bc0b528f2bce1b8cf16d99e4a 100644 --- a/src/helpers/parsing.js +++ b/src/helpers/parsing.js @@ -29,6 +29,31 @@ function parseUserAddress(result) { ]['point']['donneesGenerales']['adresseInstallation'] } +/** + * Return User off-peak hours + * @param {string} result + * @returns {string} + * @example "3H00-8H00;13H30-16H30" + */ +function parseUserOffPeakHours(result) { + log('info', 'Parsing user off-peak hours') + const json = JSON.stringify(result) + const rawOffPeakHours = + JSON.parse(json)['Envelope']['Body'][ + 'consulterDonneesTechniquesContractuellesResponse' + ]['point']['situationComptage']['dispositifComptage']['relais'][ + 'plageHeuresCreuses' + ] + // extract off-peak hours from parentheses + let match = rawOffPeakHours.match(/\((.*?)\)/) + // Check if there is a match and return the content + if (match) { + return match[1] + } else { + throw new Error('invalid off-peak hours format') + } +} + /** * Return User contract start date * @param {string} result @@ -219,6 +244,7 @@ module.exports = { parseSgeXmlData, parseTags, parseUserAddress, + parseUserOffPeakHours, parseUserPdl, parseValue, parseValueHalfHour, diff --git a/src/index.js b/src/index.js index b64aab5151a969aec29b32c2506664f723563a06..5ddd206c06f7c717b791ac2734aef76a4f3cc7ea 100644 --- a/src/index.js +++ b/src/index.js @@ -18,10 +18,12 @@ const { parseValue, parseValueHalfHour, parsePointId, + parseUserOffPeakHours, } = require('./helpers/parsing') const { consultationMesuresDetailleesMaxPower, consultationMesuresDetaillees, + consulterDonneesTechniquesContractuelles, } = require('./requests/sge') const { updateBoConsent, @@ -352,12 +354,77 @@ async function gatherData(baseUrl, apiAuthKey, sgeLogin, pointId) { '/enedis_SGE_ConsultationMesuresDetaillees_v3/1.0', baseUrl ).href + const contractUrl = new URL( + '/enedis_SGE_ConsultationDonneesTechniquesContractuelles/1.0', + baseUrl + ).href await getData(measuresUrl, apiAuthKey, sgeLogin, pointId) await getMaxPowerData(measuresUrl, apiAuthKey, sgeLogin, pointId) await getDataHalfHour(measuresUrl, apiAuthKey, sgeLogin, pointId) + await getOffPeakHours(contractUrl, apiAuthKey, sgeLogin, pointId) log('info', 'Querying data: done') } +/** + * Get hour data + * @param {string} url + * @param {string} apiAuthKey + * @param {string} userLogin + * @param {string} pointId + */ +async function getOffPeakHours(url, apiAuthKey, userLogin, pointId) { + log('info', 'Fetching off-peak hours') + const sgeHeaders = { + 'Content-Type': 'text/xml;charset=UTF-8', + apikey: apiAuthKey, + } + + const { response } = await soapRequest({ + url: url, + headers: sgeHeaders, + xml: consulterDonneesTechniquesContractuelles(pointId, userLogin, false), + }).catch(err => { + log('error', 'consulterDonneesTechniquesContractuelles') + log('error', err) + Sentry.captureException( + `consulterDonneesTechniquesContractuelles: ${err}`, + { + tags: { section: 'getOffPeakHour' }, + extra: { + pointId: pointId, + }, + } + ) + return err + }) + + const result = await xml2js.parseStringPromise(response.body, { + tagNameProcessors: [parseTags], + valueProcessors: [parseValue], + explicitArray: false, + }) + + try { + const offPeakHours = parseUserOffPeakHours(result) + log( + 'debug', + `Found off-peak hours : ${offPeakHours}, store them in account data` + ) + const accountData = await getAccount(ACCOUNT_ID) + await saveAccountData(ACCOUNT_ID, { + ...accountData.data, + offPeakHours, + }) + } catch (error) { + log('debug', 'Off-peak hours not found, remove them from account data') + let accountData = await getAccount(ACCOUNT_ID) + delete accountData.data.offPeakHours + await saveAccountData(ACCOUNT_ID, { + ...accountData.data, + }) + } +} + /** * Get hour data * @param {string} url