refactor: Implemented solution so solve 429 status request on in finding open hubspot ticket for a contact id
parent
16410ff993
commit
c4b2e8aac9
|
@ -384,15 +384,15 @@ const createTicket = async (req, res) => {
|
||||||
mustContainProperties(req, ['companyId', 'crmPhone'])
|
mustContainProperties(req, ['companyId', 'crmPhone'])
|
||||||
|
|
||||||
const crmTicketLinks = await ticketCRM(companyId, crmPhone)
|
const crmTicketLinks = await ticketCRM(companyId, crmPhone)
|
||||||
.catch(function (error) {
|
// .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 })
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,35 @@
|
||||||
|
const getHubspotPipelines = require("./getHubspotPipelines")
|
||||||
|
const getHubspotTicketStatusByContact = require("./getHubspotTicketStatusByContact")
|
||||||
|
|
||||||
|
|
||||||
|
async function findTicketOpenHubspotByContact(authentication, contact) {
|
||||||
|
const { token } = authentication
|
||||||
|
|
||||||
|
console.log('contact: ', contact)
|
||||||
|
|
||||||
|
const pipelines = await getHubspotPipelines(token)
|
||||||
|
const tickets = await getHubspotTicketStatusByContact(token, contact.contactId)
|
||||||
|
|
||||||
|
let open_tickets = []
|
||||||
|
|
||||||
|
for (let pipeline of pipelines.results) {
|
||||||
|
const open = pipeline.stages.filter((s) => s.metadata.ticketState.toLowerCase() == "open")
|
||||||
|
|
||||||
|
if (open && open.length > 0) {
|
||||||
|
open_tickets = [...open_tickets, ...open];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const ticket_ids = open_tickets.map((t) => t.id)
|
||||||
|
|
||||||
|
if (ticket_ids && ticket_ids.length > 0 && tickets?.results?.length > 0) {
|
||||||
|
|
||||||
|
const ticket = tickets.results.find(t => ticket_ids.includes(t.properties.hs_pipeline_stage))
|
||||||
|
|
||||||
|
return ticket
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = findTicketOpenHubspotByContact
|
|
@ -0,0 +1,48 @@
|
||||||
|
const axios = require('axios')
|
||||||
|
|
||||||
|
async function getHubspotTicketStatusByContact(token, contactId,) {
|
||||||
|
try {
|
||||||
|
const config = {
|
||||||
|
method: 'post',
|
||||||
|
url: `https://api.hubapi.com/crm/v3/objects/tickets/search`,
|
||||||
|
headers: {
|
||||||
|
'Authorization': `Bearer ${token}`,
|
||||||
|
"Content-Type": "application/json"
|
||||||
|
},
|
||||||
|
data: {
|
||||||
|
"filterGroups": [
|
||||||
|
{
|
||||||
|
"filters": [
|
||||||
|
{
|
||||||
|
"propertyName": "associations.contact",
|
||||||
|
"operator": "EQ",
|
||||||
|
"value": `${contactId}`
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"propertyName": "hs_pipeline_stage",
|
||||||
|
"operator": "NEQ",
|
||||||
|
"value": "open"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"properties": [
|
||||||
|
"hs_pipeline_stage",
|
||||||
|
"subject"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const { data } = await axios(config)
|
||||||
|
|
||||||
|
console.log('============ data: ', JSON.stringify(data, null, 6))
|
||||||
|
return data
|
||||||
|
} catch (error) {
|
||||||
|
console.error(error)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = getHubspotTicketStatusByContact
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -167,8 +167,6 @@ async function lookupContactByPhone(rest, authentication, crmPhone, companyId, t
|
||||||
contactId: { $ne: auxContactId }
|
contactId: { $ne: auxContactId }
|
||||||
})
|
})
|
||||||
|
|
||||||
console.log(" ====== CONTACTS: ", JSON.stringify(contacts, null, 6))
|
|
||||||
|
|
||||||
if (contacts && contacts.length > 0) {
|
if (contacts && contacts.length > 0) {
|
||||||
|
|
||||||
for (const contact of contacts) {
|
for (const contact of contacts) {
|
||||||
|
|
|
@ -13,6 +13,8 @@ const CRM_Ticket = require('../models/CRM_Ticket')
|
||||||
|
|
||||||
const getHubspotPipelines = require('./getHubspotPipelines')
|
const getHubspotPipelines = require('./getHubspotPipelines')
|
||||||
const { pipeline } = require('stream')
|
const { pipeline } = require('stream')
|
||||||
|
const findTicketOpenHubspotByContact = require('./findTicketOpenHubspotByContact')
|
||||||
|
const lookupContactByPhone = require('./lookupCRMContactByPhone')
|
||||||
|
|
||||||
|
|
||||||
async function lookupCrmTicket(rest, authentication, crmPhone, companyId, test = {}, ticketId) {
|
async function lookupCrmTicket(rest, authentication, crmPhone, companyId, test = {}, ticketId) {
|
||||||
|
@ -102,34 +104,21 @@ async function lookupCrmTicket(rest, authentication, crmPhone, companyId, test =
|
||||||
console.log(`[${new Date()}] 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)
|
|
||||||
|
|
||||||
let pipeline
|
const contact = await lookupContactByPhone(rest, authentication, crmPhone, companyId)
|
||||||
|
|
||||||
for (let pip of pipelines.results) {
|
if (contact) {
|
||||||
pipeline = pip.stages.find(({ id }) => id == auxTicketStatus)
|
const ticket = await findTicketOpenHubspotByContact(authentication, contact)
|
||||||
|
console.log('=========> OPEN TICKET 1: ', JSON.stringify(ticket, null, 6))
|
||||||
|
|
||||||
if (pipeline) {
|
if (ticket && Object.keys(ticket).length != 0) {
|
||||||
const { ticketState } = pipeline.metadata
|
console.log('**** TICKET ESTA ABERTO 1 ****')
|
||||||
const { label } = pipeline
|
return { auxTicketId: ticket.id, auxTicketStatus: 1 }
|
||||||
|
|
||||||
console.log(pipeline)
|
|
||||||
|
|
||||||
pipeline = { ticketState, label }
|
|
||||||
break
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log('=========> Hubspot pipeline: ', pipeline)
|
return { auxTicketId, auxTicketStatus }
|
||||||
|
|
||||||
const { ticketState, label } = pipeline
|
|
||||||
|
|
||||||
// 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
|
|
||||||
if (ticketState == 'OPEN' /*&& label != "Closed"*/) {
|
|
||||||
console.log('**** TICKET ESTA ABERTO ****')
|
|
||||||
return { auxTicketId, auxTicketStatus: 1 }
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -14,6 +14,8 @@ const getHubspotTicketStatus = require('./getHubspotTicketStatus')
|
||||||
const getHubspotTicket = require('./getHubspotTicket')
|
const getHubspotTicket = require('./getHubspotTicket')
|
||||||
const getHubspotPipelines = require('./getHubspotPipelines')
|
const getHubspotPipelines = require('./getHubspotPipelines')
|
||||||
const mongoose = require('mongoose')
|
const mongoose = require('mongoose')
|
||||||
|
const getHubspotTicketStatusByContact = require('./getHubspotTicketStatusByContact')
|
||||||
|
const findTicketOpenHubspotByContact = require('./findTicketOpenHubspotByContact')
|
||||||
|
|
||||||
async function ticketCRM(companyId, crmPhone, crmAgent = "0000", crmFirstName = 'Username') {
|
async function ticketCRM(companyId, crmPhone, crmAgent = "0000", crmFirstName = 'Username') {
|
||||||
|
|
||||||
|
@ -83,7 +85,6 @@ async function ticketCRM(companyId, crmPhone, crmAgent = "0000", crmFirstName =
|
||||||
|
|
||||||
const { ticketId } = result
|
const { ticketId } = result
|
||||||
|
|
||||||
// Closed é a label padrão do ticket no hubspot
|
|
||||||
await sincTicket(authentication, contact, companyId, crm, obj_contact, ticketId)
|
await sincTicket(authentication, contact, companyId, crm, obj_contact, ticketId)
|
||||||
|
|
||||||
ticket_id = ticketId
|
ticket_id = ticketId
|
||||||
|
@ -192,41 +193,15 @@ async function sincTicket(authentication, contact, companyId, crm, obj_contact,
|
||||||
}
|
}
|
||||||
|
|
||||||
async function findTicketOpenHubspot(authentication, contact) {
|
async function findTicketOpenHubspot(authentication, contact) {
|
||||||
const { token } = authentication
|
|
||||||
const data = await getHubspotTicket(token, contact.contactId)
|
|
||||||
|
|
||||||
const pipelines = await getHubspotPipelines(token)
|
const ticket = await findTicketOpenHubspotByContact(authentication, contact)
|
||||||
|
console.log('=========> OPEN TICKET 2: ', JSON.stringify(ticket, null, 6))
|
||||||
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 }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
if (ticket && Object.keys(ticket).length != 0) {
|
||||||
|
console.log('**** TICKET ESTA ABERTO 2 ****')
|
||||||
|
return { ticketId: ticket.id, auxTicketStatus: 1 }
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function _lookupContact(rest, authentication, crmPhone, companyId, crmFirstName, url) {
|
async function _lookupContact(rest, authentication, crmPhone, companyId, crmFirstName, url) {
|
||||||
|
|
Loading…
Reference in New Issue