2024-07-19 18:48:34 +00:00
|
|
|
const flatten = require('flat')
|
|
|
|
|
const unflatten = require('flat').unflatten
|
|
|
|
|
const axios = require('axios')
|
|
|
|
|
const CRM_Contact = require('../models/CRM_Contact')
|
|
|
|
|
const CRM_Ticket = require('../models/CRM_Ticket')
|
|
|
|
|
const { URL } = require('url')
|
|
|
|
|
const findProperty = require('./findProperty')
|
|
|
|
|
const CRM = require('../models/CRM')
|
|
|
|
|
const requestConfigHeader = require('./requestConfigHeader')
|
2024-07-19 20:23:25 +00:00
|
|
|
const sendEventTicketCreatedToSocket = require('./sendEventTicketCreatedToSocket')
|
2025-10-21 12:18:51 +00:00
|
|
|
const generateC4CServiceRequestDeepLinkBase64 = require('./generateC4CServiceRequestDeepLinkBase64')
|
2024-07-19 18:48:34 +00:00
|
|
|
|
2024-07-19 20:23:25 +00:00
|
|
|
|
2025-10-21 12:18:51 +00:00
|
|
|
async function createTicket(companyId, rest, authentication, crmPhone, crmFirstName = 'Username', crmLastName = 'Last name', crmEmail = '', test = {}, contact, crmAgent) {
|
2024-11-28 12:17:54 +00:00
|
|
|
|
2025-03-11 20:44:47 +00:00
|
|
|
let ticketUrl = ''
|
2025-10-21 12:18:51 +00:00
|
|
|
let crmContactId = contact.contactId
|
|
|
|
|
|
|
|
|
|
let flow = contact?.flow
|
|
|
|
|
let propertyKey
|
|
|
|
|
|
|
|
|
|
switch (flow) {
|
|
|
|
|
case 2:
|
|
|
|
|
propertyKey = 'createTicketRecord2'
|
|
|
|
|
break
|
|
|
|
|
default:
|
|
|
|
|
propertyKey = 'createTicketRecord'
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
console.log('==============> propertyKey createTicketRecord2: ', propertyKey)
|
|
|
|
|
|
|
|
|
|
let { request, body, response } = findProperty(rest, propertyKey)
|
2024-07-19 18:48:34 +00:00
|
|
|
|
2025-04-03 14:37:20 +00:00
|
|
|
console.log('==============> crmContactId: ', crmContactId)
|
2025-10-21 12:18:51 +00:00
|
|
|
console.log('==============> crmPhone: ', crmPhone)
|
|
|
|
|
console.log("==============> contact?.flow: ", contact?.flow)
|
|
|
|
|
|
2025-04-03 14:37:20 +00:00
|
|
|
|
2024-07-19 18:48:34 +00:00
|
|
|
const { requestContentType, requestEncoding, requestType, responseType, url } = request
|
2024-11-28 12:17:54 +00:00
|
|
|
|
2025-04-03 14:37:20 +00:00
|
|
|
console.log('========> body1: ', JSON.stringify(body, null, 6))
|
2025-10-21 12:18:51 +00:00
|
|
|
console.log("==========>createTicket contact: ", JSON.stringify(contact, null, 6))
|
2024-11-28 12:17:54 +00:00
|
|
|
|
2024-07-19 18:48:34 +00:00
|
|
|
body = flatten(body)
|
|
|
|
|
|
2025-04-03 14:37:20 +00:00
|
|
|
console.log('========> body2: ', JSON.stringify(body, null, 6))
|
|
|
|
|
|
2024-07-19 18:48:34 +00:00
|
|
|
const mapping = {
|
|
|
|
|
crmFirstName,
|
|
|
|
|
crmLastName,
|
|
|
|
|
crmPhone,
|
|
|
|
|
crmEmail,
|
2025-10-21 12:18:51 +00:00
|
|
|
crmContactId,
|
|
|
|
|
crmAccountId: contact?.accountId
|
2024-07-19 18:48:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (const prop in body) {
|
|
|
|
|
if (mapping.hasOwnProperty(body[prop])) {
|
|
|
|
|
const variable = mapping[body[prop]]
|
|
|
|
|
if (variable) {
|
|
|
|
|
body[prop] = variable
|
|
|
|
|
} else {
|
|
|
|
|
if (body[prop] == 'crmLastName' && !crmLastName) {
|
2024-09-30 13:53:32 +00:00
|
|
|
body[prop] = 'Username'
|
2024-07-19 18:48:34 +00:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
delete body[prop]
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
body = unflatten(body)
|
|
|
|
|
|
2025-04-03 14:37:20 +00:00
|
|
|
console.log('========> body3: ', JSON.stringify(body, null, 6))
|
|
|
|
|
|
2025-04-11 19:32:24 +00:00
|
|
|
const { type, userName, passWord, token, crmClientId, crmAccountId } = authentication
|
2024-07-19 18:48:34 +00:00
|
|
|
|
2025-04-03 14:37:20 +00:00
|
|
|
//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)
|
2024-07-19 18:48:34 +00:00
|
|
|
|
|
|
|
|
if (test?.testing) {
|
|
|
|
|
msg = `Tentanto criar ticket do numero ${crmPhone} no crm`
|
|
|
|
|
sendMessageSocket({ companyId, status: 'processing', data: { request: config, msg } })
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-21 12:18:51 +00:00
|
|
|
console.log('===============> createTicket: ', JSON.stringify(config, null, 6))
|
2025-04-03 14:37:20 +00:00
|
|
|
|
|
|
|
|
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
|
2024-07-19 18:48:34 +00:00
|
|
|
|
|
|
|
|
data = flatten(data)
|
|
|
|
|
|
|
|
|
|
let auxTicketId
|
2025-10-21 12:18:51 +00:00
|
|
|
let auxTicketId2
|
|
|
|
|
|
2024-07-19 18:48:34 +00:00
|
|
|
|
|
|
|
|
for (const prop in data) {
|
|
|
|
|
|
|
|
|
|
const _prop = prop.replace(/^\d+\./, '').replace(/(?:^|\.)\d+\b/g, '')
|
|
|
|
|
|
|
|
|
|
if (_prop == response?.id?.trim()) {
|
|
|
|
|
auxTicketId = data[prop]
|
2025-10-21 12:18:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (_prop == response?.objectId?.trim()) {
|
|
|
|
|
auxTicketId2 = data[prop]
|
2024-07-19 18:48:34 +00:00
|
|
|
}
|
2024-11-28 12:17:54 +00:00
|
|
|
}
|
2024-07-19 18:48:34 +00:00
|
|
|
|
2025-10-21 12:18:51 +00:00
|
|
|
console.log("========> createTicket auxTicketId: ", auxTicketId, " | auxTicketId2: ", auxTicketId2)
|
|
|
|
|
|
2024-07-19 18:48:34 +00:00
|
|
|
if (auxTicketId && !test?.testing) {
|
2025-10-21 12:18:51 +00:00
|
|
|
|
|
|
|
|
let contact2 = await CRM_Contact.findOne({ companyId, crmBaseURL: new URL(url).hostname, phone: crmPhone })
|
|
|
|
|
|
|
|
|
|
if (!contact2) {
|
|
|
|
|
const crm = await CRM.findOne({ companyId, crmBaseURL: new URL(url).hostname })
|
|
|
|
|
contact2 = await CRM_Contact.create({ companyId, crm, crmBaseURL: new URL(url).hostname, contactId: contact.contactId, phone: crmPhone })
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-19 18:48:34 +00:00
|
|
|
const crm = await CRM.findOne({ companyId, crmBaseURL: new URL(url).hostname })
|
|
|
|
|
|
2025-10-21 12:18:51 +00:00
|
|
|
await CRM_Ticket.create({ companyId, contact: contact2, ticketId: auxTicketId, ticketId2: auxTicketId2, crm })
|
2024-07-19 20:23:25 +00:00
|
|
|
|
2025-10-21 12:18:51 +00:00
|
|
|
if (url.includes("hubapi")) {
|
|
|
|
|
ticketUrl = `https://app.hubspot.com/contacts/${crmAccountId}/ticket/${auxTicketId}`
|
|
|
|
|
}
|
|
|
|
|
else if (url.includes("c4codataapi")) {
|
|
|
|
|
ticketUrl = `https://my365398.crm.ondemand.com/sap/ap/ui/clogin?saml2=disabled&app.component=%2fSAP_UI_CT%2fMain%2froot.uiccwoc&rootWindow=X&redirectUrl=%2fsap%2fpublic%2fbyd%2fruntime&supressAutoLogon=true&sap-ui-language=en_us#Nav/1/${generateC4CServiceRequestDeepLinkBase64(auxTicketId2)}`
|
|
|
|
|
}
|
2025-03-11 20:44:47 +00:00
|
|
|
|
|
|
|
|
sendEventTicketCreatedToSocket({ companyId, extension: crmAgent, ticketUrl: ticketUrl })
|
2024-07-19 20:23:25 +00:00
|
|
|
|
2024-07-19 18:48:34 +00:00
|
|
|
}
|
|
|
|
|
|
2025-10-21 12:18:51 +00:00
|
|
|
return { exist: true, ticketId: auxTicketId, ticketId2: auxTicketId2, phone: crmPhone, ticketUrl }
|
2024-07-19 18:48:34 +00:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-28 12:17:54 +00:00
|
|
|
module.exports = createTicket
|