diff --git a/backend/controllers/crmController.js b/backend/controllers/crmController.js index 09d097b..696786e 100644 --- a/backend/controllers/crmController.js +++ b/backend/controllers/crmController.js @@ -39,7 +39,7 @@ const deleteCrm = async (req, res) => { if (!crmBaseURL.startsWith('http://') && !crmBaseURL.startsWith('https://')) { crmBaseURL = `https://${crmBaseURL.trim()}` - } + } const crm = await CRM.findOne({ companyId, crmBaseURL: new URL(crmBaseURL.trim()).hostname @@ -77,7 +77,7 @@ const callJournaling = async (req, res) => { console.log('========> COMPANY ID before: ', companyId) companyId = '40' console.log('========> COMPANY ID after: ', companyId) - + mustContainProperties(req, ['companyId', 'operation', 'crmPhone', 'crmAgent',]) // if (operation == 'inboundAnsweredCall' && !crmCallDuration) @@ -123,8 +123,8 @@ const oauthCallBack = async (req, res) => { const parsedUrl = new URL(storedAuthUrl) const clientId = parsedUrl.searchParams.get('client_id') - - if (code) { + + if (code) { let crmOauth = await CRM.findOne({ 'crm.authentication.crmClientId': clientId }) crmOauth = crmOauth.toObject() @@ -133,19 +133,35 @@ const oauthCallBack = async (req, res) => { client_id: crmOauth.crm.authentication.crmClientId, client_secret: crmOauth.crm.authentication.crmClientSecret, redirect_uri: process.env.URL_OAUTH_CALLBACK, - - //remove this - // refactor this. To salesforce only when using oauth2 - code_verifier: `gwkPueV2GSzkFvGFiHbNjpRuq_XBEGBsihM59pMaiFalZrOQ_7J4hgtBR8GIbLHutcuUwba2k0xeKhD8ELjWEswE3xv-H2e6Jh8EOwOccj2i_rYPUlMGdPDqpuRs8D3w`, - code: `${code.split('%')[0]}` - // - - //enable this - // code - } + 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 - await exchangeForTokens(crmOauth, authCodeProof) + await exchangeForTokens(crmOauth, authCodeProof) 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 { clientId, companyId } = req.body + const { clientId, companyId } = req.body let crmOauth = await CRM.findOne({ 'crm.authentication.crmClientId': clientId, testing: true }) if (!crmOauth) return res.status(StatusCodes.OK).send() - crmOauth = crmOauth.toObject() + crmOauth = crmOauth.toObject() 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.testing = false