Newer
Older
// @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')
/**
* @return {Promise<string | null>} User Pdl
*/
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,
}
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,
})
try {
return parseUserPdl(parsedReply)
} catch (error) {
throw errors.LOGIN_FAILED
}