Route to create ticket separated

master
adriano 2025-03-11 17:44:47 -03:00
parent 7c05166f16
commit c3b51baa1c
4 changed files with 78 additions and 35 deletions

View File

@ -116,6 +116,8 @@ const callJournaling = async (req, res) => {
else if (operationStatus == "update-answer") { else if (operationStatus == "update-answer") {
console.log('update-answer') console.log('update-answer')
// Referente a cliente a gabriel, pois os tickets estao sendo abertos diretamente em um botao do hitphone
if (companyId != "14223")
await ticketCRM(companyId, crmPhone, crmAgent, crmFirstName) await ticketCRM(companyId, crmPhone, crmAgent, crmFirstName)
const resp = await redirectContactLinkCRM(companyId, crmPhone, crmAgent, crmFirstName) const resp = await redirectContactLinkCRM(companyId, crmPhone, crmAgent, crmFirstName)
@ -367,6 +369,15 @@ const getCrms = async (req, res) => {
res.status(StatusCodes.OK).send(crms) res.status(StatusCodes.OK).send(crms)
} }
const createTicket = async (req, res) => {
let { companyId, crmPhone } = req.body
const crmTicketLinks = await ticketCRM(companyId, crmPhone)
return res.status(StatusCodes.OK).json({ crmTicketLinks })
}
const webhook = async (req, res) => { const webhook = async (req, res) => {
@ -513,7 +524,8 @@ module.exports = {
webhook, webhook,
webhook_crm, webhook_crm,
sfCreateCase, sfCreateCase,
sfUpdateCase sfUpdateCase,
createTicket
} }

View File

@ -1,10 +1,11 @@
const express = require('express') const express = require('express')
const router = express.Router() const router = express.Router()
const { authorization, } = require('../middleware/authentication') const { authorization, } = require('../middleware/authentication')
const { contactCreate, sfCreateCase, sfUpdateCase, testTemplate, webhook_crm, uploadCrmConfig, callJournaling, oauthCallBack, install, deleteCrm, deleteCompany, getCrms, webhook } = require('../controllers/crmController') const { contactCreate, sfCreateCase, sfUpdateCase, createTicket, testTemplate, webhook_crm, uploadCrmConfig, callJournaling, oauthCallBack, install, deleteCrm, deleteCompany, getCrms, webhook } = require('../controllers/crmController')
const { fileUpload } = require("../utils") const { fileUpload } = require("../utils")
router.route('/create-contact').post(authorization, contactCreate) router.route('/create-contact').post(authorization, contactCreate)
router.route('/create-ticket').post(authorization, createTicket)
router.route('/call-journaling').post(authorization, callJournaling) router.route('/call-journaling').post(authorization, callJournaling)
router.route('/upload').post(fileUpload.single('crm'), authorization, uploadCrmConfig) router.route('/upload').post(fileUpload.single('crm'), authorization, uploadCrmConfig)
router.route('/delete').post(authorization, deleteCrm) router.route('/delete').post(authorization, deleteCrm)

View File

@ -12,6 +12,7 @@ const sendEventTicketCreatedToSocket = require('./sendEventTicketCreatedToSocket
async function createTicket(companyId, rest, authentication, crmPhone, crmFirstName = 'Username', crmLastName = 'Last name', crmEmail = '', test = {}, crmContactId, crmAgent) { async function createTicket(companyId, rest, authentication, crmPhone, crmFirstName = 'Username', crmLastName = 'Last name', crmEmail = '', test = {}, crmContactId, crmAgent) {
let ticketUrl = ''
let { request, body, response } = findProperty(rest, 'createTicketRecord') let { request, body, response } = findProperty(rest, 'createTicketRecord')
const { requestContentType, requestEncoding, requestType, responseType, url } = request const { requestContentType, requestEncoding, requestType, responseType, url } = request
@ -79,11 +80,13 @@ async function createTicket(companyId, rest, authentication, crmPhone, crmFirstN
await CRM_Ticket.create({ companyId, contact, ticketId: auxTicketId, crm }) await CRM_Ticket.create({ companyId, contact, ticketId: auxTicketId, crm })
sendEventTicketCreatedToSocket({ companyId, extension: crmAgent, ticketUrl: `https://app.hubspot.com/contacts/23636141/ticket/${auxTicketId}` }) ticketUrl = `https://app.hubspot.com/contacts/23636141/ticket/${auxTicketId}`
sendEventTicketCreatedToSocket({ companyId, extension: crmAgent, ticketUrl: ticketUrl })
} }
return { exist: true, ticketId: auxTicketId, phone: crmPhone } return { exist: true, ticketId: auxTicketId, phone: crmPhone, ticketUrl }
} }

View File

@ -12,11 +12,12 @@ const sendEventTicketCreatedToSocket = require('./sendEventTicketCreatedToSocket
const journalingRequest = require('./journalingRequest') const journalingRequest = require('./journalingRequest')
async function ticketCRM(companyId, crmPhone, crmAgent, crmFirstName = 'Username') { async function ticketCRM(companyId, crmPhone, crmAgent="0000", crmFirstName = 'Username') {
const crmFiles = await loadCRM(companyId) const crmFiles = await loadCRM(companyId)
// const crmContactIds = [] // const crmContactIds = []
const crmTicketLinks = []
for (const crmConfig of crmFiles) { for (const crmConfig of crmFiles) {
@ -27,6 +28,9 @@ async function ticketCRM(companyId, crmPhone, crmAgent, crmFirstName = 'Username
// 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')
let ticket_id = ''
let isCreated = false
if (obj) { if (obj) {
let { url } = obj.request let { url } = obj.request
@ -46,6 +50,12 @@ async function ticketCRM(companyId, crmPhone, crmAgent, crmFirstName = 'Username
console.log('obj_ticket1: ', obj_ticket) console.log('obj_ticket1: ', obj_ticket)
const { ticketId } = obj_ticket
if (ticketId) {
ticket_id = ticketId
}
const obj_ticket_status = await lookupCRMTicket( const obj_ticket_status = await lookupCRMTicket(
rest, rest,
authentication, authentication,
@ -61,7 +71,13 @@ async function ticketCRM(companyId, crmPhone, crmAgent, crmFirstName = 'Username
// 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 })
crmFirstName = await _createTicket(rest, crmPhone, companyId, authentication, crmFirstName, contact, crmAgent)
const { ticketUrl, ticketId } = await _createTicket(rest, crmPhone, companyId, authentication, crmFirstName, contact, crmAgent)
ticket_id = ticketId
isCreated = true
// crmTicketLinks.push({ ticketUrl, ticketId })
} }
} }
@ -69,19 +85,23 @@ async function ticketCRM(companyId, crmPhone, crmAgent, crmFirstName = 'Username
else { else {
console.log('_createTicket xxxxx ') console.log('_createTicket xxxxx ')
crmFirstName = await _createTicket(rest, crmPhone, companyId, authentication, crmFirstName, contact, crmAgent) const { ticketUrl, ticketId } = await _createTicket(rest, crmPhone, companyId, authentication, crmFirstName, contact, crmAgent)
ticket_id = ticketId
isCreated = true
} }
const { contactId } = contact crmTicketLinks.push({
ticketId: `https://app.hubspot.com/contacts/23636141/ticket/${ticket_id}`,
created: isCreated,
})
// crmContactIds.push({ crm: new URL(url).hostname, contactId, created })
} }
// //
} }
return crmTicketLinks
// return crmContactIds
} }
@ -106,15 +126,19 @@ async function _lookupContact(rest, authentication, crmPhone, companyId, crmFirs
async function _createTicket(rest, crmPhone, companyId, authentication, crmFirstName, contact, crmAgent) { async function _createTicket(rest, crmPhone, companyId, authentication, crmFirstName, contact, crmAgent) {
let obj = findProperty(rest, 'createTicketRecord') let obj = findProperty(rest, 'createTicketRecord')
let ticket_url = ''
let ticket_id = ''
if (obj) { if (obj) {
let { request, response } = obj let { request, response } = obj
if (request) { if (request) {
msg = `Tentando criar ticket para o contato ${crmPhone}` msg = `Tentando criar ticket para o contato ${crmPhone}`
// return { exist: true, ticketId: auxTicketId, phone: crmPhone, ticketUrl }
// console.log('xxxxxxxxxxxxxxxxxxxxxxxxx1') // console.log('xxxxxxxxxxxxxxxxxxxxxxxxx1')
// return { exist: true, ticketId: auxTicketId, phone: crmPhone, ticketUrl }
await createTicket(companyId, const { ticketUrl, ticketId, } = await createTicket(companyId,
rest, rest,
authentication, authentication,
crmPhone, crmPhone,
@ -125,8 +149,11 @@ async function _createTicket(rest, crmPhone, companyId, authentication, crmFirst
crmContactId = contact.contactId, crmContactId = contact.contactId,
crmAgent crmAgent
) )
ticket_id = ticketId
ticket_url = ticketUrl
} }
} }
return crmFirstName return { crmFirstName, ticketUrl: ticket_url, ticketId: ticket_id }
} }