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')
|
2025-04-03 14:37:20 +00:00
|
|
|
const CRM_Ticket = require('../models/CRM_Ticket')
|
2023-11-29 20:05:48 +00:00
|
|
|
|
2025-04-03 14:37:20 +00:00
|
|
|
async function lookupContactByPhone(rest, authentication, crmPhone, companyId, test = {}, cacheContact = false) {
|
2025-04-10 18:28:38 +00:00
|
|
|
|
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
|
|
|
|
|
2025-04-03 14:37:20 +00:00
|
|
|
if (cacheContact) {
|
|
|
|
const crmInfo = await CRM_Contact.findOne({ companyId, crmBaseURL: new URL(url).hostname, phone: crmPhone })
|
2024-11-05 18:37:36 +00:00
|
|
|
|
2025-04-03 14:37:20 +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-04-03 14:37:20 +00:00
|
|
|
console.log("PAYLOAD CONFIG LOOKUP CONTACT BY PHONE: ", JSON.stringify(config, null, 6))
|
|
|
|
|
2025-01-14 18:23:45 +00:00
|
|
|
let data
|
|
|
|
|
|
|
|
try {
|
|
|
|
let { data: _data } = await axios(config)
|
|
|
|
data = _data
|
2025-04-03 14:37:20 +00:00
|
|
|
} catch (error) {
|
2025-01-14 18:23:45 +00:00
|
|
|
if (error.response) {
|
2025-04-03 14:37:20 +00:00
|
|
|
console.error('==================> lookupContactByPhone Erro na resposta da API:', {
|
2025-01-14 18:23:45 +00:00
|
|
|
status: error.response.status,
|
|
|
|
data: error.response.data,
|
|
|
|
})
|
2025-04-03 14:37:20 +00:00
|
|
|
}
|
2025-01-14 18:23:45 +00:00
|
|
|
else if (error.request) {
|
2025-04-03 14:37:20 +00:00
|
|
|
console.error('==================> lookupContactByPhone Nenhuma resposta recebida da API:', error.request)
|
|
|
|
}
|
2025-01-14 18:23:45 +00:00
|
|
|
else {
|
2025-04-03 14:37:20 +00:00
|
|
|
console.error('==================> lookupContactByPhone Erro ao configurar a request:', error.message)
|
2025-01-14 18:23:45 +00:00
|
|
|
}
|
2025-04-03 14:37:20 +00:00
|
|
|
|
2025-01-15 14:53:39 +00:00
|
|
|
// throw error
|
2025-01-14 18:23:45 +00:00
|
|
|
}
|
|
|
|
|
2025-04-03 14:37:20 +00:00
|
|
|
console.log('CONTACT LOOKUP BY PHONE DATA: ', JSON.stringify(data, null, 6))
|
|
|
|
|
|
|
|
if (url.includes("hubapi") && data?.contacts.length > 1) {
|
|
|
|
|
|
|
|
const auxContatWithName = data.contacts.find((c) => c.properties?.hs_full_name_or_email?.value?.trim().length > 2)
|
|
|
|
|
|
|
|
if (auxContatWithName) {
|
|
|
|
|
|
|
|
console.log(`[${new Date()}] ****** HUBSPOT CONTATO DUPLICADO. CONTACTID ${auxContatWithName.vid} A SER CONSIDERADO ****** `)
|
|
|
|
|
|
|
|
data.contacts = data.contacts = [auxContatWithName]
|
|
|
|
|
|
|
|
const contacts = await CRM_Contact.find({ companyId, crmBaseURL: new URL(url).hostname, phone: crmPhone })
|
|
|
|
|
|
|
|
if (contacts && contacts.length > 1) {
|
|
|
|
|
|
|
|
for (const contact of contacts.slice(0, -1)) {
|
|
|
|
|
|
|
|
await CRM_Ticket.deleteMany({ companyId, contact: contact })
|
|
|
|
|
|
|
|
await contact.deleteOne()
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
console.log(`[${new Date()}] dados do contato duplicado no crm: `, data)
|
|
|
|
|
|
|
|
const updateResult = await CRM_Contact.updateOne(
|
|
|
|
{ companyId, crmBaseURL: new URL(url).hostname, phone: crmPhone },
|
|
|
|
{
|
|
|
|
$set: { contactId: auxContatWithName.vid }
|
|
|
|
})
|
|
|
|
|
|
|
|
if (updateResult.modifiedCount > 0)
|
|
|
|
return { exist: true, contactId: auxContatWithName.vid }
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2025-01-14 18:23:45 +00:00
|
|
|
|
2023-11-29 20:05:48 +00:00
|
|
|
|
2024-07-19 18:48:34 +00:00
|
|
|
|
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
|
|
|
|
2025-04-03 14:37:20 +00:00
|
|
|
console.log('---------> auxPhone: ', auxPhone, ' | auxContactId: ', auxContactId)
|
|
|
|
|
2023-11-29 20:05:48 +00:00
|
|
|
if (auxPhone) {
|
|
|
|
|
2025-04-03 14:37:20 +00:00
|
|
|
if (auxPhone && auxContactId) {
|
|
|
|
|
|
|
|
// Garante apenas 1 contato
|
|
|
|
const contacts = await CRM_Contact.find({
|
|
|
|
companyId,
|
|
|
|
crmBaseURL: new URL(url).hostname,
|
|
|
|
phone: crmPhone,
|
|
|
|
contactId: { $ne: auxContactId }
|
|
|
|
})
|
2025-04-09 18:41:01 +00:00
|
|
|
|
2025-04-03 14:37:20 +00:00
|
|
|
if (contacts && contacts.length > 0) {
|
|
|
|
|
|
|
|
for (const contact of contacts) {
|
|
|
|
|
|
|
|
console.log("=====> DELETING CONTACTS: ", contact)
|
|
|
|
|
|
|
|
await CRM_Ticket.deleteMany({ companyId, contact: contact })
|
|
|
|
|
|
|
|
await contact.deleteOne()
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
//
|
|
|
|
|
|
|
|
const contactInfo = await CRM_Contact.findOne({ companyId, crmBaseURL: new URL(url).hostname, contactId: auxContactId })
|
|
|
|
|
|
|
|
console.log('contactInfo: ', contactInfo, " | crmPhone: ", crmPhone)
|
|
|
|
|
|
|
|
if (!contactInfo) {
|
|
|
|
console.log('----------------> CREATE CONTACT MONGO')
|
|
|
|
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 })
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-11-29 20:05:48 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|