chore: refined the code

master
adriano 2024-10-02 10:06:07 -03:00
parent e76cf69f3f
commit fcb7cbd94d
5 changed files with 373 additions and 14 deletions

View File

@ -0,0 +1,155 @@
{
"authentication": {
"type": "oauth2",
"crmClientId": "3MVG9JJwBBbcN47Kv0Z7EuNd19INI1Bhe7uX_Wz6M0VlMyWJD4xPKTtn_b39bGn6LmdSkKJ.aLNGdV1brj16C",
"crmClientSecret": "870E8D51A1CA06896D966A3D92ABD885346DAD4428926E965776C479055969E7",
"crmScopes": "full refresh_token",
"crmPhoneTest": "5514983253326"
},
"crmRest": [
{
"authorizationEndpoint": {
"request": {
"requestContentType": "empty",
"requestEncoding": "empty",
"requestType": "Get",
"responseType": "empty",
"url": "https://login.salesforce.com/services/oauth2/authorize?response_type=code&client_id=crmClientId&code_challenge=bDXEJ0wxr0s369lGxHwewLULiOuyl6Y3W7QZABmn2S4&redirect_uri=crmRedirectURI&scope=crmScopes&code_challenge_method=S256"
},
"body": {},
"response": {}
}
},
{
"tokenEndpoint": {
"request": {
"requestContentType": "none",
"requestEncoding": "Json",
"requestType": "Post",
"responseType": "Json",
"url": "https://login.salesforce.com/services/oauth2/token"
},
"body": {},
"response": {}
}
},
{
"createContactRecord": {
"request": {
"requestContentType": "application/json",
"requestEncoding": "Json",
"requestType": "Post",
"responseType": "Json",
"url": "https://nocompany-a9-dev-ed.develop.my.salesforce.com/services/data/v61.0/sobjects/Lead"
},
"body": {
"Phone": "crmPhone",
"LastName": "crmLastName",
"FirstName": "crmFirstName",
"Company" : "Unknown",
"Status" : "Working - Contacted"
},
"response": {
"id": "id"
}
}
},
{
"lookupContactByPhone": {
"request": {
"requestContentType": "application/json",
"requestEncoding": "Json",
"requestType": "Get",
"responseType": "Json",
"url": "https://nocompany-a9-dev-ed.develop.my.salesforce.com/services/data/v61.0/query/?q=SELECT+Id,Phone+FROM+Lead+WHERE+Phone='crmPhone'"
},
"response": {
"phone": "records[0].Phone",
"id": "records[0].Id"
}
}
},
{
"callJournaling": {
"request": {
"requestContentType": "application/json",
"requestEncoding": "Json",
"requestType": "Post",
"responseType": "Json",
"url": "https://nocompany-a9-dev-ed.develop.my.salesforce.com/services/data/v61.0/sobjects/Task"
},
"calls": [
{
"inboundAnsweredCall": {
"Subject": "Call Journal",
"WhoId": "crmContactId",
"Description": "Ligação recebida",
"Status": "Completed",
"Priority": "Normal",
"CallType": "Outbound",
"CallDurationInSeconds": {
"_prop": "crmCallDuration",
"_type": "number",
"_format": "seconds"
},
"ActivityDate": "YYYY-MM-DD",
"TaskSubtype": "Call"
}
},
{
"inboundMissedCall": {
"Subject": "Call Journal",
"WhoId": "crmContactId",
"Description": "Ligação recebida perdida",
"Status": "Completed",
"Priority": "Normal",
"CallType": "Outbound",
"CallDurationInSeconds": {
"_prop": "crmCallDuration",
"_type": "number",
"_format": "seconds"
},
"ActivityDate": "YYYY-MM-DD",
"TaskSubtype": "Call"
}
},
{
"outboundAnsweredCall": {
"Subject": "Call Journal",
"WhoId": "crmContactId",
"Description": "Ligação realizada",
"Status": "Completed",
"Priority": "Normal",
"CallType": "Outbound",
"CallDurationInSeconds": {
"_prop": "crmCallDuration",
"_type": "number",
"_format": "seconds"
},
"ActivityDate": "YYYY-MM-DD",
"TaskSubtype": "Call"
}
},
{
"outboundUnansweredCall": {
"Subject": "Call Journal",
"WhoId": "crmContactId",
"Description": "Ligação realizada nao atendida",
"Status": "Completed",
"Priority": "Normal",
"CallType": "Outbound",
"CallDurationInSeconds": {
"_prop": "crmCallDuration",
"_type": "number",
"_format": "seconds"
},
"ActivityDate": "YYYY-MM-DD",
"TaskSubtype": "Call"
}
}
],
"response": {}
}
}
]
}

