From 89db3e83c940d34c86f99a24675e3fa0d65be448 Mon Sep 17 00:00:00 2001 From: adriano Date: Fri, 20 Jun 2025 12:57:24 -0300 Subject: [PATCH] feat: get company by omnihit accountId --- backend/controllers/crmController.js | 38 ++++++++++++++++++++-------- 1 file changed, 27 insertions(+), 11 deletions(-) diff --git a/backend/controllers/crmController.js b/backend/controllers/crmController.js index 821e263..7c73e0b 100644 --- a/backend/controllers/crmController.js +++ b/backend/controllers/crmController.js @@ -145,16 +145,16 @@ const callJournaling = async (req, res) => { res.status(StatusCodes.OK).send() } catch (error) { // console.log(`[ERROR - ${new Date()}] Erro no Call Journaling`, error?.response?.data) - + if (error.response) { console.error('==================> callJournaling Erro na resposta da API:', { status: error.response.status, data: error.response.data, }) - } + } else if (error.request) { console.error('==================> callJournaling Nenhuma resposta recebida da API:', error.request) - } + } else { console.error('==================> callJournaling Erro ao configurar a request:', error.message) } @@ -170,7 +170,7 @@ const sfCreateCase = async (req, res) => { const { companyId, crmPhone } = req.body - mustContainProperties(req, ['companyId', 'crmPhone',]) + mustContainProperties(req, ['companyId', 'crmPhone',]) const sfCaseRes = await sfcase(companyId, crmPhone) @@ -223,7 +223,7 @@ const oauthCallBack = async (req, res) => { if (code) { console.log('xxxxxxxxxx passed') - + let crmOauth = await CRM.findOne({ 'crm.authentication.crmClientId': clientId, 'companyId': companyId }) crmOauth = crmOauth.toObject() @@ -421,12 +421,12 @@ const webhook = async (req, res) => { if (!req.body?.meta) return res.status(StatusCodes.OK).send() - let { name, phone_number } = req.body.meta.sender - - const {id: ticketId, account_id: accountId} = req.body + let { name, phone_number } = req.body.meta.sender + + const { id: ticketId, account_id: accountId } = req.body //channel: 'Channel::FacebookPage' - console.log("2 =============> ticketId id: ", ticketId, " | accountId account_id: ",accountId) + console.log("2 =============> ticketId id: ", ticketId, " | accountId account_id: ", accountId) if (!phone_number) { @@ -437,11 +437,27 @@ const webhook = async (req, res) => { let crmPhone = phone_number.replace('+', '') + + // TEST + const company = await Company.findOne({ + integrations: { + $elemMatch: { + name: "omnihit", + "config.accountId": "15" + } + } + }) + + console.log("======> company omnihit: ", company) + // + const companies = [ // { companyId: "99", account_id: "1" }, { companyId: "99", account_id: "15" }, ] + + const obj = companies.find(c => +c.account_id == +accountId) console.log('=======> name: ', name) @@ -521,12 +537,12 @@ const webhook_crm = async (req, res) => { // const obj = omnihitV2Integration.find(o => o.companyId == companyId) - const config = await getIntegrationsConfig(companyId, 'omnihit') + const config = await getIntegrationsConfig(companyId, 'omnihit') if (config) { const { accountId, api: { url, token } = {}, - createConversation: { inbox_id, status, team_id } = {} = {} } = config + createConversation: { inbox_id, status, team_id } = {} = {} } = config const contactIdChatwoot = await getContactIdChatwoot(url, token, phone)