feat: add handshake connection auth verifier, creation of the hitphone web connection logic and sendEventticketCreatedToSocket function
parent
8f9afd5f23
commit
4b5140ce8b
|
@ -0,0 +1,30 @@
|
||||||
|
const { getIO } = require("./socketIO")
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sends a ticket creation event to the hitphone socket client
|
||||||
|
*
|
||||||
|
* @param {Object} ticketInformations Object representing the information needed to notify the Hitphone front-end
|
||||||
|
* @param {string} ticketInformations.companyId Company codWeb or identifier (Example: 1)
|
||||||
|
* @param {string} ticketInformations.extension Agent Extension that received the call (Example: 3005)
|
||||||
|
* @param {string} ticketInformations.ticketUrl URL of ticket created
|
||||||
|
*/
|
||||||
|
function sendEventTicketCreatedToSocket (ticketInformations) {
|
||||||
|
const { companyId, extension, ticketUrl } = ticketInformations
|
||||||
|
try {
|
||||||
|
console.log(`${new Date().toISOString()} ==========> sendEventTicketCreatedToSocket: Sending ticket event created to socket with data ${JSON.stringify(ticketInformations)}`)
|
||||||
|
|
||||||
|
const io = getIO()
|
||||||
|
const extensionAgentRoom = `${companyId}@${extension}`
|
||||||
|
const roomExists = io.sockets.adapter.rooms.has(extensionAgentRoom)
|
||||||
|
if (roomExists) {
|
||||||
|
io.to(extensionAgentRoom).emit('ticket-created', { url: ticketUrl } )
|
||||||
|
console.log(`${new Date().toISOString()} ==========> sendEventTicketCreatedToSocket: Ticket-created event successfully sent to: ${JSON.stringify(ticketInformations)}`)
|
||||||
|
} else {
|
||||||
|
console.log(`${new Date().toISOString()} ==========> sendEventTicketCreatedToSocket: Unable to send the event ticket-created because the Extension Agent (CompanyID: ${extension} | Extension: ${extension}) is not connected to the Hitphone`)
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.log(`${new Date().toISOString()} ==========> sendEventTicketCreatedToSocket: Unable to send the event ticket-created to Extension Agent (CompanyID: ${extension} | Extension: ${extension}) because an error occurred: \n${error}`)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = sendEventTicketCreatedToSocket
|
|
@ -1,28 +1,81 @@
|
||||||
|
|
||||||
|
|
||||||
const SocketIO = require('socket.io')
|
const SocketIO = require('socket.io')
|
||||||
let io
|
let io
|
||||||
|
|
||||||
|
const onSocketHandshakeAuthVerifier = (socket, next) => {
|
||||||
|
|
||||||
|
console.log(`${new Date().toISOString()} ===========> MIDDLEWARE: Socket trying to connect with data ${JSON.stringify(socket.handshake.auth)}`)
|
||||||
|
|
||||||
|
const codWeb = socket.handshake.auth.codWeb
|
||||||
|
const extension = socket.handshake.auth.extension
|
||||||
|
const host = socket.handshake.headers.host
|
||||||
|
|
||||||
|
if (!host) {
|
||||||
|
return next(new Error(`Host must be created`))
|
||||||
|
}
|
||||||
|
|
||||||
|
const isFromHitphoneWebClient = true
|
||||||
|
// const isFromHitphoneWebClient = host.includes("https://ms-teamsapp.omnihit.app.br/")
|
||||||
|
if (!isFromHitphoneWebClient) {
|
||||||
|
socket.data.isFromHitphoneWebClient = false
|
||||||
|
next()
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!codWeb || !extension) {
|
||||||
|
return next(new Error(`Invalid authentication information, required attributes host, codWeb, extension`))
|
||||||
|
}
|
||||||
|
|
||||||
|
socket.data.codWeb = codWeb
|
||||||
|
socket.data.extension = extension
|
||||||
|
socket.data.isFromHitphoneWebClient = true
|
||||||
|
return next()
|
||||||
|
}
|
||||||
|
|
||||||
|
const onConnectionHitphoneWebClient = (socket) => {
|
||||||
|
const { isFromHitphoneWebClient } = socket.data
|
||||||
|
if (!isFromHitphoneWebClient) return
|
||||||
|
console.log(`${new Date().toISOString()} ===========> SOCKET CONNECTION: Client connected from "Hitphone WEB Client"`)
|
||||||
|
|
||||||
|
const { codWeb, extension } = socket.data
|
||||||
|
socket.join(`${codWeb}@${extension}`)
|
||||||
|
|
||||||
|
socket.on("disconnect", (data) => {
|
||||||
|
console.log(`${new Date().toISOString()} ==========> SOCKET DISCONNECT: "Hitphone WEB Client" Client disconnected socket: ${data}`)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
const onConnectionCrmWizardClient = (socket) => {
|
||||||
|
const { isFromHitphoneWebClient } = socket.data
|
||||||
|
if (!isFromHitphoneWebClient) return
|
||||||
|
console.log(`${new Date().toISOString()} ===========> SOCKET CONNECTION: Client connected from "Hitphone WEB Client"`)
|
||||||
|
|
||||||
|
const { codWeb, extension } = socket.data
|
||||||
|
socket.join(`${codWeb}@${extension}`)
|
||||||
|
|
||||||
|
socket.on("disconnect", (data) => {
|
||||||
|
console.log(`${new Date().toISOString()} ==========> SOCKET DISCONNECT: "Hitphone WEB Client" Client disconnected socket: ${data}`)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
const initIO = (httpServer) => {
|
const initIO = (httpServer) => {
|
||||||
|
const IS_DEV = process.env.IS_DEV ? Boolean(process.env.IS_DEV) : false
|
||||||
|
|
||||||
io = SocketIO(httpServer, {
|
io = SocketIO(httpServer, {
|
||||||
cors: {
|
cors: {
|
||||||
origin: process.env.URL_OAUTH_FRONTEND_SUCCESS_REDIRECT
|
origin: IS_DEV ? "*" : [process.env.URL_OAUTH_FRONTEND_SUCCESS_REDIRECT, process.env.URL_HITPHONE_FRONTEND]
|
||||||
},
|
},
|
||||||
maxHttpBufferSize: 1e8
|
maxHttpBufferSize: 1e8
|
||||||
})
|
})
|
||||||
|
|
||||||
io.on("connection", socket => {
|
io.use(onSocketHandshakeAuthVerifier)
|
||||||
console.log('CLIENT CONNECTED')
|
/**
|
||||||
|
* CRM Wizard Client
|
||||||
|
*/
|
||||||
|
io.use("connection", onConnectionCrmWizardClient)
|
||||||
|
/**
|
||||||
|
* Hitphone Client Flow
|
||||||
|
*/
|
||||||
|
io.use("connection", onConnectionHitphoneWebClient)
|
||||||
|
|
||||||
socket.on("companySession", (companyId) => {
|
|
||||||
console.log(`A client joined a companySession channel: ${companyId}`)
|
|
||||||
socket.join(`company_${companyId}`)
|
|
||||||
});
|
|
||||||
|
|
||||||
socket.on("disconnect", (data) => {
|
|
||||||
console.log(`Client disconnected socket: ${data}`)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
return io
|
return io
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue