public static ChannelSummaryResponse ToChannelSummaryResponse(this Domain.Channel.Channel channel,
                                                                      bool isMuted,
                                                                      Domain.Message.Message lastReadMessage,
                                                                      MemberSummary creator,
                                                                      CloudStorageConfiguration configuration)
        {
            var channelListResponse = new ChannelSummaryResponse();

            if (channel != null)
            {
                channelListResponse.Id                  = channel.Id;
                channelListResponse.IsClosed            = channel.IsClosed;
                channelListResponse.Created             = channel.Created;
                channelListResponse.Updated             = channel.Updated;
                channelListResponse.Name                = channel.Name;
                channelListResponse.Description         = channel.Description;
                channelListResponse.WelcomeMessage      = channel.WelcomeMessage;
                channelListResponse.Type                = channel.Type;
                channelListResponse.IsMuted             = isMuted;
                channelListResponse.CreatorId           = channel.CreatorId ?? creator?.Id;
                channelListResponse.Creator             = channel.Creator?.ToMemberSummary(configuration) ?? creator;
                channelListResponse.CreatorSaasUserId   = channel.Creator?.SaasUserId ?? creator?.SaasUserId;
                channelListResponse.LastMessage         = channel.Messages?.FirstOrDefault()?.ToMessageResponse(lastReadMessage, configuration);
                channelListResponse.UnreadMessagesCount = lastReadMessage != null?channel.Messages?.Count(x => x.Created > lastReadMessage.Created) ?? 0 : channel.Messages?.Count ?? 0;
            }

            return(channelListResponse);
        }
        public static ChannelResponse ToChannelResponse(this Domain.Channel.Channel channel, CloudStorageConfiguration configuration)
        {
            var channelResponse = new ChannelResponse();

            if (channel != null)
            {
                channelResponse.Id             = channel.Id;
                channelResponse.IsClosed       = channel.IsClosed;
                channelResponse.Updated        = channel.Updated;
                channelResponse.Created        = channel.Created;
                channelResponse.MembersCount   = channel.MembersCount;
                channelResponse.Name           = channel.Name;
                channelResponse.Description    = channel.Description;
                channelResponse.WelcomeMessage = channel.WelcomeMessage;
                channelResponse.Type           = channel.Type;
                channelResponse.CreatorId      = channel.CreatorId;
            }
            return(channelResponse);
        }
        public async Task <ChannelSummaryResponse> CreateChannelAsync(CreateChannelRequest request)
        {
            var profile = await UnitOfWork.MemberRepository.GetMemberBySaasUserIdAsync(request.SaasUserId);

            Ensure.That(profile)
            .WithException(x => new NotFoundException(new ErrorDto(ErrorCode.NotFound, "Member does not exist.")))
            .IsNotNull();

            string permanentChannelImageUrl = await CopyImageToDestinationContainerAsync(request.PhotoUrl);

            var newChannel = new Domain.Channel.Channel
            {
                Id             = Guid.NewGuid(),
                Created        = DateTimeOffset.UtcNow,
                Name           = request.Name,
                Description    = request.Description,
                WelcomeMessage = request.WelcomeMessage,
                Type           = request.Type,
                Members        = new List <ChannelMembers>(),
                CreatorId      = profile.Id,
                Creator        = profile,
                MembersCount   = 0,
                PhotoUrl       = permanentChannelImageUrl
            };

            var creator = new ChannelMembers
            {
                ChannelId         = newChannel.Id,
                MemberId          = profile.Id,
                LastReadMessageId = null,
                IsMuted           = false
            };

            newChannel.Members.Add(creator);

            if (request.Type == ChannelType.Private && request.AllowedMembers.Any())
            {
                foreach (var saasUserId in request.AllowedMembers)
                {
                    var member = await UnitOfWork.MemberRepository.GetMemberBySaasUserIdAsync(saasUserId);

                    Ensure.That(member).WithException(x => new NotFoundException(new ErrorDto(ErrorCode.NotFound, "Member does not exist."))).IsNotNull();
                    var model = new ChannelMembers
                    {
                        ChannelId         = newChannel.Id,
                        MemberId          = member.Id,
                        LastReadMessageId = null
                    };

                    newChannel.Members.Add(model);
                }
            }

            var channelMembers = newChannel.Members.DistinctBy(x => x.MemberId);

            using (var transactionScope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
            {
                await UnitOfWork.ChannelRepository.AddChannelAsync(newChannel);

                foreach (var member in channelMembers)
                {
                    await UnitOfWork.ChannelMemberRepository.AddChannelMemberAsync(member);

                    await UnitOfWork.ChannelRepository.IncrementChannelMembersCount(newChannel.Id);
                }

                transactionScope.Complete();
            }

            var channel = await UnitOfWork.ChannelRepository.GetChannelByIdAsync(newChannel.Id);

            return(channel.ToChannelSummaryResponse(creator.IsMuted, null, null, _configuration));
        }