Newer
Older

Bastien DUMONT
committed
// @ts-check
const { log, errors } = require('cozy-konnector-libs')
const {
getAccountRev,
getAccountSecret,
getAccountId,
} = require('./helpers/account')

Bastien DUMONT
committed
const { getBoConsent, deleteBoConsent } = require('./requests/bo')
const { terminateContract } = require('./core/contractTermination')

Bastien DUMONT
committed
const moment = require('moment')
require('moment-timezone')
moment.locale('fr') // set the language
moment.tz.setDefault('Europe/Paris') // set the timezone
// const ACCOUNT_ID = isLocal() ? 'default_account_id' : 'enedis-sge-grandlyon'

Bastien DUMONT
committed
async function onDeleteAccount() {
log('info', 'Deleting account ...')
log('info', 'Getting secrets ...')

Bastien DUMONT
committed
const accountRev = getAccountRev()
if (accountRev) {
log('info', 'Account rev exist')
// Parse local info for deletion test
if (isLocal()) {
const fields = JSON.parse(
process.env.COZY_FIELDS ? process.env.COZY_FIELDS : '{}'
)
process.env.COZY_FIELDS = JSON.stringify({
...fields,
...accountData.auth,
})
}
const secrets = getAccountSecret()

Bastien DUMONT
committed
const userConsent = await getBoConsent(
secrets.boBaseUrl,
secrets.boToken,
accountData.data.consentId
)
if (userConsent.ID && userConsent.pointID) {
log('log', `Consent ${userConsent.ID} found for user`)
// Verify if it's dev env to prevent delete of real data
log('info', isAlpha())
if (userConsent.serviceID && !isAlpha()) {

Bastien DUMONT
committed
await deleteBoConsent(
secrets.boBaseUrl,
secrets.boToken,
userConsent.ID
)
await terminateContract(
secrets.wso2BaseUrl,
secrets.apiToken,

Bastien DUMONT
committed
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
secrets.sgeLogin,
secrets.contractId,
userConsent.pointID,
userConsent.serviceID
)
} else {
log('error', `No service id retrieved from BO`)
throw errors.VENDOR_DOWN
}
}
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 }