diff --git a/src/core/contractVerification.js b/src/core/contractVerification.js index 1dd96cf37c8fe01918786ad311cfbb6ff1e3c55e..abc62f013b4ea520e4cd92b80f1fc0cf6a2c435e 100644 --- a/src/core/contractVerification.js +++ b/src/core/contractVerification.js @@ -37,7 +37,7 @@ async function verifyContract(url, apiAuthKey, appLogin, contractId, pointId) { }) try { - const currentContracts = parseContracts(parsedReply)[0] + const currentContracts = parseContracts(parsedReply) let currentContract = null if (Array.isArray(currentContracts)) { currentContract = parseContracts(parsedReply)[0] @@ -45,7 +45,8 @@ async function verifyContract(url, apiAuthKey, appLogin, contractId, pointId) { currentContract = parseContracts(parsedReply) } if ( - currentContract.etatCode === contractState.ACTIF && + (currentContract.etatCode === contractState.ACTIF || + currentContract.etatCode === contractState.DEMANDE) && currentContract.serviceSouscritLibelle === contractLibelle.ACTIF ) return currentContract.serviceSouscritId diff --git a/src/core/types/enum.js b/src/core/types/enum.js index e1cf0f322c54a41361a23d390f055d06b88532c6..485c3f76c0769b016cb906053566b47fd965a1ca 100644 --- a/src/core/types/enum.js +++ b/src/core/types/enum.js @@ -6,6 +6,7 @@ const contractState = { TERMINE: 'TERMINE', ACTIF: 'ACTIF', + DEMANDE: 'DEMANDE', } /** diff --git a/src/core/types/types.js b/src/core/types/types.js index 326b4e6dc084163be96d48154fd3965b80b8b676..f857db4cbf2419239eb6569fb6c7df5728aafb9c 100644 --- a/src/core/types/types.js +++ b/src/core/types/types.js @@ -24,19 +24,21 @@ * @property {string} contractId * @property {number} pointId * @property {string} lastname + * @property {string} boBaseUrl + * @property {string} boToken */ /** * Consent definition * @typedef {object} Consent - * @property {number} pointId + * @property {number} pointID * @property {string} lastname * @property {string} firstname * @property {string} address * @property {string} postalCode * @property {string} inseeCode * @property {number} [serviceId] - * @property {number} [id] + * @property {number} [ID] */ /** diff --git a/src/index.js b/src/index.js index eacaca76d0524cfea61cbd387070d7bc84958c16..876e025d5ee61b26f80f6cf3cbd8e05471d6ae57 100644 --- a/src/index.js +++ b/src/index.js @@ -99,6 +99,8 @@ async function start(fields, cozyParameters) { const user = await verifyUserIdentity(fields, baseUrl, apiAuthKey, sgeLogin) let consent = await createBoConsent( + fields.boBaseUrl, + fields.boToken, pointId, user.lastname, user.firstname, @@ -141,18 +143,27 @@ async function start(fields, cozyParameters) { contractEndDate ) } - consent = await updateBoConsent(consent, serviceId) + consent = await updateBoConsent( + fields.boBaseUrl, + fields.boToken, + consent, + serviceId.toString() + ) // Save bo id into account const accountData = await getAccount(ACCOUNT_ID) await saveAccountData(this.accountId, { ...accountData.data, - consentId: consent.id, + consentId: consent.ID, }) } else { // AlternateStart const accountData = await getAccount(ACCOUNT_ID) - const userConsent = await getBoConsent(accountData.data.consentId) + const userConsent = await getBoConsent( + fields.boBaseUrl, + fields.boToken, + accountData.data.consentId + ) const user = await verifyUserIdentity(fields, baseUrl, apiAuthKey, sgeLogin) if ( @@ -170,7 +181,11 @@ async function start(fields, cozyParameters) { fields.pointId, userConsent.serviceId ) - await deleteBoConsent() + await deleteBoConsent( + fields.boBaseUrl, + fields.boToken, + userConsent.ID ? userConsent.ID : 0 + ) } else { log('error', `No service id retrieved from BO`) throw errors.VENDOR_DOWN diff --git a/src/requests/bo.js b/src/requests/bo.js index ab4ed16ba3446ebc16794297e8dfda0970cfefbb..5cbc03d23368a5220872deb7437e47cc275b3efe 100644 --- a/src/requests/bo.js +++ b/src/requests/bo.js @@ -1,75 +1,126 @@ // @ts-check -const { log } = require('cozy-konnector-libs') +const { log, errors } = require('cozy-konnector-libs') +const { default: axios } = require('axios') /** - * @param {number} pointId + * @param {number} pointID * @param {string} lastname * @param {string} firstname * @param {string} address * @param {string} postalCode * @param {string} inseeCode - * @returns {Consent} + * @returns {Promise<Consent>} */ -function createBoConsent( - pointId, +async function createBoConsent( + url, + token, + pointID, lastname, firstname, address, postalCode, inseeCode ) { - //TODO: Implement log('info', `Query createBoConsent`) - return { - id: 1, - pointId, - lastname, - firstname, - address, - postalCode, - inseeCode, + const headers = { + headers: { + Authorization: `Bearer ${token}`, + }, + } + try { + const { data } = await axios.post( + `${url}/consent`, + { + pointID, + lastname, + firstname, + address, + postalCode, + inseeCode, + }, + headers + ) + return data + } catch (e) { + log('error', `BO replied with ${e}`) + throw errors.MAINTENANCE } } /** + * @param {string} url + * @param {string} token * @param {Consent} consent - * @param {number} serviceId - * @returns {Consent} + * @param {string} serviceId + * @returns {Promise<Consent>} */ -function updateBoConsent(consent, serviceId) { - //TODO: Implement +async function updateBoConsent(url, token, consent, serviceId) { log('info', `Query updateBoConsent`) - return { - ...consent, - serviceId: serviceId, + const headers = { + headers: { + Authorization: `Bearer ${token}`, + }, + } + + try { + const { data } = await axios.put( + `${url}/consent/${consent.ID?.toString()}`, + { + ...consent, + serviceId: parseInt(serviceId), + }, + headers + ) + return data + } catch (e) { + console.log(e) + log('error', `BO replied with ${e}`) + throw errors.MAINTENANCE } } /** * @param {number} boId - * @returns {Consent} + * @returns {Promise<Consent>} */ -function getBoConsent(boId) { +async function getBoConsent(url, token, boId) { //TODO: Implement log('info', `Query getBoConsent ${boId}`) - return { - pointId: 1234, - lastname: 'SUBTIL', - firstname: 'hugo', - address: 'mad', - postalCode: '69007', - inseeCode: '69383', - serviceId: 1234, + 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 } - // throw errors.VENDOR_DOWN } + /** - * + * Delete BO consent + * @param {string} url + * @param {string} token + * @param {number} boId + * @returns */ -function deleteBoConsent() { - //TODO: deleteBoConsent - log('info', `Query deleteBoConsent`) - throw new Error('Function not implemented.') +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 = {