-
Rémi PAILHAREY authoredRémi PAILHAREY authored
grdf.js 3.68 KiB
// @ts-check
const { default: Axios } = require('axios')
const { errors, log } = require('cozy-konnector-libs')
const qs = require('qs')
const Sentry = require('@sentry/node')
/**
*
* @param {string} client_id
* @param {string} client_secret
* @returns {Promise<{ access_token: string, scope: string, token_type: string, expires_in: number }>}
*/
async function getAuthToken(client_id, client_secret) {
log('info', 'getAuthToken')
const body = {
scope: '/adict/v2',
grant_type: 'client_credentials',
client_id: client_id,
client_secret: client_secret
}
try {
const response = await Axios({
method: 'POST',
url:
'https://sofit-sso-oidc.grdf.fr/openam/oauth2/realms/externeGrdf/access_token',
headers: { 'content-type': 'application/x-www-form-urlencoded' },
data: qs.stringify(body)
})
return response.data
} catch (error) {
log('error', 'Error inside getAuthToken', error)
Sentry.captureException('Error while getting auth token', {
tags: {
section: 'getAuthToken'
}
})
throw errors.VENDOR_DOWN
}
}
/**
*
* @param {string} bearerToken
* @param {string} pce
* @returns {Promise<GRDFConsent[]>}
*/
async function getConsents(bearerToken, pce) {
try {
const response = await fetch('https://api.grdf.fr/adict/v2/droits_acces', {
method: 'POST',
headers: {
Authorization: `Bearer ${bearerToken}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({ id_pce: [pce] })
})
const rawData = await response.text()
const consents = []
if (rawData !== null && rawData !== '') {
// @ts-ignore
rawData.match(/.+/g).map(rawConsent => {
const consent = JSON.parse(rawConsent)
if (consent.id_droit_acces) {
consents.push(consent)
}
})
}
log('info', `Found ${consents.length} consents for pce`)
const etatDroitsAcces = consents.map(consent => consent.etat_droit_acces)
log('info', `Droits d'accès des consentements trouvés: ${etatDroitsAcces}`)
return consents
} catch (error) {
log('error', 'Error while getting consent')
log('error', error)
Sentry.captureException('Error while getting consent', {
tags: {
section: 'getConsents'
}
})
throw errors.VENDOR_DOWN
}
}
async function createGRDFConsent({
bearerToken,
pce,
email,
lastname,
postalCode,
startDate,
endDate
}) {
try {
log('info', `Creating GRDF consent from ${startDate} to ${endDate}`)
const response = await Axios({
method: 'PUT',
url: `https://api.grdf.fr/adict/v2/pce/${pce}/droit_acces`,
headers: { Authorization: `Bearer ${bearerToken}` },
data: {
role_tiers: 'AUTORISE_CONTRAT_FOURNITURE',
raison_sociale: '',
nom_titulaire: lastname,
code_postal: postalCode,
courriel_titulaire: email,
// numero_telephone_mobile_titulaire: '',
date_debut_droit_acces: startDate,
perim_donnees_conso_debut: startDate,
date_fin_droit_acces: endDate,
perim_donnees_conso_fin: endDate,
perim_donnees_contractuelles: 'Vrai',
perim_donnees_techniques: 'Vrai',
perim_donnees_informatives: 'Vrai',
perim_donnees_publiees: 'Vrai'
}
})
log('info', response.data.message_retour_traitement)
return true
} catch (error) {
log('error', `Failed to create GRDF consent`)
log('error', error.response.data)
Sentry.captureException('Failed to create GRDF consent', {
tags: {
section: 'createGRDFConsent'
}
})
throw errors.USER_ACTION_NEEDED_CGU_FORM
}
}
module.exports = { createGRDFConsent, getAuthToken, getConsents }