diff --git a/src/findUserPdl.js b/src/findUserPdl.js index ba1871244813edb30b3f7ff343ccbe9cdf19e613..5db6d7b98530ad633ec9b487d8f853957b6a51c4 100644 --- a/src/findUserPdl.js +++ b/src/findUserPdl.js @@ -49,6 +49,7 @@ async function findUserPdl( try { return parseUserPdl(parsedReply) } catch (error) { + log('error', 'Error while parsing user PDL: ' + error) throw errors.LOGIN_FAILED } } diff --git a/src/index.js b/src/index.js index 20790eeda46e1e5895a2ce5dcfb0cd9171eecd5b..a454ddb3336b22fbe57957f8e15d9d2c90f90305 100644 --- a/src/index.js +++ b/src/index.js @@ -13,10 +13,10 @@ const xml2js = require('xml2js') const { buildAgregatedData } = require('./aggregate') const { parseSgeXmlData, - parseSgeXmlTechnicalData, formateDataForDoctype, parseTags, parseValue, + parseContractStartDate, } = require('./parsing') const { consulterDonneesTechniquesContractuelles, @@ -57,13 +57,16 @@ module.exports = new BaseKonnector(start) // secret api key. async function start(fields, cozyParameters) { log('info', 'Gathering data ...') + let pointId = fields.pointId let baseUrl = fields.wso2BaseUrl let apiAuthKey = fields.apiToken + //TODO switch variable to english let loginUtilisateur = fields.loginUtilisateur log('info', 'Authenticating ...') //TODO: Verify if condition is working in local and on build version if (cozyParameters && Object.keys(cozyParameters).length !== 0) { log('debug', 'Found COZY_PARAMETERS') + pointId = cozyParameters.secret.pointId baseUrl = cozyParameters.secret.wso2BaseUrl apiAuthKey = cozyParameters.secret.apiToken loginUtilisateur = cozyParameters.secret.loginUtilisateur @@ -82,7 +85,7 @@ async function start(fields, cozyParameters) { if (await isFirstStart()) { await verifyUserIdentity(fields, baseUrl, apiAuthKey, loginUtilisateur) - await createBoConsent() + // await createBoConsent() //TODO: remove because useless ? Done later in code // const startDate = await getDataStartDate( // baseUrl, @@ -90,6 +93,13 @@ async function start(fields, cozyParameters) { // loginUtilisateur, // fields.pointId // ) + // console.log(startDate) + await getContractStartDate(baseUrl, apiAuthKey, loginUtilisateur, pointId) + + console.log(startDailyDate) + console.log(startDailyDateString) + + throw new Error('Rest not implemented.') await commanderCollectePublicationMesures() await updateBoConsent() } else { @@ -115,7 +125,7 @@ async function start(fields, cozyParameters) { */ async function gatherData(baseUrl, apiAuthKey, loginUtilisateur, pointId) { log('info', 'Querying data...') - await getDataStartDate( + await getContractStartDate( `${baseUrl}/enedis_SGE_ConsultationDonneesTechniquesContractuelles/1.0`, apiAuthKey, loginUtilisateur, @@ -148,8 +158,9 @@ async function gatherData(baseUrl, apiAuthKey, loginUtilisateur, pointId) { * @param {string} apiAuthKey * @param {string} userLogin * @param {number} pointId + * @returns {Promise<void>} */ -async function getDataStartDate(url, apiAuthKey, userLogin, pointId) { +async function getContractStartDate(url, apiAuthKey, userLogin, pointId) { log('info', 'Fetching data start date') const sgeHeaders = { 'Content-Type': 'text/xml;charset=UTF-8', @@ -157,7 +168,7 @@ async function getDataStartDate(url, apiAuthKey, userLogin, pointId) { } const { response } = await soapRequest({ - url: url, + url: `${url}/enedis_SGE_ConsultationDonneesTechniquesContractuelles/1.0`, headers: sgeHeaders, xml: consulterDonneesTechniquesContractuelles(pointId, userLogin), }).catch(err => { @@ -372,12 +383,11 @@ function processStartDate() { } // update start Date with contract openning date try { - startDailyDate = moment(parseSgeXmlTechnicalData(result), 'YYYY-MM-DD') + startDailyDate = moment(parseContractStartDate(result), 'YYYY-MM-DD') startDailyDateString = startDailyDate.format('YYYY-MM-DD') - } catch (err) { - log('error', err) - //TODO: custom error ? - throw err + } catch (error) { + log('error', 'Error while processing contract start date: ' + error) + throw errors.NOT_EXISTING_DIRECTORY } } } diff --git a/src/parsing.js b/src/parsing.js index 05439e132dbcf03fbb96462b38e16e5b23c032f4..b4be6d884415c1464ce5981df7f3c822fc9ca1fb 100644 --- a/src/parsing.js +++ b/src/parsing.js @@ -16,12 +16,12 @@ function parseUserPdl(result) { } /** - * Return start date + * Return User contract start date * @param {string} result * @returns {string} */ -function parseSgeXmlTechnicalData(result) { - log('info', 'Parsing technical data') +function parseContractStartDate(result) { + log('info', 'Parsing contract start date') const json = JSON.stringify(result) return JSON.parse(json)['Envelope']['Body'][ 'consulterDonneesTechniquesContractuellesResponse' @@ -91,9 +91,9 @@ function parseValue(value, name) { module.exports = { parseSgeXmlData, - parseSgeXmlTechnicalData, formateDataForDoctype, parseTags, parseValue, parseUserPdl, + parseContractStartDate, }