feat/hitphone socket integration (#1)
* feat: add handshake connection auth verifier, creation of the hitphone web connection logic and sendEventticketCreatedToSocket function * refactor: add logs and fix connection listenermaster
parent
8f9afd5f23
commit
b2f8de21ff
|
@ -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,79 @@
|
|||
|
||||
|
||||
const SocketIO = require('socket.io')
|
||||
let io
|
||||
|
||||
const onSocketHandshakeAuthVerifier = (socket, next) => {
|
||||
|
||||
console.log(`${new Date().toISOString()} ===========> MIDDLEWARE: Socket trying to connect with data ${JSON.stringify(socket.handshake.auth)} and origin ${socket.handshake.headers.origin}`)
|
||||
const codWeb = socket.handshake.auth.codWeb
|
||||
const extension = socket.handshake.auth.extension
|
||||
const origin = socket.handshake.headers.origin
|
||||
if (!origin) {
|
||||
console.log(`${new Date().toISOString()} ===========> MIDDLEWARE: Socket with data ${JSON.stringify(socket.handshake.auth)} disconnected because didn't send the origin`)
|
||||
return next(new Error(`Invalid handshake header information information origin must be specified`))
|
||||
}
|
||||
const isFromHitphoneWebClient = origin.includes(process.env.URL_HITPHONE_FRONTEND)
|
||||
if (!isFromHitphoneWebClient) {
|
||||
socket.data.isFromHitphoneWebClient = false
|
||||
return next()
|
||||
}
|
||||
|
||||
if (!codWeb || !extension) {
|
||||
console.log(`${new Date().toISOString()} ===========> MIDDLEWARE: Socket with data ${JSON.stringify(socket.handshake.auth)} disconnected because didn't send extension or codWeb`)
|
||||
return next(new Error(`Invalid handshake auth information, required attributes 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 "CRM Wizard client"`)
|
||||
const { codWeb, extension } = socket.data
|
||||
socket.join(`${codWeb}@${extension}`)
|
||||
|
||||
socket.on("disconnect", (data) => {
|
||||
console.log(`${new Date().toISOString()} ==========> SOCKET DISCONNECT: "CRM Wizard client" Client disconnected, data: ${data}`)
|
||||
})
|
||||
}
|
||||
|
||||
const initIO = (httpServer) => {
|
||||
const IS_DEV = process.env.IS_DEV ? Boolean(process.env.IS_DEV) : false
|
||||
|
||||
io = SocketIO(httpServer, {
|
||||
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
|
||||
})
|
||||
|
||||
io.on("connection", socket => {
|
||||
console.log('CLIENT CONNECTED')
|
||||
io.use(onSocketHandshakeAuthVerifier)
|
||||
/**
|
||||
* CRM Wizard Client
|
||||
*/
|
||||
io.on("connection", onConnectionCrmWizardClient)
|
||||
/**
|
||||
* Hitphone Client Flow
|
||||
*/
|
||||
io.on("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
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue