2024-07-19 18:48:34 +00:00
|
|
|
const loadCRM = require('./loadCRM')
|
|
|
|
const lookupContactByPhone = require('./lookupCRMContactByPhone')
|
|
|
|
const createContact = require('./createContact')
|
|
|
|
const findProperty = require('./findProperty')
|
|
|
|
const CRM_Contact = require('../models/CRM_Contact')
|
|
|
|
const CRM_Ticket = require('../models/CRM_Ticket')
|
|
|
|
const CRM = require('../models/CRM')
|
|
|
|
const createTicket = require('./createTicket')
|
|
|
|
|
|
|
|
const lookupCRMTicket = require('./lookupCRMTicket')
|
2024-10-02 13:06:07 +00:00
|
|
|
const sendEventTicketCreatedToSocket = require('./sendEventTicketCreatedToSocket')
|
2024-07-19 18:48:34 +00:00
|
|
|
|
|
|
|
|
2024-09-30 13:53:32 +00:00
|
|
|
async function ticketCRM(companyId, crmPhone, crmAgent, crmFirstName = 'Username') {
|
2024-07-19 18:48:34 +00:00
|
|
|
|
2024-10-02 13:06:07 +00:00
|
|
|
const crmFiles = await loadCRM(companyId)
|
|
|
|
|
|
|
|
for (const crmConfig of crmFiles) {
|
2024-07-19 18:48:34 +00:00
|
|
|
|
2024-10-02 13:06:07 +00:00
|
|
|
const { crmRest: rest, authentication } = crmConfig.crm
|
2024-09-24 12:10:18 +00:00
|
|
|
|
2024-10-02 13:06:07 +00:00
|
|
|
// Send the edited contact/lead link url to hitphone to open on another browser tab
|
|
|
|
let redirectLink = findProperty(rest, 'redirectLink')
|
2024-07-19 18:48:34 +00:00
|
|
|
|
2024-10-02 13:06:07 +00:00
|
|
|
if (redirectLink) {
|
|
|
|
let contact = await _lookupContact(rest, authentication, crmPhone, companyId, crmFirstName)
|
2024-07-19 18:48:34 +00:00
|
|
|
|
2024-10-02 13:06:07 +00:00
|
|
|
const { contactId } = contact
|
2024-07-19 18:48:34 +00:00
|
|
|
|
2024-10-02 13:06:07 +00:00
|
|
|
const url = redirectLink?.request?.url?.replace(/crmContactId/g, contactId)
|
2024-07-19 18:48:34 +00:00
|
|
|
|
2024-10-02 13:06:07 +00:00
|
|
|
console.log('===============> Edit url rediret sended to hitphone: ', url)
|
2024-07-19 18:48:34 +00:00
|
|
|
|
2024-10-02 13:06:07 +00:00
|
|
|
sendEventTicketCreatedToSocket({ companyId, extension: crmAgent, ticketUrl: url })
|
|
|
|
}
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
|
|
// Send the ticket url link to hitphone to open on another browser tab
|
|
|
|
let obj = findProperty(rest, 'lookupTicket')
|
|
|
|
|
|
|
|
if (obj) {
|
|
|
|
|
|
|
|
let { url } = obj.request
|
|
|
|
let contact = await _lookupContact(rest, authentication, crmPhone, companyId, crmFirstName)
|
2024-07-19 18:48:34 +00:00
|
|
|
|
|
|
|
const crm = await CRM.findOne({
|
|
|
|
companyId, crmBaseURL: new URL(url.trim()).hostname
|
|
|
|
})
|
|
|
|
|
|
|
|
const obj_contact = await CRM_Contact.findOne({ companyId, crmBaseURL: new URL(url).hostname, phone: crmPhone })
|
|
|
|
|
|
|
|
const obj_ticket = await CRM_Ticket.findOne(
|
|
|
|
{ companyId, crm, contact: obj_contact }
|
|
|
|
).select('ticketId')
|
|
|
|
|
|
|
|
if (obj_ticket) {
|
|
|
|
|
|
|
|
const obj_ticket_status = await lookupCRMTicket(
|
|
|
|
rest,
|
|
|
|
authentication,
|
|
|
|
crmPhone,
|
|
|
|
companyId,
|
|
|
|
test = { testing: false },
|
|
|
|
obj_ticket.ticketId
|
|
|
|
)
|
|
|
|
|
|
|
|
if (obj_ticket_status) {
|
|
|
|
const { auxTicketStatus, error } = obj_ticket_status
|
|
|
|
|
|
|
|
// 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 })
|
2024-07-19 20:23:25 +00:00
|
|
|
crmFirstName = await _createTicket(rest, crmPhone, companyId, authentication, crmFirstName, contact, crmAgent)
|
2024-07-19 18:48:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
else {
|
2024-07-19 20:23:25 +00:00
|
|
|
crmFirstName = await _createTicket(rest, crmPhone, companyId, authentication, crmFirstName, contact, crmAgent)
|
2024-07-19 18:48:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2024-10-02 13:06:07 +00:00
|
|
|
//
|
2024-07-19 18:48:34 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = ticketCRM
|
|
|
|
|
|
|
|
|
2024-10-02 13:06:07 +00:00
|
|
|
async function _lookupContact(rest, authentication, crmPhone, companyId, crmFirstName) {
|
|
|
|
let contact = await lookupContactByPhone(rest, authentication, crmPhone, companyId)
|
|
|
|
|
|
|
|
if (!contact?.exist) {
|
|
|
|
contact = await createContact(companyId, rest, authentication, crmPhone, crmFirstName)
|
|
|
|
}
|
|
|
|
return contact
|
|
|
|
}
|
|
|
|
|
2024-07-19 20:23:25 +00:00
|
|
|
async function _createTicket(rest, crmPhone, companyId, authentication, crmFirstName, contact, crmAgent) {
|
2024-07-19 18:48:34 +00:00
|
|
|
let obj = findProperty(rest, 'createTicketRecord')
|
|
|
|
|
|
|
|
if (obj) {
|
|
|
|
let { request, response } = obj
|
|
|
|
|
|
|
|
if (request) {
|
|
|
|
msg = `Tentando criar ticket para o contato ${crmPhone}`
|
|
|
|
|
|
|
|
await createTicket(companyId,
|
|
|
|
rest,
|
|
|
|
authentication,
|
|
|
|
crmPhone,
|
2024-09-30 13:53:32 +00:00
|
|
|
crmFirstName = 'Username',
|
2024-07-19 18:48:34 +00:00
|
|
|
crmLastName = '',
|
|
|
|
crmEmail = '',
|
|
|
|
test = { testing: false },
|
2024-07-19 20:23:25 +00:00
|
|
|
crmContactId = contact.contactId,
|
|
|
|
crmAgent
|
2024-07-19 18:48:34 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return crmFirstName
|
|
|
|
}
|
|
|
|
|