Skip to content
Snippets Groups Projects
findUserPdl.js 1.32 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 soapRequest = require('easy-soap-request')
    const { parseUserPdl, parseTags, parseValue } = require('./parsing')
    const { rechercherPoint } = require('./requests/sge')
    const xml2js = require('xml2js')
    
    /**
    
     * @param {string} url
     * @param {string} apiAuthKey
     * @param {string} appLogin
     * @param {string} name
     * @param {string} address
     * @param {string} postalCode
     * @param {string} inseeCode
    
     * @return {Promise<string | null>} User Pdl
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
     */
    async function findUserPdl(
      url,
      apiAuthKey,
      appLogin,
      name,
      address,
      postalCode,
      inseeCode
    ) {
      log('info', 'Fetching user data')
      const sgeHeaders = {
        'Content-Type': 'text/xml;charset=UTF-8',
        apikey: apiAuthKey,
      }
    
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
      const { response } = await soapRequest({
        url: url,
        headers: sgeHeaders,
        xml: rechercherPoint(appLogin, name, postalCode, inseeCode, address),
      }).catch(err => {
        log('error', 'rechercherPointResponse')
        log('error', err)
        throw errors.LOGIN_FAILED
      })
    
      const parsedReply = await xml2js.parseStringPromise(response.body, {
        tagNameProcessors: [parseTags],
        valueProcessors: [parseValue],
        explicitArray: false,
      })
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
        return parseUserPdl(parsedReply)
    
      } catch (error) {
        throw errors.LOGIN_FAILED
      }
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
    }
    
    module.exports = { findUserPdl }