diff --git a/onDeleteAccount.js b/onDeleteAccount.js index 464128d9deba064f9bc7bbcd9c2c2c6e3ea95a57..a4c24ccc176deebb7a5e6614d7e4e99bd965adb3 100644 --- a/onDeleteAccount.js +++ b/onDeleteAccount.js @@ -271178,7 +271178,7 @@ function parseRequest(event, req, options = {}) { /* 2048 */ /***/ (function(module, exports, __webpack_require__) { -const { cozyClient, log, errors } = __webpack_require__(1) +const { cozyClient, log } = __webpack_require__(1) const getAccountId = __webpack_require__(1767) const getAccountRev = __webpack_require__(2049) const getAccountSecret = __webpack_require__(2050) @@ -271315,47 +271315,51 @@ async function onDeleteAccount(accountId) { let backoff = INITIAL_BACKOFF let accountDeleted = false log('info', 'Deleting account ...') - log('info', 'Getting secrets ...') while (retries < MAX_RETRIES && !accountDeleted) { try { const accountRev = getAccountRev() - if (accountRev) { - let oauthAccount = await getAccessToken(accountId, accountRev) - let fetchedNewAccessToken = oauthAccount.oauth.access_token - if (moment().diff(oauthAccount.oauth.expires_at) > 0) { - log('info', 'expired token, need a new one') - // token is expired, need a new one. - // we request a new one from a client_credentials query - // with credentials secrets from account-type - const accountSecret = getAccountSecret() - if (accountSecret) { - fetchedNewAccessToken = await fetchNewAccessToken(accountSecret) - } else { - throw new Error( - 'Access Token is expired and konnector failed to get a new one' - ) - } - } - let accessRights = await fetchDroitAccessGrdf( - fetchedNewAccessToken, - oauthAccount.oauth_callback_results.pce - ) - // remove unwanted commas - accessRights = accessRights.toString().replace(/,\s*$/, '') - if (accessRights) { - accountDeleted = await deleteUserConsent( - fetchedNewAccessToken, - accessRights - ) - } else { - log('debug', 'No active consent') - throw new Error('No active access right was found for given user') - } - } else { + if (!accountRev) { throw new Error( 'No account revision was found, something went wrong during the deletion of said account' ) } + + const oauthAccount = await getAccessToken(accountId, accountRev) + let fetchedNewAccessToken = oauthAccount.oauth.access_token + + if (moment().diff(oauthAccount.oauth.expires_at) <= 0) { + log('info', 'Token still valid, no need to fetch a new one') + } else { + log('info', 'expired token, need a new one') + // token is expired, need a new one. + // we request a new one from a client_credentials query + // with credentials secrets from account-type + const accountSecret = getAccountSecret() + if (!accountSecret) { + throw new Error( + 'Access Token is expired and konnector failed to get a new one' + ) + } + fetchedNewAccessToken = await fetchNewAccessToken(accountSecret) + } + + let accessRights = await fetchDroitAccessGrdf( + fetchedNewAccessToken, + oauthAccount.oauth_callback_results.pce + ) + + // remove unwanted commas + accessRights = accessRights.toString().replace(/,\s*$/, '') + + if (!accessRights) { + log('debug', 'No active consent') + throw new Error('No active access right was found for given user') + } + + accountDeleted = await deleteUserConsent( + fetchedNewAccessToken, + accessRights + ) } catch (error) { // If the API call fails, log the error and retry with an increased backoff interval log( @@ -271389,7 +271393,6 @@ onDeleteAccount(accountId).then( const errorMessage = `onDeleteAccount GRDF: An error occurred during script: ${err.message}` log('error', errorMessage) Sentry.captureException(errorMessage) - throw errors.VENDOR_DOWN } )