const axios = require('axios')

async function getSalesforceUser(token, email, url) {
  try {

    const parsedUrl = new URL(url);
    const baseUrl = `${parsedUrl.protocol}//${parsedUrl.hostname}`;

    const config = {
      method: 'get',
      url: `${baseUrl}/services/data/v59.0/query/?q=SELECT+Id+FROM+User+WHERE+Email='${email}'`,
      headers: {
        'Authorization': `Bearer ${token}`,
        "Content-Type": "application/json"
      }
    }

    const { data } = await axios(config)
    return data
  } catch (error) {
    if (error.response) {
      console.error('==================> getSalesforceUser Erro na resposta da API:', {
        status: error.response.status,
        data: error.response.data,
      })
    }
    else if (error.request) {
      console.error('==================> getSalesforceUser Nenhuma resposta recebida da API:', error.request)
    }
    else {
      console.error('==================> getSalesforceUser Erro ao configurar a request:', error.message)
    }
  }
}

module.exports = getSalesforceUser



