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

@ -79,49 +79,51 @@ const callJournaling = async (req, res) => {
let { companyId, operation, crmPhone, crmAgent, crmCallDuration, crmFirstName, operationStatus } = req.body
console.log(`[REQ.BODY - ${new Date()}] callJournaling: `, JSON.stringify(req.body, null, 6))
// test remove
// return res.status(StatusCodes.OK).send()
// Refactor this in the future.
crmPhone = '55' + crmPhone
console.log('========> CRMPHONE: ', crmPhone)
console.log('========> COMPANY ID before: ', companyId)
if (companyId == "1")
companyId = '99'
console.log('========> COMPANY ID after: ', companyId)
if (!crmAgent)
crmAgent = "0000"
//
mustContainProperties(req, ['companyId', 'operation', 'crmPhone', /*'crmAgent'*/,])
// if (operation == 'inboundAnsweredCall' && !crmCallDuration)
// throw new CustomError.BadRequestError(`The crmCallDuration property must be provided when operation is inboundAnsweredCall`)
// if (operation == 'outboundAsweredCall' && !crmCallDuration)
// throw new CustomError.BadRequestError(`The crmCallDuration property must be provided when operation is outboundAsweredCall`)
if (!crmCallDuration || crmCallDuration.trim() == "" || crmCallDuration == "0")
crmCallDuration = "10"
if (operationStatus == "hangup")
await journaling(companyId, operation, crmPhone, crmAgent, crmCallDuration, crmFirstName)
else if (operationStatus == "update-answer") {
console.log('update-answer')
await ticketCRM(companyId, crmPhone, crmAgent, crmFirstName)
// 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)
const resp = await redirectContactLinkCRM(companyId, crmPhone, crmAgent, crmFirstName)
return res.status(StatusCodes.OK).json({ contact: resp })
}
res.status(StatusCodes.OK).send()
} catch (error) {
// console.log(`[ERROR - ${new Date()}] Erro no Call Journaling`, error?.response?.data)
@ -131,10 +133,10 @@ const callJournaling = async (req, res) => {
status: error.response.status,
data: error.response.data,
})
}
}
else if (error.request) {
console.error('==================> callJournaling Nenhuma resposta recebida da API:', error.request)
}
}
else {
console.error('==================> callJournaling Erro ao configurar a request:', error.message)
}
@ -204,7 +206,7 @@ const oauthCallBack = async (req, res) => {
console.log('xxxxxxxxxx passed')
let crmOauth = await CRM.findOne({ 'crm.authentication.crmClientId': clientId, 'companyId': companyId })
let crmOauth = await CRM.findOne({ 'crm.authentication.crmClientId': clientId, 'companyId': companyId })
crmOauth = crmOauth.toObject()
@ -367,6 +369,15 @@ const getCrms = async (req, res) => {
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) => {
@ -513,7 +524,8 @@ module.exports = {
webhook,
webhook_crm,
sfCreateCase,
sfUpdateCase
sfUpdateCase,
createTicket
}

View File

@ -1,10 +1,11 @@
const express = require('express')
const router = express.Router()
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")
router.route('/create-contact').post(authorization, contactCreate)
router.route('/create-ticket').post(authorization, createTicket)
router.route('/call-journaling').post(authorization, callJournaling)
router.route('/upload').post(fileUpload.single('crm'), authorization, uploadCrmConfig)
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) {
let ticketUrl = ''
let { request, body, response } = findProperty(rest, 'createTicketRecord')
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 })
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')
async function ticketCRM(companyId, crmPhone, crmAgent, crmFirstName = 'Username') {
async function ticketCRM(companyId, crmPhone, crmAgent="0000", crmFirstName = 'Username') {
const crmFiles = await loadCRM(companyId)
// const crmContactIds = []
const crmTicketLinks = []
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
let obj = findProperty(rest, 'lookupTicket')
let ticket_id = ''
let isCreated = false
if (obj) {
let { url } = obj.request
@ -46,6 +50,12 @@ async function ticketCRM(companyId, crmPhone, crmAgent, crmFirstName = 'Username
console.log('obj_ticket1: ', obj_ticket)
const { ticketId } = obj_ticket
if (ticketId) {
ticket_id = ticketId
}
const obj_ticket_status = await lookupCRMTicket(
rest,
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
if ((auxTicketStatus && auxTicketStatus != '1') || (error && error == 404)) {
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 {
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 crmContactIds
}
return crmTicketLinks
}
@ -106,15 +126,19 @@ async function _lookupContact(rest, authentication, crmPhone, companyId, crmFirs
async function _createTicket(rest, crmPhone, companyId, authentication, crmFirstName, contact, crmAgent) {
let obj = findProperty(rest, 'createTicketRecord')
let ticket_url = ''
let ticket_id = ''
if (obj) {
let { request, response } = obj
if (request) {
msg = `Tentando criar ticket para o contato ${crmPhone}`
// return { exist: true, ticketId: auxTicketId, phone: crmPhone, ticketUrl }
// console.log('xxxxxxxxxxxxxxxxxxxxxxxxx1')
await createTicket(companyId,
// return { exist: true, ticketId: auxTicketId, phone: crmPhone, ticketUrl }
const { ticketUrl, ticketId, } = await createTicket(companyId,
rest,
authentication,
crmPhone,
@ -125,8 +149,11 @@ async function _createTicket(rest, crmPhone, companyId, authentication, crmFirst
crmContactId = contact.contactId,
crmAgent
)
ticket_id = ticketId
ticket_url = ticketUrl
}
}
return crmFirstName
return { crmFirstName, ticketUrl: ticket_url, ticketId: ticket_id }
}