Compare commits
1 Commits
47baefd047
...
e9b5011eca
Author | SHA1 | Date |
---|---|---|
|
e9b5011eca |
|
@ -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,6 +1,6 @@
|
||||||
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,
|
||||||
|
@ -87,64 +87,55 @@ 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,
|
||||||
|
@ -169,7 +160,7 @@ const sfCreateCase = async (req, res) => {
|
||||||
|
|
||||||
const { companyId, crmPhone } = req.body
|
const { companyId, crmPhone } = req.body
|
||||||
|
|
||||||
mustContainProperties(req, ['companyId', 'crmPhone',])
|
mustContainProperties(req, ['companyId', 'crmPhone',])
|
||||||
|
|
||||||
const sfCaseRes = await sfcase(companyId, crmPhone)
|
const sfCaseRes = await sfcase(companyId, crmPhone)
|
||||||
|
|
||||||
|
@ -438,12 +429,12 @@ const webhook = async (req, res) => {
|
||||||
if (!req.body?.meta)
|
if (!req.body?.meta)
|
||||||
return res.status(StatusCodes.OK).send()
|
return res.status(StatusCodes.OK).send()
|
||||||
|
|
||||||
let { name, phone_number } = req.body.meta.sender
|
let { name, phone_number } = req.body.meta.sender
|
||||||
|
|
||||||
const { id: ticketId, account_id: accountId } = req.body
|
const {id: ticketId, account_id: accountId} = req.body
|
||||||
|
|
||||||
//channel: 'Channel::FacebookPage'
|
//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) {
|
if (!phone_number) {
|
||||||
|
|
||||||
|
@ -453,30 +444,24 @@ const webhook = async (req, res) => {
|
||||||
}
|
}
|
||||||
|
|
||||||
let crmPhone = phone_number.replace('+', '')
|
let crmPhone = phone_number.replace('+', '')
|
||||||
|
|
||||||
const company = await Company.findOne({
|
|
||||||
integrations: {
|
|
||||||
$elemMatch: {
|
|
||||||
name: "omnihit",
|
|
||||||
"config.accountId": `${accountId}`
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
if (!company) {
|
const companies = [
|
||||||
return res.status(StatusCodes.NOT_FOUND).send({ msg: "companyId not found!" })
|
// { companyId: "99", account_id: "1" },
|
||||||
}
|
{ companyId: "99", account_id: "15" },
|
||||||
|
]
|
||||||
|
|
||||||
|
const obj = companies.find(c => +c.account_id == +accountId)
|
||||||
|
|
||||||
console.log('=======> name: ', name)
|
console.log('=======> name: ', name)
|
||||||
console.log('=======> crmPhone: ', crmPhone)
|
console.log('=======> crmPhone: ', crmPhone)
|
||||||
console.log('=======> accountId: ', accountId)
|
console.log('=======> accountId: ', accountId)
|
||||||
console.log('=======> companyId: ', company.companyId)
|
console.log('=======> companyId: ', obj.companyId)
|
||||||
|
|
||||||
console.log('=======> ticketId: ', ticketId)
|
console.log('=======> ticketId: ', ticketId)
|
||||||
|
|
||||||
|
|
||||||
await whatsappJournalingCRM(
|
await whatsappJournalingCRM(
|
||||||
companyId = company.companyId,
|
companyId = obj.companyId,
|
||||||
crmPhone = crmPhone,
|
crmPhone = crmPhone,
|
||||||
crmAgent = '0000',
|
crmAgent = '0000',
|
||||||
crmFirstName = name,
|
crmFirstName = name,
|
||||||
|
@ -540,14 +525,15 @@ const webhook_crm = async (req, res) => {
|
||||||
|
|
||||||
if (!contact) return res.send()
|
if (!contact) return res.send()
|
||||||
|
|
||||||
const { phone } = contact
|
const { phone } = contact
|
||||||
|
|
||||||
const config = await getIntegrationsConfig(companyId, 'omnihit')
|
const obj = omnihitV2Integration.find(o => o.companyId == companyId)
|
||||||
|
|
||||||
if (config) {
|
if (obj) {
|
||||||
|
|
||||||
const { accountId, api: { url, token } = {},
|
const { companyId,
|
||||||
createConversation: { inbox_id, status, team_id } = {} = {} } = config
|
omnihit: { accountId, api: { url, token } = {},
|
||||||
|
createConversation: { inbox_id, status, team_id } = {} } = {} } = obj
|
||||||
|
|
||||||
const contactIdChatwoot = await getContactIdChatwoot(url, token, phone)
|
const contactIdChatwoot = await getContactIdChatwoot(url, token, phone)
|
||||||
|
|
||||||
|
@ -570,7 +556,7 @@ 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 associateTicketToCaller = async (req, res) => {
|
const associateTicketToCaller = async (req, res) => {
|
||||||
try {
|
try {
|
||||||
|
|
|
@ -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