diff --git a/backend/controllers/crmController.js b/backend/controllers/crmController.js index 825f430..41a1808 100644 --- a/backend/controllers/crmController.js +++ b/backend/controllers/crmController.js @@ -94,11 +94,14 @@ const checkContact = async (req, res) => { contact = await lookupContactByEmail(rest, authentication, crmEmail, companyId) } if (contact?.exist) { - return res.status(StatusCodes.OK).json({ exist: contact.exist }) + + + + return res.status(StatusCodes.OK).json({ exist: contact.exist, flow: contact?.flow || 1 }) } } - res.status(StatusCodes.OK).json({ exist: false }) + res.status(StatusCodes.OK).json({ exist: false, flow: -1 }) } // const contactActivity = async (req, res) => { @@ -564,11 +567,11 @@ const getCrms = async (req, res) => { const createTicket = async (req, res) => { - let { companyId, crmPhone } = req.body + let { companyId, crmPhone, newTicketAllways = false, dynamicBodyRequest = {} } = req.body mustContainProperties(req, ['companyId', 'crmPhone']) - const crmTicketLinks = await ticketCRM(companyId, crmPhone) + const crmTicketLinks = await ticketCRM(companyId, crmPhone, '0000', 'Username', 1, newTicketAllways, dynamicBodyRequest) .catch(function (error) { console.error(`Error on create ticket: companyID ${companyId} | crmPhone: ${crmPhone}`) diff --git a/backend/utils/createCRMContact.js b/backend/utils/createCRMContact.js index 816f47d..80ee339 100644 --- a/backend/utils/createCRMContact.js +++ b/backend/utils/createCRMContact.js @@ -15,7 +15,8 @@ async function createCRMContact(companyId, crmFirstName, crmPhone = '', crmEmail if (crmPhone) { contact = await lookupContactByPhone(rest, authentication, crmPhone, companyId) } - if (!contact?.exist && crmEmail) { + // Por enquanto não esta verificando contato por email para cliente gradezco 4953 + if (!contact?.exist && crmEmail && companyId != "4953") { contact = await lookupContactByEmail(rest, authentication, crmEmail, companyId) } if (contact?.exist) { @@ -25,7 +26,10 @@ async function createCRMContact(companyId, crmFirstName, crmPhone = '', crmEmail // if (contact.exist) continue - await createContact(companyId, rest, authentication, crmPhone, crmFirstName, crmLastName, crmEmail, {}, dynamicBodyRequest) + console.log("==============> contact: ", JSON.stringify(contact, null, 6)) + + + await createContact(companyId, rest, authentication, crmPhone, crmFirstName, crmLastName, crmEmail, {}, dynamicBodyRequest, contact?.flow || 1) } diff --git a/backend/utils/createContact.js b/backend/utils/createContact.js index c2d5d2e..5980b17 100644 --- a/backend/utils/createContact.js +++ b/backend/utils/createContact.js @@ -81,7 +81,7 @@ async function createContact(companyId, rest, authentication, crmPhone = '', crm console.log('CREATE CONTACT PAYLOAD: ', JSON.stringify(config, null, 6)) console.log('#####################') - if (dynamicBodyRequest && Object.keys(dynamicBodyRequest) !== 0) { + if (dynamicBodyRequest && Object.keys(dynamicBodyRequest).length !== 0) { config.data = { ...config.data, ...dynamicBodyRequest } console.log('#####################') console.log('CREATE CONTACT PAYLOAD UPDATED BY DYNAMIC BODY REQUEST: ', JSON.stringify(config, null, 6)) diff --git a/backend/utils/createTicket.js b/backend/utils/createTicket.js index 81dc6df..e339837 100644 --- a/backend/utils/createTicket.js +++ b/backend/utils/createTicket.js @@ -11,7 +11,7 @@ const sendEventTicketCreatedToSocket = require('./sendEventTicketCreatedToSocket const generateC4CServiceRequestDeepLinkBase64 = require('./generateC4CServiceRequestDeepLinkBase64') -async function createTicket(companyId, rest, authentication, crmPhone, crmFirstName = 'Username', crmLastName = 'Last name', crmEmail = '', test = {}, contact, crmAgent) { +async function createTicket(companyId, rest, authentication, crmPhone, crmFirstName = 'Username', crmLastName = 'Last name', crmEmail = '', test = {}, contact, crmAgent, saveTicket = true, dynamicBodyRequest = {}) { let ticketUrl = '' let crmContactId = contact.contactId @@ -77,7 +77,7 @@ async function createTicket(companyId, rest, authentication, crmPhone, crmFirstN const { type, userName, passWord, token, crmClientId, crmAccountId } = authentication //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) + let config = await requestConfigHeader(url, crmPhone, requestType, requestContentType, type, userName, passWord, token, crmClientId, body, '', companyId) if (test?.testing) { msg = `Tentanto criar ticket do numero ${crmPhone} no crm` @@ -86,6 +86,15 @@ async function createTicket(companyId, rest, authentication, crmPhone, crmFirstN console.log('===============> createTicket: ', JSON.stringify(config, null, 6)) + console.log('===============> dynamicBodyRequest: ', JSON.stringify(dynamicBodyRequest, null, 6)) + + if (dynamicBodyRequest && Object.keys(dynamicBodyRequest).length !== 0) { + config.data = { ...config.data, ...dynamicBodyRequest } + console.log('#####################') + console.log('CREATE TICKET PAYLOAD UPDATED BY DYNAMIC BODY REQUEST: ', JSON.stringify(config, null, 6)) + console.log('#####################') + } + let resp try { @@ -145,9 +154,10 @@ async function createTicket(companyId, rest, authentication, crmPhone, crmFirstN contact2 = await CRM_Contact.create({ companyId, crm, crmBaseURL: new URL(url).hostname, contactId: contact.contactId, phone: crmPhone }) } - const crm = await CRM.findOne({ companyId, crmBaseURL: new URL(url).hostname }) - - await CRM_Ticket.create({ companyId, contact: contact2, ticketId: auxTicketId, ticketId2: auxTicketId2, crm }) + if (saveTicket) { + const crm = await CRM.findOne({ companyId, crmBaseURL: new URL(url).hostname }) + await CRM_Ticket.create({ companyId, contact: contact2, ticketId: auxTicketId, ticketId2: auxTicketId2, crm }) + } if (url.includes("hubapi")) { ticketUrl = `https://app.hubspot.com/contacts/${crmAccountId}/ticket/${auxTicketId}` diff --git a/backend/utils/ticketCRM.js b/backend/utils/ticketCRM.js index ceb74ae..301e31c 100644 --- a/backend/utils/ticketCRM.js +++ b/backend/utils/ticketCRM.js @@ -8,17 +8,11 @@ const CRM = require('../models/CRM') const createTicket = require('./createTicket') const lookupCRMTicket = require('./lookupCRMTicket') -const sendEventTicketCreatedToSocket = require('./sendEventTicketCreatedToSocket') -const journalingRequest = require('./journalingRequest') -const getHubspotTicketStatus = require('./getHubspotTicketStatus') const getHubspotTicket = require('./getHubspotTicket') -const getHubspotPipelines = require('./getHubspotPipelines') -const mongoose = require('mongoose') -const getHubspotTicketStatusByContact = require('./getHubspotTicketStatusByContact') const findTicketOpenHubspotByContact = require('./findTicketOpenHubspotByContact') const generateC4CServiceRequestDeepLinkBase64 = require('./generateC4CServiceRequestDeepLinkBase64') -async function ticketCRM(companyId, crmPhone, crmAgent = "0000", crmFirstName = 'Username', flow = 1) { +async function ticketCRM(companyId, crmPhone, crmAgent = "0000", crmFirstName = 'Username', flow = 1, newTicketAllways = false, dynamicBodyRequest = {}) { const crmFiles = await loadCRM(companyId) @@ -76,7 +70,7 @@ async function ticketCRM(companyId, crmPhone, crmAgent = "0000", crmFirstName = console.log("------------> ticketCRM obj_ticket: ", JSON.stringify(obj_ticket, null, 6)) - if (obj_ticket) { + if (obj_ticket && newTicketAllways == false) { const { ticketId, ticketId2 } = obj_ticket @@ -148,7 +142,7 @@ async function ticketCRM(companyId, crmPhone, crmAgent = "0000", crmFirstName = } else { - if (url.includes("hubapi")) { + if (url.includes("hubapi") && newTicketAllways == false) { console.log(`[${new Date()}] ****** IS HUBSPOT CRM LETS TRY FINDING AN OPEN TICKET TO THE CONTACT****** `) @@ -172,7 +166,7 @@ async function ticketCRM(companyId, crmPhone, crmAgent = "0000", crmFirstName = } } // To sap crm try to find ticket open - else if (url.includes("c4codataapi")) { + else if (url.includes("c4codataapi") && newTicketAllways == false) { const result = await lookupCRMTicket( rest, authentication, @@ -217,7 +211,14 @@ async function ticketCRM(companyId, crmPhone, crmAgent = "0000", crmFirstName = console.log(`[${new Date()}] ****** CREATE TICKET ****** `) - const { ticketUrl, ticketId, ticketId2 } = await _createTicket(rest, crmPhone, companyId, authentication, crmFirstName, contact, crmAgent, flow) + let saveTicket = true + + if (newTicketAllways) + saveTicket = false + + console.log("================> ticketCRM saveTicket: ", saveTicket) + + const { ticketUrl, ticketId, ticketId2 } = await _createTicket(rest, crmPhone, companyId, authentication, crmFirstName, contact, crmAgent, flow, saveTicket, dynamicBodyRequest) ticket_id = ticketId ticket_id2 = ticketId2 // ticket_url = ticketUrl @@ -321,7 +322,7 @@ async function _lookupContact(rest, authentication, crmPhone, companyId, crmFirs return { created: true, contactId: contact.contactId, contactId2: contact.contactId2, accountId: contact?.accountId, flow: contact?.flow } } -async function _createTicket(rest, crmPhone, companyId, authentication, crmFirstName, contact, crmAgent, flow = 1) { +async function _createTicket(rest, crmPhone, companyId, authentication, crmFirstName, contact, crmAgent, flow = 1, saveTicket = true, dynamicBodyRequest = {}) { let propertyKey @@ -356,7 +357,9 @@ async function _createTicket(rest, crmPhone, companyId, authentication, crmFirst crmEmail = '', test = { testing: false }, contact, - crmAgent + crmAgent, + saveTicket, + dynamicBodyRequest ) ticket_id = ticketId