fix(integration): The companyId was missing when testing the integration between the Salesforce JSON template and the API. This change ensures it's properly passed to the function.

master
adriano 2025-04-10 15:28:38 -03:00
parent 5e0402699b
commit 6497728899
8 changed files with 452 additions and 8 deletions

View File

@ -0,0 +1,235 @@
{
"authentication":{
"type":"bearer",
"token":"pat-na1-37da6668-e0b1-44cb-bd2d-596f5f65634a",
"crmPhoneTest":"5514987659932"
},
"crmRest":[
{
"createContactRecord":{
"request":{
"requestContentType":"application/json",
"requestEncoding":"Json",
"requestType":"Post",
"responseType":"Json",
"url":"https://api.hubapi.com/contacts/v1/contact"
},
"body":{
"properties":[
{
"property":"phone",
"value":"crmPhone"
}
]
},
"response":{
"id":"vid"
}
}
},
{
"lookupContactByPhone":{
"request":{
"requestContentType":"application/json",
"requestEncoding":"Json",
"requestType":"Get",
"responseType":"Json",
"url":"https://api.hubapi.com/contacts/v1/search/query?q=crmPhone"
},
"response":{
"phone":"contacts.properties.phone.value",
"id":"contacts.vid"
}
}
},
{
"callJournaling":{
"request":{
"requestContentType":"application/json",
"requestEncoding":"Json",
"requestType":"Post",
"responseType":"Json",
"url":"https://api.hubapi.com/engagements/v1/engagements"
},
"calls":[
{
"inboundAnsweredCall":{
"engagement":{
"active":true,
"type":"CALL"
},
"associations":{
"contactIds":[
{
"_prop":"crmContactId",
"_type":"number"
}
]
},
"metadata":{
"toNumber":{
"_prop":"crmAgent",
"_type":"string"
},
"fromNumber":{
"_prop":"crmPhone",
"_type":"string"
},
"status":"COMPLETED",
"durationMilliseconds":{
"_prop":"crmCallDuration",
"_type":"number",
"_format":"milliseconds"
},
"body":"Ligação recebida - inbound call",
"disposition":"f240bbac-87c9-4f6e-bf70-924b57d47db7"
}
}
},
{
"inboundMissedCall":{
"engagement":{
"active":true,
"type":"CALL"
},
"associations":{
"contactIds":[
{
"_prop":"crmContactId",
"_type":"number"
}
]
},
"metadata":{
"toNumber":{
"_prop":"crmAgent",
"_type":"string"
},
"fromNumber":{
"_prop":"crmPhone",
"_type":"string"
},
"status":"COMPLETED",
"body":"Ligação perdida - inbound call",
"disposition":"f240bbac-87c9-4f6e-bf70-924b57d47db7"
}
}
},
{
"outboundAnsweredCall":{
"engagement":{
"active":true,
"type":"CALL"
},
"associations":{
"contactIds":[
{
"_prop":"crmContactId",
"_type":"number"
}
]
},
"metadata":{
"fromNumber":{
"_prop":"crmPhone",
"_type":"string"
},
"toNumber":{
"_prop":"crmAgent",
"_type":"string"
},
"status":"COMPLETED",
"durationMilliseconds":{
"_prop":"crmCallDuration",
"_type":"number",
"_format":"milliseconds"
},
"body":"Ligação atendida - outbound call",
"disposition":"f240bbac-87c9-4f6e-bf70-924b57d47db7"
}
}
},
{
"outboundUnansweredCall":{
"engagement":{
"active":true,
"type":"CALL"
},
"associations":{
"contactIds":[
{
"_prop":"crmContactId",
"_type":"number"
}
]
},
"metadata":{
"fromNumber":{
"_prop":"crmAgent",
"_type":"string"
},
"toNumber":{
"_prop":"crmPhone",
"_type":"string"
},
"status":"COMPLETED",
"body":"Ligação perdida - oubound call",
"disposition":"f240bbac-87c9-4f6e-bf70-924b57d47db7"
}
}
}
]
}
},
{
"createTicketRecord":{
"request":{
"requestContentType":"application/json",
"requestEncoding":"Json",
"requestType":"Post",
"responseType":"Json",
"url":"https://api.hubapi.com/crm/v3/objects/tickets"
},
"body":{
"properties":{
"hs_pipeline":"0",
"hs_pipeline_stage":"1",
"hs_ticket_priority":"HIGH",
"subject":"Teste"
},
"associations":[
{
"to":{
"id":"crmContactId"
},
"types":[
{
"associationCategory":"HUBSPOT_DEFINED",
"associationTypeId":16
}
]
}
]
},
"response":{
"id":"id"
}
}
},
{
"lookupTicket":{
"request":{
"requestContentType":"application/json",
"requestEncoding":"Json",
"requestType":"Get",
"responseType":"Json",
"url":"https://api.hubapi.com/crm/v3/objects/tickets/ticketId"
},
"response":{
"status":"properties.hs_pipeline_stage",
"id":"id"
}
}
}
]
}

View File

