diff --git a/index.js b/index.js index 21ef1498af85853d7d31feb29f20e17dcba0171c..c114502833870bd64859f43d2b28993f63d38947 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, @@ -173,7 +174,7 @@ async function start(fields, cozyParameters) { await saveAccountData(this.accountId, { ...accountData.data, consentId: consent.ID, - inseeCode: user.inseeCode, + expirationDate: contractEndDate, }) } else { log('info', 'Alternate start...') @@ -188,8 +189,7 @@ async function start(fields, cozyParameters) { baseUrl, apiAuthKey, sgeLogin, - true, - accountData.data.inseeCode + true ) if (!userConsent) { @@ -228423,14 +228423,9 @@ async function verifyUserIdentity( baseUrl, apiAuthKey, loginUtilisateur, - isAlternateStart = false, - inseeCode = '' + isAlternateStart = false ) { - // If first start get InseeCode - log('debug', 'verifyUserIdentity') - if (!isAlternateStart) { - inseeCode = await getInseeCode(fields.postalCode, fields.city) - } + const inseeCode = await getInseeCode(fields.postalCode, fields.city) const pdl = await findUserPdl( `${baseUrl}/enedis_SDE_recherche-point/1.0`, @@ -228484,7 +228479,6 @@ async function getInseeCode(postalCode, city) { try { log('info', `Query getInseeCode for postalCode ${postalCode} / ${city}`) const response = await axios.get(`${API_URL}/${postalCode}`) - console.log('pouet') if (response.data.length === 1) { return response.data[0].codeCommune @@ -228530,11 +228524,6 @@ async function saveAccountData(accountId, accountData) { return account } -/** - * Return account - * @param {string} accountId - * @returns {Account} - */ async function getAccount(accountId) { log('info', `getAccount: ${accountId}`) const accounts = await cozyClient.data.findAll('io.cozy.accounts') @@ -228543,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 } /***/ }), @@ -228558,7 +228558,18 @@ function isLocal() { ) } -module.exports = { isLocal } +/** + * Verify if it's an alpha URL + * @returns {boolean} + */ +function isDev() { + return ( + process.env.COZY_URL.includes('alpha') || + process.env.COZY_URL.includes('cozy.tools') + ) +} + +module.exports = { isLocal, isDev } /***/ }) diff --git a/onDeleteAccount.js b/onDeleteAccount.js index db90e7610e6a209669de5147a0d37efec0afed43..1124abd34c50ea0ae639b0ebb2050bddf320ff1d 100644 --- a/onDeleteAccount.js +++ b/onDeleteAccount.js @@ -227423,11 +227423,6 @@ async function saveAccountData(accountId, accountData) { return account } -/** - * Return account - * @param {string} accountId - * @returns {Account} - */ async function getAccount(accountId) { log('info', `getAccount: ${accountId}`) const accounts = await cozyClient.data.findAll('io.cozy.accounts') @@ -227436,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 } /***/ }), @@ -227451,7 +227457,18 @@ function isLocal() { ) } -module.exports = { isLocal } +/** + * Verify if it's an alpha URL + * @returns {boolean} + */ +function isDev() { + return ( + process.env.COZY_URL.includes('alpha') || + process.env.COZY_URL.includes('cozy.tools') + ) +} + +module.exports = { isLocal, isDev } /***/ }), @@ -227460,28 +227477,32 @@ 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, isDev } = __webpack_require__(1605) 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 : '{}' ) @@ -227497,6 +227518,8 @@ async function onDeleteAccount() { 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) { @@ -227505,14 +227528,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 (!isDev()) { + 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 @@ -227563,6 +227589,7 @@ function getAccountId() { function getAccountRev() { log('info', `getAccountRev`) + log('info', `getAccountRev: ${JSON.stringify(process.env.COZY_FIELDS)}`) try { return isLocal() ? 'fakeAccountRev' @@ -227578,6 +227605,7 @@ function getAccountRev() { * @returns {Fields} */ function getAccountSecret() { + log('info', `getAccountSecret`) try { return isLocal() ? JSON.parse(process.env.COZY_FIELDS)