diff --git a/index.js b/index.js index 3cbe9178583e59d379e7fcdf0e149f08c29defa8..b127538252177579b8ee1ceb2bbbdaaf2ca1146b 100644 --- a/index.js +++ b/index.js @@ -32,13 +32,15 @@ const { getBoConsent, deleteBoConsent, } = __webpack_require__(1557) -const { verifyUserIdentity } = __webpack_require__(1595) -const { activateContract } = __webpack_require__(1598) -const { verifyContract } = __webpack_require__(1599) -const { terminateContract } = __webpack_require__(1601) -const { getContractStartDate } = __webpack_require__(1602) -const { getAccount, saveAccountData } = __webpack_require__(1603) -const { isLocal } = __webpack_require__(1604) +const { + verifyUserIdentity, + activateContract, + verifyContract, + terminateContract, + getContractStartDate, +} = __webpack_require__(1595) +const { getAccount, saveAccountData } = __webpack_require__(1604) +const { isLocal } = __webpack_require__(1605) moment.locale('fr') // set the language moment.tz.setDefault('Europe/Paris') // set the timezone @@ -109,7 +111,7 @@ async function start(fields, cozyParameters) { */ log('info', 'User Logging...') - if (await isFirstStart(await getAccount(ACCOUNT_ID))) { + if (isFirstStart(await getAccount(ACCOUNT_ID))) { const user = await verifyUserIdentity(fields, baseUrl, apiAuthKey, sgeLogin) let consent = await createBoConsent( @@ -541,7 +543,7 @@ async function agregateMonthAndYearData(data) { * @returns {boolean} */ function isFirstStart(account) { - if (account && account.data && account.data.consentId) { + if (account?.data?.consentId) { log('info', 'Konnector not first start') return false } @@ -227971,60 +227973,22 @@ module.exports = function isAxiosError(payload) { /* 1595 */ /***/ ((module, __unused_webpack_exports, __webpack_require__) => { -// @ts-check -const { log, errors } = __webpack_require__(1) -const { findUserPdl } = __webpack_require__(1596) -const { getInseeCode } = __webpack_require__(1597) - -/** - * Verify user identity - * @param {object} fields - * @param {string} baseUrl - * @param {string} apiAuthKey - * @param {string} loginUtilisateur - * @param {boolean} isAlternateStart - * @returns {Promise<User>} - */ -async function verifyUserIdentity( - fields, - baseUrl, - apiAuthKey, - loginUtilisateur, - isAlternateStart = false -) { - const inseeCode = await getInseeCode(fields.postalCode, fields.city) - - const pdl = await findUserPdl( - `${baseUrl}/enedis_SDE_recherche-point/1.0`, - apiAuthKey, - loginUtilisateur, - fields.lastname, - fields.address, - fields.postalCode, - inseeCode - ) - - if (fields.pointId != pdl) { - log('error', 'PointId does not match') - if (isAlternateStart) { - throw errors.TERMS_VERSION_MISMATCH - } else { - throw errors.LOGIN_FAILED - } - } +const { activateContract } = __webpack_require__(1596) +const { getContractStartDate } = __webpack_require__(1597) +const { terminateContract } = __webpack_require__(1598) +const { verifyContract } = __webpack_require__(1599) +const { findUserPdl } = __webpack_require__(1601) +const { verifyUserIdentity } = __webpack_require__(1602) - return { - lastname: fields.lastname, - firstname: fields.firstname, - pointId: fields.pointId, - inseeCode, - postalCode: fields.postalCode, - address: fields.address, - } +module.exports = { + activateContract, + getContractStartDate, + terminateContract, + verifyContract, + findUserPdl, + verifyUserIdentity, } -module.exports = { verifyUserIdentity } - /***/ }), /* 1596 */ @@ -228033,8 +227997,8 @@ module.exports = { verifyUserIdentity } // @ts-check const { log, errors } = __webpack_require__(1) const soapRequest = __webpack_require__(1331) -const { parseUserPdl, parseTags, parseValue } = __webpack_require__(1555) -const { rechercherPoint } = __webpack_require__(1556) +const { parseTags, parseValue, parseServiceId } = __webpack_require__(1555) +const { commanderCollectePublicationMesures } = __webpack_require__(1556) const xml2js = __webpack_require__(1513) /** @@ -228042,32 +228006,40 @@ const xml2js = __webpack_require__(1513) * @param {string} apiAuthKey * @param {string} appLogin * @param {string} name - * @param {string} address - * @param {string} postalCode - * @param {string} inseeCode - * @return {Promise<string | null>} User Pdl + * @param {number} pointId + * @param {string} startDate + * @param {string} endDate + * @return {Promise<number>} User contractId */ -async function findUserPdl( +async function activateContract( url, apiAuthKey, appLogin, + contractId, name, - address, - postalCode, - inseeCode + pointId, + startDate, + endDate ) { - log('info', 'Fetching user data') + log('info', 'activateContract') const sgeHeaders = { 'Content-Type': 'text/xml;charset=UTF-8', apikey: apiAuthKey, } const { response } = await soapRequest({ - url: url, + url: `${url}/enedis_SGE_CommandeCollectePublicationMesures/1.0`, headers: sgeHeaders, - xml: rechercherPoint(appLogin, name, postalCode, inseeCode, address), + xml: commanderCollectePublicationMesures( + appLogin, + contractId, + pointId, + name, + startDate, + endDate + ), }).catch(err => { - log('error', 'rechercherPointResponse') + log('error', 'commanderCollectePublicationMesures') log('error', err) throw errors.LOGIN_FAILED }) @@ -228079,18 +228051,19 @@ async function findUserPdl( }) try { - return parseUserPdl(parsedReply) + return parseServiceId(parsedReply) } catch (error) { - log('error', 'Error while parsing user PDL: ' + error) + log('error', 'Error while activating contract: ' + error) log( 'error', `Enedis issue ${parsedReply.Envelope.Body.Fault.detail.erreur.resultat.$.code}: ${parsedReply.Envelope.Body.Fault.faultstring}` ) + //TODO: handle SGT4B8: Il existe déjà plusieurs demandes en cours sur le point ? throw errors.LOGIN_FAILED } } -module.exports = { findUserPdl } +module.exports = { activateContract } /***/ }), @@ -228098,44 +228071,58 @@ module.exports = { findUserPdl } /***/ ((module, __unused_webpack_exports, __webpack_require__) => { // @ts-check -const { default: axios } = __webpack_require__(1558) const { log, errors } = __webpack_require__(1) - -const API_URL = 'https://apicarto.ign.fr/api/codes-postaux/communes' +const soapRequest = __webpack_require__(1331) +const { + parseTags, + parseValue, + parseContractStartDate, +} = __webpack_require__(1555) +const xml2js = __webpack_require__(1513) +const { consulterDonneesTechniquesContractuelles } = __webpack_require__(1556) /** - * Return inseeCode given a postalCode - * @param {string} postalCode - * @param {string} [city] - * @return {Promise<string>} inseeCode + * Get user contract start date + * @param {string} url + * @param {string} apiAuthKey + * @param {string} userLogin + * @param {number} pointId + * @returns {Promise<string>} */ -async function getInseeCode(postalCode, city) { - try { - log('info', `Query getInseeCode for postalCode ${postalCode} / ${city}`) - const response = await axios.get(`${API_URL}/${postalCode}`) +async function getContractStartDate(url, apiAuthKey, userLogin, pointId) { + log('info', 'Fetching data start date') + const sgeHeaders = { + 'Content-Type': 'text/xml;charset=UTF-8', + apikey: apiAuthKey, + } - if (response.data.length === 1) { - return response.data[0].codeCommune - } else { - if (!city) throw errors.USER_ACTION_NEEDED + const { response } = await soapRequest({ + url: `${url}/enedis_SGE_ConsultationDonneesTechniquesContractuelles/1.0`, + headers: sgeHeaders, + xml: consulterDonneesTechniquesContractuelles(pointId, userLogin), + }).catch(err => { + log('error', 'Error while fetching contract start date : ' + err) + throw errors.VENDOR_DOWN + }) - const filteredResponse = response.data.filter( - town => town.nomCommune.toLowerCase() === city.toLowerCase() - ) - return filteredResponse[0].codeCommune - } + const result = await xml2js.parseStringPromise(response.body, { + tagNameProcessors: [parseTags], + valueProcessors: [parseValue], + explicitArray: false, + }) + try { + return parseContractStartDate(result) } catch (error) { + log('error', 'Error while processing contract start date: ' + error) log( 'error', - `Query getInseeCode failed for postalCode ${postalCode} / ${city}` + `Enedis issue ${result.Envelope.Body.Fault.detail.erreur.resultat.$.code}: ${result.Envelope.Body.Fault.faultstring}` ) - throw errors.USER_ACTION_NEEDED + throw errors.NOT_EXISTING_DIRECTORY } } -module.exports = { - getInseeCode, -} +module.exports = { getContractStartDate } /***/ }), @@ -228145,51 +228132,45 @@ module.exports = { // @ts-check const { log, errors } = __webpack_require__(1) const soapRequest = __webpack_require__(1331) -const { parseTags, parseValue, parseServiceId } = __webpack_require__(1555) -const { commanderCollectePublicationMesures } = __webpack_require__(1556) +const { parseTags, parseValue } = __webpack_require__(1555) +const { commanderArretServiceSouscritMesures } = __webpack_require__(1556) const xml2js = __webpack_require__(1513) /** * @param {string} url * @param {string} apiAuthKey * @param {string} appLogin - * @param {string} name * @param {number} pointId - * @param {string} startDate - * @param {string} endDate - * @return {Promise<number>} User contractId + * @param {number} serviceId + * @return {Promise<string>} User contractId */ -async function activateContract( +async function terminateContract( url, apiAuthKey, appLogin, contractId, - name, pointId, - startDate, - endDate + serviceId ) { - log('info', 'activateContract') + log('info', 'terminateContract') const sgeHeaders = { 'Content-Type': 'text/xml;charset=UTF-8', apikey: apiAuthKey, } const { response } = await soapRequest({ - url: `${url}/enedis_SGE_CommandeCollectePublicationMesures/1.0`, + url: `${url}/enedis_SGE_CommandeArretServiceSouscritMesures/1.0`, headers: sgeHeaders, - xml: commanderCollectePublicationMesures( + xml: commanderArretServiceSouscritMesures( appLogin, contractId, pointId, - name, - startDate, - endDate + serviceId ), }).catch(err => { - log('error', 'commanderCollectePublicationMesures') + log('error', 'commanderArretServiceSouscritMesures') log('error', err) - throw errors.LOGIN_FAILED + throw errors.VENDOR_DOWN }) const parsedReply = await xml2js.parseStringPromise(response.body, { @@ -228199,19 +228180,22 @@ async function activateContract( }) try { - return parseServiceId(parsedReply) + // We don't need any action on reply for now + if (parsedReply.Envelope.Body.Fault) { + log( + 'error', + `Enedis issue ${parsedReply.Envelope.Body.Fault.detail.erreur.resultat.$.code}: ${parsedReply.Envelope.Body.Fault.faultstring}` + ) + } + return parsedReply } catch (error) { - log('error', 'Error while activating contract: ' + error) - log( - 'error', - `Enedis issue ${parsedReply.Envelope.Body.Fault.detail.erreur.resultat.$.code}: ${parsedReply.Envelope.Body.Fault.faultstring}` - ) - //TODO: handle SGT4B8: Il existe déjà plusieurs demandes en cours sur le point ? - throw errors.LOGIN_FAILED + log('error', 'Error while parsing user contract termination: ' + error) + log('error', `Enedis issue ${JSON.stringify(parsedReply.Envelope.Body)}`) + throw errors.VENDOR_DOWN } } -module.exports = { activateContract } +module.exports = { terminateContract } /***/ }), @@ -228319,45 +228303,43 @@ module.exports = { contractState, contractLibelle } // @ts-check const { log, errors } = __webpack_require__(1) const soapRequest = __webpack_require__(1331) -const { parseTags, parseValue } = __webpack_require__(1555) -const { commanderArretServiceSouscritMesures } = __webpack_require__(1556) +const { parseUserPdl, parseTags, parseValue } = __webpack_require__(1555) +const { rechercherPoint } = __webpack_require__(1556) const xml2js = __webpack_require__(1513) /** * @param {string} url * @param {string} apiAuthKey * @param {string} appLogin - * @param {number} pointId - * @param {number} serviceId - * @return {Promise<string>} User contractId + * @param {string} name + * @param {string} address + * @param {string} postalCode + * @param {string} inseeCode + * @return {Promise<string | null>} User Pdl */ -async function terminateContract( +async function findUserPdl( url, apiAuthKey, appLogin, - contractId, - pointId, - serviceId + name, + address, + postalCode, + inseeCode ) { - log('info', 'terminateContract') + log('info', 'Fetching user data') const sgeHeaders = { 'Content-Type': 'text/xml;charset=UTF-8', apikey: apiAuthKey, } const { response } = await soapRequest({ - url: `${url}/enedis_SGE_CommandeArretServiceSouscritMesures/1.0`, + url: url, headers: sgeHeaders, - xml: commanderArretServiceSouscritMesures( - appLogin, - contractId, - pointId, - serviceId - ), + xml: rechercherPoint(appLogin, name, postalCode, inseeCode, address), }).catch(err => { - log('error', 'commanderArretServiceSouscritMesures') + log('error', 'rechercherPointResponse') log('error', err) - throw errors.VENDOR_DOWN + throw errors.LOGIN_FAILED }) const parsedReply = await xml2js.parseStringPromise(response.body, { @@ -228367,22 +228349,18 @@ async function terminateContract( }) try { - // We don't need any action on reply for now - if (parsedReply.Envelope.Body.Fault) { - log( - 'error', - `Enedis issue ${parsedReply.Envelope.Body.Fault.detail.erreur.resultat.$.code}: ${parsedReply.Envelope.Body.Fault.faultstring}` - ) - } - return parsedReply + return parseUserPdl(parsedReply) } catch (error) { - log('error', 'Error while parsing user contract termination: ' + error) - log('error', `Enedis issue ${JSON.stringify(parsedReply.Envelope.Body)}`) - throw errors.VENDOR_DOWN + log('error', 'Error while parsing user PDL: ' + error) + log( + 'error', + `Enedis issue ${parsedReply.Envelope.Body.Fault.detail.erreur.resultat.$.code}: ${parsedReply.Envelope.Body.Fault.faultstring}` + ) + throw errors.LOGIN_FAILED } } -module.exports = { terminateContract } +module.exports = { findUserPdl } /***/ }), @@ -228391,65 +228369,110 @@ module.exports = { terminateContract } // @ts-check const { log, errors } = __webpack_require__(1) -const soapRequest = __webpack_require__(1331) -const { - parseTags, - parseValue, - parseContractStartDate, -} = __webpack_require__(1555) -const xml2js = __webpack_require__(1513) -const { consulterDonneesTechniquesContractuelles } = __webpack_require__(1556) +const { findUserPdl } = __webpack_require__(1601) +const { getInseeCode } = __webpack_require__(1603) /** - * Get user contract start date - * @param {string} url + * Verify user identity + * @param {object} fields + * @param {string} baseUrl * @param {string} apiAuthKey - * @param {string} userLogin - * @param {number} pointId - * @returns {Promise<string>} + * @param {string} loginUtilisateur + * @param {boolean} isAlternateStart + * @returns {Promise<User>} */ -async function getContractStartDate(url, apiAuthKey, userLogin, pointId) { - log('info', 'Fetching data start date') - const sgeHeaders = { - 'Content-Type': 'text/xml;charset=UTF-8', - apikey: apiAuthKey, +async function verifyUserIdentity( + fields, + baseUrl, + apiAuthKey, + loginUtilisateur, + isAlternateStart = false +) { + const inseeCode = await getInseeCode(fields.postalCode, fields.city) + + const pdl = await findUserPdl( + `${baseUrl}/enedis_SDE_recherche-point/1.0`, + apiAuthKey, + loginUtilisateur, + fields.lastname, + fields.address, + fields.postalCode, + inseeCode + ) + + if (fields.pointId != pdl) { + log('error', 'PointId does not match') + if (isAlternateStart) { + throw errors.TERMS_VERSION_MISMATCH + } else { + throw errors.LOGIN_FAILED + } } - const { response } = await soapRequest({ - url: `${url}/enedis_SGE_ConsultationDonneesTechniquesContractuelles/1.0`, - headers: sgeHeaders, - xml: consulterDonneesTechniquesContractuelles(pointId, userLogin), - }).catch(err => { - log('error', 'Error while fetching contract start date : ' + err) - throw errors.VENDOR_DOWN - }) + return { + lastname: fields.lastname, + firstname: fields.firstname, + pointId: fields.pointId, + inseeCode, + postalCode: fields.postalCode, + address: fields.address, + } +} - const result = await xml2js.parseStringPromise(response.body, { - tagNameProcessors: [parseTags], - valueProcessors: [parseValue], - explicitArray: false, - }) +module.exports = { verifyUserIdentity } + + +/***/ }), +/* 1603 */ +/***/ ((module, __unused_webpack_exports, __webpack_require__) => { + +// @ts-check +const { default: axios } = __webpack_require__(1558) +const { log, errors } = __webpack_require__(1) + +const API_URL = 'https://apicarto.ign.fr/api/codes-postaux/communes' + +/** + * Return inseeCode given a postalCode + * @param {string} postalCode + * @param {string} [city] + * @return {Promise<string>} inseeCode + */ +async function getInseeCode(postalCode, city) { try { - return parseContractStartDate(result) + log('info', `Query getInseeCode for postalCode ${postalCode} / ${city}`) + const response = await axios.get(`${API_URL}/${postalCode}`) + + if (response.data.length === 1) { + return response.data[0].codeCommune + } else { + if (!city) throw errors.USER_ACTION_NEEDED + + const filteredResponse = response.data.filter( + town => town.nomCommune.toLowerCase() === city.toLowerCase() + ) + return filteredResponse[0].codeCommune + } } catch (error) { - log('error', 'Error while processing contract start date: ' + error) log( 'error', - `Enedis issue ${result.Envelope.Body.Fault.detail.erreur.resultat.$.code}: ${result.Envelope.Body.Fault.faultstring}` + `Query getInseeCode failed for postalCode ${postalCode} / ${city}` ) - throw errors.NOT_EXISTING_DIRECTORY + throw errors.USER_ACTION_NEEDED } } -module.exports = { getContractStartDate } +module.exports = { + getInseeCode, +} /***/ }), -/* 1603 */ +/* 1604 */ /***/ ((module, __unused_webpack_exports, __webpack_require__) => { const { log, updateOrCreate } = __webpack_require__(1) -const { isLocal } = __webpack_require__(1604) +const { isLocal } = __webpack_require__(1605) const cozyClient = __webpack_require__(485) async function saveAccountData(accountId, accountData) { @@ -228466,7 +228489,6 @@ async function saveAccountData(accountId, accountData) { async function getAccount(accountId) { log('info', `getAccount: ${accountId}`) - //TODO: refactor with usageof cozy-libs. Not working during implementation const accounts = await cozyClient.data.findAll('io.cozy.accounts') return accounts.filter(account => isLocal() ? account._id === accountId : account.account_type === accountId @@ -228477,7 +228499,7 @@ module.exports = { getAccount, saveAccountData } /***/ }), -/* 1604 */ +/* 1605 */ /***/ ((module) => { function isLocal() { diff --git a/onDeleteAccount.js b/onDeleteAccount.js index 12b657902795ec1d340726c6f0aede9049a897fd..0fad35104e406d71158c1b99d33121939b5690a3 100644 --- a/onDeleteAccount.js +++ b/onDeleteAccount.js @@ -227310,10 +227310,7 @@ module.exports = function isAxiosError(payload) { /* 1595 */, /* 1596 */, /* 1597 */, -/* 1598 */, -/* 1599 */, -/* 1600 */, -/* 1601 */ +/* 1598 */ /***/ ((module, __unused_webpack_exports, __webpack_require__) => { // @ts-check @@ -227386,12 +227383,16 @@ module.exports = { terminateContract } /***/ }), +/* 1599 */, +/* 1600 */, +/* 1601 */, /* 1602 */, -/* 1603 */ +/* 1603 */, +/* 1604 */ /***/ ((module, __unused_webpack_exports, __webpack_require__) => { const { log, updateOrCreate } = __webpack_require__(1) -const { isLocal } = __webpack_require__(1604) +const { isLocal } = __webpack_require__(1605) const cozyClient = __webpack_require__(485) async function saveAccountData(accountId, accountData) { @@ -227408,7 +227409,6 @@ async function saveAccountData(accountId, accountData) { async function getAccount(accountId) { log('info', `getAccount: ${accountId}`) - //TODO: refactor with usageof cozy-libs. Not working during implementation const accounts = await cozyClient.data.findAll('io.cozy.accounts') return accounts.filter(account => isLocal() ? account._id === accountId : account.account_type === accountId @@ -227419,7 +227419,7 @@ module.exports = { getAccount, saveAccountData } /***/ }), -/* 1604 */ +/* 1605 */ /***/ ((module) => { function isLocal() { @@ -227434,21 +227434,20 @@ module.exports = { isLocal } /***/ }), -/* 1605 */ +/* 1606 */ /***/ ((module, __unused_webpack_exports, __webpack_require__) => { // @ts-check const { log, errors } = __webpack_require__(1) -const { getAccountRev, getAccountSecret } = __webpack_require__(1606) +const { getAccountRev, getAccountSecret } = __webpack_require__(1607) const { getBoConsent, deleteBoConsent } = __webpack_require__(1557) -const { terminateContract } = __webpack_require__(1601) -const { getAccount } = __webpack_require__(1603) -// const getAccountSecret = require('./helpers/getAccountSecret') +const { terminateContract } = __webpack_require__(1598) +const { getAccount } = __webpack_require__(1604) const moment = __webpack_require__(1373) __webpack_require__(1510) moment.locale('fr') // set the language moment.tz.setDefault('Europe/Paris') // set the timezone -const { isLocal } = __webpack_require__(1604) +const { isLocal } = __webpack_require__(1605) const ACCOUNT_ID = isLocal() ? 'default_account_id' : 'enedis-sge-grandlyon' async function onDeleteAccount() { @@ -227466,7 +227465,7 @@ async function onDeleteAccount() { accountData.data.consentId ) - if (userConsent.ID && userConsent.pointID && userConsent.pointID) { + if (userConsent.ID && userConsent.pointID) { log('log', `Consent ${userConsent.ID} found for user`) if (userConsent.serviceID) { await deleteBoConsent( @@ -227515,11 +227514,11 @@ module.exports = { onDeleteAccount } /***/ }), -/* 1606 */ +/* 1607 */ /***/ ((module, __unused_webpack_exports, __webpack_require__) => { const { log } = __webpack_require__(1) -const { isLocal } = __webpack_require__(1604) +const { isLocal } = __webpack_require__(1605) function getAccountId() { log('info', `getAccountId`) @@ -227654,7 +227653,7 @@ module.exports = { getAccountId, getAccountRev, getAccountSecret } /******/ // module cache are used so entry inlining is disabled /******/ // startup /******/ // Load entry module and return exports -/******/ var __webpack_exports__ = __webpack_require__(__webpack_require__.s = 1605); +/******/ var __webpack_exports__ = __webpack_require__(__webpack_require__.s = 1606); /******/ /******/ })() ; \ No newline at end of file