feat: Added routes to create and update salesforce case
parent
b654ce11a9
commit
0c7612cb16
|
@ -0,0 +1,209 @@
|
|||
{
|
||||
"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/Contact"
|
||||
},
|
||||
"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+Contact+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": {}
|
||||
}
|
||||
},
|
||||
{
|
||||
"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"
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"createCase": {
|
||||
"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/Case"
|
||||
},
|
||||
"body": {
|
||||
|
||||
"Subject": "Assunto do Caso teste",
|
||||
"Description": "Descrição detalhada do caso test",
|
||||
"Status": "Novo",
|
||||
"Priority": "Alta",
|
||||
"Departamento__c": "Financeiro"
|
||||
|
||||
},
|
||||
"response": {
|
||||
"id": "id"
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
|
@ -166,7 +166,7 @@
|
|||
"chatDone":{
|
||||
"WhoId": "crmContactId",
|
||||
"Subject": "WhatsApp Chat",
|
||||
"Description": "Conversation started via WhatsApp.",
|
||||
"Description": "Conversation started via WhatsApp. Conversation link: chatLink",
|
||||
"ActivityDate": "YYYY-MM-DD",
|
||||
"Status": "Completed",
|
||||
"Priority": "Normal"
|
||||
|
|
|
@ -24,6 +24,10 @@ const axios = require('axios')
|
|||
const { get, set } = require('../utils/redisClient')
|
||||
const { getContactIdChatwoot, createConversation, toggleConversationStatus } = require('../utils/ScheduleTicketCRM')
|
||||
const timeStamp = require('../utils/toTimestamp')
|
||||
const whatsappJournalingCRM = require('../utils/whatsappJournalingCRM')
|
||||
const redirectContactLinkCRM = require('../utils/redirectContactLinkCRM')
|
||||
const sfcase = require('../utils/sfCase')
|
||||
const sfCaseUpdate = require('../utils/sfCaseUpdate')
|
||||
|
||||
const contactCreate = async (req, res) => {
|
||||
|
||||
|
@ -75,11 +79,18 @@ const callJournaling = async (req, res) => {
|
|||
|
||||
console.log('REQ.BODY CRM TESTe: ', JSON.stringify(req.body, null, 6))
|
||||
|
||||
|
||||
// test remove
|
||||
// return res.status(StatusCodes.OK).send()
|
||||
|
||||
// Refactor this in the future.
|
||||
crmPhone = '55' + crmPhone
|
||||
console.log('========> CRMPHONE: ', crmPhone)
|
||||
console.log('========> COMPANY ID before: ', companyId)
|
||||
companyId = '99'
|
||||
|
||||
if (companyId == "1")
|
||||
companyId = '99'
|
||||
|
||||
console.log('========> COMPANY ID after: ', companyId)
|
||||
|
||||
if (!crmAgent)
|
||||
|
@ -104,7 +115,9 @@ const callJournaling = async (req, res) => {
|
|||
else if (operationStatus == "update-answer") {
|
||||
console.log('update-answer')
|
||||
|
||||
const resp = await ticketCRM(companyId, crmPhone, crmAgent, crmFirstName)
|
||||
await ticketCRM(companyId, crmPhone, crmAgent, crmFirstName)
|
||||
|
||||
const resp = await redirectContactLinkCRM(companyId, crmPhone, crmAgent, crmFirstName)
|
||||
|
||||
return res.status(StatusCodes.OK).json({ contact: resp })
|
||||
}
|
||||
|
@ -112,6 +125,32 @@ const callJournaling = async (req, res) => {
|
|||
res.status(StatusCodes.OK).send()
|
||||
}
|
||||
|
||||
|
||||
const sfCreateCase = async (req, res) => {
|
||||
|
||||
const { companyId, crmPhone } = req.body
|
||||
|
||||
mustContainProperties(req, ['companyId', 'crmPhone',])
|
||||
|
||||
const sfCaseRes = await sfcase(companyId, crmPhone)
|
||||
|
||||
res.status(StatusCodes.OK).json({ ...sfCaseRes })
|
||||
}
|
||||
|
||||
const sfUpdateCase = async (req, res) => {
|
||||
|
||||
const { companyId, caseId, case: caseUpdate } = req.body
|
||||
|
||||
mustContainProperties(req, ['companyId', 'caseId', 'case'])
|
||||
|
||||
const resCaseUpdate = await sfCaseUpdate(companyId, caseId, caseUpdate)
|
||||
|
||||
if (resCaseUpdate) res.status(StatusCodes.OK).send()
|
||||
|
||||
if (!resCaseUpdate) res.status(StatusCodes.UNPROCESSABLE_ENTITY).send()
|
||||
|
||||
}
|
||||
|
||||
const install = async (req, res) => {
|
||||
|
||||
const { authUrl, companyId } = req.query
|
||||
|
@ -220,6 +259,8 @@ const uploadCrmConfig = async (req, res) => {
|
|||
|
||||
newCrm = JSON.parse(newCrm)
|
||||
|
||||
// console.log('===========> NEW CRM: ', JSON.stringify(newCrm, null, 6))
|
||||
|
||||
const index = newCrm.crmRest.findIndex(rest => rest?.createContactRecord)
|
||||
|
||||
const crmBaseURL = new URL(newCrm.crmRest[index].createContactRecord.request.url.trim()).hostname
|
||||
|
@ -307,6 +348,8 @@ const webhook = async (req, res) => {
|
|||
|
||||
let { name, phone_number } = req.body.meta.sender
|
||||
|
||||
const ticketId = req.body?.id
|
||||
|
||||
// const accountId = req.body.account_id
|
||||
const accountId = "15"
|
||||
|
||||
|
@ -324,11 +367,16 @@ const webhook = async (req, res) => {
|
|||
console.log('=======> accountId: ', accountId)
|
||||
console.log('=======> companyId: ', obj.companyId)
|
||||
|
||||
await ticketCRM(
|
||||
console.log('=======> ticketId: ', ticketId)
|
||||
|
||||
|
||||
await whatsappJournalingCRM(
|
||||
companyId = obj.companyId,
|
||||
crmPhone = crmPhone,
|
||||
crmAgent = '0000',
|
||||
crmFirstName = name)
|
||||
crmFirstName = name,
|
||||
ticketId
|
||||
)
|
||||
|
||||
res.status(StatusCodes.OK).send()
|
||||
}
|
||||
|
@ -429,7 +477,9 @@ module.exports = {
|
|||
testTemplate,
|
||||
getCrms,
|
||||
webhook,
|
||||
webhook_crm
|
||||
webhook_crm,
|
||||
sfCreateCase,
|
||||
sfUpdateCase
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
"accountId":"15",
|
||||
"api":{
|
||||
"url":"http://172.31.187.47:3333",
|
||||
"urlHttps":"https://el-vendas.omnihit.app.br",
|
||||
"token": "WpeGuicvuQ3pyLvYQ11eAxxL"
|
||||
},
|
||||
"createConversation":{
|
||||
|
|
|
@ -54,7 +54,6 @@ const ChatJournalingSchema = new Schema({
|
|||
response: Object
|
||||
})
|
||||
|
||||
|
||||
const RedirectUrlSchema = new Schema({
|
||||
url: {
|
||||
type: String,
|
||||
|
@ -95,7 +94,12 @@ const CRMRestSchema = new Schema({
|
|||
request: RedirectUrlSchema
|
||||
},
|
||||
callJournaling: CallJournalingSchema,
|
||||
chatJournaling: ChatJournalingSchema
|
||||
chatJournaling: ChatJournalingSchema,
|
||||
createCase: {
|
||||
request: RequestSchema,
|
||||
body: Object,
|
||||
response: Object
|
||||
},
|
||||
})
|
||||
|
||||
const AuthenticationSchema = new Schema({
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
const express = require('express')
|
||||
const router = express.Router()
|
||||
const { authorization, } = require('../middleware/authentication')
|
||||
const { contactCreate, testTemplate, webhook_crm, uploadCrmConfig, callJournaling, oauthCallBack, install, deleteCrm, deleteCompany, getCrms, webhook } = require('../controllers/crmController')
|
||||
const { contactCreate, sfCreateCase, sfUpdateCase, testTemplate, webhook_crm, uploadCrmConfig, callJournaling, oauthCallBack, install, deleteCrm, deleteCompany, getCrms, webhook } = require('../controllers/crmController')
|
||||
const { fileUpload } = require("../utils")
|
||||
|
||||
router.route('/create-contact').post(authorization, contactCreate)
|
||||
|
@ -9,6 +9,8 @@ router.route('/call-journaling').post(authorization, callJournaling)
|
|||
router.route('/upload').post(fileUpload.single('crm'), authorization, uploadCrmConfig)
|
||||
router.route('/delete').post(authorization, deleteCrm)
|
||||
router.route('/delete-company').post(authorization, deleteCompany)
|
||||
router.route('/salesforce/case').post(authorization, sfCreateCase)
|
||||
router.route('/salesforce/case').patch(authorization, sfUpdateCase)
|
||||
router.route('/oauth-callback').get(oauthCallBack)
|
||||
router.route('/install').get(install)
|
||||
router.route('/test').post(testTemplate)
|
||||
|
|
|
@ -19,6 +19,8 @@ async function journalingRequest(request, body, crmCallDuration, contact, crmAge
|
|||
|
||||
for (let key in body) {
|
||||
|
||||
// console.log('----------> key: ', key)
|
||||
|
||||
const k = Object.keys(body).find(k => {
|
||||
if (!ignore.includes(k) && k.includes('._prop') && k.replace('._prop', '._type') ==
|
||||
key.replace('._prop', '._type')) {
|
||||
|
@ -98,22 +100,23 @@ async function journalingRequest(request, body, crmCallDuration, contact, crmAge
|
|||
break
|
||||
}
|
||||
|
||||
// switch (true){
|
||||
// case body[key].includes('chatLink'):
|
||||
// console.log('----------> includes chatlink: ', body[key])
|
||||
// body[key] = body[key].replace('chatLink', 'https://el-vendas.omnihit.app.br/app/accounts/15/conversations/81')
|
||||
// break
|
||||
// }
|
||||
|
||||
// console.log('----------> body[key]: ', body[key])
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
const data = unflatten(body)
|
||||
|
||||
|
||||
// Refactor this. Some times the crmCallDuration is comming with the value like 'crmCallDuration' instead of number when
|
||||
// creating call journaling for salesforce
|
||||
// if (url.includes('salesforce')) {
|
||||
// for (let key in data) {
|
||||
// if (data[key] == 'crmCallDuration') {
|
||||
// data[key] = 300
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
|
||||
const { type, userName, passWord, token, crmClientId } = authentication
|
||||
|
||||
const config = await requestConfigHeader(url, crmPhone, requestType, requestContentType, type, userName, passWord, token, crmClientId, data)
|
||||
|
|
|
@ -10,7 +10,7 @@ const findProperty = require('./findProperty')
|
|||
const requestConfigHeader = require('./requestConfigHeader')
|
||||
const sendMessageSocket = require('./sendMessageSocket')
|
||||
|
||||
async function lookupContactByPhone(rest, authentication, crmPhone, companyId, test = {}) {
|
||||
async function lookupContactByPhone(rest, authentication, crmPhone, companyId, test = {}, cacheContact = true) {
|
||||
|
||||
let { request, body, response } = findProperty(rest, 'lookupContactByPhone')
|
||||
|
||||
|
@ -25,7 +25,7 @@ async function lookupContactByPhone(rest, authentication, crmPhone, companyId, t
|
|||
|
||||
const config = await requestConfigHeader(url, crmPhone, requestType, requestContentType, type, userName, passWord, token, crmClientId)
|
||||
|
||||
if (test?.testing){
|
||||
if (test?.testing) {
|
||||
let msg = `Tentanto checar se o contato de numero ${crmPhone} existe no crm`
|
||||
sendMessageSocket({ companyId, status: 'processing', data: { request: config, msg } })
|
||||
}
|
||||
|
@ -38,6 +38,7 @@ async function lookupContactByPhone(rest, authentication, crmPhone, companyId, t
|
|||
|
||||
let auxPhone
|
||||
let auxContactId
|
||||
let auxName
|
||||
|
||||
for (const prop in data) {
|
||||
|
||||
|
@ -55,11 +56,19 @@ async function lookupContactByPhone(rest, authentication, crmPhone, companyId, t
|
|||
|
||||
}
|
||||
|
||||
console.log('===========> !auxPhone && !auxContactId: ', !auxPhone && !auxContactId)
|
||||
|
||||
|
||||
if (!auxPhone && !auxContactId) {
|
||||
for (const prop in data) {
|
||||
|
||||
let _prop = prop.replace(/\.(\d+)(\.|$)/g, '[$1]$2')
|
||||
|
||||
// SALESFORCE GETTING THE NAME
|
||||
if (_prop == response?.name?.trim()) {
|
||||
auxName = data[prop]
|
||||
}
|
||||
|
||||
if (_prop == response?.phone?.trim()) {
|
||||
auxPhone = data[prop].replace('+', '')
|
||||
}
|
||||
|
@ -75,12 +84,12 @@ async function lookupContactByPhone(rest, authentication, crmPhone, companyId, t
|
|||
|
||||
if (auxPhone) {
|
||||
|
||||
if (auxPhone && auxContactId) {
|
||||
if (auxPhone && auxContactId && cacheContact) {
|
||||
const crm = await CRM.findOne({ companyId, crmBaseURL: new URL(url).hostname })
|
||||
await CRM_Contact.create({ companyId, crm, crmBaseURL: new URL(url).hostname, contactId: auxContactId, phone: auxPhone })
|
||||
}
|
||||
|
||||
return { exist: true, contactId: auxContactId, phone: crmPhone }
|
||||
return { exist: true, contactId: auxContactId, phone: crmPhone, name: auxName }
|
||||
}
|
||||
|
||||
return { exist: false }
|
||||
|
|
|
@ -0,0 +1,69 @@
|
|||
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')
|
||||
const sendEventTicketCreatedToSocket = require('./sendEventTicketCreatedToSocket')
|
||||
const journalingRequest = require('./journalingRequest')
|
||||
|
||||
|
||||
async function redirectContactLinkCRM(companyId, crmPhone, crmAgent, crmFirstName = 'Username') {
|
||||
|
||||
const crmFiles = await loadCRM(companyId)
|
||||
|
||||
const crmContactIds = []
|
||||
|
||||
for (const crmConfig of crmFiles) {
|
||||
|
||||
const { crmRest: rest, authentication } = crmConfig.crm
|
||||
|
||||
// Send the edited contact/lead link url to hitphone to open on another browser tab
|
||||
let redirectLink = findProperty(rest, 'redirectLink')
|
||||
|
||||
if (redirectLink) {
|
||||
let contact = await _lookupContact(rest, authentication, crmPhone, companyId, crmFirstName)
|
||||
|
||||
const { contactId, created } = contact
|
||||
|
||||
const url = redirectLink?.request?.url?.replace(/crmContactId/g, contactId)
|
||||
|
||||
console.log('===============> Edit url rediret sended to hitphone: ', url)
|
||||
|
||||
console.log('new URL(url).hostname: ', new URL(url).hostname)
|
||||
|
||||
crmContactIds.push({ crm: new URL(url).hostname, contactId, created })
|
||||
|
||||
// sendEventTicketCreatedToSocket({ companyId, extension: crmAgent, ticketUrl: url })
|
||||
}
|
||||
//
|
||||
|
||||
}
|
||||
|
||||
return crmContactIds
|
||||
|
||||
}
|
||||
|
||||
module.exports = redirectContactLinkCRM
|
||||
|
||||
|
||||
async function _lookupContact(rest, authentication, crmPhone, companyId, crmFirstName) {
|
||||
|
||||
let contact = await lookupContactByPhone(rest, authentication, crmPhone, companyId)
|
||||
|
||||
if (contact?.exist) {
|
||||
return { created: false, contactId: contact.contactId }
|
||||
}
|
||||
|
||||
if (!contact?.exist) {
|
||||
contact = await createContact(companyId, rest, authentication, crmPhone, crmFirstName)
|
||||
}
|
||||
|
||||
return { created: true, contactId: contact.contactId }
|
||||
}
|
||||
|
||||
|
|
@ -0,0 +1,67 @@
|
|||
const loadCRM = require('./loadCRM')
|
||||
const lookupContactByPhone = require('./lookupCRMContactByPhone')
|
||||
const findProperty = require('./findProperty')
|
||||
const axios = require('axios')
|
||||
const requestConfigHeader = require('./requestConfigHeader')
|
||||
|
||||
async function sfcase(companyId, crmPhone) {
|
||||
|
||||
const crmFiles = await loadCRM(companyId)
|
||||
|
||||
let res = { contactId: "" }
|
||||
|
||||
for (const crmConfig of crmFiles) {
|
||||
|
||||
const { crmRest: rest, authentication } = crmConfig.crm
|
||||
|
||||
let createCase = findProperty(rest, 'createCase')
|
||||
|
||||
console.log('===============> createCase: ', createCase)
|
||||
|
||||
if (createCase) {
|
||||
|
||||
let contact = await lookupContactByPhone(rest, authentication, crmPhone, companyId, {}, false)
|
||||
|
||||
console.log('==========> contact: ', contact)
|
||||
|
||||
if (!contact?.exist) {
|
||||
console.log('===============> ContactExist: ', JSON.stringify(contact, null, 6))
|
||||
break
|
||||
}
|
||||
|
||||
const { contactId, created } = contact
|
||||
|
||||
let { request, body } = createCase
|
||||
|
||||
console.log("====> request: ", request)
|
||||
console.log("====> body: ", body)
|
||||
|
||||
const { type, userName, passWord, token, crmClientId } = authentication
|
||||
const { requestContentType, requestEncoding, requestType, responseType, url } = request
|
||||
|
||||
const config = await requestConfigHeader(url, crmPhone, requestType, requestContentType, type, userName, passWord, token, crmClientId, body)
|
||||
|
||||
console.log("====> config: ", config)
|
||||
|
||||
try {
|
||||
const { data } = await axios(config)
|
||||
|
||||
console.log('Data from case created: ', JSON.stringify(data, null, 6))
|
||||
|
||||
res.contactId = contactId
|
||||
res.caseId = data.id
|
||||
|
||||
} catch (error) {
|
||||
console.log(`CASE CREATE ERROR: `, error)
|
||||
}
|
||||
}
|
||||
|
||||
break
|
||||
|
||||
}
|
||||
|
||||
return res
|
||||
|
||||
}
|
||||
|
||||
module.exports = sfcase
|
|
@ -0,0 +1,58 @@
|
|||
const loadCRM = require('./loadCRM')
|
||||
const lookupContactByPhone = require('./lookupCRMContactByPhone')
|
||||
const findProperty = require('./findProperty')
|
||||
const axios = require('axios')
|
||||
const requestConfigHeader = require('./requestConfigHeader')
|
||||
|
||||
async function sfCaseUpdate(companyId, caseId, caseUpdate) {
|
||||
|
||||
const crmFiles = await loadCRM(companyId)
|
||||
|
||||
for (const crmConfig of crmFiles) {
|
||||
|
||||
const { crmRest: rest, authentication } = crmConfig.crm
|
||||
|
||||
let createCase = findProperty(rest, 'createCase')
|
||||
|
||||
console.log('===============> createCase: ', createCase)
|
||||
|
||||
if (createCase) {
|
||||
|
||||
let { request, body } = createCase
|
||||
|
||||
console.log("====> request: ", request)
|
||||
console.log("====> body: ", body)
|
||||
|
||||
const { type, userName, passWord, token, crmClientId } = authentication
|
||||
const { requestContentType, requestEncoding, requestType, responseType, url } = request
|
||||
|
||||
let config = await requestConfigHeader(url, "17999999999", requestType, requestContentType, type, userName, passWord, token, crmClientId, body)
|
||||
|
||||
|
||||
config = {
|
||||
...config, ...{
|
||||
method: "Patch",
|
||||
url: `${url}/${caseId}`,
|
||||
data: { ...caseUpdate }
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
const { data } = await axios(config)
|
||||
|
||||
console.log('Data from case updated: ', JSON.stringify(data, null, 6))
|
||||
|
||||
return true
|
||||
|
||||
} catch (error) {
|
||||
console.log(`CASE UPDATED ERROR: `, error)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return false
|
||||
|
||||
}
|
||||
|
||||
module.exports = sfCaseUpdate
|
|
@ -16,53 +16,12 @@ async function ticketCRM(companyId, crmPhone, crmAgent, crmFirstName = 'Username
|
|||
|
||||
const crmFiles = await loadCRM(companyId)
|
||||
|
||||
const crmContactIds = []
|
||||
// const crmContactIds = []
|
||||
|
||||
for (const crmConfig of crmFiles) {
|
||||
|
||||
const { crmRest: rest, authentication } = crmConfig.crm
|
||||
|
||||
// Record whatsapp conversation that happened in omnihit v2
|
||||
let chatJournaling = findProperty(rest, 'chatJournaling')
|
||||
|
||||
console.log('===============> chatJournaling: ', chatJournaling)
|
||||
|
||||
|
||||
if (chatJournaling) {
|
||||
|
||||
let contact = await _lookupContact(rest, authentication, crmPhone, companyId, crmFirstName)
|
||||
|
||||
const { contactId, created } = contact
|
||||
|
||||
let { request, chats, response } = chatJournaling
|
||||
|
||||
let body = findProperty(chats, 'chatDone')
|
||||
|
||||
await journalingRequest(request, body, crmCallDuration = 0, contact, crmAgent, crmPhone, authentication, rest)
|
||||
}
|
||||
|
||||
|
||||
// Send the edited contact/lead link url to hitphone to open on another browser tab
|
||||
let redirectLink = findProperty(rest, 'redirectLink')
|
||||
|
||||
if (redirectLink) {
|
||||
let contact = await _lookupContact(rest, authentication, crmPhone, companyId, crmFirstName)
|
||||
|
||||
const { contactId, created } = contact
|
||||
|
||||
const url = redirectLink?.request?.url?.replace(/crmContactId/g, contactId)
|
||||
|
||||
console.log('===============> Edit url rediret sended to hitphone: ', url)
|
||||
|
||||
console.log('new URL(url).hostname: ', new URL(url).hostname)
|
||||
|
||||
crmContactIds.push({ crm: new URL(url).hostname, contactId, created })
|
||||
|
||||
// sendEventTicketCreatedToSocket({ companyId, extension: crmAgent, ticketUrl: url })
|
||||
}
|
||||
//
|
||||
|
||||
|
||||
// Send the ticket url link to hitphone to open on another browser tab
|
||||
let obj = findProperty(rest, 'lookupTicket')
|
||||
|
||||
|
@ -109,14 +68,14 @@ async function ticketCRM(companyId, crmPhone, crmAgent, crmFirstName = 'Username
|
|||
|
||||
const { contactId } = contact
|
||||
|
||||
crmContactIds.push({ crm: new URL(url).hostname, contactId, created })
|
||||
// crmContactIds.push({ crm: new URL(url).hostname, contactId, created })
|
||||
|
||||
}
|
||||
//
|
||||
|
||||
}
|
||||
|
||||
return crmContactIds
|
||||
// return crmContactIds
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,103 @@
|
|||
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')
|
||||
const sendEventTicketCreatedToSocket = require('./sendEventTicketCreatedToSocket')
|
||||
const journalingRequest = require('./journalingRequest')
|
||||
|
||||
const omnihitV2Integration = require('../data/omihitV2IntegrationCRM.json')
|
||||
|
||||
|
||||
async function whatsappJournalingCRM(companyId, crmPhone, crmAgent, crmFirstName = 'Username', ticketId=null) {
|
||||
|
||||
const crmFiles = await loadCRM(companyId)
|
||||
|
||||
const crmContactIds = []
|
||||
|
||||
for (const crmConfig of crmFiles) {
|
||||
|
||||
const { crmRest: rest, authentication } = crmConfig.crm
|
||||
|
||||
// Record whatsapp conversation that happened in omnihit v2
|
||||
let chatJournaling = findProperty(rest, 'chatJournaling')
|
||||
|
||||
console.log('===============> chatJournaling: ', chatJournaling)
|
||||
|
||||
|
||||
if (chatJournaling) {
|
||||
|
||||
let contact = await _lookupContact(rest, authentication, crmPhone, companyId, crmFirstName)
|
||||
|
||||
const { contactId, created } = contact
|
||||
|
||||
let { request, chats, response } = chatJournaling
|
||||
|
||||
let body = findProperty(chats, 'chatDone')
|
||||
|
||||
|
||||
|
||||
|
||||
const obj = omnihitV2Integration.find(o => o.companyId == companyId)
|
||||
|
||||
if (ticketId && obj) {
|
||||
|
||||
const { companyId,
|
||||
omnihit: { accountId, api: { url, urlHttps, token } = {},
|
||||
createConversation: { inbox_id, status, team_id } = {} } = {} } = obj
|
||||
|
||||
|
||||
console.log('===============> body1: ', body)
|
||||
body = JSON.stringify(body)
|
||||
|
||||
if (body.includes('chatLink')) {
|
||||
body = body.replace('chatLink', `${urlHttps}/app/accounts/${accountId}/conversations/${ticketId}`)
|
||||
}
|
||||
|
||||
console.log('===============> body2: ', body)
|
||||
body = JSON.parse(body)
|
||||
console.log('===============> body3: ', body)
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
await journalingRequest(request, body, crmCallDuration = 0, contact, crmAgent, crmPhone, authentication, rest)
|
||||
}
|
||||
//
|
||||
|
||||
|
||||
}
|
||||
|
||||
return crmContactIds
|
||||
|
||||
}
|
||||
|
||||
module.exports = whatsappJournalingCRM
|
||||
|
||||
|
||||
async function _lookupContact(rest, authentication, crmPhone, companyId, crmFirstName) {
|
||||
|
||||
let contact = await lookupContactByPhone(rest, authentication, crmPhone, companyId)
|
||||
|
||||
if (contact?.exist) {
|
||||
return { created: false, contactId: contact.contactId }
|
||||
}
|
||||
|
||||
if (!contact?.exist) {
|
||||
contact = await createContact(companyId, rest, authentication, crmPhone, crmFirstName)
|
||||
}
|
||||
|
||||
return { created: true, contactId: contact.contactId }
|
||||
}
|
||||
|
||||
|
|
@ -1,13 +1,13 @@
|
|||
{
|
||||
"files": {
|
||||
"main.css": "/static/css/main.ae60ab08.css",
|
||||
"main.js": "/static/js/main.fb177319.js",
|
||||
"main.js": "/static/js/main.e52625df.js",
|
||||
"index.html": "/index.html",
|
||||
"main.ae60ab08.css.map": "/static/css/main.ae60ab08.css.map",
|
||||
"main.fb177319.js.map": "/static/js/main.fb177319.js.map"
|
||||
"main.e52625df.js.map": "/static/js/main.e52625df.js.map"
|
||||
},
|
||||
"entrypoints": [
|
||||
"static/css/main.ae60ab08.css",
|
||||
"static/js/main.fb177319.js"
|
||||
"static/js/main.e52625df.js"
|
||||
]
|
||||
}
|
|
@ -1 +1 @@
|
|||
<!doctype html><html lang="en"><head><meta charset="utf-8"/><link rel="icon" href="/favicon.ico"/><meta name="viewport" content="width=device-width,initial-scale=1"/><meta name="theme-color" content="#000000"/><meta name="description" content="Web site created using create-react-app"/><link rel="apple-touch-icon" href="/logo192.png"/><link rel="manifest" href="/manifest.json"/><title>React App</title><script defer="defer" src="/static/js/main.fb177319.js"></script><link href="/static/css/main.ae60ab08.css" rel="stylesheet"></head><body><noscript>You need to enable JavaScript to run this app.</noscript><div id="root"></div></body></html>
|
||||
<!doctype html><html lang="en"><head><meta charset="utf-8"/><link rel="icon" href="/favicon.ico"/><meta name="viewport" content="width=device-width,initial-scale=1"/><meta name="theme-color" content="#000000"/><meta name="description" content="Web site created using create-react-app"/><link rel="apple-touch-icon" href="/logo192.png"/><link rel="manifest" href="/manifest.json"/><title>React App</title><script defer="defer" src="/static/js/main.e52625df.js"></script><link href="/static/css/main.ae60ab08.css" rel="stylesheet"></head><body><noscript>You need to enable JavaScript to run this app.</noscript><div id="root"></div></body></html>
|
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue