Skip to content
Snippets Groups Projects
onDeleteAccount.js 7.96 MiB
Newer Older
  • Learn to ignore specific revisions
  • } = __webpack_require__(1732)
    
    const { getBoConsent, deleteBoConsent } = __webpack_require__(1563)
    
    const { terminateContract } = __webpack_require__(1684)
    const { getAccountForDelete } = __webpack_require__(1691)
    
    const moment = __webpack_require__(1379)
    __webpack_require__(1516)
    
    const { isLocal, isDev } = __webpack_require__(1692)
    const Sentry = __webpack_require__(1601)
    
    
    Hugo SUBTIL's avatar
    Hugo SUBTIL committed
    moment.locale('fr') // set the language
    moment.tz.setDefault('Europe/Paris') // set the timezone
    
    async function onDeleteAccount() {
    
      try {
        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 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 : '{}'
            )
            process.env.COZY_FIELDS = JSON.stringify({
              ...fields,
              ...accountData.auth,
            })
          }
          const secrets = getAccountSecret()
    
          const userConsent = await getBoConsent(
            secrets.boBaseUrl,
            secrets.boToken,
            accountData.data.consentId
          )
    
          log('info', `isAlpha: ${isDev()}`)
          log('info', `userConsent: ${JSON.stringify(userConsent)}`)
          if (userConsent.ID && userConsent.pointID) {
            log('log', `Consent ${userConsent.ID} found for user`)
            if (userConsent.serviceID) {
              await deleteBoConsent(
                secrets.boBaseUrl,
                secrets.boToken,
                userConsent.ID
    
              // Verify if it's dev env to prevent delete of real data
              if (!isDev()) {
                await terminateContract(
                  secrets.wso2BaseUrl,
                  secrets.apiToken,
                  secrets.sgeLogin,
                  secrets.contractId,
                  userConsent.pointID,
                  userConsent.serviceID
                )
              }
            } else {
              const errorMessage = `No service id retrieved from BO`
              log('error', errorMessage)
              Sentry.captureException(errorMessage)
              throw errors.VENDOR_DOWN
    
          log('info', 'Deleting account succeed')
        } else {
          const errorMessage =
            'No account revision was found, something went wrong during the deletion of said account'
          log('error', errorMessage)
          Sentry.captureException(errorMessage)
          throw errors.VENDOR_DOWN
        }
      } catch (error) {
        log('debug', 'error catched in onDeleteAccount()', error)
        await Sentry.flush()
        throw error
    
    Hugo SUBTIL's avatar
    Hugo SUBTIL committed
      }
    }
    
    onDeleteAccount().then(
      () => {
        log('info', `onDeleteAccount: Successfully delete consent and account.`)
      },
      err => {
    
        const errorMessage = `onDeleteAccount: An error occurred during script: ${err.message}`
        log('error', errorMessage)
        Sentry.captureException(errorMessage)
    
    Hugo SUBTIL's avatar
    Hugo SUBTIL committed
        throw errors.VENDOR_DOWN
      }
    )
    
    module.exports = { onDeleteAccount }
    
    
    /***/ }),
    
    /* 1732 */
    
    Hugo SUBTIL's avatar
    Hugo SUBTIL committed
    /***/ ((module, __unused_webpack_exports, __webpack_require__) => {
    
    const { log } = __webpack_require__(1)
    
    const { isLocal } = __webpack_require__(1692)
    const Sentry = __webpack_require__(1601)
    
    Hugo SUBTIL's avatar
    Hugo SUBTIL committed
    
    function getAccountId() {
      log('info', `getAccountId`)
      try {
        return JSON.parse(process.env.COZY_FIELDS).account
      } catch (err) {
    
        const errorMessage = `You must provide 'account' in COZY_FIELDS: ${err.message}`
        Sentry.captureException(errorMessage)
        throw new Error(errorMessage)
    
    Hugo SUBTIL's avatar
    Hugo SUBTIL committed
      }
    }
    
    function getAccountRev() {
      log('info', `getAccountRev`)
    
      log('info', `getAccountRev: ${JSON.stringify(process.env.COZY_FIELDS)}`)
    
    Hugo SUBTIL's avatar
    Hugo SUBTIL committed
      try {
    
        return isLocal()
    
    Hugo SUBTIL's avatar
    Hugo SUBTIL committed
          ? 'fakeAccountRev'
          : JSON.parse(process.env.COZY_FIELDS).account_rev
      } catch (err) {
    
        const errorMessage = `You must provide 'account' in COZY_FIELDS: ${err.message}`
        Sentry.captureException(errorMessage)
        throw new Error(errorMessage)
    
    Hugo SUBTIL's avatar
    Hugo SUBTIL committed
      }
    }
    
    /**
     * Return account secrets.
     * For local testing, change value with values from your konnector-dev-config.json
    
    Hugo SUBTIL's avatar
    Hugo SUBTIL committed
     */
    function getAccountSecret() {
    
      log('info', `getAccountSecret`)
    
    Hugo SUBTIL's avatar
    Hugo SUBTIL committed
      try {
    
        return isLocal()
    
          ? JSON.parse(process.env.COZY_FIELDS)
    
    Hugo SUBTIL's avatar
    Hugo SUBTIL committed
          : JSON.parse(process.env.COZY_PARAMETERS).secret
      } catch (err) {
    
        const errorMessage = `You must provide 'account-types' in COZY_PARAMETERS: ${err.message}`
        Sentry.captureException(errorMessage)
        throw new Error(errorMessage)
    
    Hugo SUBTIL's avatar
    Hugo SUBTIL committed
      }
    }
    module.exports = { getAccountId, getAccountRev, getAccountSecret }
    
    
    /***/ })
    /******/ 	]);
    /************************************************************************/
    /******/ 	// The module cache
    /******/ 	var __webpack_module_cache__ = {};
    /******/ 	
    /******/ 	// The require function
    /******/ 	function __webpack_require__(moduleId) {
    /******/ 		// Check if module is in cache
    /******/ 		var cachedModule = __webpack_module_cache__[moduleId];
    /******/ 		if (cachedModule !== undefined) {
    /******/ 			return cachedModule.exports;
    /******/ 		}
    /******/ 		// Create a new module (and put it into the cache)
    /******/ 		var module = __webpack_module_cache__[moduleId] = {
    /******/ 			id: moduleId,
    /******/ 			loaded: false,
    /******/ 			exports: {}
    /******/ 		};
    /******/ 	
    /******/ 		// Execute the module function
    /******/ 		__webpack_modules__[moduleId].call(module.exports, module, module.exports, __webpack_require__);
    /******/ 	
    /******/ 		// Flag the module as loaded
    /******/ 		module.loaded = true;
    /******/ 	
    /******/ 		// Return the exports of the module
    /******/ 		return module.exports;
    /******/ 	}
    /******/ 	
    /******/ 	// expose the module cache
    /******/ 	__webpack_require__.c = __webpack_module_cache__;
    /******/ 	
    /************************************************************************/
    /******/ 	/* webpack/runtime/compat get default export */
    /******/ 	(() => {
    /******/ 		// getDefaultExport function for compatibility with non-harmony modules
    /******/ 		__webpack_require__.n = (module) => {
    /******/ 			var getter = module && module.__esModule ?
    /******/ 				() => (module['default']) :
    /******/ 				() => (module);
    /******/ 			__webpack_require__.d(getter, { a: getter });
    /******/ 			return getter;
    /******/ 		};
    /******/ 	})();
    /******/ 	
    /******/ 	/* webpack/runtime/define property getters */
    /******/ 	(() => {
    /******/ 		// define getter functions for harmony exports
    /******/ 		__webpack_require__.d = (exports, definition) => {
    /******/ 			for(var key in definition) {
    /******/ 				if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {
    /******/ 					Object.defineProperty(exports, key, { enumerable: true, get: definition[key] });
    /******/ 				}
    /******/ 			}
    /******/ 		};
    /******/ 	})();
    /******/ 	
    
    /******/ 	/* webpack/runtime/harmony module decorator */
    /******/ 	(() => {
    /******/ 		__webpack_require__.hmd = (module) => {
    /******/ 			module = Object.create(module);
    /******/ 			if (!module.children) module.children = [];
    /******/ 			Object.defineProperty(module, 'exports', {
    /******/ 				enumerable: true,
    /******/ 				set: () => {
    /******/ 					throw new Error('ES Modules may not assign module.exports or exports.*, Use ESM export syntax, instead: ' + module.id);
    /******/ 				}
    /******/ 			});
    /******/ 			return module;
    /******/ 		};
    /******/ 	})();
    /******/ 	
    
    Hugo SUBTIL's avatar
    Hugo SUBTIL committed
    /******/ 	/* webpack/runtime/hasOwnProperty shorthand */
    /******/ 	(() => {
    /******/ 		__webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop))
    /******/ 	})();
    /******/ 	
    /******/ 	/* webpack/runtime/make namespace object */
    /******/ 	(() => {
    /******/ 		// define __esModule on exports
    /******/ 		__webpack_require__.r = (exports) => {
    /******/ 			if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
    /******/ 				Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
    /******/ 			}
    /******/ 			Object.defineProperty(exports, '__esModule', { value: true });
    /******/ 		};
    /******/ 	})();
    /******/ 	
    /******/ 	/* webpack/runtime/node module decorator */
    /******/ 	(() => {
    /******/ 		__webpack_require__.nmd = (module) => {
    /******/ 			module.paths = [];
    /******/ 			if (!module.children) module.children = [];
    /******/ 			return module;
    /******/ 		};
    /******/ 	})();
    /******/ 	
    /************************************************************************/
    /******/ 	
    /******/ 	// module cache are used so entry inlining is disabled
    /******/ 	// startup
    /******/ 	// Load entry module and return exports
    
    /******/ 	var __webpack_exports__ = __webpack_require__(__webpack_require__.s = 1731);
    
    Hugo SUBTIL's avatar
    Hugo SUBTIL committed
    /******/ 	
    /******/ })()
    ;