From c040836ec9dc04607fcf939f7b19d22c86dbd81d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?R=C3=A9mi=20Pailharey?= <rpailharey@grandlyon.com>
Date: Thu, 1 Feb 2024 16:49:27 +0100
Subject: [PATCH] publish: feat: get off-peak hours and store them in account
data
generated from commit af1d4c914100d06bc812695a900a0649188f2e92
---
index.js | 130 ++++++++++++++++++++++++++++++++++++++++-----
onDeleteAccount.js | 26 +++++++++
2 files changed, 143 insertions(+), 13 deletions(-)
diff --git a/index.js b/index.js
index a08278d..98e8742 100644
--- a/index.js
+++ b/index.js
@@ -23,10 +23,12 @@ const {
parseValue,
parseValueHalfHour,
parsePointId,
+ parseUserOffPeakHours,
} = __webpack_require__(1599)
const {
consultationMesuresDetailleesMaxPower,
consultationMesuresDetaillees,
+ consulterDonneesTechniquesContractuelles,
} = __webpack_require__(1680)
const {
updateBoConsent,
@@ -153,7 +155,6 @@ async function start(fields, cozyParameters) {
*/
log('info', 'User Logging...')
- const boUrlSGE = new URL('/api/sge', boBaseUrl).href
if (isFirstStart(await getAccount(ACCOUNT_ID))) {
log('info', 'First start...')
transaction.startChild({ op: 'First start' })
@@ -167,7 +168,7 @@ async function start(fields, cozyParameters) {
exitIfDebug(user)
let consent = await createBoConsent(
- boUrlSGE,
+ boBaseUrl,
boToken,
pointId,
user.lastname,
@@ -202,12 +203,12 @@ async function start(fields, cozyParameters) {
contractStartDate,
contractEndDate
).catch(async err => {
- await deleteBoConsent(boUrlSGE, boToken, consent.ID)
+ await deleteBoConsent(boBaseUrl, boToken, consent.ID)
throw err
})
}
consent = await updateBoConsent(
- boUrlSGE,
+ boBaseUrl,
boToken,
consent,
serviceId.toString()
@@ -226,7 +227,7 @@ async function start(fields, cozyParameters) {
transaction.startChild({ op: 'Alternate start' })
const accountData = await getAccount(ACCOUNT_ID)
const userConsent = await getBoConsent(
- boUrlSGE,
+ boBaseUrl,
boToken,
accountData.data.consentId
)
@@ -265,7 +266,7 @@ async function start(fields, cozyParameters) {
sgeLogin,
contractId,
pointId,
- boUrlSGE,
+ boBaseUrl,
boToken,
consentEndDate < today
)
@@ -353,16 +354,93 @@ async function deleteConsent(
*/
async function gatherData(baseUrl, apiAuthKey, sgeLogin, pointId) {
log('info', 'Querying data...')
- const measuresUrl = new URL(
- '/enedis_SGE_ConsultationMesuresDetaillees_v3/1.0',
- baseUrl
- ).href
- await getData(measuresUrl, apiAuthKey, sgeLogin, pointId)
- await getMaxPowerData(measuresUrl, apiAuthKey, sgeLogin, pointId)
- await getDataHalfHour(measuresUrl, apiAuthKey, sgeLogin, pointId)
+ await getOffPeakHours(
+ `${baseUrl}/enedis_SGE_ConsultationDonneesTechniquesContractuelles/1.0`,
+ apiAuthKey,
+ sgeLogin,
+ pointId
+ )
+ await getData(
+ `${baseUrl}/enedis_SGE_ConsultationMesuresDetaillees_v3/1.0`,
+ apiAuthKey,
+ sgeLogin,
+ pointId
+ )
+ await getMaxPowerData(
+ `${baseUrl}/enedis_SGE_ConsultationMesuresDetaillees_v3/1.0`,
+ apiAuthKey,
+ sgeLogin,
+ pointId
+ )
+ await getDataHalfHour(
+ `${baseUrl}/enedis_SGE_ConsultationMesuresDetaillees_v3/1.0`,
+ 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
@@ -229086,6 +229164,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
@@ -229276,6 +229379,7 @@ module.exports = {
parseSgeXmlData,
parseTags,
parseUserAddress,
+ parseUserOffPeakHours,
parseUserPdl,
parseValue,
parseValueHalfHour,
diff --git a/onDeleteAccount.js b/onDeleteAccount.js
index db81f44..30be199 100644
--- a/onDeleteAccount.js
+++ b/onDeleteAccount.js
@@ -228345,6 +228345,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
@@ -228535,6 +228560,7 @@ module.exports = {
parseSgeXmlData,
parseTags,
parseUserAddress,
+ parseUserOffPeakHours,
parseUserPdl,
parseValue,
parseValueHalfHour,
--
GitLab