UNPKG

radius-read

Version:

Realtime Dashboard

503 lines 28.3 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SocketIoClientService = void 0; const tslib_1 = require("tslib"); const util_service_1 = require("./util.service"); const isEqual_1 = tslib_1.__importDefault(require("lodash/isEqual")); const request_pool_service_1 = require("./request.pool.service"); const socketio_client_register_service_1 = require("./socketio.client.register.service"); const callback_service_1 = require("./callback.service"); const socketio_enum_1 = require("./../enums/socketio.enum"); const io = require("socket.io-client"); /** * SocketIoClientService is a service class that manages the Socket.IO client connection and event handling. */ class SocketIoClientService { /** * Creates an instance of SocketIoClientService. * @param appConfig */ constructor(appConfig) { this._appConfig = appConfig; } /** * Initializes the Socket.IO client service with the provided client information, light event, custom light service entity, and light service entity. * @param clientInfo * @param lightEvent * @param customLightServiceEntity * @param lightServiceEntity */ init(clientInfo, lightEvent, customLightServiceEntity, lightServiceEntity) { var _a, _b; this._serverURL = util_service_1.UtilService.getSocketIoBaseURL((_a = this._appConfig) === null || _a === void 0 ? void 0 : _a.port, (_b = this._appConfig) === null || _b === void 0 ? void 0 : _b.isSSL); this._clientInfo = clientInfo; this.initSocketIo(lightEvent, customLightServiceEntity, lightServiceEntity); } /** * Initializes the Socket.IO client connection and registers event handlers for the provided light event, custom light service entity, and light service entity. * @param lightEvent * @param customLightServiceEntity * @param lightServiceEntity */ initSocketIo(lightEvent, customLightServiceEntity, lightServiceEntity) { var _a, _b, _c; this._clientInfo.socket = socketio_client_register_service_1.SocketIoClientRegisterService.getSocket((_a = this._clientInfo) === null || _a === void 0 ? void 0 : _a.userInfo); if (!this._clientInfo.socket) { this._clientInfo.socket = io(this._serverURL, { transports: ["websocket"], secure: true, rejectUnauthorized: false, }); socketio_client_register_service_1.SocketIoClientRegisterService.register(this._clientInfo); this._clientInfo.socket.on("connect", () => { var _a, _b, _c, _d; this._clientInfo.socket.emit("register", { userId: (_b = (_a = this._clientInfo) === null || _a === void 0 ? void 0 : _a.userInfo) === null || _b === void 0 ? void 0 : _b.LoginId, tenantCode: (_d = (_c = this._clientInfo) === null || _c === void 0 ? void 0 : _c.userInfo) === null || _d === void 0 ? void 0 : _d.TenantCode, }); }); this._clientInfo.socket.on("message", (data) => { data = JSON.parse(data); // this.processSocketIoEvents(data); this.processLightEvents(data, lightEvent, customLightServiceEntity, lightServiceEntity); }); this._clientInfo.socket.on("disconnect", () => { var _a; socketio_client_register_service_1.SocketIoClientRegisterService.clear((_a = this._clientInfo) === null || _a === void 0 ? void 0 : _a.userInfo); }); this._clientInfo.socket.on("connect_error", (error) => { var _a; socketio_client_register_service_1.SocketIoClientRegisterService.clear((_a = this._clientInfo) === null || _a === void 0 ? void 0 : _a.userInfo); }); } else { (_c = (_b = this._clientInfo) === null || _b === void 0 ? void 0 : _b.services) === null || _c === void 0 ? void 0 : _c.forEach(service => { var _a, _b, _c, _d; var lightService = this.getLightServiceEntity(service, customLightServiceEntity, lightServiceEntity); if (this._clientInfo.userInfo && lightService) { var payload = (_b = (_a = this._clientInfo) === null || _a === void 0 ? void 0 : _a.payload) === null || _b === void 0 ? void 0 : _b.registerService; if (payload) { this._clientInfo.socket.emit("broadcast", { userId: (_c = this._clientInfo.userInfo) === null || _c === void 0 ? void 0 : _c.LoginId, tenantCode: (_d = this._clientInfo.userInfo) === null || _d === void 0 ? void 0 : _d.TenantCode, message: JSON.stringify(payload) }); request_pool_service_1.RequestPoolService.add(payload); } } }); } } /** * Processes Socket.IO events based on the event type and performs actions accordingly. * @param event * @param lightEvent * @param lightServiceCustomEntity * @param lightServiceEntity */ processSocketIoEvents(event, lightEvent, lightServiceCustomEntity, lightServiceEntity) { return tslib_1.__awaiter(this, void 0, void 0, function* () { var _a, _b, _c; switch (event === null || event === void 0 ? void 0 : event.evtType) { case socketio_enum_1.SocketIoEvent.Connected: request_pool_service_1.RequestPoolService.clear(); yield this.onSocketIoConnected(event); break; case socketio_enum_1.SocketIoEvent.ConnectionExists: request_pool_service_1.RequestPoolService.clear(); yield this.onSocketIoConnected(event); break; case socketio_enum_1.SocketIoEvent.Disconnected: request_pool_service_1.RequestPoolService.clear(); break; case socketio_enum_1.SocketIoEvent.Error: if (!this.isLastCallbackPayload(event)) { this._lastCallbackPayload = { User: (_a = this._clientInfo) === null || _a === void 0 ? void 0 : _a.userInfo, Event: event === null || event === void 0 ? void 0 : event.data }; callback_service_1.CallbackService.register((_b = this._clientInfo) === null || _b === void 0 ? void 0 : _b.callbackURL); callback_service_1.CallbackService.sendCallbackEvents({ User: (_c = this._clientInfo) === null || _c === void 0 ? void 0 : _c.userInfo, Event: event === null || event === void 0 ? void 0 : event.data }); } break; case socketio_enum_1.SocketIoEvent.MessageRecieved: yield this.processLightEvents(event, lightEvent, lightServiceCustomEntity, lightServiceEntity); break; case socketio_enum_1.SocketIoEvent.MessageSent: break; default: break; } }); } /** * Callback function that is called when the Socket.IO client is connected to the server. * @param event */ onSocketIoConnected(event) { return tslib_1.__awaiter(this, void 0, void 0, function* () { var _a, _b, _c, _d, _e, _f, _g; if ((_a = this._clientInfo) === null || _a === void 0 ? void 0 : _a.userInfo) { var payload = (_c = (_b = this._clientInfo) === null || _b === void 0 ? void 0 : _b.payload) === null || _c === void 0 ? void 0 : _c.userRegister; if (payload) { this._clientInfo.socket.emit("broadcast", { userId: (_e = (_d = this._clientInfo) === null || _d === void 0 ? void 0 : _d.userInfo) === null || _e === void 0 ? void 0 : _e.LoginId, tenantCode: (_g = (_f = this._clientInfo) === null || _f === void 0 ? void 0 : _f.userInfo) === null || _g === void 0 ? void 0 : _g.TenantCode, message: JSON.stringify(payload) }); request_pool_service_1.RequestPoolService.add(payload); } } }); } /** * Processes light events based on the event type and performs actions accordingly. * @param event * @param lightEvent * @param lightServiceCustomEntity * @param lightServiceEntity */ processLightEvents(event, lightEvent, lightServiceCustomEntity, lightServiceEntity) { return tslib_1.__awaiter(this, void 0, void 0, function* () { var _a, _b, _c; var evtData = (event === null || event === void 0 ? void 0 : event.data) && JSON.parse(event === null || event === void 0 ? void 0 : event.data); switch (event === null || event === void 0 ? void 0 : event.EvCodeApp) { case lightEvent.UserRegistered: yield this.onUserRegisteredEvent(event, lightServiceCustomEntity, lightServiceEntity); break; case lightEvent.ServiceRegistered: yield this.onServiceRegisteredEvent(event); break; case lightEvent.RtServiceRegistered: yield this.onRtServiceRegisteredEvent(event, lightServiceCustomEntity, lightServiceEntity); break; case lightEvent.ServiceAlreadyRegistered: yield this.onRtServiceRegisteredEvent(event, lightServiceCustomEntity, lightServiceEntity); break; case lightEvent.AOPsStateChanged: yield this.onAOPsStateChangedEvent(event); break; case lightEvent.AgentStateChanged: yield this.onAgentStateChangedEvent(event); break; case lightEvent.AgentTerminalStateChanged: yield this.onAgentTerminalStateChangedEvent(event); break; case lightEvent.AgentQueueStateChanged: yield this.onAgentQueueStateChangedEvent(event); break; case lightEvent.CallStateChanged: yield this.onCallStateChangedEvent(event); break; case lightEvent.ChatStateChanged: yield this.onChatStateChangedEvent(event); break; case lightEvent.EmailStateChanged: yield this.onEmailStateChangedEvent(event); break; case lightEvent.QueueStateChanged: yield this.onQueueStateChangedEvent(event); break; case lightEvent.QueueXionStateChanged: yield this.onQueueXionStateChangedEvent(event); break; case lightEvent.SocialStateChanged: yield this.onSocialStateChangedEvent(event); break; case lightEvent.VideoStateChanged: yield this.onVideoStateChangedEvent(event); break; case socketio_enum_1.SocketIoEvent.WsConnected: request_pool_service_1.RequestPoolService.clear(); yield this.onSocketIoConnected(evtData); break; case socketio_enum_1.SocketIoEvent.WsDisconnected: request_pool_service_1.RequestPoolService.clear(); break; case socketio_enum_1.SocketIoEvent.WsError: if (!this.isLastCallbackPayload(event)) { this._lastCallbackPayload = { User: (_a = this._clientInfo) === null || _a === void 0 ? void 0 : _a.userInfo, Event: event }; callback_service_1.CallbackService.register((_b = this._clientInfo) === null || _b === void 0 ? void 0 : _b.callbackURL); callback_service_1.CallbackService.sendCallbackEvents({ User: (_c = this._clientInfo) === null || _c === void 0 ? void 0 : _c.userInfo, Event: event }); } break; case socketio_enum_1.SocketIoEvent.WsMessageRecieved: break; case socketio_enum_1.SocketIoEvent.WsMessageSent: break; default: const { tenantCode, userId, data } = event; var evtData = data && JSON.parse(data); break; } request_pool_service_1.RequestPoolService.remove(event === null || event === void 0 ? void 0 : event.ReqId); }); } /** * Gets the light service entity based on the provided service name and returns the corresponding entity. * @param serviceName * @param LightServiceCustomEntity * @param LightServiceEntity * @returns */ getLightServiceEntity(serviceName, LightServiceCustomEntity, LightServiceEntity) { switch (serviceName) { case LightServiceCustomEntity.Agent: return LightServiceEntity.Agent; case LightServiceCustomEntity.UserTerminal: return LightServiceEntity.UserTerminal; case LightServiceCustomEntity.QueueAgent: return LightServiceEntity.QueueAgent; case LightServiceCustomEntity.QueueInteraction: return LightServiceEntity.QueueInteraction; case LightServiceCustomEntity.Campaign: return LightServiceEntity.Campaign; case LightServiceCustomEntity.Call: return LightServiceEntity.Call; case LightServiceCustomEntity.Chat: return LightServiceEntity.Chat; case LightServiceCustomEntity.Email: return LightServiceEntity.Email; case LightServiceCustomEntity.Video: return LightServiceEntity.Video; case LightServiceCustomEntity.Social: return LightServiceEntity.Social; case LightServiceCustomEntity.Queue: return LightServiceEntity.Queue; default: return undefined; } } /** * Callback function that is called when a user registration event occurs. * @param event * @param customLightServiceEntity * @param lightServiceEntity */ onUserRegisteredEvent(event, customLightServiceEntity, lightServiceEntity) { return tslib_1.__awaiter(this, void 0, void 0, function* () { var _a, _b; if (request_pool_service_1.RequestPoolService.isRequestPending(event === null || event === void 0 ? void 0 : event.ReqId)) { (_b = (_a = this._clientInfo) === null || _a === void 0 ? void 0 : _a.services) === null || _b === void 0 ? void 0 : _b.forEach(service => { var _a, _b, _c, _d, _e, _f, _g; var lightService = this.getLightServiceEntity(service, customLightServiceEntity, lightServiceEntity); if (((_a = this._clientInfo) === null || _a === void 0 ? void 0 : _a.userInfo) && lightService) { var payload = (_c = (_b = this._clientInfo) === null || _b === void 0 ? void 0 : _b.payload) === null || _c === void 0 ? void 0 : _c.registerService; if (payload) { this._clientInfo.socket.emit("broadcast", { userId: (_e = (_d = this._clientInfo) === null || _d === void 0 ? void 0 : _d.userInfo) === null || _e === void 0 ? void 0 : _e.LoginId, tenantCode: (_g = (_f = this._clientInfo) === null || _f === void 0 ? void 0 : _f.userInfo) === null || _g === void 0 ? void 0 : _g.TenantCode, message: JSON.stringify(payload) }); request_pool_service_1.RequestPoolService.add(payload); } } }); } }); } /** * Callback function that is called when a real-time service registration event occurs. * @param event * @param customLightServiceEntity * @param lightServiceEntity */ onRtServiceRegisteredEvent(event, customLightServiceEntity, lightServiceEntity) { return tslib_1.__awaiter(this, void 0, void 0, function* () { var _a, _b; if (request_pool_service_1.RequestPoolService.isRequestPending(event === null || event === void 0 ? void 0 : event.ReqId)) { (_b = (_a = this._clientInfo) === null || _a === void 0 ? void 0 : _a.services) === null || _b === void 0 ? void 0 : _b.forEach(service => { var _a, _b, _c, _d, _e, _f, _g; var request = request_pool_service_1.RequestPoolService.getRequestObject(event === null || event === void 0 ? void 0 : event.ReqId); var lightService = this.getLightServiceEntity(service, customLightServiceEntity, lightServiceEntity); if (((_a = this._clientInfo) === null || _a === void 0 ? void 0 : _a.userInfo) && lightService && (request === null || request === void 0 ? void 0 : request.ServiceName) === lightService) { var payload = (_c = (_b = this._clientInfo) === null || _b === void 0 ? void 0 : _b.payload) === null || _c === void 0 ? void 0 : _c.registeredServiceEntity; if (payload) { this._clientInfo.socket.emit("broadcast", { userId: (_e = (_d = this._clientInfo) === null || _d === void 0 ? void 0 : _d.userInfo) === null || _e === void 0 ? void 0 : _e.LoginId, tenantCode: (_g = (_f = this._clientInfo) === null || _f === void 0 ? void 0 : _f.userInfo) === null || _g === void 0 ? void 0 : _g.TenantCode, message: JSON.stringify(payload) }); request_pool_service_1.RequestPoolService.add(payload); } } }); } }); } /** * Callback function that is called when a service registration event occurs. * @param event */ onServiceRegisteredEvent(event) { return tslib_1.__awaiter(this, void 0, void 0, function* () { var _a, _b, _c; if (!this.isLastCallbackPayload(event)) { this._lastCallbackPayload = { User: (_a = this._clientInfo) === null || _a === void 0 ? void 0 : _a.userInfo, Event: event }; callback_service_1.CallbackService.register((_b = this._clientInfo) === null || _b === void 0 ? void 0 : _b.callbackURL); callback_service_1.CallbackService.sendCallbackEvents({ User: (_c = this._clientInfo) === null || _c === void 0 ? void 0 : _c.userInfo, Event: event }); } }); } /** * Callback function that is called when the state of AOPs changes. * @param event */ onAOPsStateChangedEvent(event) { return tslib_1.__awaiter(this, void 0, void 0, function* () { var _a, _b, _c; if (!this.isLastCallbackPayload(event)) { this._lastCallbackPayload = { User: (_a = this._clientInfo) === null || _a === void 0 ? void 0 : _a.userInfo, Event: event }; callback_service_1.CallbackService.register((_b = this._clientInfo) === null || _b === void 0 ? void 0 : _b.callbackURL); callback_service_1.CallbackService.sendCallbackEvents({ User: (_c = this._clientInfo) === null || _c === void 0 ? void 0 : _c.userInfo, Event: event }); } }); } /** * Callback function that is called when the state of an agent changes. * @param event */ onAgentStateChangedEvent(event) { return tslib_1.__awaiter(this, void 0, void 0, function* () { var _a, _b, _c; if (!this.isLastCallbackPayload(event)) { this._lastCallbackPayload = { User: (_a = this._clientInfo) === null || _a === void 0 ? void 0 : _a.userInfo, Event: event }; callback_service_1.CallbackService.register((_b = this._clientInfo) === null || _b === void 0 ? void 0 : _b.callbackURL); callback_service_1.CallbackService.sendCallbackEvents({ User: (_c = this._clientInfo) === null || _c === void 0 ? void 0 : _c.userInfo, Event: event }); } }); } /** * Callback function that is called when the state of an agent's terminal changes. * @param event */ onAgentTerminalStateChangedEvent(event) { return tslib_1.__awaiter(this, void 0, void 0, function* () { var _a, _b, _c; if (!this.isLastCallbackPayload(event)) { this._lastCallbackPayload = { User: (_a = this._clientInfo) === null || _a === void 0 ? void 0 : _a.userInfo, Event: event }; callback_service_1.CallbackService.register((_b = this._clientInfo) === null || _b === void 0 ? void 0 : _b.callbackURL); callback_service_1.CallbackService.sendCallbackEvents({ User: (_c = this._clientInfo) === null || _c === void 0 ? void 0 : _c.userInfo, Event: event }); } }); } /** * Callback function that is called when the state of an agent's queue changes. * @param event */ onAgentQueueStateChangedEvent(event) { return tslib_1.__awaiter(this, void 0, void 0, function* () { var _a, _b, _c; if (!this.isLastCallbackPayload(event)) { this._lastCallbackPayload = { User: (_a = this._clientInfo) === null || _a === void 0 ? void 0 : _a.userInfo, Event: event }; callback_service_1.CallbackService.register((_b = this._clientInfo) === null || _b === void 0 ? void 0 : _b.callbackURL); callback_service_1.CallbackService.sendCallbackEvents({ User: (_c = this._clientInfo) === null || _c === void 0 ? void 0 : _c.userInfo, Event: event }); } }); } /** * Callback function that is called when the state of a call changes. * @param event */ onCallStateChangedEvent(event) { return tslib_1.__awaiter(this, void 0, void 0, function* () { var _a, _b, _c; if (!this.isLastCallbackPayload(event)) { this._lastCallbackPayload = { User: (_a = this._clientInfo) === null || _a === void 0 ? void 0 : _a.userInfo, Event: event }; callback_service_1.CallbackService.register((_b = this._clientInfo) === null || _b === void 0 ? void 0 : _b.callbackURL); callback_service_1.CallbackService.sendCallbackEvents({ User: (_c = this._clientInfo) === null || _c === void 0 ? void 0 : _c.userInfo, Event: event }); } }); } /** * Callback function that is called when the state of a chat changes. * @param event */ onChatStateChangedEvent(event) { return tslib_1.__awaiter(this, void 0, void 0, function* () { var _a, _b, _c; if (!this.isLastCallbackPayload(event)) { this._lastCallbackPayload = { User: (_a = this._clientInfo) === null || _a === void 0 ? void 0 : _a.userInfo, Event: event }; callback_service_1.CallbackService.register((_b = this._clientInfo) === null || _b === void 0 ? void 0 : _b.callbackURL); callback_service_1.CallbackService.sendCallbackEvents({ User: (_c = this._clientInfo) === null || _c === void 0 ? void 0 : _c.userInfo, Event: event }); } }); } /** * Callback function that is called when the state of an email changes. * @param event */ onEmailStateChangedEvent(event) { return tslib_1.__awaiter(this, void 0, void 0, function* () { var _a, _b, _c; if (!this.isLastCallbackPayload(event)) { this._lastCallbackPayload = { User: (_a = this._clientInfo) === null || _a === void 0 ? void 0 : _a.userInfo, Event: event }; callback_service_1.CallbackService.register((_b = this._clientInfo) === null || _b === void 0 ? void 0 : _b.callbackURL); callback_service_1.CallbackService.sendCallbackEvents({ User: (_c = this._clientInfo) === null || _c === void 0 ? void 0 : _c.userInfo, Event: event }); } }); } /** * Callback function that is called when the state of a queue changes. * @param event */ onQueueStateChangedEvent(event) { return tslib_1.__awaiter(this, void 0, void 0, function* () { var _a, _b, _c; if (!this.isLastCallbackPayload(event)) { this._lastCallbackPayload = { User: (_a = this._clientInfo) === null || _a === void 0 ? void 0 : _a.userInfo, Event: event }; callback_service_1.CallbackService.register((_b = this._clientInfo) === null || _b === void 0 ? void 0 : _b.callbackURL); callback_service_1.CallbackService.sendCallbackEvents({ User: (_c = this._clientInfo) === null || _c === void 0 ? void 0 : _c.userInfo, Event: event }); } }); } /** * Callback function that is called when the state of a queue interaction changes. * @param event */ onQueueXionStateChangedEvent(event) { return tslib_1.__awaiter(this, void 0, void 0, function* () { var _a, _b, _c; if (!this.isLastCallbackPayload(event)) { this._lastCallbackPayload = { User: (_a = this._clientInfo) === null || _a === void 0 ? void 0 : _a.userInfo, Event: event }; callback_service_1.CallbackService.register((_b = this._clientInfo) === null || _b === void 0 ? void 0 : _b.callbackURL); callback_service_1.CallbackService.sendCallbackEvents({ User: (_c = this._clientInfo) === null || _c === void 0 ? void 0 : _c.userInfo, Event: event }); } }); } /** * Callback function that is called when the state of a social interaction changes. * @param event */ onSocialStateChangedEvent(event) { return tslib_1.__awaiter(this, void 0, void 0, function* () { var _a, _b, _c; if (!this.isLastCallbackPayload(event)) { this._lastCallbackPayload = { User: (_a = this._clientInfo) === null || _a === void 0 ? void 0 : _a.userInfo, Event: event }; callback_service_1.CallbackService.register((_b = this._clientInfo) === null || _b === void 0 ? void 0 : _b.callbackURL); callback_service_1.CallbackService.sendCallbackEvents({ User: (_c = this._clientInfo) === null || _c === void 0 ? void 0 : _c.userInfo, Event: event }); } }); } /** * Callback function that is called when the state of a video interaction changes. * @param event */ onVideoStateChangedEvent(event) { return tslib_1.__awaiter(this, void 0, void 0, function* () { var _a, _b, _c; if (!this.isLastCallbackPayload(event)) { this._lastCallbackPayload = { User: (_a = this._clientInfo) === null || _a === void 0 ? void 0 : _a.userInfo, Event: event }; callback_service_1.CallbackService.register((_b = this._clientInfo) === null || _b === void 0 ? void 0 : _b.callbackURL); callback_service_1.CallbackService.sendCallbackEvents({ User: (_c = this._clientInfo) === null || _c === void 0 ? void 0 : _c.userInfo, Event: event }); } }); } /** * Callback function that checks if the provided event is the last callback payload to avoid sending duplicate callbacks. * @param event * @returns */ isLastCallbackPayload(event) { return (0, isEqual_1.default)(event, this._lastCallbackPayload); } } exports.SocketIoClientService = SocketIoClientService; //# sourceMappingURL=socketio.client.service.js.map