diff --git a/index.js b/index.js index 79e06651d91eb735f54abfe16c900973e90c142c..b5b91171ecd910c5d9c052bc77a533100f8fe715 100644 --- a/index.js +++ b/index.js @@ -161,6 +161,7 @@ async function start(fields, cozyParameters) { contractEndDate ) } + console.log('testgf') consent = await updateBoConsent( boBaseUrl, boToken, @@ -228531,7 +228532,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 } /***/ }), @@ -228546,7 +228558,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 37f98e38efae710d7d326709dc72a44bc3bd7678..b77b00bbea71cd07789f5cf043650de4e19af7b7 100644 --- a/onDeleteAccount.js +++ b/onDeleteAccount.js @@ -227431,7 +227431,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 } /***/ }), @@ -227446,7 +227457,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 } /***/ }), @@ -227455,28 +227474,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 : '{}' ) @@ -227492,6 +227516,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) { @@ -227500,14 +227526,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 @@ -227558,6 +227587,7 @@ function getAccountId() { function getAccountRev() { log('info', `getAccountRev`) + log('info', `getAccountRev: ${JSON.stringify(process.env.COZY_FIELDS)}`) try { return isLocal() ? 'fakeAccountRev' @@ -227573,6 +227603,7 @@ function getAccountRev() { * @returns {Fields} */ function getAccountSecret() { + log('info', `getAccountSecret`) try { return isLocal() ? JSON.parse(process.env.COZY_FIELDS)