/// <summary> /// This method should be call right after a user with Agent profile logs in /// </summary> /// <param name="userDetails">Current user details</param> /// <returns>A async task</returns> public async Task OnAgentConnectedAsync(UserDetails userDetails) { if (userDetails == null) { throw new System.ArgumentNullException(nameof(userDetails)); } var currentUserDetails = userDetails; // Assign to a new session var livechatSession = new LivechatUserSession { ConnectionId = currentUserDetails.ConnectionId, LivechatUserId = currentUserDetails.LivechatUserId }; await livechatRules.CreateNewSession(livechatSession); var channels = await conversationHandler.SendChannelListToAgent(currentUserDetails.ConnectionId, livechatSession.LivechatUserId); currentUserDetails.Channels = channels; // Assign all agent channels to the new connectionId await AddAgentChannels(currentUserDetails.ConnectionId, channels); }
/// <summary> /// This method should be call right after a user with Customer profile logs in /// </summary> /// <param name="userDetails"></param> /// <returns></returns> public async Task OnCustomerConnectedAsync(UserDetails userDetails) { if (userDetails == null) { throw new ArgumentNullException(nameof(userDetails)); } var userStoreId = userDetails.CustomerStoreId; if (userStoreId <= 0) { context.Connection.Abort(); return; } // CAUTION userDetails.AddStore(userStoreId); // Assign to a new session var livechatSession = new LivechatUserSession { ConnectionId = userDetails.ConnectionId, LivechatUserId = userDetails.LivechatUserId }; // Include connected user inside Nop Customer table await livechatRules.CloneUserToAStore(userDetails.CustomerId, userDetails.CustomerStoreId); await livechatRules.CreateNewSession(livechatSession); // Check if LivechatId is already registered in some chat with this current store // Gets only active stores (IsFinished is false) var userCurrentChannel = await livechatRules.GetCustomerChannel( userStoreId, livechatSession.LivechatUserId ); // If there is no channel for Customer x Store, it creates a new one and deal with it if (userCurrentChannel == null) { userCurrentChannel = await AssignLivechatUserToChannel(userDetails); // Persists current user channel on details so it can be recovery later on online/offline status userDetails.Channels.Add(userCurrentChannel); // Adds user payload await conversationHandler.PersistPayload( livechatUserId : livechatSession.LivechatUserId, channelId : userCurrentChannel.ChannelId, userPayload : userDetails.Payload, overwritePayload : false ); // This is the right time to call a new agent on IChatConversationHandler instance await conversationHandler.OnChannelCreated(userCurrentChannel); return; } // If it went this far, is probably because the currentUser's connection went down and its connecting again // so basically we should provide its current channel and check if there is some available agent to continue this call // // Adds current connectionId to correct user group // After that it sends the new channel registration to the user await NotifyAndAddUserToGroup( userDetails.ConnectionId, userCurrentChannel.ChannelId ); // Persists current user channel on details so it can be recovery later on online/offline status userDetails.Channels.Add(userCurrentChannel); // Adds user payload await conversationHandler.PersistPayload( livechatUserId : livechatSession.LivechatUserId, channelId : userCurrentChannel.ChannelId, userPayload : userDetails.Payload, overwritePayload : false ); // Verifies if user is alone. If it is try to find a new agent again var agentsOnChannel = await conversationHandler.GetAgentsOnChannel( userCurrentChannel.ChannelId, userDetails.LivechatUserId ); if (agentsOnChannel.Count == 0) { await conversationHandler.FindNewAgent(userCurrentChannel); } else { // TODO: Send an warning telling agent about new users online on this channel //foreach (var agent in agentsOnChannel) //{ // await conversationHandler.SendChannelListToAgent(string.Empty, agent.LivechatUserId); //} } }