2024-11-05 18:37:36 +00:00
|
|
|
const loadCRM = require('./loadCRM')
|
|
|
|
const lookupContactByPhone = require('./lookupCRMContactByPhone')
|
|
|
|
const findProperty = require('./findProperty')
|
|
|
|
const axios = require('axios')
|
|
|
|
const requestConfigHeader = require('./requestConfigHeader')
|
|
|
|
|
|
|
|
async function sfcase(companyId, crmPhone) {
|
|
|
|
|
|
|
|
const crmFiles = await loadCRM(companyId)
|
|
|
|
|
|
|
|
let res = { contactId: "" }
|
|
|
|
|
|
|
|
for (const crmConfig of crmFiles) {
|
|
|
|
|
|
|
|
const { crmRest: rest, authentication } = crmConfig.crm
|
|
|
|
|
|
|
|
let createCase = findProperty(rest, 'createCase')
|
|
|
|
|
|
|
|
console.log('===============> createCase: ', createCase)
|
|
|
|
|
|
|
|
if (createCase) {
|
|
|
|
|
|
|
|
let contact = await lookupContactByPhone(rest, authentication, crmPhone, companyId, {}, false)
|
|
|
|
|
|
|
|
console.log('==========> contact: ', contact)
|
|
|
|
|
|
|
|
if (!contact?.exist) {
|
|
|
|
console.log('===============> ContactExist: ', JSON.stringify(contact, null, 6))
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2024-11-28 12:17:54 +00:00
|
|
|
const { contactId, created, accountId } = contact
|
2024-11-05 18:37:36 +00:00
|
|
|
|
|
|
|
let { request, body } = createCase
|
|
|
|
|
|
|
|
console.log("====> request: ", request)
|
|
|
|
console.log("====> body: ", body)
|
|
|
|
|
|
|
|
const { type, userName, passWord, token, crmClientId } = authentication
|
|
|
|
const { requestContentType, requestEncoding, requestType, responseType, url } = request
|
|
|
|
|
2025-01-15 14:53:39 +00:00
|
|
|
let config = await requestConfigHeader(url, crmPhone, requestType, requestContentType, type, userName, passWord, token, crmClientId, body, '', companyId)
|
2024-11-05 18:37:36 +00:00
|
|
|
|
2025-01-15 14:53:39 +00:00
|
|
|
console.log("====> config", config)
|
2024-11-28 12:17:54 +00:00
|
|
|
console.log("====> accountId: ", accountId, " | contactId: ", contactId)
|
|
|
|
|
2025-01-14 18:23:45 +00:00
|
|
|
const complementarInformations = {
|
|
|
|
AccountId: accountId
|
|
|
|
}
|
|
|
|
if (!config?.data?.ContactId && !config?.data?.ContactId === "") {
|
|
|
|
complementarInformations.ContactId = contactId
|
|
|
|
}
|
2024-11-28 12:17:54 +00:00
|
|
|
if (accountId && contactId)
|
|
|
|
config.data = {
|
2025-01-14 18:23:45 +00:00
|
|
|
...config.data,
|
|
|
|
...complementarInformations
|
2024-11-28 12:17:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
console.log("====> create case request config: ", config)
|
2024-11-05 18:37:36 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
const { data } = await axios(config)
|
|
|
|
|
|
|
|
console.log('Data from case created: ', JSON.stringify(data, null, 6))
|
|
|
|
|
|
|
|
res.contactId = contactId
|
|
|
|
res.caseId = data.id
|
|
|
|
|
|
|
|
} catch (error) {
|
2025-01-15 14:53:39 +00:00
|
|
|
// console.log(`CASE CREATE ERROR: `, error)
|
|
|
|
|
|
|
|
if (error.response) {
|
|
|
|
console.error('==================> sfCase Erro na resposta da API:', {
|
|
|
|
status: error.response.status,
|
|
|
|
data: error.response.data,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
else if (error.request) {
|
|
|
|
console.error('==================> sfCase Nenhuma resposta recebida da API:', error.request)
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
console.error('==================> sfCase Erro ao configurar a request:', error.message)
|
|
|
|
}
|
2024-11-05 18:37:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
break
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return res
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2025-01-14 18:23:45 +00:00
|
|
|
module.exports = sfcase
|