diff --git a/index.js b/index.js index 87d44543565a894551e8f7eba2f03d9aa147a902..1592b375dd864b030bb8b30c52bd8019c0dc0b3b 100644 --- a/index.js +++ b/index.js @@ -228542,7 +228542,18 @@ async function getAccount(accountId) { )[0] } -module.exports = { getAccount, saveAccountData } +async function getAccountForDelete(accountId, accountRev) { + log('info', `getAccountForDelete: ${accountId} ${accountRev}`) + const body = await cozyClient.fetchJSON( + 'GET', + `/data/io.cozy.accounts/${accountId}?rev=${accountRev}` + ) + + log('debug', `getAccountForDelete: ${body}`) + return body +} + +module.exports = { getAccount, saveAccountData, getAccountForDelete } /***/ }), @@ -228557,7 +228568,15 @@ function isLocal() { ) } -module.exports = { isLocal } +/** + * Verify if it's an alpha URL + * @returns {boolean} + */ +function isAlpha() { + return process.env.COZY_URL.includes('alpha') +} + +module.exports = { isLocal, isAlpha } /***/ }) diff --git a/onDeleteAccount.js b/onDeleteAccount.js index db90e7610e6a209669de5147a0d37efec0afed43..ac8ae2862c115bf830d3a697b3efef314b0396bf 100644 --- a/onDeleteAccount.js +++ b/onDeleteAccount.js @@ -227436,7 +227436,18 @@ async function getAccount(accountId) { )[0] } -module.exports = { getAccount, saveAccountData } +async function getAccountForDelete(accountId, accountRev) { + log('info', `getAccountForDelete: ${accountId} ${accountRev}`) + const body = await cozyClient.fetchJSON( + 'GET', + `/data/io.cozy.accounts/${accountId}?rev=${accountRev}` + ) + + log('debug', `getAccountForDelete: ${body}`) + return body +} + +module.exports = { getAccount, saveAccountData, getAccountForDelete } /***/ }), @@ -227451,7 +227462,15 @@ function isLocal() { ) } -module.exports = { isLocal } +/** + * Verify if it's an alpha URL + * @returns {boolean} + */ +function isAlpha() { + return process.env.COZY_URL.includes('alpha') +} + +module.exports = { isLocal, isAlpha } /***/ }), @@ -227460,28 +227479,33 @@ module.exports = { isLocal } // @ts-check const { log, errors } = __webpack_require__(1) -const { getAccountRev, getAccountSecret } = __webpack_require__(1607) +const { + getAccountRev, + getAccountSecret, + getAccountId, +} = __webpack_require__(1607) const { getBoConsent, deleteBoConsent } = __webpack_require__(1557) const { terminateContract } = __webpack_require__(1598) -const { getAccount } = __webpack_require__(1604) +const { getAccountForDelete } = __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__(1605) -const ACCOUNT_ID = isLocal() ? 'default_account_id' : 'enedis-sge-grandlyon' +const { isLocal, isAlpha } = __webpack_require__(1605) +// const ACCOUNT_ID = isLocal() ? 'default_account_id' : 'enedis-sge-grandlyon' async function onDeleteAccount() { log('info', 'Deleting account ...') log('info', 'Getting secrets ...') - + const ACCOUNT_ID = getAccountId() const accountRev = getAccountRev() if (accountRev) { log('info', 'Account rev exist') - const accountData = await getAccount(ACCOUNT_ID) + const accountData = await getAccountForDelete(ACCOUNT_ID, accountRev) // Parse local info for deletion test if (isLocal()) { + log('warn', 'Local run') const fields = JSON.parse( process.env.COZY_FIELDS ? process.env.COZY_FIELDS : '{}' ) @@ -227497,6 +227521,8 @@ async function onDeleteAccount() { accountData.data.consentId ) + log('info', `isAlpha: ${isAlpha()}`) + log('info', `userConsent: ${JSON.stringify(userConsent)}`) if (userConsent.ID && userConsent.pointID) { log('log', `Consent ${userConsent.ID} found for user`) if (userConsent.serviceID) { @@ -227505,14 +227531,17 @@ async function onDeleteAccount() { secrets.boToken, userConsent.ID ) - await terminateContract( - secrets.wso2BaseUrl, - secrets.apiToken, - secrets.sgeLogin, - secrets.contractId, - userConsent.pointID, - userConsent.serviceID - ) + // Verify if it's dev env to prevent delete of real data + if (!isAlpha()) { + await terminateContract( + secrets.wso2BaseUrl, + secrets.apiToken, + secrets.sgeLogin, + secrets.contractId, + userConsent.pointID, + userConsent.serviceID + ) + } } else { log('error', `No service id retrieved from BO`) throw errors.VENDOR_DOWN @@ -227563,6 +227592,7 @@ function getAccountId() { function getAccountRev() { log('info', `getAccountRev`) + log('info', `getAccountRev: ${JSON.stringify(process.env.COZY_FIELDS)}`) try { return isLocal() ? 'fakeAccountRev' @@ -227578,6 +227608,7 @@ function getAccountRev() { * @returns {Fields} */ function getAccountSecret() { + log('info', `getAccountSecret`) try { return isLocal() ? JSON.parse(process.env.COZY_FIELDS) diff --git a/package.json b/package.json index 2e0bba113cfe88a702f98d7de96adca622aedf41..3bab086b5063e6f6c6aa968c754a585ddae37e81 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { - "name": "enedis-sge-konnector", - "version": "1.0.0", + "name": "enedissgekonnector", + "version": "1.0.1", "description": "", "repository": { "type": "https", @@ -54,7 +54,7 @@ "xml2js": "^0.4.23" }, "devDependencies": { - "cozy-jobs-cli": "1.19.2", + "cozy-jobs-cli": "1.19.1", "cozy-konnector-build": "1.3.4", "eslint-config-cozy-app": "1.3.3", "eslint-plugin-prettier": "^4.0.0",