feat: Sentry added for error and log monitoring
parent
3e204da937
commit
16410ff993
|
@ -1,7 +1,9 @@
|
||||||
|
require("./utils/instrument.js")
|
||||||
|
const Sentry = require('@sentry/node')
|
||||||
|
|
||||||
require('dotenv').config()
|
require('dotenv').config()
|
||||||
require('express-async-errors')
|
require('express-async-errors')
|
||||||
|
|
||||||
|
|
||||||
const { socketIO } = require('./utils')
|
const { socketIO } = require('./utils')
|
||||||
const { initIO, getIO } = socketIO
|
const { initIO, getIO } = socketIO
|
||||||
|
|
||||||
|
@ -10,15 +12,15 @@ const express = require('express')
|
||||||
const app = express()
|
const app = express()
|
||||||
const session = require('express-session')
|
const session = require('express-session')
|
||||||
|
|
||||||
const bodyParser = require('body-parser'); // Para JSON
|
const bodyParser = require('body-parser') // Para JSON
|
||||||
require('body-parser-xml')(bodyParser); // Para XML
|
require('body-parser-xml')(bodyParser) // Para XML
|
||||||
|
|
||||||
// rest of the packages
|
// rest of the packages
|
||||||
const morgan = require('morgan')
|
const morgan = require('morgan')
|
||||||
// const fileUpload = require('express-fileupload')
|
// const fileUpload = require('express-fileupload')
|
||||||
|
|
||||||
// Middleware para lidar com JSON
|
// Middleware para lidar com JSON
|
||||||
app.use(bodyParser.json());
|
app.use(bodyParser.json())
|
||||||
|
|
||||||
// Middleware para lidar com XML
|
// Middleware para lidar com XML
|
||||||
app.use(bodyParser.xml({
|
app.use(bodyParser.xml({
|
||||||
|
@ -26,7 +28,7 @@ app.use(bodyParser.xml({
|
||||||
xmlParseOptions: {
|
xmlParseOptions: {
|
||||||
explicitArray: false,
|
explicitArray: false,
|
||||||
}
|
}
|
||||||
}));
|
}))
|
||||||
|
|
||||||
const rateLimiter = require('express-rate-limit')
|
const rateLimiter = require('express-rate-limit')
|
||||||
|
|
||||||
|
@ -77,6 +79,7 @@ app.use('/api-docs', swaggerUI.serve, swaggerUI.setup(swaggerDocument))
|
||||||
|
|
||||||
app.use('/api/v1/crm', crmRouter)
|
app.use('/api/v1/crm', crmRouter)
|
||||||
|
|
||||||
|
Sentry.setupExpressErrorHandler(app)
|
||||||
app.use(notFoundMiddlware)
|
app.use(notFoundMiddlware)
|
||||||
// app.use(errorHandlerMiddleware)
|
// app.use(errorHandlerMiddleware)
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
|
|
||||||
const path = require('path')
|
const path = require('path')
|
||||||
|
const Sentry = require('@sentry/node')
|
||||||
const omnihitV2Integration = require('../data/omihitV2IntegrationCRM.json')
|
const omnihitV2Integration = require('../data/omihitV2IntegrationCRM.json')
|
||||||
const { StatusCodes } = require("http-status-codes")
|
const { StatusCodes } = require("http-status-codes")
|
||||||
const { createCRMContact,
|
const { createCRMContact,
|
||||||
|
@ -133,6 +134,8 @@ const callJournaling = async (req, res) => {
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
// console.log(`[ERROR - ${new Date()}] Erro no Call Journaling`, error?.response?.data)
|
// console.log(`[ERROR - ${new Date()}] Erro no Call Journaling`, error?.response?.data)
|
||||||
|
|
||||||
|
Sentry.captureException(error)
|
||||||
|
|
||||||
if (error.response) {
|
if (error.response) {
|
||||||
console.error('==================> callJournaling Erro na resposta da API:', {
|
console.error('==================> callJournaling Erro na resposta da API:', {
|
||||||
status: error.response.status,
|
status: error.response.status,
|
||||||
|
@ -380,15 +383,16 @@ const createTicket = async (req, res) => {
|
||||||
|
|
||||||
mustContainProperties(req, ['companyId', 'crmPhone'])
|
mustContainProperties(req, ['companyId', 'crmPhone'])
|
||||||
|
|
||||||
const crmTicketLinks = await ticketCRM(companyId, crmPhone).catch(function (error) {
|
const crmTicketLinks = await ticketCRM(companyId, crmPhone)
|
||||||
|
.catch(function (error) {
|
||||||
|
|
||||||
console.error(`Error on create ticket: companyID ${companyId} | crmPhone: ${crmPhone}`)
|
console.error(`Error on create ticket: companyID ${companyId} | crmPhone: ${crmPhone}`)
|
||||||
console.error(error?.response?.data)
|
console.error(error?.response?.data)
|
||||||
console.error(error?.response?.status)
|
console.error(error?.response?.status)
|
||||||
console.error(error?.response?.headers)
|
console.error(error?.response?.headers)
|
||||||
|
|
||||||
throw new Error(`Error on create ticket: companyID ${companyId} | crmPhone: ${crmPhone}`)
|
throw new Error(`Error on create ticket: companyID ${companyId} | crmPhone: ${crmPhone}`)
|
||||||
})
|
})
|
||||||
|
|
||||||
return res.status(StatusCodes.OK).json({ crmTicketLinks })
|
return res.status(StatusCodes.OK).json({ crmTicketLinks })
|
||||||
}
|
}
|
||||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -10,6 +10,8 @@
|
||||||
"author": "",
|
"author": "",
|
||||||
"license": "ISC",
|
"license": "ISC",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"@sentry/node": "^9.9.0",
|
||||||
|
"@sentry/profiling-node": "^9.9.0",
|
||||||
"axios": "^1.6.1",
|
"axios": "^1.6.1",
|
||||||
"body-parser-xml": "^2.0.5",
|
"body-parser-xml": "^2.0.5",
|
||||||
"cors": "^2.8.5",
|
"cors": "^2.8.5",
|
||||||
|
|
|
@ -15,11 +15,16 @@ async function createTicket(companyId, rest, authentication, crmPhone, crmFirstN
|
||||||
let ticketUrl = ''
|
let ticketUrl = ''
|
||||||
let { request, body, response } = findProperty(rest, 'createTicketRecord')
|
let { request, body, response } = findProperty(rest, 'createTicketRecord')
|
||||||
|
|
||||||
|
console.log('==============> crmContactId: ', crmContactId)
|
||||||
|
|
||||||
const { requestContentType, requestEncoding, requestType, responseType, url } = request
|
const { requestContentType, requestEncoding, requestType, responseType, url } = request
|
||||||
|
|
||||||
|
console.log('========> body1: ', JSON.stringify(body, null, 6))
|
||||||
|
|
||||||
body = flatten(body)
|
body = flatten(body)
|
||||||
|
|
||||||
|
console.log('========> body2: ', JSON.stringify(body, null, 6))
|
||||||
|
|
||||||
const mapping = {
|
const mapping = {
|
||||||
crmFirstName,
|
crmFirstName,
|
||||||
crmLastName,
|
crmLastName,
|
||||||
|
@ -45,10 +50,12 @@ async function createTicket(companyId, rest, authentication, crmPhone, crmFirstN
|
||||||
|
|
||||||
body = unflatten(body)
|
body = unflatten(body)
|
||||||
|
|
||||||
|
console.log('========> body3: ', JSON.stringify(body, null, 6))
|
||||||
|
|
||||||
const { type, userName, passWord, token, crmClientId } = authentication
|
const { type, userName, passWord, token, crmClientId } = authentication
|
||||||
|
|
||||||
//url, crmPhone, requestType, requestContentType, type, userName, passWord, token, crmClientId, data = '', ticketId = '', companyId
|
//url, crmPhone, requestType, requestContentType, type, userName, passWord, token, crmClientId, data = '', ticketId = '', companyId
|
||||||
const config = await requestConfigHeader(url, crmPhone, requestType, requestContentType, type, userName, passWord, token, crmClientId, body, '', companyId)
|
const config = await requestConfigHeader(url, crmPhone, requestType, requestContentType, type, userName, passWord, token, crmClientId, body, '', companyId)
|
||||||
|
|
||||||
if (test?.testing) {
|
if (test?.testing) {
|
||||||
msg = `Tentanto criar ticket do numero ${crmPhone} no crm`
|
msg = `Tentanto criar ticket do numero ${crmPhone} no crm`
|
||||||
|
@ -58,7 +65,37 @@ async function createTicket(companyId, rest, authentication, crmPhone, crmFirstN
|
||||||
// console.log('===============> createTicket: ', JSON.stringify(config, null, 6))
|
// console.log('===============> createTicket: ', JSON.stringify(config, null, 6))
|
||||||
|
|
||||||
|
|
||||||
let { data } = await axios(config)
|
|
||||||
|
|
||||||
|
|
||||||
|
let resp
|
||||||
|
|
||||||
|
try {
|
||||||
|
|
||||||
|
console.log("========> createTicket PAYLOAD: ", JSON.stringify(config, null, 6))
|
||||||
|
|
||||||
|
resp = await axios(config)
|
||||||
|
|
||||||
|
} catch (error) {
|
||||||
|
|
||||||
|
if (error.response) {
|
||||||
|
console.error('==================> createTicket Erro na resposta da API:', {
|
||||||
|
status: error.response.status,
|
||||||
|
data: error.response.data,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
else if (error.request) {
|
||||||
|
console.error('==================> createTicket Nenhuma resposta recebida da API:', error?.request)
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
console.error('==================> createTicket Erro ao configurar a request:', error?.message)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (error?.response?.status == 404)
|
||||||
|
return { error: 404 }
|
||||||
|
}
|
||||||
|
|
||||||
|
let { data } = resp
|
||||||
|
|
||||||
data = flatten(data)
|
data = flatten(data)
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,24 @@
|
||||||
|
const axios = require('axios')
|
||||||
|
|
||||||
|
async function getHubspotTicket(token, contactId,) {
|
||||||
|
try {
|
||||||
|
const config = {
|
||||||
|
method: 'get',
|
||||||
|
url: `https://api.hubapi.com/crm/v3/objects/contacts/${contactId}/associations/tickets`,
|
||||||
|
headers: {
|
||||||
|
'Authorization': `Bearer ${token}`,
|
||||||
|
"Content-Type": "application/json"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const { data } = await axios(config)
|
||||||
|
return data
|
||||||
|
} catch (error) {
|
||||||
|
console.error(error)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = getHubspotTicket
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,24 @@
|
||||||
|
const axios = require('axios')
|
||||||
|
|
||||||
|
async function getHubspotTicketStatus(token, ticketId,) {
|
||||||
|
try {
|
||||||
|
const config = {
|
||||||
|
method: 'get',
|
||||||
|
url: `https://api.hubapi.com/crm/v3/objects/tickets/${ticketId}`,
|
||||||
|
headers: {
|
||||||
|
'Authorization': `Bearer ${token}`,
|
||||||
|
"Content-Type": "application/json"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const { data } = await axios(config)
|
||||||
|
return data
|
||||||
|
} catch (error) {
|
||||||
|
console.error(error)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = getHubspotTicketStatus
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,16 @@
|
||||||
|
|
||||||
|
const path = require('path')
|
||||||
|
const Sentry = require("@sentry/node")
|
||||||
|
require('dotenv').config()
|
||||||
|
const { nodeProfilingIntegration } = require("@sentry/profiling-node")
|
||||||
|
|
||||||
|
Sentry.init({
|
||||||
|
dsn: process.env.SENTRY_DSN,
|
||||||
|
tracesSampleRate: 1.0,
|
||||||
|
profilesSampleRate: 1.0,
|
||||||
|
debug: false,
|
||||||
|
integrations: [
|
||||||
|
nodeProfilingIntegration(),
|
||||||
|
],
|
||||||
|
})
|
||||||
|
|
|
@ -9,19 +9,21 @@ const { getAccessToken } = require('./oauth2')
|
||||||
const findProperty = require('./findProperty')
|
const findProperty = require('./findProperty')
|
||||||
const requestConfigHeader = require('./requestConfigHeader')
|
const requestConfigHeader = require('./requestConfigHeader')
|
||||||
const sendMessageSocket = require('./sendMessageSocket')
|
const sendMessageSocket = require('./sendMessageSocket')
|
||||||
|
const CRM_Ticket = require('../models/CRM_Ticket')
|
||||||
|
|
||||||
async function lookupContactByPhone(rest, authentication, crmPhone, companyId, test = {}, cacheContact = true) {
|
async function lookupContactByPhone(rest, authentication, crmPhone, companyId, test = {}, cacheContact = false) {
|
||||||
|
|
||||||
let { request, body, response } = findProperty(rest, 'lookupContactByPhone')
|
let { request, body, response } = findProperty(rest, 'lookupContactByPhone')
|
||||||
|
|
||||||
let { requestContentType, requestEncoding, requestType, responseType, url } = request
|
let { requestContentType, requestEncoding, requestType, responseType, url } = request
|
||||||
|
|
||||||
|
|
||||||
const { type, userName, passWord, token, crmClientId } = authentication
|
const { type, userName, passWord, token, crmClientId } = authentication
|
||||||
|
|
||||||
const crmInfo = await CRM_Contact.findOne({ companyId, crmBaseURL: new URL(url).hostname, phone: crmPhone })
|
if (cacheContact) {
|
||||||
|
const crmInfo = await CRM_Contact.findOne({ companyId, crmBaseURL: new URL(url).hostname, phone: crmPhone })
|
||||||
|
|
||||||
if (crmInfo) return { exist: true, contactId: crmInfo.contactId, phone: crmPhone }
|
if (crmInfo) return { exist: true, contactId: crmInfo.contactId, phone: crmPhone }
|
||||||
|
}
|
||||||
|
|
||||||
const config = await requestConfigHeader(url, crmPhone, requestType, requestContentType, type, userName, passWord, token, crmClientId, '', '', companyId)
|
const config = await requestConfigHeader(url, crmPhone, requestType, requestContentType, type, userName, passWord, token, crmClientId, '', '', companyId)
|
||||||
|
|
||||||
|
@ -30,6 +32,8 @@ async function lookupContactByPhone(rest, authentication, crmPhone, companyId, t
|
||||||
sendMessageSocket({ companyId, status: 'processing', data: { request: config, msg } })
|
sendMessageSocket({ companyId, status: 'processing', data: { request: config, msg } })
|
||||||
}
|
}
|
||||||
|
|
||||||
|
console.log("PAYLOAD CONFIG LOOKUP CONTACT BY PHONE: ", JSON.stringify(config, null, 6))
|
||||||
|
|
||||||
let data
|
let data
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
@ -37,24 +41,61 @@ async function lookupContactByPhone(rest, authentication, crmPhone, companyId, t
|
||||||
data = _data
|
data = _data
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (error.response) {
|
if (error.response) {
|
||||||
console.error('==================> Erro na resposta da API:', {
|
console.error('==================> lookupContactByPhone Erro na resposta da API:', {
|
||||||
status: error.response.status,
|
status: error.response.status,
|
||||||
data: error.response.data,
|
data: error.response.data,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
else if (error.request) {
|
else if (error.request) {
|
||||||
console.error('==================> Nenhuma resposta recebida da API:', error.request)
|
console.error('==================> lookupContactByPhone Nenhuma resposta recebida da API:', error.request)
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
console.error('==================> Erro ao configurar a request:', error.message)
|
console.error('==================> lookupContactByPhone Erro ao configurar a request:', error.message)
|
||||||
}
|
}
|
||||||
|
|
||||||
// throw error
|
// throw error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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 }
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
console.log('DATA: ', JSON.stringify(data, null, 6))
|
|
||||||
|
|
||||||
data = flatten(data)
|
data = flatten(data)
|
||||||
|
|
||||||
|
@ -112,11 +153,48 @@ async function lookupContactByPhone(rest, authentication, crmPhone, companyId, t
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
console.log('---------> auxPhone: ', auxPhone, ' | auxContactId: ', auxContactId)
|
||||||
|
|
||||||
if (auxPhone) {
|
if (auxPhone) {
|
||||||
|
|
||||||
if (auxPhone && auxContactId && cacheContact) {
|
if (auxPhone && auxContactId) {
|
||||||
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 })
|
// Garante apenas 1 contato
|
||||||
|
const contacts = await CRM_Contact.find({
|
||||||
|
companyId,
|
||||||
|
crmBaseURL: new URL(url).hostname,
|
||||||
|
phone: crmPhone,
|
||||||
|
contactId: { $ne: auxContactId }
|
||||||
|
})
|
||||||
|
|
||||||
|
console.log(" ====== CONTACTS: ", JSON.stringify(contacts, null, 6))
|
||||||
|
|
||||||
|
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 })
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return { exist: true, contactId: auxContactId, phone: crmPhone, name: auxName, accountId: auxAccountId }
|
return { exist: true, contactId: auxContactId, phone: crmPhone, name: auxName, accountId: auxAccountId }
|
||||||
|
|
|
@ -99,7 +99,7 @@ async function lookupCrmTicket(rest, authentication, crmPhone, companyId, test =
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log('auxTicketStatus: ', auxTicketStatus, ' | auxTicketId: ', auxTicketId)
|
console.log(`[${new Date()}] auxTicketStatus: ${auxTicketStatus} | auxTicketId: ${auxTicketId}`)
|
||||||
|
|
||||||
if (url.includes('api.hubapi.com')) {
|
if (url.includes('api.hubapi.com')) {
|
||||||
const pipelines = await getHubspotPipelines(token)
|
const pipelines = await getHubspotPipelines(token)
|
||||||
|
@ -122,12 +122,12 @@ async function lookupCrmTicket(rest, authentication, crmPhone, companyId, test =
|
||||||
|
|
||||||
console.log('=========> Hubspot pipeline: ', pipeline)
|
console.log('=========> Hubspot pipeline: ', pipeline)
|
||||||
|
|
||||||
const {ticketState, label} = pipeline
|
const { ticketState, label } = pipeline
|
||||||
|
|
||||||
// o label esta sendo usado aqui poque no hubspot padrao que estamos usando mesmo que o ticket esteja
|
// o label esta sendo usado aqui poque no hubspot padrao que estamos usando mesmo que o ticket esteja
|
||||||
// fechado ele continua com ticketState OPEN ao inves de CLOSED
|
// fechado ele continua com ticketState OPEN ao inves de CLOSED
|
||||||
if (ticketState == 'OPEN' && label != "Closed"){
|
if (ticketState == 'OPEN' /*&& label != "Closed"*/) {
|
||||||
console.log ('**** TICKET ESTA ABERTO ****')
|
console.log('**** TICKET ESTA ABERTO ****')
|
||||||
return { auxTicketId, auxTicketStatus: 1 }
|
return { auxTicketId, auxTicketStatus: 1 }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -10,7 +10,10 @@ const createTicket = require('./createTicket')
|
||||||
const lookupCRMTicket = require('./lookupCRMTicket')
|
const lookupCRMTicket = require('./lookupCRMTicket')
|
||||||
const sendEventTicketCreatedToSocket = require('./sendEventTicketCreatedToSocket')
|
const sendEventTicketCreatedToSocket = require('./sendEventTicketCreatedToSocket')
|
||||||
const journalingRequest = require('./journalingRequest')
|
const journalingRequest = require('./journalingRequest')
|
||||||
|
const getHubspotTicketStatus = require('./getHubspotTicketStatus')
|
||||||
|
const getHubspotTicket = require('./getHubspotTicket')
|
||||||
|
const getHubspotPipelines = require('./getHubspotPipelines')
|
||||||
|
const mongoose = require('mongoose')
|
||||||
|
|
||||||
async function ticketCRM(companyId, crmPhone, crmAgent = "0000", crmFirstName = 'Username') {
|
async function ticketCRM(companyId, crmPhone, crmAgent = "0000", crmFirstName = 'Username') {
|
||||||
|
|
||||||
|
@ -23,8 +26,6 @@ async function ticketCRM(companyId, crmPhone, crmAgent = "0000", crmFirstName =
|
||||||
|
|
||||||
const { crmRest: rest, authentication } = crmConfig.crm
|
const { crmRest: rest, authentication } = crmConfig.crm
|
||||||
|
|
||||||
// console.log("ticketCRM crmRest: ", JSON.stringify(rest, null, 6))
|
|
||||||
|
|
||||||
// Send the ticket url link to hitphone to open on another browser tab
|
// Send the ticket url link to hitphone to open on another browser tab
|
||||||
let obj = findProperty(rest, 'lookupTicket')
|
let obj = findProperty(rest, 'lookupTicket')
|
||||||
|
|
||||||
|
@ -34,7 +35,7 @@ async function ticketCRM(companyId, crmPhone, crmAgent = "0000", crmFirstName =
|
||||||
if (obj) {
|
if (obj) {
|
||||||
|
|
||||||
let { url } = obj.request
|
let { url } = obj.request
|
||||||
let contact = await _lookupContact(rest, authentication, crmPhone, companyId, crmFirstName)
|
let contact = await _lookupContact(rest, authentication, crmPhone, companyId, crmFirstName, url)
|
||||||
|
|
||||||
const crm = await CRM.findOne({
|
const crm = await CRM.findOne({
|
||||||
companyId, crmBaseURL: new URL(url.trim()).hostname
|
companyId, crmBaseURL: new URL(url.trim()).hostname
|
||||||
|
@ -48,8 +49,6 @@ async function ticketCRM(companyId, crmPhone, crmAgent = "0000", crmFirstName =
|
||||||
|
|
||||||
if (obj_ticket) {
|
if (obj_ticket) {
|
||||||
|
|
||||||
console.log('obj_ticket1: ', obj_ticket)
|
|
||||||
|
|
||||||
const { ticketId } = obj_ticket
|
const { ticketId } = obj_ticket
|
||||||
|
|
||||||
if (ticketId) {
|
if (ticketId) {
|
||||||
|
@ -73,8 +72,34 @@ async function ticketCRM(companyId, crmPhone, crmAgent = "0000", crmFirstName =
|
||||||
|
|
||||||
// refactor this for production. For now only working with hubspot where status new is equal 1
|
// refactor this for production. For now only working with hubspot where status new is equal 1
|
||||||
if ((auxTicketStatus && auxTicketStatus != '1') || (error && error == 404)) {
|
if ((auxTicketStatus && auxTicketStatus != '1') || (error && error == 404)) {
|
||||||
|
|
||||||
await CRM_Ticket.deleteMany({ contact: obj_contact })
|
await CRM_Ticket.deleteMany({ contact: obj_contact })
|
||||||
|
|
||||||
|
if (url.includes("hubapi")) {
|
||||||
|
|
||||||
|
const result = await findTicketOpenHubspot(authentication, contact)
|
||||||
|
|
||||||
|
if (result) {
|
||||||
|
|
||||||
|
const { ticketId } = result
|
||||||
|
|
||||||
|
// Closed é a label padrão do ticket no hubspot
|
||||||
|
await sincTicket(authentication, contact, companyId, crm, obj_contact, ticketId)
|
||||||
|
|
||||||
|
ticket_id = ticketId
|
||||||
|
isCreated = false
|
||||||
|
|
||||||
|
crmTicketLinks.push({
|
||||||
|
ticketId: `https://app.hubspot.com/contacts/23636141/ticket/${ticket_id}`,
|
||||||
|
created: isCreated,
|
||||||
|
})
|
||||||
|
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
const { ticketUrl, ticketId } = await _createTicket(rest, crmPhone, companyId, authentication, crmFirstName, contact, crmAgent)
|
const { ticketUrl, ticketId } = await _createTicket(rest, crmPhone, companyId, authentication, crmFirstName, contact, crmAgent)
|
||||||
|
|
||||||
ticket_id = ticketId
|
ticket_id = ticketId
|
||||||
|
@ -86,10 +111,36 @@ async function ticketCRM(companyId, crmPhone, crmAgent = "0000", crmFirstName =
|
||||||
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
console.log('_createTicket xxxxx ')
|
|
||||||
|
|
||||||
|
if (url.includes("hubapi")) {
|
||||||
|
|
||||||
|
console.log(`[${new Date()}] ****** IS HUBSPOT CRM LETS TRY FINDING AN OPEN TICKET TO THE CONTACT****** `)
|
||||||
|
|
||||||
|
const result = await findTicketOpenHubspot(authentication, contact)
|
||||||
|
|
||||||
|
if (result) {
|
||||||
|
const { ticketId } = result
|
||||||
|
|
||||||
|
// Closed é a label padrão do ticket no hubspot
|
||||||
|
await sincTicket(authentication, contact, companyId, crm, obj_contact, ticketId)
|
||||||
|
|
||||||
|
ticket_id = ticketId
|
||||||
|
isCreated = false
|
||||||
|
|
||||||
|
crmTicketLinks.push({
|
||||||
|
ticketId: `https://app.hubspot.com/contacts/23636141/ticket/${ticket_id}`,
|
||||||
|
created: isCreated,
|
||||||
|
})
|
||||||
|
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log(`[${new Date()}] ****** CREATE TICKET ****** `)
|
||||||
|
|
||||||
|
|
||||||
const { ticketUrl, ticketId } = await _createTicket(rest, crmPhone, companyId, authentication, crmFirstName, contact, crmAgent)
|
const { ticketUrl, ticketId } = await _createTicket(rest, crmPhone, companyId, authentication, crmFirstName, contact, crmAgent)
|
||||||
|
|
||||||
ticket_id = ticketId
|
ticket_id = ticketId
|
||||||
isCreated = true
|
isCreated = true
|
||||||
}
|
}
|
||||||
|
@ -111,7 +162,74 @@ async function ticketCRM(companyId, crmPhone, crmAgent = "0000", crmFirstName =
|
||||||
module.exports = ticketCRM
|
module.exports = ticketCRM
|
||||||
|
|
||||||
|
|
||||||
async function _lookupContact(rest, authentication, crmPhone, companyId, crmFirstName) {
|
async function sincTicket(authentication, contact, companyId, crm, obj_contact, ticketId) {
|
||||||
|
const { token } = authentication
|
||||||
|
const data = await getHubspotTicket(token, contact.contactId)
|
||||||
|
|
||||||
|
if (data && data?.results.length > 0) {
|
||||||
|
|
||||||
|
for (const ticket of data.results) {
|
||||||
|
|
||||||
|
const _ticket = await CRM_Ticket.findOne(
|
||||||
|
{ companyId, crm, ticketId: ticket.id }
|
||||||
|
)
|
||||||
|
|
||||||
|
if (_ticket) {
|
||||||
|
await _ticket.deleteOne()
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
await CRM_Ticket.create({
|
||||||
|
companyId,
|
||||||
|
contact: obj_contact,
|
||||||
|
ticketId: ticketId,
|
||||||
|
crm
|
||||||
|
})
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
async function findTicketOpenHubspot(authentication, contact) {
|
||||||
|
const { token } = authentication
|
||||||
|
const data = await getHubspotTicket(token, contact.contactId)
|
||||||
|
|
||||||
|
const pipelines = await getHubspotPipelines(token)
|
||||||
|
|
||||||
|
console.log(`[FUNCTION - ${new Date()}] getHubspotTicket total de tickets do contato: `, data?.results?.length)
|
||||||
|
|
||||||
|
if (data && data?.results.length > 0) {
|
||||||
|
|
||||||
|
for (const ticket of data.results) {
|
||||||
|
|
||||||
|
const { properties } = await getHubspotTicketStatus(token, ticket.id)
|
||||||
|
|
||||||
|
const { hs_pipeline_stage } = properties
|
||||||
|
|
||||||
|
let pipeline
|
||||||
|
|
||||||
|
for (let pip of pipelines.results) {
|
||||||
|
pipeline = pip.stages.find(({ id }) => id == hs_pipeline_stage)
|
||||||
|
|
||||||
|
if (pipeline) {
|
||||||
|
const { ticketState } = pipeline.metadata
|
||||||
|
const { label } = pipeline
|
||||||
|
|
||||||
|
console.log(`[FUNCTION - ${new Date()}] findTicketOpenHubspot: `, { ticketState, label, ticketId: ticket.id, hs_pipeline_stage })
|
||||||
|
|
||||||
|
if (ticketState == "OPEN")
|
||||||
|
return { ticketState, label, ticketId: ticket.id }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function _lookupContact(rest, authentication, crmPhone, companyId, crmFirstName, url) {
|
||||||
|
|
||||||
let contact = await lookupContactByPhone(rest, authentication, crmPhone, companyId)
|
let contact = await lookupContactByPhone(rest, authentication, crmPhone, companyId)
|
||||||
|
|
||||||
|
@ -120,6 +238,17 @@ async function _lookupContact(rest, authentication, crmPhone, companyId, crmFirs
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!contact?.exist) {
|
if (!contact?.exist) {
|
||||||
|
const contacts = await CRM_Contact.find({ companyId, crmBaseURL: new URL(url).hostname, phone: crmPhone })
|
||||||
|
|
||||||
|
if (contacts) {
|
||||||
|
for (const contact of contacts) {
|
||||||
|
|
||||||
|
await CRM_Ticket.deleteMany({ companyId, contact: contact })
|
||||||
|
|
||||||
|
await contact.deleteOne()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
contact = await createContact(companyId, rest, authentication, crmPhone, crmFirstName)
|
contact = await createContact(companyId, rest, authentication, crmPhone, crmFirstName)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -139,7 +268,6 @@ async function _createTicket(rest, crmPhone, companyId, authentication, crmFirst
|
||||||
msg = `Tentando criar ticket para o contato ${crmPhone}`
|
msg = `Tentando criar ticket para o contato ${crmPhone}`
|
||||||
// return { exist: true, ticketId: auxTicketId, phone: crmPhone, ticketUrl }
|
// return { exist: true, ticketId: auxTicketId, phone: crmPhone, ticketUrl }
|
||||||
|
|
||||||
// console.log('xxxxxxxxxxxxxxxxxxxxxxxxx1')
|
|
||||||
// return { exist: true, ticketId: auxTicketId, phone: crmPhone, ticketUrl }
|
// return { exist: true, ticketId: auxTicketId, phone: crmPhone, ticketUrl }
|
||||||
const { ticketUrl, ticketId, } = await createTicket(companyId,
|
const { ticketUrl, ticketId, } = await createTicket(companyId,
|
||||||
rest,
|
rest,
|
||||||
|
|
Loading…
Reference in New Issue