Skip to content
Snippets Groups Projects
Commit af1d4c91 authored by Rémi PAILHAREY's avatar Rémi PAILHAREY :fork_knife_plate:
Browse files

feat: get off-peak hours and store them in account data

parent aa9b2674
No related branches found
No related tags found
1 merge request!52feat: off-peak hours ratio
Pipeline #91364 passed
......@@ -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,
......
......@@ -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
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment