fix: temporary adjustment for Salesforce OAuth2 template handling

master
adriano 2024-09-24 10:23:08 -03:00
parent 75659672bf
commit 6691427b37
1 changed files with 34 additions and 18 deletions

View File

@ -39,7 +39,7 @@ const deleteCrm = async (req, res) => {
if (!crmBaseURL.startsWith('http://') && !crmBaseURL.startsWith('https://')) { if (!crmBaseURL.startsWith('http://') && !crmBaseURL.startsWith('https://')) {
crmBaseURL = `https://${crmBaseURL.trim()}` crmBaseURL = `https://${crmBaseURL.trim()}`
} }
const crm = await CRM.findOne({ const crm = await CRM.findOne({
companyId, crmBaseURL: new URL(crmBaseURL.trim()).hostname companyId, crmBaseURL: new URL(crmBaseURL.trim()).hostname
@ -77,7 +77,7 @@ const callJournaling = async (req, res) => {
console.log('========> COMPANY ID before: ', companyId) console.log('========> COMPANY ID before: ', companyId)
companyId = '40' companyId = '40'
console.log('========> COMPANY ID after: ', companyId) console.log('========> COMPANY ID after: ', companyId)
mustContainProperties(req, ['companyId', 'operation', 'crmPhone', 'crmAgent',]) mustContainProperties(req, ['companyId', 'operation', 'crmPhone', 'crmAgent',])
// if (operation == 'inboundAnsweredCall' && !crmCallDuration) // if (operation == 'inboundAnsweredCall' && !crmCallDuration)
@ -123,8 +123,8 @@ const oauthCallBack = async (req, res) => {
const parsedUrl = new URL(storedAuthUrl) const parsedUrl = new URL(storedAuthUrl)
const clientId = parsedUrl.searchParams.get('client_id') const clientId = parsedUrl.searchParams.get('client_id')
if (code) { if (code) {
let crmOauth = await CRM.findOne({ 'crm.authentication.crmClientId': clientId }) let crmOauth = await CRM.findOne({ 'crm.authentication.crmClientId': clientId })
crmOauth = crmOauth.toObject() crmOauth = crmOauth.toObject()
@ -133,19 +133,35 @@ const oauthCallBack = async (req, res) => {
client_id: crmOauth.crm.authentication.crmClientId, client_id: crmOauth.crm.authentication.crmClientId,
client_secret: crmOauth.crm.authentication.crmClientSecret, client_secret: crmOauth.crm.authentication.crmClientSecret,
redirect_uri: process.env.URL_OAUTH_CALLBACK, redirect_uri: process.env.URL_OAUTH_CALLBACK,
code
//remove this }
// refactor this. To salesforce only when using oauth2
code_verifier: `gwkPueV2GSzkFvGFiHbNjpRuq_XBEGBsihM59pMaiFalZrOQ_7J4hgtBR8GIbLHutcuUwba2k0xeKhD8ELjWEswE3xv-H2e6Jh8EOwOccj2i_rYPUlMGdPDqpuRs8D3w`,
code: `${code.split('%')[0]}`
//
//enable this
// code
}
// Refactor this. This is for salesforce only oauth2 that need a code_verifier. Try to find another
// way of gettig the code without need the code_verifier
const rest = crmOauth.crm.crmRest
const salesforceUrls = rest.map(endpoint => {
const key = Object.keys(endpoint)[0]
const url = endpoint[key].request.url
if (url.includes("salesforce")) {
return `${key} URL: ${url}`
}
}).filter(Boolean)
if (salesforceUrls.find(url => url.includes('salesforce'))) {
authCodeProof = {
...authCodeProof, ...{
code_verifier: `gwkPueV2GSzkFvGFiHbNjpRuq_XBEGBsihM59pMaiFalZrOQ_7J4hgtBR8GIbLHutcuUwba2k0xeKhD8ELjWEswE3xv-H2e6Jh8EOwOccj2i_rYPUlMGdPDqpuRs8D3w`,
code: `${code.split('%')[0]}`
}
}
}
//
// Exchange the authorization code for an access token and refresh token // Exchange the authorization code for an access token and refresh token
await exchangeForTokens(crmOauth, authCodeProof) 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)}`
@ -158,17 +174,17 @@ const oauthCallBack = async (req, res) => {
const testTemplate = async (req, res) => { const testTemplate = async (req, res) => {
const { clientId, companyId } = req.body 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, testing: true })
if (!crmOauth) if (!crmOauth)
return res.status(StatusCodes.OK).send() return res.status(StatusCodes.OK).send()
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 }) crmOauth = await CRM.findOne({ 'crm.authentication.crmClientId': clientId })
crmOauth.testing = false crmOauth.testing = false