/* Updates Chat Helper record to mark them offline, no longer connected and closes any changes they have open in chatSessions */ public static void UpdateChatHelper(string connectionId) { using (tsc_tools db = new tsc_tools()) { chatHelper chatHelper = new chatHelper(); var chatHelperRecord = db.chatHelpers.Where(c => c.connectionId == connectionId).SingleOrDefault(); if (chatHelperRecord != null) { chatHelperRecord.connectionId = ""; chatHelperRecord.currentChats = 0; chatHelperRecord.onlineStatus = "Offline"; } db.Entry(chatHelperRecord).CurrentValues.SetValues(chatHelperRecord); db.SaveChanges(); chatSession chatSession = new chatSession(); foreach (var chatSessionRecord in db.chatSessions.Where(c => c.helperConnectionId == connectionId && c.completeDate == "").ToList()) { chatSessionRecord.completeDate = System.DateTime.UtcNow.ToString("yyyy-MM-ddTHH\\:mm\\:ssZ"); } db.SaveChanges(); } }
/* Sets Database tables to an initialized state on application startup * Empties records from table sashaSessionRecords * chatSessionRecords * set completeDate to 'Auto Closed' for any records that were not closed * ChatHelpers * connectionId "" * currentChats 0 * lastChatTime CurrentTime */ public static void InitializeTables() { using (tsc_tools db = new tsc_tools()) { sashaSession sashaSession = new sashaSession(); var sashaSessionRecords = db.sashaSessions; db.sashaSessions.RemoveRange(sashaSessionRecords); db.SaveChanges(); chatSession chatSession = new chatSession(); foreach (var chatSessionRecord in db.chatSessions.Where(c => c.completeDate == "").ToList()) { chatSessionRecord.completeDate = "Auto Closed"; } db.SaveChanges(); foreach (var chatHelperRecord in db.chatHelpers.ToList()) { chatHelperRecord.connectionId = ""; chatHelperRecord.currentChats = 0; chatHelperRecord.lastChatTime = System.DateTime.UtcNow.ToString("yyyy-MM-ddTHH\\:mm\\:ssZ"); } db.SaveChanges(); } }
/* Checks for an available helper and connects them if ready */ public static bool GetAvailableHelper(string smpSessionId, string userId, string userName, string connectionId, string flowName, string stepName) { Dictionary <string, string> returnInfo = new Dictionary <string, string>(); using (tsc_tools db = new tsc_tools()) { chatHelper chatHelper = new chatHelper(); var chatHelperRecord = (from c in db.chatHelpers where c.onlineStatus == "Online" select c ).FirstOrDefault(); if (chatHelperRecord == null) { /* No Online Chat Helpers */ var context = GlobalHost.ConnectionManager.GetHubContext <MyHub>(); var time = System.DateTime.UtcNow.ToString("yyyy-MM-ddTHH\\:mm\\:ssZ"); context.Clients.Client(connectionId).throwMessage("Notice", "There are currently no chat helpers online.", false); context.Clients.Client(connectionId).broadcastMessage(smpSessionId, time, "SYSTEM", "There are currently no chat helpers online."); chatSession ChatSession = new chatSession(); ChatSession.chatGUID = Guid.NewGuid(); ChatSession.sashaSessionId = smpSessionId; ChatSession.agentConnectionId = connectionId; ChatSession.agentId = userId; ChatSession.lastActivity = System.DateTime.UtcNow.ToString("yyyy-MM-ddTHH\\:mm\\:ssZ"); ChatSession.requestDate = System.DateTime.UtcNow.ToString("yyyy-MM-ddTHH\\:mm\\:ssZ"); ChatSession.flowName = flowName; ChatSession.stepName = stepName; db.chatSessions.Add(ChatSession); db.SaveChanges(); return(false); } chatHelperRecord = (from c in db.chatHelpers where c.onlineStatus == "Online" && c.currentChats < c.maximumChats && c.userId != userId orderby c.lastChatTime ascending select c ).FirstOrDefault(); if (chatHelperRecord == null) { /* Helpers online but all at maximum sessions */ var context = GlobalHost.ConnectionManager.GetHubContext <MyHub>(); var time = System.DateTime.UtcNow.ToString("yyyy-MM-ddTHH\\:mm\\:ssZ"); context.Clients.Client(connectionId).throwMessage("Notice", "All available chat helpers are busy.", false); context.Clients.Client(connectionId).broadcastMessage(smpSessionId, time, "SYSTEM", "All chat helpers are busy."); chatSession ChatSession = new chatSession(); ChatSession.chatGUID = Guid.NewGuid(); ChatSession.sashaSessionId = smpSessionId; ChatSession.agentConnectionId = connectionId; ChatSession.agentId = userId; ChatSession.lastActivity = System.DateTime.UtcNow.ToString("yyyy-MM-ddTHH\\:mm\\:ssZ"); ChatSession.requestDate = System.DateTime.UtcNow.ToString("yyyy-MM-ddTHH\\:mm\\:ssZ"); ChatSession.completeDate = ""; ChatSession.flowName = flowName; ChatSession.stepName = stepName; db.chatSessions.Add(ChatSession); db.SaveChanges(); return(false); } if (chatHelperRecord != null) { string chatHelperId = chatHelperRecord.userId; string chatHelperName = chatHelperRecord.user.userName; string chatHelperConnectionId = chatHelperRecord.connectionId; string lastChatTime = System.DateTime.UtcNow.ToString("yyyy-MM-ddTHH\\:mm\\:ssZ"); int currentChats = chatHelperRecord.currentChats + 1; /* Helper Found */ var time = System.DateTime.UtcNow.ToString("yyyy-MM-ddTHH\\:mm\\:ssZ"); var context = GlobalHost.ConnectionManager.GetHubContext <MyHub>(); context.Clients.Client(connectionId).broadcastMessage(smpSessionId, time, chatHelperName, "Hello " + userName + " how may I assist you?"); returnInfo.Add("Available", "True"); returnInfo.Add("chatHelperId", chatHelperId); returnInfo.Add("chatHelperName", chatHelperName); chatHelperRecord.currentChats = currentChats; db.Entry(chatHelperRecord).CurrentValues.SetValues(chatHelperRecord); db.SaveChanges(); chatSession ChatSession = new chatSession(); ChatSession.chatGUID = Guid.NewGuid(); ChatSession.sashaSessionId = smpSessionId; ChatSession.agentConnectionId = connectionId; ChatSession.helperConnectionId = chatHelperConnectionId; ChatSession.agentId = userId; ChatSession.helperId = chatHelperId; ChatSession.lastActivity = System.DateTime.UtcNow.ToString("yyyy-MM-ddTHH\\:mm\\:ssZ"); ChatSession.requestDate = System.DateTime.UtcNow.ToString("yyyy-MM-ddTHH\\:mm\\:ssZ"); ChatSession.completeDate = ""; ChatSession.flowName = flowName; ChatSession.stepName = stepName; db.chatSessions.Add(ChatSession); db.SaveChanges(); context.Groups.Add(chatHelperConnectionId, smpSessionId); context.Clients.Client(chatHelperConnectionId).addChatTab(smpSessionId, userName, "pull"); } return(true); } }