crm-api-template-generator/backend/utils/ticketCRM.js

163 lines
5.2 KiB
JavaScript
Raw Normal View History

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')
const journalingRequest = require('./journalingRequest')
2024-07-19 18:48:34 +00:00
async function ticketCRM(companyId, crmPhone, crmAgent = "0000", crmFirstName = 'Username') {
2024-07-19 18:48:34 +00:00
2024-10-03 20:53:55 +00:00
const crmFiles = await loadCRM(companyId)
// const crmContactIds = []
2025-03-11 20:44:47 +00:00
const crmTicketLinks = []
2024-10-03 20:53:55 +00:00
2024-10-02 13:06:07 +00:00
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
// console.log("ticketCRM crmRest: ", JSON.stringify(rest, null, 6))
2024-10-02 13:06:07 +00:00
// Send the ticket url link to hitphone to open on another browser tab
let obj = findProperty(rest, 'lookupTicket')
2025-03-11 20:44:47 +00:00
let ticket_id = ''
let isCreated = false
2024-10-02 13:06:07 +00:00
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) {
console.log('obj_ticket1: ', obj_ticket)
2025-03-11 20:44:47 +00:00
const { ticketId } = obj_ticket
if (ticketId) {
ticket_id = ticketId
}
2024-07-19 18:48:34 +00:00
const obj_ticket_status = await lookupCRMTicket(
rest,
authentication,
crmPhone,
companyId,
test = { testing: false },
obj_ticket.ticketId
)
if (obj_ticket_status)
console.log("obj_ticket_status: ", JSON.stringify(obj_ticket_status, null, 6))
2024-07-19 18:48:34 +00:00
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 })
2025-03-11 20:44:47 +00:00
const { ticketUrl, ticketId } = await _createTicket(rest, crmPhone, companyId, authentication, crmFirstName, contact, crmAgent)
ticket_id = ticketId
isCreated = true
// crmTicketLinks.push({ ticketUrl, ticketId })
2024-07-19 18:48:34 +00:00
}
}
}
else {
console.log('_createTicket xxxxx ')
2025-03-11 20:44:47 +00:00
const { ticketUrl, ticketId } = await _createTicket(rest, crmPhone, companyId, authentication, crmFirstName, contact, crmAgent)
ticket_id = ticketId
isCreated = true
2024-07-19 18:48:34 +00:00
}
2025-03-11 20:44:47 +00:00
crmTicketLinks.push({
ticketId: `https://app.hubspot.com/contacts/23636141/ticket/${ticket_id}`,
created: isCreated,
})
2024-10-03 20:53:55 +00:00
2024-07-19 18:48:34 +00:00
}
2024-10-03 20:53:55 +00:00
//
2024-07-19 18:48:34 +00:00
}
2025-03-11 20:44:47 +00:00
return crmTicketLinks
2024-10-03 20:53:55 +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) {
2024-10-03 20:53:55 +00:00
2024-10-02 13:06:07 +00:00
let contact = await lookupContactByPhone(rest, authentication, crmPhone, companyId)
2024-10-03 20:53:55 +00:00
if (contact?.exist) {
return { created: false, contactId: contact.contactId }
}
2024-10-02 13:06:07 +00:00
if (!contact?.exist) {
contact = await createContact(companyId, rest, authentication, crmPhone, crmFirstName)
}
2024-10-03 20:53:55 +00:00
return { created: true, contactId: contact.contactId }
2024-10-02 13:06:07 +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')
2025-03-11 20:44:47 +00:00
let ticket_url = ''
let ticket_id = ''
2024-07-19 18:48:34 +00:00
if (obj) {
let { request, response } = obj
if (request) {
msg = `Tentando criar ticket para o contato ${crmPhone}`
2025-03-11 20:44:47 +00:00
// return { exist: true, ticketId: auxTicketId, phone: crmPhone, ticketUrl }
2024-07-19 18:48:34 +00:00
// console.log('xxxxxxxxxxxxxxxxxxxxxxxxx1')
2025-03-11 20:44:47 +00:00
// return { exist: true, ticketId: auxTicketId, phone: crmPhone, ticketUrl }
const { ticketUrl, ticketId, } = await createTicket(companyId,
2024-07-19 18:48:34 +00:00
rest,
authentication,
crmPhone,
crmFirstName = 'Username',
2024-07-19 18:48:34 +00:00
crmLastName = '',
crmEmail = '',
test = { testing: false },
crmContactId = contact.contactId,
crmAgent
2024-07-19 18:48:34 +00:00
)
2025-03-11 20:44:47 +00:00
ticket_id = ticketId
ticket_url = ticketUrl
2024-07-19 18:48:34 +00:00
}
}
2025-03-11 20:44:47 +00:00
return { crmFirstName, ticketUrl: ticket_url, ticketId: ticket_id }
2024-07-19 18:48:34 +00:00
}