Newer
Older

Bastien DUMONT
committed
const { log, errors } = require('cozy-konnector-libs')
const { default: axios } = require('axios')

Bastien DUMONT
committed
* @param {number} pointID
* @param {string} lastname
* @param {string} firstname
* @param {string} address
* @param {string} postalCode
* @param {string} inseeCode

Bastien DUMONT
committed
* @returns {Promise<Consent>}

Bastien DUMONT
committed
async function createBoConsent(
url,
token,
pointID,
lastname,
firstname,
address,
postalCode,

Bastien DUMONT
committed
) {

Bastien DUMONT
committed
const headers = {
headers: {
Authorization: `Bearer ${token}`,
},
}
try {
const { data } = await axios.post(
`${url}/consent`,
{
pointID,
lastname,
firstname,
address,
postalCode,
inseeCode,

Bastien DUMONT
committed
},
headers
)
return data
} catch (e) {
log('error', `BO replied with ${e}`)
throw errors.MAINTENANCE
}

Bastien DUMONT
committed
* @param {string} url
* @param {string} token
* @param {Consent} consent
* @param {string} serviceId
* @returns {Promise<Consent>}

Bastien DUMONT
committed
async function updateBoConsent(url, token, consent, serviceId) {

Bastien DUMONT
committed
const headers = {
headers: {
Authorization: `Bearer ${token}`,
},
}
try {
let consentId = ''
if (consent.ID) {
consentId = consent.ID.toString()
}
const { data } = await axios.put(
`${url}/consent/${consentId}`,
{
...consent,
serviceId: parseInt(serviceId),
},
headers
)
return data
} catch (e) {
log('error', `BO replied with ${e}`)
throw errors.MAINTENANCE
}

Bastien DUMONT
committed
* @param {number} boId
* @returns {Promise<Consent>}

Bastien DUMONT
committed
async function getBoConsent(url, token, boId) {
log('info', `Query getBoConsent ${boId}`)
const headers = {
headers: {
Authorization: `Bearer ${token}`,
},
}
try {
const { data } = await axios.get(`${url}/consent/${boId}`, headers)
return data
} catch (e) {
log('error', `BO replied with ${e}`)
throw errors.MAINTENANCE
}

Bastien DUMONT
committed

Bastien DUMONT
committed
* Delete BO consent
* @param {string} url
* @param {string} token
* @param {number} boId
* @returns

Bastien DUMONT
committed
async function deleteBoConsent(url, token, boId) {
log('info', `Query deleteBoConsent ${boId}`)
const headers = {
headers: {
Authorization: `Bearer ${token}`,
},
}
try {
const { data } = await axios.delete(`${url}/consent/${boId}`, headers)
return data
} catch (e) {
log('error', `BO replied with ${e}`)
throw errors.MAINTENANCE
}
}
module.exports = {
createBoConsent,
updateBoConsent,
getBoConsent,
deleteBoConsent,
}