2023-11-29 20:05:48 +00:00
|
|
|
const axios = require('axios')
|
|
|
|
const flatten = require('flat')
|
|
|
|
const CustomError = require('../errors')
|
|
|
|
const CRM_Contact = require('../models/CRM_Contact')
|
|
|
|
const CRM = require('../models/CRM')
|
|
|
|
|
|
|
|
const { URL } = require('url')
|
|
|
|
const { getAccessToken } = require('./oauth2')
|
|
|
|
const findProperty = require('./findProperty')
|
|
|
|
const requestConfigHeader = require('./requestConfigHeader')
|
|
|
|
const sendMessageSocket = require('./sendMessageSocket')
|
|
|
|
|
2024-11-05 18:37:36 +00:00
|
|
|
async function lookupContactByPhone(rest, authentication, crmPhone, companyId, test = {}, cacheContact = true) {
|
2023-11-29 20:05:48 +00:00
|
|
|
|
|
|
|
let { request, body, response } = findProperty(rest, 'lookupContactByPhone')
|
2024-11-05 18:37:36 +00:00
|
|
|
|
2023-11-29 20:05:48 +00:00
|
|
|
let { requestContentType, requestEncoding, requestType, responseType, url } = request
|
|
|
|
|
|
|
|
|
|
|
|
const { type, userName, passWord, token, crmClientId } = authentication
|
|
|
|
|
|
|
|
const crmInfo = await CRM_Contact.findOne({ companyId, crmBaseURL: new URL(url).hostname, phone: crmPhone })
|
2024-11-05 18:37:36 +00:00
|
|
|
|
2023-11-29 20:05:48 +00:00
|
|
|
if (crmInfo) return { exist: true, contactId: crmInfo.contactId, phone: crmPhone }
|
2024-11-05 18:37:36 +00:00
|
|
|
|
2025-01-15 14:53:39 +00:00
|
|
|
const config = await requestConfigHeader(url, crmPhone, requestType, requestContentType, type, userName, passWord, token, crmClientId, '', '', companyId)
|
2024-11-05 18:37:36 +00:00
|
|
|
|
|
|
|
if (test?.testing) {
|
2023-11-29 20:05:48 +00:00
|
|
|
let msg = `Tentanto checar se o contato de numero ${crmPhone} existe no crm`
|
2024-11-05 18:37:36 +00:00
|
|
|
sendMessageSocket({ companyId, status: 'processing', data: { request: config, msg } })
|
2023-11-29 20:05:48 +00:00
|
|
|
}
|
2024-11-05 18:37:36 +00:00
|
|
|
|
2025-01-14 18:23:45 +00:00
|
|
|
let data
|
|
|
|
|
|
|
|
try {
|
|
|
|
let { data: _data } = await axios(config)
|
|
|
|
data = _data
|
|
|
|
} catch (error) {
|
|
|
|
if (error.response) {
|
|
|
|
console.error('==================> Erro na resposta da API:', {
|
|
|
|
status: error.response.status,
|
|
|
|
data: error.response.data,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
else if (error.request) {
|
|
|
|
console.error('==================> Nenhuma resposta recebida da API:', error.request)
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
console.error('==================> Erro ao configurar a request:', error.message)
|
|
|
|
}
|
|
|
|
|
2025-01-15 14:53:39 +00:00
|
|
|
// throw error
|
2025-01-14 18:23:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-11-29 20:05:48 +00:00
|
|
|
|
2024-07-19 18:48:34 +00:00
|
|
|
console.log('DATA: ', JSON.stringify(data, null, 6))
|
|
|
|
|
2023-11-29 20:05:48 +00:00
|
|
|
data = flatten(data)
|
|
|
|
|
|
|
|
let auxPhone
|
|
|
|
let auxContactId
|
2024-11-05 18:37:36 +00:00
|
|
|
let auxName
|
2024-11-28 12:17:54 +00:00
|
|
|
let auxAccountId
|
2023-11-29 20:05:48 +00:00
|
|
|
|
|
|
|
for (const prop in data) {
|
|
|
|
|
|
|
|
const _prop = prop.replace(/^\d+\./, '').replace(/(?:^|\.)\d+\b/g, '')
|
|
|
|
|
|
|
|
if (_prop == response?.phone?.trim()) {
|
|
|
|
auxPhone = data[prop].replace('+', '')
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_prop == response?.id?.trim()) {
|
|
|
|
auxContactId = data[prop]
|
|
|
|
}
|
|
|
|
|
|
|
|
if (auxPhone && auxContactId) break
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-11-05 18:37:36 +00:00
|
|
|
console.log('===========> !auxPhone && !auxContactId: ', !auxPhone && !auxContactId)
|
|
|
|
|
|
|
|
|
2024-07-19 00:41:03 +00:00
|
|
|
if (!auxPhone && !auxContactId) {
|
|
|
|
for (const prop in data) {
|
|
|
|
|
|
|
|
let _prop = prop.replace(/\.(\d+)(\.|$)/g, '[$1]$2')
|
|
|
|
|
2024-11-05 18:37:36 +00:00
|
|
|
// SALESFORCE GETTING THE NAME
|
|
|
|
if (_prop == response?.name?.trim()) {
|
|
|
|
auxName = data[prop]
|
|
|
|
}
|
|
|
|
|
2024-11-28 12:17:54 +00:00
|
|
|
// SALESFORCE GETTING THE ACCOUNT ID
|
|
|
|
if (_prop == response?.accountId?.trim()) {
|
|
|
|
auxAccountId = data[prop]
|
|
|
|
}
|
|
|
|
|
2024-07-19 00:41:03 +00:00
|
|
|
if (_prop == response?.phone?.trim()) {
|
|
|
|
auxPhone = data[prop].replace('+', '')
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_prop == response?.id?.trim()) {
|
|
|
|
auxContactId = data[prop]
|
|
|
|
}
|
|
|
|
|
2024-11-28 12:17:54 +00:00
|
|
|
// SALESFORCE CASE LOOOK UP ALL THE OBJECT PROPERTIES
|
|
|
|
if (!url.includes('salesforce'))
|
|
|
|
if (auxPhone && auxContactId) break
|
2024-07-19 00:41:03 +00:00
|
|
|
|
|
|
|
}
|
2024-11-05 18:37:36 +00:00
|
|
|
}
|
2024-07-19 00:41:03 +00:00
|
|
|
|
2023-11-29 20:05:48 +00:00
|
|
|
if (auxPhone) {
|
|
|
|
|
2024-11-05 18:37:36 +00:00
|
|
|
if (auxPhone && auxContactId && cacheContact) {
|
2023-11-29 20:05:48 +00:00
|
|
|
const crm = await CRM.findOne({ companyId, crmBaseURL: new URL(url).hostname })
|
|
|
|
await CRM_Contact.create({ companyId, crm, crmBaseURL: new URL(url).hostname, contactId: auxContactId, phone: auxPhone })
|
|
|
|
}
|
|
|
|
|
2024-11-28 12:17:54 +00:00
|
|
|
return { exist: true, contactId: auxContactId, phone: crmPhone, name: auxName, accountId: auxAccountId }
|
2024-11-05 18:37:36 +00:00
|
|
|
}
|
2023-11-29 20:05:48 +00:00
|
|
|
|
2024-07-19 18:48:34 +00:00
|
|
|
return { exist: false }
|
2023-11-29 20:05:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = lookupContactByPhone
|
|
|
|
|
|
|
|
|