Compare commits
No commits in common. "344dafbb48ff3d938a4912e642793d88c21f8fec" and "2fed69eaee9732202a0cae34c0e1160d28a43894" have entirely different histories.
344dafbb48
...
2fed69eaee
|
@ -43,7 +43,6 @@ 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')
|
||||||
|
@ -79,7 +78,6 @@ 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 getIntegrationsConfig = require('../utils/getIntegrationsConfig')
|
const Sentry = require('@sentry/node')
|
||||||
|
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,73 +88,64 @@ 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')
|
||||||
|
|
||||||
await ticketCRM(companyId, crmPhone, crmAgent, crmFirstName)
|
// Referente a cliente a gabriel, pois os tickets estao sendo abertos diretamente em um botao do hitphone
|
||||||
|
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)
|
||||||
|
|
||||||
|
Sentry.captureException(error)
|
||||||
|
|
||||||
if (error.response) {
|
if (error.response) {
|
||||||
console.error('==================> callJournaling Erro na resposta da API:', {
|
console.error('==================> callJournaling Erro na resposta da API:', {
|
||||||
status: error.response.status,
|
status: error.response.status,
|
||||||
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)
|
||||||
}
|
}
|
||||||
|
@ -223,7 +214,7 @@ const oauthCallBack = async (req, res) => {
|
||||||
if (code) {
|
if (code) {
|
||||||
|
|
||||||
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 })
|
||||||
|
|
||||||
crmOauth = crmOauth.toObject()
|
crmOauth = crmOauth.toObject()
|
||||||
|
@ -519,14 +510,13 @@ 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)
|
||||||
|
|
||||||
const config = await getIntegrationsConfig(companyId, 'omnihit')
|
if (obj) {
|
||||||
|
|
||||||
if (config) {
|
const { companyId,
|
||||||
|
omnihit: { accountId, api: { url, token } = {},
|
||||||
const { accountId, api: { url, token } = {},
|
createConversation: { inbox_id, status, team_id } = {} } = {} } = obj
|
||||||
createConversation: { inbox_id, status, team_id } = {} = {} } = config
|
|
||||||
|
|
||||||
const contactIdChatwoot = await getContactIdChatwoot(url, token, phone)
|
const contactIdChatwoot = await getContactIdChatwoot(url, token, phone)
|
||||||
|
|
||||||
|
@ -551,15 +541,6 @@ 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,
|
||||||
|
|
|
@ -1,39 +0,0 @@
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,11 @@
|
||||||
|
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,16 +10,7 @@ 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,
|
||||||
|
@ -35,7 +26,7 @@ crmCompany.virtual('crms', {
|
||||||
// match: { rating: 4 }
|
// match: { rating: 4 }
|
||||||
})
|
})
|
||||||
|
|
||||||
crmCompany.pre('deleteOne', { document: true, query: false }, async function () {
|
crmCompany.pre('deleteOne', { document: true, query: false }, async function () {
|
||||||
const crms = await this.model('CRM').find({ company: this._id })
|
const crms = await this.model('CRM').find({ company: this._id })
|
||||||
for (const crm of crms) {
|
for (const crm of crms) {
|
||||||
await crm.deleteOne() // Delete each CRM
|
await crm.deleteOne() // Delete each CRM
|
||||||
|
|
|
@ -1,8 +0,0 @@
|
||||||
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
|
|
|
@ -1,13 +0,0 @@
|
||||||
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
|
|
|
@ -10,14 +10,15 @@ const createTicket = require('./createTicket')
|
||||||
const lookupCRMTicket = require('./lookupCRMTicket')
|
const lookupCRMTicket = require('./lookupCRMTicket')
|
||||||
const sendEventTicketCreatedToSocket = require('./sendEventTicketCreatedToSocket')
|
const sendEventTicketCreatedToSocket = require('./sendEventTicketCreatedToSocket')
|
||||||
const journalingRequest = require('./journalingRequest')
|
const journalingRequest = require('./journalingRequest')
|
||||||
|
|
||||||
const getIntegrationsConfig = require('../utils/getIntegrationsConfig')
|
|
||||||
|
|
||||||
async function whatsappJournalingCRM(companyId, crmPhone, crmAgent, crmFirstName = 'Username', ticketId = null) {
|
const omnihitV2Integration = require('../data/omihitV2IntegrationCRM.json')
|
||||||
|
|
||||||
|
|
||||||
|
async function whatsappJournalingCRM(companyId, crmPhone, crmAgent, crmFirstName = 'Username', ticketId=null) {
|
||||||
|
|
||||||
const crmFiles = await loadCRM(companyId)
|
const crmFiles = await loadCRM(companyId)
|
||||||
|
|
||||||
const crmContactIds = []
|
const crmContactIds = []
|
||||||
|
|
||||||
for (const crmConfig of crmFiles) {
|
for (const crmConfig of crmFiles) {
|
||||||
|
|
||||||
|
@ -31,19 +32,25 @@ async function whatsappJournalingCRM(companyId, crmPhone, crmAgent, crmFirstName
|
||||||
|
|
||||||
if (chatJournaling) {
|
if (chatJournaling) {
|
||||||
|
|
||||||
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 { accountId, api: { url, urlHttps, token } = {},
|
const obj = omnihitV2Integration.find(o => o.companyId == companyId)
|
||||||
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