diff --git a/src/index.js b/src/index.js index 43ea0823b2a5ab5482b9e9d39869e5ef49b7617f..f120a0db38c026f4dfd58a980148df3ef43d67ce 100644 --- a/src/index.js +++ b/src/index.js @@ -4,6 +4,7 @@ const { log, hydrateAndFilter, addData, + errors, } = require('cozy-konnector-libs') const soapRequest = require('easy-soap-request') const moment = require('moment') @@ -19,8 +20,16 @@ const { } = require('./parsing') const { userTechnicalData, - userMesureDetailles, userMaxPower, + userMesureDetailles, + searchUser, + activateDataCollect, + stopDataCollect, + getInseeCode, + updateBoConsent, + createBoConsent, + getBoConsent, + deleteBoConsent, } = require('./request') moment.locale('fr') // set the language moment.tz.setDefault('Europe/Paris') // set the timezone @@ -49,42 +58,106 @@ async function start(fields, cozyParameters) { let apiAuthKey = fields.apiToken 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') baseUrl = cozyParameters.secret.wso2BaseUrl apiAuthKey = cozyParameters.secret.apiToken loginUtilisateur = cozyParameters.secret.loginUtilisateur } - //TODO: authentification ? + + /** + * If it's first start we have to do the following operations: + * - verify pdl are matching + * - BO: create backoffice consent + * - get contract start date and store it + * - activate half-hour + * - BO: update consent with service ID + */ + log('info', 'User Logging...') + + if (await isFirstStart()) { + if (!(await verifyUserIdentity(fields))) { + throw errors.LOGIN_FAILED + } + await createBoConsent() + //TODO: remove because useless ? Done later in code + // const startDate = await getDataStartDate( + // baseUrl, + // apiAuthKey, + // loginUtilisateur, + // fields.pointId + // ) + await activateDataCollect() + await updateBoConsent() + } else { + await getBoConsent() + if (!(await verifyUserIdentity(fields))) { + await deleteBoConsent() + await stopDataCollect() + throw errors.TERMS_VERSION_MISMATCH + } + } log('info', 'Successfully logged in') + await gatherData(baseUrl, apiAuthKey, loginUtilisateur, fields.pointId) +} + +/** + * Verify user identity + * @param {object} fields + */ +async function verifyUserIdentity(fields) { + const inseeCode = getInseeCode(fields.postalCode) + const user = await findUser( + fields.name, + fields.addresse, + fields.postalCode, + inseeCode + ) + if (fields.pointId !== user.pointId) { + log('error', 'PointId does not match') + return false + } + return true +} + +/** + * Main method for gathering data + * @param {string} baseUrl + * @param {string} apiAuthKey + * @param {string} loginUtilisateur + * @param {number} pointId + */ +async function gatherData(baseUrl, apiAuthKey, loginUtilisateur, pointId) { log('info', 'Querying data...') await getDataStartDate( `${baseUrl}/enedis_SGE_ConsultationDonneesTechniquesContractuelles/1.0`, apiAuthKey, loginUtilisateur, - fields.pointId + pointId ) await getData( `${baseUrl}/enedis_SGE_ConsultationMesuresDetaillees/1.0`, apiAuthKey, loginUtilisateur, - fields.pointId + pointId ) await getMaxPowerData( `${baseUrl}/enedis_SGE_ConsultationMesuresDetaillees/1.0`, apiAuthKey, loginUtilisateur, - fields.pointId + pointId ) await getDataHalfHour( `${baseUrl}/enedis_SGE_ConsultationMesuresDetaillees/1.0`, apiAuthKey, loginUtilisateur, - fields.pointId + pointId ) log('info', 'Querying data: done') } + /** * * @param {string} url @@ -367,3 +440,52 @@ async function agregateMonthAndYearData(data) { await storeData(agregatedYearData, 'com.grandlyon.enedis.year', ['year']) } } + +/** + * @returns {boolean} + */ +function isFirstStart() { + //TODO: Implement + return false +} + +/** + * @return {User} + */ +async function findUser( + url, + apiAuthKey, + appLogin, + name, + addresse, + postalCode, + inseeCode +) { + log('info', 'Fetching user data') + const sampleHeaders = { + 'Content-Type': 'text/xml;charset=UTF-8', + apikey: apiAuthKey, + } + + const { response } = await soapRequest({ + url: url, + headers: sampleHeaders, + xml: searchUser(appLogin, name, addresse, postalCode, inseeCode), + }).catch(err => { + log('error', 'rechercherPointResponse') + log('error', err) + throw errors.LOGIN_FAILED + //TODO: handling code error SGT4F6 and SGT432 into USER_ACTIon_NEEDED + }) + + //TODO: handle reply + xml2js.parseString( + response.body, + { + tagNameProcessors: [parseTags], + valueProcessors: [parseValue], + explicitArray: false, + }, + processStartDate() + ) +} diff --git a/src/request.js b/src/request.js index 12d5786dfdc66d8ad5547a22d8aa5cb28f55c7ff..6c1e90e45470263720ffcea44343a5b5ed7343b7 100644 --- a/src/request.js +++ b/src/request.js @@ -252,6 +252,52 @@ function stopDataCollect(appLogin, contractId, pointId, serviceSouscritId) { </soapenv:Envelope>` } +/** + * Return inseeCode given a postalCode + * @param {string} postalCode + * @return {string} inseeCode + */ +function getInseeCode(postalCode) { + //TODO: Implement + log('info', `Query getInseeCode for postalCode ${postalCode}`) + throw new Error('Function not implemented.') +} + +/** + * + */ +function createBoConsent() { + //TODO: Implement + log('info', `Query createBoConsent`) + throw new Error('Function not implemented.') +} + +/** + * + */ +function updateBoConsent() { + //TODO: Implement + log('info', `Query updateBoConsent`) + throw new Error('Function not implemented.') +} + +/** + * + */ +function getBoConsent() { + //TODO: Implement + log('info', `Query getBoConsent`) + throw new Error('Function not implemented.') +} +/** + * + */ +function deleteBoConsent() { + //TODO: deleteBoConsent + log('info', `Query createBoConsent`) + throw new Error('Function not implemented.') +} + module.exports = { userTechnicalData, userMaxPower, @@ -260,4 +306,9 @@ module.exports = { searchServiceSouscrit, activateDataCollect, stopDataCollect, + getInseeCode, + createBoConsent, + updateBoConsent, + getBoConsent, + deleteBoConsent, } diff --git a/src/types.js b/src/types.js index fd86ac644973a019f9a47e35352ed0307ea39aed..b2d1f4016528c80d44ce27bc9b6c461fd1c170c6 100644 --- a/src/types.js +++ b/src/types.js @@ -14,3 +14,13 @@ * @property {number} v * @property {string} d */ + +/** + * User definition + * @typedef {object} User + * @property {string} name + * @property {string} address + * @property {string} postalCode + * @property {string} pointId + * @property {string} [inseeCode] + */