feat: created webhook route

master
adriano 2024-10-03 17:53:55 -03:00
parent fcb7cbd94d
commit a64747c426
9 changed files with 85 additions and 20 deletions

View File

@ -47,6 +47,7 @@
"LastName": "crmLastName",
"FirstName": "crmFirstName",
"Company": "Unknown",
"LeadSource": "Phone Inquiry",
"Status": "Working - Contacted"
},
"response": {

View File

@ -67,17 +67,20 @@ const callJournaling = async (req, res) => {
let { companyId, operation, crmPhone, crmAgent, crmCallDuration, crmFirstName, operationStatus } = req.body
console.log('REQ.BODY CRM TESTe: ', JSON.stringify(req.body, null, 6))
console.log('REQ.BODY CRM TESTe: ', JSON.stringify(req.body, null, 6))
// Refactor this in the future.
crmPhone = '55' + crmPhone
console.log('========> CRMPHONE: ', crmPhone)
console.log('========> COMPANY ID before: ', companyId)
companyId = '40'
companyId = '99'
console.log('========> COMPANY ID after: ', companyId)
if (!crmAgent)
crmAgent = "0000"
//
mustContainProperties(req, ['companyId', 'operation', 'crmPhone', 'crmAgent',])
mustContainProperties(req, ['companyId', 'operation', 'crmPhone', /*'crmAgent'*/,])
// if (operation == 'inboundAnsweredCall' && !crmCallDuration)
// throw new CustomError.BadRequestError(`The crmCallDuration property must be provided when operation is inboundAnsweredCall`)
@ -91,9 +94,13 @@ const callJournaling = async (req, res) => {
if (operationStatus == "hangup")
await journaling(companyId, operation, crmPhone, crmAgent, crmCallDuration, crmFirstName)
else if (operationStatus == "update-answer") {
console.log('update-answer')
await ticketCRM(companyId, crmPhone, crmAgent, crmFirstName)
const resp = await ticketCRM(companyId, crmPhone, crmAgent, crmFirstName)
return res.status(StatusCodes.OK).json({ contact: resp })
}
res.status(StatusCodes.OK).send()
@ -279,6 +286,42 @@ const getCrms = async (req, res) => {
res.status(StatusCodes.OK).send(crms)
}
const webhook = async (req, res) => {
console.log('========> webhook req.body: ', JSON.stringify(req.body, null, 6))
console.log('========> req.body: ', req.body)
if (!req.body?.meta)
return res.status(StatusCodes.OK).send()
let { name, phone_number } = req.body.meta.sender
const accountId = req.body.account_id
let crmPhone = phone_number.replace('+', '')
const companies = [
{ companyId: "99", account_id: "1" },
]
const obj = companies.find(c => +c.account_id == +accountId)
console.log('=======> name: ', name)
console.log('=======> crmPhone: ', crmPhone)
console.log('=======> accountId: ', accountId)
console.log('=======> companyId: ', obj.companyId)
await ticketCRM(
companyId = obj.companyId,
crmPhone = crmPhone,
crmAgent = '0000',
crmFirstName = name)
res.status(StatusCodes.OK).send()
}
module.exports = {
contactCreate,
uploadCrmConfig,
@ -288,7 +331,8 @@ module.exports = {
deleteCrm,
deleteCompany,
testTemplate,
getCrms
getCrms,
webhook
}

View File

@ -1,7 +1,7 @@
const express = require('express')
const router = express.Router()
const { authorization, } = require('../middleware/authentication')
const { contactCreate, testTemplate, uploadCrmConfig, callJournaling, oauthCallBack, install, deleteCrm, deleteCompany, getCrms } = require('../controllers/crmController')
const { contactCreate, testTemplate, uploadCrmConfig, callJournaling, oauthCallBack, install, deleteCrm, deleteCompany, getCrms, webhook } = require('../controllers/crmController')
const { fileUpload } = require("../utils")
router.route('/create-contact').post(authorization, contactCreate)
@ -12,6 +12,8 @@ router.route('/delete-company').post(authorization, deleteCompany)
router.route('/oauth-callback').get(oauthCallBack)
router.route('/install').get(install)
router.route('/test').post(testTemplate)
router.route('/webhook').post(webhook)
router.route('/:companyId').get(authorization, getCrms)
module.exports = router

View File

@ -13,8 +13,10 @@ const sendEventTicketCreatedToSocket = require('./sendEventTicketCreatedToSocket
async function ticketCRM(companyId, crmPhone, crmAgent, crmFirstName = 'Username') {
const crmFiles = await loadCRM(companyId)
const crmFiles = await loadCRM(companyId)
const crmContactIds = []
for (const crmConfig of crmFiles) {
const { crmRest: rest, authentication } = crmConfig.crm
@ -25,13 +27,17 @@ async function ticketCRM(companyId, crmPhone, crmAgent, crmFirstName = 'Username
if (redirectLink) {
let contact = await _lookupContact(rest, authentication, crmPhone, companyId, crmFirstName)
const { contactId } = contact
const { contactId, created } = contact
const url = redirectLink?.request?.url?.replace(/crmContactId/g, contactId)
console.log('===============> Edit url rediret sended to hitphone: ', url)
sendEventTicketCreatedToSocket({ companyId, extension: crmAgent, ticketUrl: url })
console.log('new URL(url).hostname: ', new URL(url).hostname)
crmContactIds.push({ crm: new URL(url).hostname, contactId, created })
// sendEventTicketCreatedToSocket({ companyId, extension: crmAgent, ticketUrl: url })
}
//
@ -80,23 +86,35 @@ async function ticketCRM(companyId, crmPhone, crmAgent, crmFirstName = 'Username
crmFirstName = await _createTicket(rest, crmPhone, companyId, authentication, crmFirstName, contact, crmAgent)
}
const { contactId } = contact
crmContactIds.push({ crm: new URL(url).hostname, contactId, created })
}
//
//
}
return crmContactIds
}
module.exports = ticketCRM
async function _lookupContact(rest, authentication, crmPhone, companyId, crmFirstName) {
let contact = await lookupContactByPhone(rest, authentication, crmPhone, companyId)
if (contact?.exist) {
return { created: false, contactId: contact.contactId }
}
if (!contact?.exist) {
contact = await createContact(companyId, rest, authentication, crmPhone, crmFirstName)
}
return contact
return { created: true, contactId: contact.contactId }
}
async function _createTicket(rest, crmPhone, companyId, authentication, crmFirstName, contact, crmAgent) {

View File

@ -1,13 +1,13 @@
{
"files": {
"main.css": "/static/css/main.ae60ab08.css",
"main.js": "/static/js/main.0f480c22.js",
"main.js": "/static/js/main.fb177319.js",
"index.html": "/index.html",
"main.ae60ab08.css.map": "/static/css/main.ae60ab08.css.map",
"main.0f480c22.js.map": "/static/js/main.0f480c22.js.map"
"main.fb177319.js.map": "/static/js/main.fb177319.js.map"
},
"entrypoints": [
"static/css/main.ae60ab08.css",
"static/js/main.0f480c22.js"
"static/js/main.fb177319.js"
]
}

View File

@ -1 +1 @@
<!doctype html><html lang="en"><head><meta charset="utf-8"/><link rel="icon" href="/favicon.ico"/><meta name="viewport" content="width=device-width,initial-scale=1"/><meta name="theme-color" content="#000000"/><meta name="description" content="Web site created using create-react-app"/><link rel="apple-touch-icon" href="/logo192.png"/><link rel="manifest" href="/manifest.json"/><title>React App</title><script defer="defer" src="/static/js/main.0f480c22.js"></script><link href="/static/css/main.ae60ab08.css" rel="stylesheet"></head><body><noscript>You need to enable JavaScript to run this app.</noscript><div id="root"></div></body></html>
<!doctype html><html lang="en"><head><meta charset="utf-8"/><link rel="icon" href="/favicon.ico"/><meta name="viewport" content="width=device-width,initial-scale=1"/><meta name="theme-color" content="#000000"/><meta name="description" content="Web site created using create-react-app"/><link rel="apple-touch-icon" href="/logo192.png"/><link rel="manifest" href="/manifest.json"/><title>React App</title><script defer="defer" src="/static/js/main.fb177319.js"></script><link href="/static/css/main.ae60ab08.css" rel="stylesheet"></head><body><noscript>You need to enable JavaScript to run this app.</noscript><div id="root"></div></body></html>

File diff suppressed because one or more lines are too long