View File

@ -0,0 +1,162 @@
{
"authentication": {
"type": "oauth2",
"crmClientId": "3MVG9JJwBBbcN47Kv0Z7EuNd19INI1Bhe7uX_Wz6M0VlMyWJD4xPKTtn_b39bGn6LmdSkKJ.aLNGdV1brj16C",
"crmClientSecret": "870E8D51A1CA06896D966A3D92ABD885346DAD4428926E965776C479055969E7",
"crmScopes": "full refresh_token",
"crmPhoneTest": "5514983253326"
},
"crmRest": [
{
"authorizationEndpoint": {
"request": {
"requestContentType": "empty",
"requestEncoding": "empty",
"requestType": "Get",
"responseType": "empty",
"url": "https://login.salesforce.com/services/oauth2/authorize?response_type=code&client_id=crmClientId&code_challenge=bDXEJ0wxr0s369lGxHwewLULiOuyl6Y3W7QZABmn2S4&redirect_uri=crmRedirectURI&scope=crmScopes&code_challenge_method=S256"
},
"body": {},
"response": {}
}
},
{
"tokenEndpoint": {
"request": {
"requestContentType": "none",
"requestEncoding": "Json",
"requestType": "Post",
"responseType": "Json",
"url": "https://login.salesforce.com/services/oauth2/token"
},
"body": {},
"response": {}
}
},
{
"createContactRecord": {
"request": {
"requestContentType": "application/json",
"requestEncoding": "Json",
"requestType": "Post",
"responseType": "Json",
"url": "https://nocompany-a9-dev-ed.develop.my.salesforce.com/services/data/v61.0/sobjects/Lead"
},
"body": {
"Phone": "crmPhone",
"LastName": "crmLastName",
"FirstName": "crmFirstName",
"Company": "Unknown",
"Status": "Working - Contacted"
},
"response": {
"id": "id"
}
}
},
{
"lookupContactByPhone": {
"request": {
"requestContentType": "application/json",
"requestEncoding": "Json",
"requestType": "Get",
"responseType": "Json",
"url": "https://nocompany-a9-dev-ed.develop.my.salesforce.com/services/data/v61.0/query/?q=SELECT+Id,Phone+FROM+Lead+WHERE+Phone='crmPhone'"
},
"response": {
"phone": "records[0].Phone",
"id": "records[0].Id"
}
}
},
{
"callJournaling": {
"request": {
"requestContentType": "application/json",
"requestEncoding": "Json",
"requestType": "Post",
"responseType": "Json",
"url": "https://nocompany-a9-dev-ed.develop.my.salesforce.com/services/data/v61.0/sobjects/Task"
},
"calls": [
{
"inboundAnsweredCall": {
"Subject": "Call Journal",
"WhoId": "crmContactId",
"Description": "Ligação recebida",
"Status": "Completed",
"Priority": "Normal",
"CallType": "Outbound",
"CallDurationInSeconds": {
"_prop": "crmCallDuration",
"_type": "number",
"_format": "seconds"
},
"ActivityDate": "YYYY-MM-DD",
"TaskSubtype": "Call"
}
},
{
"inboundMissedCall": {
"Subject": "Call Journal",
"WhoId": "crmContactId",
"Description": "Ligação recebida perdida",
"Status": "Completed",
"Priority": "Normal",
"CallType": "Outbound",
"CallDurationInSeconds": {
"_prop": "crmCallDuration",
"_type": "number",
"_format": "seconds"
},
"ActivityDate": "YYYY-MM-DD",
"TaskSubtype": "Call"
}
},
{
"outboundAnsweredCall": {
"Subject": "Call Journal",
"WhoId": "crmContactId",
"Description": "Ligação realizada",
"Status": "Completed",
"Priority": "Normal",
"CallType": "Outbound",
"CallDurationInSeconds": {
"_prop": "crmCallDuration",
"_type": "number",
"_format": "seconds"
},
"ActivityDate": "YYYY-MM-DD",
"TaskSubtype": "Call"
}
},
{
"outboundUnansweredCall": {
"Subject": "Call Journal",
"WhoId": "crmContactId",
"Description": "Ligação realizada nao atendida",
"Status": "Completed",
"Priority": "Normal",
"CallType": "Outbound",
"CallDurationInSeconds": {
"_prop": "crmCallDuration",
"_type": "number",
"_format": "seconds"
},
"ActivityDate": "YYYY-MM-DD",
"TaskSubtype": "Call"
}
}
],
"response": {}
}
},
{
"redirectLink": {
"request": {
"url": "https://nocompany-a9-dev-ed.develop.lightning.force.com/lightning/r/Lead/crmContactId/edit?count=1&backgroundContext=%2Flightning%2Fr%2FLead%2F00Qak0000098YFhEAM%2Fview"
}
}
}
]
}

