Exemplo n.º 1
0
        /// <summary>
        /// It updates payload
        /// </summary>
        /// <param name="extraPayload"></param>
        /// <returns></returns>
        public async Task UpdatePayload(Dictionary <string, object> extraPayload)
        {
            var userDetails = await CurrentUserDetails();

            var userStoreId        = userDetails.CustomerStoreId;
            var userCurrentChannel = await livechatRules.GetCustomerChannel(
                userStoreId,
                userDetails.LivechatUserId
                );

            var storeRepository = uow.GetRepository <StoreRepository>();
            var store           = await storeRepository.GetAICredentials(userStoreId);

            var city = await storeRepository.GetCity(store.CityId);

            extraPayload.Add("nome", userDetails.Payload["name"]);
            extraPayload.Add("telefone", userDetails.Payload["phone"]);
            extraPayload.Add("loja", userDetails.Payload["currentStore"]);
            extraPayload.Add("store", store);
            extraPayload.Add("city", city);

            var userRepository = uow.GetRepository <UserRepository>();

            //userDetails.Payload[3] =
            await userRepository.SetNewUserPayloadFromWidget(
                channelId : userCurrentChannel.ChannelId,
                newPayload : extraPayload,
                exceptUserId : userDetails.LivechatUserId);

            await CurrentUser.SendAsync(HubMessages.EDIT_CUSTOMER_PROFILE_SAVED);

            /*
             * await conversationHandler.PersistPayload(
             *  livechatUserId: userDetails.LivechatUserId,
             *  channelId: userCurrentChannel.ChannelId,
             *  userPayload: userDetails.Payload,
             *  overwritePayload: false
             * ); */
            return;
        }
        /// <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);
                //}
            }
        }