Skip to content
Snippets Groups Projects
onDeleteAccount.js 2.76 KiB
Newer Older
  • Learn to ignore specific revisions
  • // @ts-check
    const { log, errors } = require('cozy-konnector-libs')
    
    Hugo SUBTIL's avatar
    Hugo SUBTIL committed
    const {
      getAccountRev,
      getAccountSecret,
      getAccountId,
    } = require('./helpers/account')
    
    const { getBoConsent, deleteBoConsent } = require('./requests/bo')
    
    Hugo SUBTIL's avatar
    Hugo SUBTIL committed
    const { terminateContract } = require('./core/contractTermination')
    
    Hugo SUBTIL's avatar
    Hugo SUBTIL committed
    const { getAccountForDelete } = require('./requests/cozy')
    
    const moment = require('moment')
    require('moment-timezone')
    moment.locale('fr') // set the language
    moment.tz.setDefault('Europe/Paris') // set the timezone
    
    Hugo SUBTIL's avatar
    Hugo SUBTIL committed
    const { isLocal, isAlpha } = require('./helpers/env')
    
    Hugo SUBTIL's avatar
    Hugo SUBTIL committed
    // const ACCOUNT_ID = isLocal() ? 'default_account_id' : 'enedis-sge-grandlyon'
    
    Hugo SUBTIL's avatar
    Hugo SUBTIL committed
      if (!isAlpha()) {
    
    Hugo SUBTIL's avatar
    Hugo SUBTIL committed
        log('info', 'Deleting account ...')
        log('info', 'Getting secrets ...')
        const ACCOUNT_ID = getAccountId()
        const accountRev = getAccountRev()
    
    Hugo SUBTIL's avatar
    Hugo SUBTIL committed
        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
    
    Hugo SUBTIL's avatar
    Hugo SUBTIL committed
          if (userConsent.ID && userConsent.pointID) {
            log('log', `Consent ${userConsent.ID} found for user`)
            if (userConsent.serviceID) {
              await deleteBoConsent(
                secrets.boBaseUrl,
                secrets.boToken,
                userConsent.ID
    
    Hugo SUBTIL's avatar
    Hugo SUBTIL committed
              )
    
    Hugo SUBTIL's avatar
    Hugo SUBTIL committed
              // 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
    
    Hugo SUBTIL's avatar
    Hugo SUBTIL committed
            }
    
    Hugo SUBTIL's avatar
    Hugo SUBTIL committed
          log('info', 'Deleting account succeed')
        } else {
          log(
            'error',
            'No account revision was found, something went wrong during the deletion of said account'
          )
          throw errors.VENDOR_DOWN
        }
    
      }
    }
    
    onDeleteAccount().then(
      () => {
        log('info', `onDeleteAccount: Successfully delete consent and account.`)
      },
      err => {
        log(
          'error',
          `onDeleteAccount: An error occured during script: ${err.message}`
        )
        throw errors.VENDOR_DOWN
      }
    )
    
    module.exports = { onDeleteAccount }