public async Task <int> Create(int userId, Language language, string deviceId)
        {
            Guest guest = null;
            var   user  = await _repository.FilterAsNoTracking <User>(u => u.Id == userId).FirstOrDefaultAsync();

            if (user == null)
            {
                guest = await _repository.FilterAsNoTracking <Guest>(g => g.DeviceId == deviceId).FirstOrDefaultAsync();

                if (guest == null)
                {
                    throw new Exception(_optionsBinder.Error().UserNotFound);
                }
            }

            var admin = await _repository.Filter <User>(x => x.RoleEnum == Role.Admin).FirstOrDefaultAsync();

            if (admin == null)
            {
                throw new Exception("configuration not found");
            }
            if (user?.Id == admin.Id)
            {
                throw new Exception(_optionsBinder.Error().ConversationWithYou);
            }
            var conversation = new SupportConversation
            {
                AdminId = admin.Id
            };

            if (user != null)
            {
                conversation.UserId  = user.Id;
                conversation.GuestId = null;
            }
            else
            {
                conversation.UserId  = null;
                conversation.GuestId = guest.Id;
            }
            _repository.Create(conversation);
            await _repository.SaveChangesAsync();

            return(conversation.Id);
        }
Exemplo n.º 2
0
        public SupportConversation AddSupportConversation(SupportConversationBindingModel model)
        {
            try
            {
                SupportConversation support = (_dbContext.SupportConversations.Any(x => x.EntityId == model.EntityId && x.userType == model.userType)) ? _dbContext.SupportConversations.First(x => x.EntityId == model.EntityId && x.userType == model.userType) : new SupportConversation();
                if (support.Id > 0)
                {
                    support.LastConversationDate = DateTime.UtcNow;
                }
                else
                {
                    Mapper.Map(model, support);
                }

                _dbContext.SupportConversations.Add(support);
                _dbContext.SaveChanges();
                return(support);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }