feat: Add find by client_id and companyId

master
adriano 2025-01-14 15:23:45 -03:00
parent 64869abc23
commit 1bdc545b4f
12 changed files with 134 additions and 69 deletions

View File

@ -75,54 +75,60 @@ const deleteCompany = async (req, res) => {
const callJournaling = async (req, res) => {
let { companyId, operation, crmPhone, crmAgent, crmCallDuration, crmFirstName, operationStatus } = req.body
try {
let { companyId, operation, crmPhone, crmAgent, crmCallDuration, crmFirstName, operationStatus } = req.body
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)
if (companyId == "1")
companyId = '99'
console.log('========> COMPANY ID after: ', companyId)
if (!crmAgent)
crmAgent = "0000"
//
mustContainProperties(req, ['companyId', 'operation', 'crmPhone', /*'crmAgent'*/,])
// if (operation == 'inboundAnsweredCall' && !crmCallDuration)
// throw new CustomError.BadRequestError(`The crmCallDuration property must be provided when operation is inboundAnsweredCall`)
// if (operation == 'outboundAsweredCall' && !crmCallDuration)
// throw new CustomError.BadRequestError(`The crmCallDuration property must be provided when operation is outboundAsweredCall`)
if (!crmCallDuration || crmCallDuration.trim() == "" || crmCallDuration == "0")
crmCallDuration = "10"
if (operationStatus == "hangup")
await journaling(companyId, operation, crmPhone, crmAgent, crmCallDuration, crmFirstName)
else if (operationStatus == "update-answer") {
console.log('update-answer')
await ticketCRM(companyId, crmPhone, crmAgent, crmFirstName)
const resp = await redirectContactLinkCRM(companyId, crmPhone, crmAgent, crmFirstName)
return res.status(StatusCodes.OK).json({ contact: resp })
// 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)
if (companyId == "1")
companyId = '99'
console.log('========> COMPANY ID after: ', companyId)
if (!crmAgent)
crmAgent = "0000"
//
mustContainProperties(req, ['companyId', 'operation', 'crmPhone', /*'crmAgent'*/,])
// if (operation == 'inboundAnsweredCall' && !crmCallDuration)
// throw new CustomError.BadRequestError(`The crmCallDuration property must be provided when operation is inboundAnsweredCall`)
// if (operation == 'outboundAsweredCall' && !crmCallDuration)
// throw new CustomError.BadRequestError(`The crmCallDuration property must be provided when operation is outboundAsweredCall`)
if (!crmCallDuration || crmCallDuration.trim() == "" || crmCallDuration == "0")
crmCallDuration = "10"
if (operationStatus == "hangup")
await journaling(companyId, operation, crmPhone, crmAgent, crmCallDuration, crmFirstName)
else if (operationStatus == "update-answer") {
console.log('update-answer')
await ticketCRM(companyId, crmPhone, crmAgent, crmFirstName)
const resp = await redirectContactLinkCRM(companyId, crmPhone, crmAgent, crmFirstName)
return res.status(StatusCodes.OK).json({ contact: resp })
}
res.status(StatusCodes.OK).send()
} catch (error) {
console.log(`[ERROR - ${new Date()}] Erro no Call Journaling`, error?.response?.data)
res.status(StatusCodes.INTERNAL_SERVER_ERROR).send()
}
res.status(StatusCodes.OK).send()
}
@ -158,7 +164,7 @@ const install = async (req, res) => {
console.log('--------> authUrl: ', authUrl)
// Store the authUrl in the session
req.session.authUrl = authUrl
req.session.authUrl = authUrl + `&companyId=${companyId}`
res.redirect(authUrl)
}
@ -174,9 +180,13 @@ const oauthCallBack = async (req, res) => {
const parsedUrl = new URL(storedAuthUrl)
const clientId = parsedUrl.searchParams.get('client_id')
const companyId = parsedUrl.searchParams.get('companyId')
console.log('xxxxxxxxxx clientId: ', clientId)
console.log('xxxxxxxxxx companyId: ', companyId)
if (code) {
let crmOauth = await CRM.findOne({ 'crm.authentication.crmClientId': clientId })
let crmOauth = await CRM.findOne({ 'crm.authentication.crmClientId': clientId, 'companyId': companyId })
crmOauth = crmOauth.toObject()
let authCodeProof = {
@ -213,7 +223,7 @@ const oauthCallBack = async (req, res) => {
// Exchange the authorization code for an access token and refresh token
await exchangeForTokens(crmOauth, authCodeProof)
const url = `${process.env.URL_OAUTH_FRONTEND_SUCCESS_REDIRECT}?clientId=${encodeURIComponent(clientId)}`
const url = `${process.env.URL_OAUTH_FRONTEND_SUCCESS_REDIRECT}?clientId=${encodeURIComponent(clientId)}&codWeb=${companyId}`
return res.redirect(url)
// return res.redirect(process.env.URL_OAUTH_FRONTEND_SUCCESS_REDIRECT)
@ -226,7 +236,7 @@ const testTemplate = async (req, res) => {
const { clientId, companyId } = req.body
let crmOauth = await CRM.findOne({ 'crm.authentication.crmClientId': clientId, testing: true })
let crmOauth = await CRM.findOne({ 'crm.authentication.crmClientId': clientId, 'companyId': companyId, testing: true })
if (!crmOauth)
return res.status(StatusCodes.OK).send()
@ -236,7 +246,7 @@ const testTemplate = async (req, res) => {
const { crmPhoneTest } = crmOauth.crm.authentication
await templateValidator(crmPhoneTest, crmOauth.crm, companyId)
crmOauth = await CRM.findOne({ 'crm.authentication.crmClientId': clientId })
crmOauth = await CRM.findOne({ 'crm.authentication.crmClientId': clientId, 'companyId': companyId })
crmOauth.testing = false
await crmOauth.save()

View File

@ -30,7 +30,29 @@ async function lookupContactByPhone(rest, authentication, crmPhone, companyId, t
sendMessageSocket({ companyId, status: 'processing', data: { request: config, msg } })
}
let { data } = await axios(config)
let data
try {
let { data: _data } = await axios(config)
data = _data
} catch (error) {
if (error.response) {
console.error('==================> Erro na resposta da API:', {
status: error.response.status,
data: error.response.data,
})
}
else if (error.request) {
console.error('==================> Nenhuma resposta recebida da API:', error.request)
}
else {
console.error('==================> Erro ao configurar a request:', error.message)
}
throw error
}
console.log('DATA: ', JSON.stringify(data, null, 6))

View File

@ -26,9 +26,35 @@ const exchangeForTokens = async (crmOauth, exchangeProof) => {
}
}
console.log('CONFIG: ', config)
console.log('======> CONFIG: ', config)
const { data } = await axios(config)
// const { data } = await axios(config)
//test
let data
try {
let { data: _data } = await axios(config)
data = _data
} catch (error) {
if (error.response) {
console.error('==================> oauth2 Erro na resposta da API:', {
status: error.response.status,
data: error.response.data,
})
}
else if (error.request) {
console.error('==================> oauth2 Nenhuma resposta recebida da API:', error.request)
}
else {
console.error('==================> oauth2 Erro ao configurar a request:', error.message)
}
throw error
}
//
const { refresh_token, access_token, token_type, expires_in, issued_at } = data

View File

@ -55,6 +55,7 @@ async function _lookupContact(rest, authentication, crmPhone, companyId, crmFirs
let contact = await lookupContactByPhone(rest, authentication, crmPhone, companyId)
if (contact?.exist) {
return { created: false, contactId: contact.contactId }
}

View File

@ -41,15 +41,19 @@ async function sfcase(companyId, crmPhone) {
let config = await requestConfigHeader(url, crmPhone, requestType, requestContentType, type, userName, passWord, token, crmClientId, body)
console.log("====> config", config)
console.log("====> accountId: ", accountId, " | contactId: ", contactId)
const complementarInformations = {
AccountId: accountId
}
if (!config?.data?.ContactId && !config?.data?.ContactId === "") {
complementarInformations.ContactId = contactId
}
if (accountId && contactId)
config.data = {
...config.data, ...{
ContactId: contactId,
AccountId: accountId
}
...config.data,
...complementarInformations
}
console.log("====> create case request config: ", config)
@ -75,4 +79,4 @@ async function sfcase(companyId, crmPhone) {
}
module.exports = sfcase
module.exports = sfcase

View File

@ -1,13 +1,13 @@
{
"files": {
"main.css": "/static/css/main.ae60ab08.css",
"main.js": "/static/js/main.2166c932.js",
"main.js": "/static/js/main.b7e32f90.js",
"index.html": "/index.html",
"main.ae60ab08.css.map": "/static/css/main.ae60ab08.css.map",
"main.2166c932.js.map": "/static/js/main.2166c932.js.map"
"main.b7e32f90.js.map": "/static/js/main.b7e32f90.js.map"
},
"entrypoints": [
"static/css/main.ae60ab08.css",
"static/js/main.2166c932.js"
"static/js/main.b7e32f90.js"
]
}

View File

@ -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.2166c932.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.b7e32f90.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

File diff suppressed because one or more lines are too long

View File

@ -85,14 +85,16 @@ const UploadFile = () => {
setCodweb(codWebFromUrl)
console.log('clientId: ', clientId)
console.log('codWebFromURl: ', codWebFromUrl)
console.log('codWeb: ', codWeb)
if (clientId && codWeb) {
if (clientId && codWebFromUrl) {
const postData = async () => {
try {
await axios.post(`${process.env.REACT_APP_URL_API}/api/v1/crm/test`, {
clientId,
companyId: codWeb,
companyId: codWebFromUrl,
})
} catch (error) {
setStatus('error')