View File

@ -139,9 +139,9 @@ const oauthCallBack = async (req, res) => {
const rest = crmOauth.crm.crmRest const rest = crmOauth.crm.crmRest
const salesforceUrls = rest.map(endpoint => { const salesforceUrls = rest.map(endpoint => {
const key = Object.keys(endpoint)[0] const key = Object.keys(endpoint)[0]
const url = endpoint[key].request.url const url = endpoint[key]?.request?.url
if (url.includes("salesforce")) { if (url?.includes("salesforce")) {
return `${key} URL: ${url}` return `${key} URL: ${url}`
} }
}).filter(Boolean) }).filter(Boolean)

View File

@ -44,6 +44,22 @@ const CallJournalingSchema = new Schema({
response: Object response: Object
}) })
const RedirectUrlSchema = new Schema({
url: {
type: String,
required: false
}
})
// const redirectLinkSchema = new Schema({
// request: RedirectUrlSchema,
// })
// Define main schema // Define main schema
const CRMRestSchema = new Schema({ const CRMRestSchema = new Schema({
authorizationEndpoint: { authorizationEndpoint: {
@ -72,6 +88,9 @@ const CRMRestSchema = new Schema({
request: RequestSchema, request: RequestSchema,
response: Object response: Object
}, },
redirectLink: {
request: RedirectUrlSchema
},
callJournaling: CallJournalingSchema callJournaling: CallJournalingSchema
}) })

View File

@ -8,28 +8,41 @@ const CRM = require('../models/CRM')
const createTicket = require('./createTicket') const createTicket = require('./createTicket')
const lookupCRMTicket = require('./lookupCRMTicket') const lookupCRMTicket = require('./lookupCRMTicket')
const sendEventTicketCreatedToSocket = require('./sendEventTicketCreatedToSocket')
async function ticketCRM(companyId, crmPhone, crmAgent, crmFirstName = 'Username') { async function ticketCRM(companyId, crmPhone, crmAgent, crmFirstName = 'Username') {
const crmFiles = await loadCRM(companyId) const crmFiles = await loadCRM(companyId)
// console.log('xxxxxxxx crmFiles: ', JSON.stringify(crmFiles, null, 6))
for (const crmConfig of crmFiles) { for (const crmConfig of crmFiles) {
const { crmRest: rest, authentication } = crmConfig.crm const { crmRest: rest, authentication } = crmConfig.crm
let obj = findProperty(rest, 'lookupTicket') // Send the edited contact/lead link url to hitphone to open on another browser tab
let redirectLink = findProperty(rest, 'redirectLink')
if (obj) { if (redirectLink) {
let contact = await _lookupContact(rest, authentication, crmPhone, companyId, crmFirstName)
const { contactId } = contact
const url = redirectLink?.request?.url?.replace(/crmContactId/g, contactId)
console.log('===============> Edit url rediret sended to hitphone: ', url)
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 { url } = obj.request
let contact = await lookupContactByPhone(rest, authentication, crmPhone, companyId) let contact = await _lookupContact(rest, authentication, crmPhone, companyId, crmFirstName)
if (!contact?.exist) {
contact = await createContact(companyId, rest, authentication, crmPhone, crmFirstName)
}
const crm = await CRM.findOne({ const crm = await CRM.findOne({
companyId, crmBaseURL: new URL(url.trim()).hostname companyId, crmBaseURL: new URL(url.trim()).hostname
@ -68,6 +81,7 @@ async function ticketCRM(companyId, crmPhone, crmAgent, crmFirstName = 'Username
} }
} }
//
} }
@ -76,6 +90,15 @@ async function ticketCRM(companyId, crmPhone, crmAgent, crmFirstName = 'Username
module.exports = ticketCRM module.exports = ticketCRM
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
}
async function _createTicket(rest, crmPhone, companyId, authentication, crmFirstName, contact, crmAgent) { async function _createTicket(rest, crmPhone, companyId, authentication, crmFirstName, contact, crmAgent) {
let obj = findProperty(rest, 'createTicketRecord') let obj = findProperty(rest, 'createTicketRecord')