@ -0,0 +1,181 @@
{
"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"
}
}
},
{
"tokenEndpoint":{
"request":{
"requestContentType":"none",
"requestEncoding":"Json",
"requestType":"Post",
"responseType":"Json",
"url":"https://login.salesforce.com/services/oauth2/token"
}
}
},
{
"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",
"LeadSource":"Phone Inquiry",
"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"
}
}
]
}
},
{
"chatJournaling":{
"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"
},
"chats":[
{
"chatDone":{
"WhoId":"crmContactId",
"Subject":"WhatsApp Chat",
"Description":"Conversation started via WhatsApp. Conversation link: chatLink",
"ActivityDate":"YYYY-MM-DD",
"Status":"Completed",
"Priority":"Normal"
}
}
]
}
},
{
"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

@ -263,7 +263,7 @@ const oauthCallBack = async (req, res) => {
const testTemplate = async (req, res) => { const testTemplate = async (req, res) => {
const { clientId, companyId } = req.body let { clientId, companyId } = req.body
let crmOauth = await CRM.findOne({ 'crm.authentication.crmClientId': clientId, 'companyId': companyId, testing: true }) let crmOauth = await CRM.findOne({ 'crm.authentication.crmClientId': clientId, 'companyId': companyId, testing: true })
@ -273,6 +273,7 @@ const testTemplate = async (req, res) => {
crmOauth = crmOauth.toObject() crmOauth = crmOauth.toObject()
const { crmPhoneTest } = crmOauth.crm.authentication const { crmPhoneTest } = crmOauth.crm.authentication
await templateValidator(crmPhoneTest, crmOauth.crm, companyId) await templateValidator(crmPhoneTest, crmOauth.crm, companyId)
crmOauth = await CRM.findOne({ 'crm.authentication.crmClientId': clientId, 'companyId': companyId }) crmOauth = await CRM.findOne({ 'crm.authentication.crmClientId': clientId, 'companyId': companyId })

View File

@ -53,7 +53,28 @@ async function createContact(companyId, rest, authentication, crmPhone, crmFirst
console.log('CREATE CONTACT PAYLOAD: ', JSON.stringify(config, null, 6)) console.log('CREATE CONTACT PAYLOAD: ', JSON.stringify(config, null, 6))
console.log('#####################') console.log('#####################')
let { data } = await axios(config) let data
try {
let { data: _data } = await axios(config)
data = _data
} catch (error) {
if (error.response) {
console.error('==================> createContact Erro na resposta da API:', {
status: error.response.status,
data: error.response.data,
})
}
else if (error.request) {
console.error('==================> createContact Nenhuma resposta recebida da API:', error.request)
}
else {
console.error('==================> createContact Erro ao configurar a request:', error.message)
}
throw error
}
// let { data } = await axios(config)
data = flatten(data) data = flatten(data)

View File

@ -92,6 +92,7 @@ const exchangeForTokens = async (crmOauth, exchangeProof, companyId) => {
const refreshAccessToken = async (clientId, companyId) => { const refreshAccessToken = async (clientId, companyId) => {
let crmOauth = await CRM.findOne({ 'crm.authentication.crmClientId': clientId, companyId }) let crmOauth = await CRM.findOne({ 'crm.authentication.crmClientId': clientId, companyId })
crmOauth = crmOauth.toObject() crmOauth = crmOauth.toObject()
const refreshTokenProof = { const refreshTokenProof = {

View File

@ -33,6 +33,7 @@ async function templateValidator(crmPhoneTest, crm, companyId) {
} }
else { else {
msg = `Contato de numero ${phoneTest} já existe no crm` msg = `Contato de numero ${phoneTest} já existe no crm`
console.log("======> msg testing: ", msg)
sendMessageSocket({ companyId, status: 'processing', data: { msg } }) sendMessageSocket({ companyId, status: 'processing', data: { msg } })
} }
@ -43,6 +44,8 @@ async function templateValidator(crmPhoneTest, crm, companyId) {
msg = `Tentando inserir registro de chamada(call journal) ${Object.keys(operation)[0]} para o contato ${phoneTest}` msg = `Tentando inserir registro de chamada(call journal) ${Object.keys(operation)[0]} para o contato ${phoneTest}`
console.log("======> msg testing: ", msg, " | companyId: ", companyId)
await journalingRequest(request, await journalingRequest(request,
body = operation[Object.keys(operation)[0]], body = operation[Object.keys(operation)[0]],
crmCallDuration = "900", crmCallDuration = "900",
@ -50,9 +53,11 @@ async function templateValidator(crmPhoneTest, crm, companyId) {
crmAgent = "2245", crmAgent = "2245",
crmPhone = phoneTest, crmPhone = phoneTest,
authentication = crm.authentication, authentication = crm.authentication,
test = { testing: true, msg, companyId }) test = { testing: true, msg, companyId },
companyId)
msg = `Registro de chamada inserido com sucesso ${Object.keys(operation)[0]} para o contato ${phoneTest}` msg = `Registro de chamada inserido com sucesso ${Object.keys(operation)[0]} para o contato ${phoneTest}`
console.log("======> msg testing: ", msg)
sendMessageSocket({ companyId, status: 'processing', data: { msg } }) sendMessageSocket({ companyId, status: 'processing', data: { msg } })
} }