diff --git a/src/helpers/parsing.js b/src/helpers/parsing.js index 862f2256078f06429357e27b141d3c076a65a7ac..9786378d60e40afd74ba6a9ec8c98492abe4c64d 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 4df3db2428e06c95d30e90c38371b74a4fd0b575..52ba5e48fe58f1653b142a3cebc015b5d4717382 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, @@ -347,6 +349,12 @@ async function deleteConsent( */ async function gatherData(baseUrl, apiAuthKey, sgeLogin, pointId) { log('info', 'Querying data...') + await getOffPeakHours( + `${baseUrl}/enedis_SGE_ConsultationDonneesTechniquesContractuelles/1.0`, + apiAuthKey, + sgeLogin, + pointId + ) await getData( `${baseUrl}/enedis_SGE_ConsultationMesuresDetaillees_v3/1.0`, apiAuthKey, @@ -368,6 +376,66 @@ async function gatherData(baseUrl, 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