93 lines
3.0 KiB
JavaScript
93 lines
3.0 KiB
JavaScript
const axios = require('axios')
|
|
const findProperty = require('./findProperty')
|
|
const requestConfigHeader = require('./requestConfigHeader')
|
|
|
|
/**
|
|
* Atualiza o Status de um Lead no Salesforce reutilizando as configs do template.
|
|
* - Usa a URL do createContactRecord (sobjects/Lead) para derivar o endpoint de update.
|
|
* - Reaproveita requestConfigHeader para montar headers com o token OAuth2 atual.
|
|
*
|
|
* Parâmetros:
|
|
* - rest: objeto crmRest do template carregado
|
|
* - authentication: credenciais presentes no template
|
|
* - leadId: Id do Lead (Salesforce)
|
|
* - newStatus: novo valor do campo Status (string não vazia)
|
|
* - companyId: usado em logs e controle de token
|
|
*/
|
|
async function updateLeadStatus(rest, authentication, leadId, newStatus = 'Working', companyId = '') {
|
|
|
|
if (!leadId || typeof leadId !== 'string' || leadId.trim().length === 0) {
|
|
console.log('updateLeadStatus: leadId inválido. Abortando.')
|
|
return false
|
|
}
|
|
|
|
if (!newStatus || typeof newStatus !== 'string' || newStatus.trim().length === 0) {
|
|
console.log('updateLeadStatus: newStatus inválido. Abortando.')
|
|
return false
|
|
}
|
|
|
|
const createContactRecord = findProperty(rest, 'createContactRecord')
|
|
|
|
if (!createContactRecord) {
|
|
console.log('updateLeadStatus: createContactRecord não encontrado no template. Abortando atualização de Status.')
|
|
return false
|
|
}
|
|
|
|
const { request } = createContactRecord
|
|
|
|
const baseUrl = request?.url
|
|
if (!baseUrl || !baseUrl.includes('/sobjects/Lead')) {
|
|
console.log('updateLeadStatus: URL base inválida para Lead. Abortando.')
|
|
return false
|
|
}
|
|
|
|
const updateUrl = `${baseUrl}/${leadId}`
|
|
|
|
const { type, userName, passWord, token, crmClientId } = authentication
|
|
const requestType = 'Patch'
|
|
const requestContentType = 'application/json'
|
|
|
|
let config = await requestConfigHeader(
|
|
updateUrl,
|
|
'17999999999',
|
|
requestType,
|
|
requestContentType,
|
|
type,
|
|
userName,
|
|
passWord,
|
|
token,
|
|
crmClientId,
|
|
{ Status: newStatus },
|
|
'',
|
|
companyId
|
|
)
|
|
|
|
// Garante método e payload corretos
|
|
config = { ...config, ...{ method: 'Patch', url: updateUrl, data: { Status: newStatus } } }
|
|
|
|
try {
|
|
console.log('updateLeadStatus: Enviando PATCH para atualizar Status do Lead', JSON.stringify({ url: updateUrl, data: config.data }, null, 6))
|
|
await axios(config)
|
|
return true
|
|
} catch (error) {
|
|
if (error.response) {
|
|
console.error('==================> updateLeadStatus Erro na resposta da API:', {
|
|
status: error.response.status,
|
|
data: error.response.data,
|
|
})
|
|
}
|
|
else if (error.request) {
|
|
console.error('==================> updateLeadStatus Nenhuma resposta recebida da API:', error.request)
|
|
}
|
|
else {
|
|
console.error('==================> updateLeadStatus Erro ao configurar a request:', error.message)
|
|
}
|
|
return false
|
|
}
|
|
}
|
|
|
|
module.exports = updateLeadStatus
|
|
|
|
|
|
|