feat: implementation to save omnihit integration config in mongo
parent
2fed69eaee
commit
c721cead8f
|
@ -43,6 +43,7 @@ const cors = require('cors')
|
||||||
|
|
||||||
// routers
|
// routers
|
||||||
const crmRouter = require('./routes/crmRoute')
|
const crmRouter = require('./routes/crmRoute')
|
||||||
|
const integrationRouter = require("./routes/integrationRoutes")
|
||||||
|
|
||||||
const notFoundMiddlware = require('./middleware/not-found')
|
const notFoundMiddlware = require('./middleware/not-found')
|
||||||
const errorHandlerMiddleware = require('./middleware/error-handler')
|
const errorHandlerMiddleware = require('./middleware/error-handler')
|
||||||
|
@ -78,6 +79,7 @@ app.get('/', (req, res) => {
|
||||||
app.use('/api-docs', swaggerUI.serve, swaggerUI.setup(swaggerDocument))
|
app.use('/api-docs', swaggerUI.serve, swaggerUI.setup(swaggerDocument))
|
||||||
|
|
||||||
app.use('/api/v1/crm', crmRouter)
|
app.use('/api/v1/crm', crmRouter)
|
||||||
|
app.use('/api/v1/integration', integrationRouter)
|
||||||
|
|
||||||
Sentry.setupExpressErrorHandler(app)
|
Sentry.setupExpressErrorHandler(app)
|
||||||
app.use(notFoundMiddlware)
|
app.use(notFoundMiddlware)
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
|
|
||||||
const path = require('path')
|
const path = require('path')
|
||||||
const Sentry = require('@sentry/node')
|
const getIntegrationsConfig = require('../utils/getIntegrationsConfig')
|
||||||
const omnihitV2Integration = require('../data/omihitV2IntegrationCRM.json')
|
|
||||||
const { StatusCodes } = require("http-status-codes")
|
const { StatusCodes } = require("http-status-codes")
|
||||||
const { createCRMContact,
|
const { createCRMContact,
|
||||||
sendMessageSocket,
|
sendMessageSocket,
|
||||||
|
@ -88,49 +88,60 @@ const callJournaling = async (req, res) => {
|
||||||
// Remove 0 from the beginning of the number. Ex: 011996067641 to 11996067641
|
// Remove 0 from the beginning of the number. Ex: 011996067641 to 11996067641
|
||||||
crmPhone = removeZeroInicial(crmPhone)
|
crmPhone = removeZeroInicial(crmPhone)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// test remove
|
||||||
|
// return res.status(StatusCodes.OK).send()
|
||||||
|
|
||||||
// Refactor this in the future.
|
// Refactor this in the future.
|
||||||
crmPhone = '55' + crmPhone
|
crmPhone = '55' + crmPhone
|
||||||
console.log('========> CRMPHONE: ', crmPhone)
|
console.log('========> CRMPHONE: ', crmPhone)
|
||||||
console.log('========> COMPANY ID before: ', companyId)
|
console.log('========> COMPANY ID before: ', companyId)
|
||||||
|
|
||||||
|
|
||||||
if (companyId == "1")
|
if (companyId == "1")
|
||||||
companyId = '99'
|
companyId = '99'
|
||||||
|
|
||||||
|
|
||||||
console.log('========> COMPANY ID after: ', companyId)
|
console.log('========> COMPANY ID after: ', companyId)
|
||||||
|
|
||||||
|
|
||||||
if (!crmAgent)
|
if (!crmAgent)
|
||||||
crmAgent = "0000"
|
crmAgent = "0000"
|
||||||
//
|
//
|
||||||
|
|
||||||
|
|
||||||
mustContainProperties(req, ['companyId', 'operation', 'crmPhone', /*'crmAgent'*/,])
|
mustContainProperties(req, ['companyId', 'operation', 'crmPhone', /*'crmAgent'*/,])
|
||||||
|
|
||||||
|
|
||||||
// if (operation == 'inboundAnsweredCall' && !crmCallDuration)
|
// if (operation == 'inboundAnsweredCall' && !crmCallDuration)
|
||||||
// throw new CustomError.BadRequestError(`The crmCallDuration property must be provided when operation is inboundAnsweredCall`)
|
// throw new CustomError.BadRequestError(`The crmCallDuration property must be provided when operation is inboundAnsweredCall`)
|
||||||
// if (operation == 'outboundAsweredCall' && !crmCallDuration)
|
// if (operation == 'outboundAsweredCall' && !crmCallDuration)
|
||||||
// throw new CustomError.BadRequestError(`The crmCallDuration property must be provided when operation is outboundAsweredCall`)
|
// throw new CustomError.BadRequestError(`The crmCallDuration property must be provided when operation is outboundAsweredCall`)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if (!crmCallDuration || crmCallDuration.trim() == "" || crmCallDuration == "0")
|
if (!crmCallDuration || crmCallDuration.trim() == "" || crmCallDuration == "0")
|
||||||
crmCallDuration = "10"
|
crmCallDuration = "10"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if (operationStatus == "hangup")
|
if (operationStatus == "hangup")
|
||||||
await journaling(companyId, operation, crmPhone, crmAgent, crmCallDuration, crmFirstName)
|
await journaling(companyId, operation, crmPhone, crmAgent, crmCallDuration, crmFirstName)
|
||||||
|
|
||||||
|
|
||||||
else if (operationStatus == "update-answer") {
|
else if (operationStatus == "update-answer") {
|
||||||
console.log('update-answer')
|
console.log('update-answer')
|
||||||
|
|
||||||
// Referente a cliente a gabriel, pois os tickets estao sendo abertos diretamente em um botao do hitphone
|
await ticketCRM(companyId, crmPhone, crmAgent, crmFirstName)
|
||||||
if (companyId != "12928") {
|
|
||||||
console.log(`################## CRIAÇÃO AUTOMATICA DE TICKET COMPANY ID ${companyId} ##################`)
|
|
||||||
await ticketCRM(companyId, crmPhone, crmAgent, crmFirstName)
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
const resp = await redirectContactLinkCRM(companyId, crmPhone, crmAgent, crmFirstName)
|
const resp = await redirectContactLinkCRM(companyId, crmPhone, crmAgent, crmFirstName)
|
||||||
return res.status(StatusCodes.OK).json({ contact: resp })
|
return res.status(StatusCodes.OK).json({ contact: resp })
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
res.status(StatusCodes.OK).send()
|
res.status(StatusCodes.OK).send()
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
// console.log(`[ERROR - ${new Date()}] Erro no Call Journaling`, error?.response?.data)
|
// console.log(`[ERROR - ${new Date()}] Erro no Call Journaling`, error?.response?.data)
|
||||||
|
@ -143,9 +154,11 @@ const callJournaling = async (req, res) => {
|
||||||
data: error.response.data,
|
data: error.response.data,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
}
|
||||||
else if (error.request) {
|
else if (error.request) {
|
||||||
console.error('==================> callJournaling Nenhuma resposta recebida da API:', error.request)
|
console.error('==================> callJournaling Nenhuma resposta recebida da API:', error.request)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
else {
|
else {
|
||||||
console.error('==================> callJournaling Erro ao configurar a request:', error.message)
|
console.error('==================> callJournaling Erro ao configurar a request:', error.message)
|
||||||
}
|
}
|
||||||
|
@ -215,6 +228,7 @@ const oauthCallBack = async (req, res) => {
|
||||||
|
|
||||||
console.log('xxxxxxxxxx passed')
|
console.log('xxxxxxxxxx passed')
|
||||||
|
|
||||||
|
let crmOauth = await CRM.findOne({ 'crm.authentication.crmClientId': clientId, 'companyId': companyId })
|
||||||
let crmOauth = await CRM.findOne({ 'crm.authentication.crmClientId': clientId, 'companyId': companyId })
|
let crmOauth = await CRM.findOne({ 'crm.authentication.crmClientId': clientId, 'companyId': companyId })
|
||||||
|
|
||||||
crmOauth = crmOauth.toObject()
|
crmOauth = crmOauth.toObject()
|
||||||
|
@ -510,13 +524,14 @@ const webhook_crm = async (req, res) => {
|
||||||
|
|
||||||
const { phone } = contact
|
const { phone } = contact
|
||||||
|
|
||||||
const obj = omnihitV2Integration.find(o => o.companyId == companyId)
|
// const obj = omnihitV2Integration.find(o => o.companyId == companyId)
|
||||||
|
|
||||||
if (obj) {
|
const config = await getIntegrationsConfig(companyId, 'omnihit')
|
||||||
|
|
||||||
const { companyId,
|
if (config) {
|
||||||
omnihit: { accountId, api: { url, token } = {},
|
|
||||||
createConversation: { inbox_id, status, team_id } = {} } = {} } = obj
|
const { accountId, api: { url, token } = {},
|
||||||
|
createConversation: { inbox_id, status, team_id } = {} = {} } = config
|
||||||
|
|
||||||
const contactIdChatwoot = await getContactIdChatwoot(url, token, phone)
|
const contactIdChatwoot = await getContactIdChatwoot(url, token, phone)
|
||||||
|
|
||||||
|
@ -541,6 +556,15 @@ const webhook_crm = async (req, res) => {
|
||||||
return res.set('Content-Type', 'text/xml').status(StatusCodes.OK).send(responseXml)
|
return res.set('Content-Type', 'text/xml').status(StatusCodes.OK).send(responseXml)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const integration = async (req, res) => {
|
||||||
|
const { companyId } = req.query
|
||||||
|
const { integration } = req.body
|
||||||
|
|
||||||
|
console.log("====> companyId: ", companyId, " | integration: ", integration)
|
||||||
|
|
||||||
|
res.send()
|
||||||
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
contactCreate,
|
contactCreate,
|
||||||
uploadCrmConfig,
|
uploadCrmConfig,
|
||||||
|
|
|
@ -0,0 +1,39 @@
|
||||||
|
const { StatusCodes } = require("http-status-codes")
|
||||||
|
const { mustContainProperties } = require("../utils")
|
||||||
|
const Company = require("../models/Company")
|
||||||
|
const getIntegrationsConfig = require("../utils/getIntegrationsConfig")
|
||||||
|
|
||||||
|
const integration = async (req, res) => {
|
||||||
|
const { companyId } = req.params
|
||||||
|
const { config, name } = req.body
|
||||||
|
|
||||||
|
mustContainProperties(req, ['name', 'config'])
|
||||||
|
|
||||||
|
const company = await Company.findOne({ companyId })
|
||||||
|
|
||||||
|
if (!company) {
|
||||||
|
return res.status(StatusCodes.NOT_FOUND).send({ msg: "companyId not found!" })
|
||||||
|
}
|
||||||
|
|
||||||
|
const existingIntegrationIndex = company.integrations.findIndex(i => i.name === name)
|
||||||
|
|
||||||
|
if (existingIntegrationIndex !== -1) {
|
||||||
|
company.integrations[existingIntegrationIndex].config = config
|
||||||
|
} else {
|
||||||
|
company.integrations.push({ name, config })
|
||||||
|
}
|
||||||
|
|
||||||
|
await company.save()
|
||||||
|
|
||||||
|
res.status(StatusCodes.OK).send({
|
||||||
|
msg: 'Integração atualizada com sucesso',
|
||||||
|
integrations: company.integrations
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
integration
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,11 +0,0 @@
|
||||||
const omnihitV2Integration = require('../data/omihitV2IntegrationCRM.json')
|
|
||||||
|
|
||||||
const obj = omnihitV2Integration.find(o => o.companyId === "99")
|
|
||||||
|
|
||||||
if (obj) {
|
|
||||||
console.log(obj)
|
|
||||||
|
|
||||||
const { companyId, omnihit: { accountId, api: { url, token } = {}, createConversation: { inbox_id, status, team_id } = {} } = {} } = obj
|
|
||||||
|
|
||||||
console.log('companyId: ', companyId, ' accountId: ', accountId, ' url: ', url, ' token: ', token, ' inbox_id: ', inbox_id, ' status: ', status, ' team_id: ', team_id)
|
|
||||||
}
|
|
|
@ -10,7 +10,16 @@ const crmCompany = new Schema({
|
||||||
},
|
},
|
||||||
name: {
|
name: {
|
||||||
type: String
|
type: String
|
||||||
}
|
},
|
||||||
|
integrations: [
|
||||||
|
{
|
||||||
|
name: { type: String, required: true },
|
||||||
|
config: {
|
||||||
|
type: Schema.Types.Mixed,
|
||||||
|
default: {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
|
||||||
}, {
|
}, {
|
||||||
timestamps: true,
|
timestamps: true,
|
||||||
|
|
|
@ -0,0 +1,8 @@
|
||||||
|
const express = require('express')
|
||||||
|
const router = express.Router()
|
||||||
|
const { authorization, } = require('../middleware/authentication')
|
||||||
|
const { integration } = require('../controllers/integrationController')
|
||||||
|
|
||||||
|
router.route('/:companyId').patch(authorization, integration)
|
||||||
|
|
||||||
|
module.exports = router
|
|
@ -0,0 +1,13 @@
|
||||||
|
const Company = require("../models/Company")
|
||||||
|
|
||||||
|
async function getIntegrationsConfig(companyId, name) {
|
||||||
|
const company = await Company.findOne({ companyId })
|
||||||
|
|
||||||
|
if (company){
|
||||||
|
let obj = company.integrations.find(i => i.name === name)
|
||||||
|
return obj?.config
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = getIntegrationsConfig
|
|
@ -11,10 +11,9 @@ const lookupCRMTicket = require('./lookupCRMTicket')
|
||||||
const sendEventTicketCreatedToSocket = require('./sendEventTicketCreatedToSocket')
|
const sendEventTicketCreatedToSocket = require('./sendEventTicketCreatedToSocket')
|
||||||
const journalingRequest = require('./journalingRequest')
|
const journalingRequest = require('./journalingRequest')
|
||||||
|
|
||||||
const omnihitV2Integration = require('../data/omihitV2IntegrationCRM.json')
|
const getIntegrationsConfig = require('../utils/getIntegrationsConfig')
|
||||||
|
|
||||||
|
async function whatsappJournalingCRM(companyId, crmPhone, crmAgent, crmFirstName = 'Username', ticketId = null) {
|
||||||
async function whatsappJournalingCRM(companyId, crmPhone, crmAgent, crmFirstName = 'Username', ticketId=null) {
|
|
||||||
|
|
||||||
const crmFiles = await loadCRM(companyId)
|
const crmFiles = await loadCRM(companyId)
|
||||||
|
|
||||||
|
@ -34,23 +33,17 @@ async function whatsappJournalingCRM(companyId, crmPhone, crmAgent, crmFirstName
|
||||||
|
|
||||||
let contact = await _lookupContact(rest, authentication, crmPhone, companyId, crmFirstName)
|
let contact = await _lookupContact(rest, authentication, crmPhone, companyId, crmFirstName)
|
||||||
|
|
||||||
const { contactId, created } = contact
|
|
||||||
|
|
||||||
let { request, chats, response } = chatJournaling
|
let { request, chats, response } = chatJournaling
|
||||||
|
|
||||||
let body = findProperty(chats, 'chatDone')
|
let body = findProperty(chats, 'chatDone')
|
||||||
|
|
||||||
|
|
||||||
|
let config = await getIntegrationsConfig(companyId, 'omnihit')
|
||||||
|
|
||||||
|
if (ticketId && config) {
|
||||||
|
|
||||||
const obj = omnihitV2Integration.find(o => o.companyId == companyId)
|
const { accountId, api: { url, urlHttps, token } = {},
|
||||||
|
createConversation: { inbox_id, status, team_id } = {} = {} } = config
|
||||||
if (ticketId && obj) {
|
|
||||||
|
|
||||||
const { companyId,
|
|
||||||
omnihit: { accountId, api: { url, urlHttps, token } = {},
|
|
||||||
createConversation: { inbox_id, status, team_id } = {} } = {} } = obj
|
|
||||||
|
|
||||||
|
|
||||||
console.log('===============> body1: ', body)
|
console.log('===============> body1: ', body)
|
||||||
body = JSON.stringify(body)
|
body = JSON.stringify(body)
|
||||||
|
|
Loading…
Reference in New Issue