Skip to content
Snippets Groups Projects
bo.js 2.61 KiB
Newer Older
  • Learn to ignore specific revisions
  • Bastien DUMONT's avatar
    Bastien DUMONT committed
    // @ts-check
    
    const { log, errors } = require('cozy-konnector-libs')
    const { default: axios } = require('axios')
    
     * @param {number} pointID
     * @param {string} lastname
     * @param {string} firstname
     * @param {string} address
     * @param {string} postalCode
     * @param {string} inseeCode
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
     * @param {string} city
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
     */
    
    async function createBoConsent(
      url,
      token,
      pointID,
      lastname,
      firstname,
      address,
      postalCode,
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
      inseeCode,
      city
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
      log('info', `Query createBoConsent`)
    
      const headers = {
        headers: {
          Authorization: `Bearer ${token}`,
        },
      }
    
      try {
        const { data } = await axios.post(
          `${url}/consent`,
          {
            pointID,
            lastname,
            firstname,
            address,
            postalCode,
            inseeCode,
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
            city,
    
          },
          headers
        )
        return data
      } catch (e) {
        log('error', `BO replied with ${e}`)
        throw errors.MAINTENANCE
      }
    
     * @param {string} url
     * @param {string} token
     * @param {Consent} consent
     * @param {string} serviceId
     * @returns {Promise<Consent>}
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
     */
    
    async function updateBoConsent(url, token, consent, serviceId) {
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
      log('info', `Query updateBoConsent`)
    
      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
      }
    
     * @param {number} boId
     * @returns {Promise<Consent>}
    
    Bastien DUMONT's avatar
    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's avatar
    Bastien DUMONT committed
    /**
    
     * Delete BO consent
     * @param {string} url
     * @param {string} token
     * @param {number} boId
     * @returns
    
    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
      }
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
    }
    
    module.exports = {
      createBoConsent,
      updateBoConsent,
      getBoConsent,
      deleteBoConsent,
    }