protected override void Configure()
        {
            CreateMap <AdvertiserQueryViewModel, AdvertiserQueryOptions>()
            .ConstructUsing(ctx =>
            {
                var source = (AdvertiserQueryViewModel)ctx.SourceValue;
                var retVal = new AdvertiserQueryOptions();

                if (source.UserId.HasValue)
                {
                    var userId          = source.UserId.Value;
                    var isAddedAsUserId = false;
                    var userBuyerRoles  = ctx.Resolve <IUserService>().GetUserBuyerRoles(userId).ToList();
                    foreach (var userBuyerRole in userBuyerRoles)
                    {
                        if (userBuyerRole.RoleName == StandardRole.Administrator)
                        {
                            // match Advertiser.BuyerAccountId if user is AgencyAdministrator
                            retVal.BuyerAccountIds.Add(userBuyerRole.BuyerAccountId);
                        }
                        else if (!isAddedAsUserId && userBuyerRole.RoleName == StandardRole.User)
                        {
                            // match Advertiser.UserAdvertiserRoles if user is AgencyUser
                            retVal.UserIds.Add(userId);
                            isAddedAsUserId = true;
                        }
                    }
                }

                return(retVal);
            });
        }
        public void GetAdvertisers_ShouldReturnAllAdvertisers_WhenNoOptions()
        {
            // Arrange
            var emptyOptions = new AdvertiserQueryOptions();

            var buyerAccount1 = new BuyerAccount {
                BuyerAccountId = 1
            };
            var buyerAccount2 = new BuyerAccount {
                BuyerAccountId = 2
            };
            var advertisers = new List <Advertiser>
            {
                new Advertiser {
                    BuyerAccountId = buyerAccount1.BuyerAccountId, BuyerAccount = buyerAccount1
                },
                new Advertiser {
                    BuyerAccountId = buyerAccount1.BuyerAccountId, BuyerAccount = buyerAccount1
                },
                new Advertiser {
                    BuyerAccountId = buyerAccount2.BuyerAccountId, BuyerAccount = buyerAccount2
                }
            };

            MockUnitOfWorkRepository <Advertiser>().Setup(x => x.Queryable())
            .Returns(advertisers.ToAsyncEnumerable());

            // Act
            var retVal = Mock.Create <AdvertiserService>().GetAdvertisers(emptyOptions).ToList();

            // Assert
            Assert.That(retVal, Is.Not.Null);
            Assert.That(retVal.Count, Is.EqualTo(3));
        }
        public void GetAdvertisers_ShouldReturnMatchedAdvertiser_WhenWithUserIdsOption()
        {
            // Arrange
            var userId       = Guid.NewGuid();
            var queryOptions = new AdvertiserQueryOptions();

            queryOptions.UserIds.Add(userId);

            var buyerAccount1 = new BuyerAccount {
                BuyerAccountId = 1
            };
            var buyerAccount2 = new BuyerAccount {
                BuyerAccountId = 2
            };
            var advertisers = new List <Advertiser>
            {
                new Advertiser
                {
                    BuyerAccountId      = buyerAccount1.BuyerAccountId,
                    BuyerAccount        = buyerAccount1,
                    UserAdvertiserRoles = new List <UserAdvertiserRole>
                    {
                        new UserAdvertiserRole {
                            UserId = userId
                        }
                    }
                },
                new Advertiser
                {
                    BuyerAccountId      = buyerAccount1.BuyerAccountId,
                    BuyerAccount        = buyerAccount1,
                    UserAdvertiserRoles = new List <UserAdvertiserRole>
                    {
                        new UserAdvertiserRole {
                            UserId = userId
                        }
                    }
                },
                new Advertiser
                {
                    BuyerAccountId      = buyerAccount2.BuyerAccountId,
                    BuyerAccount        = buyerAccount2,
                    UserAdvertiserRoles = new List <UserAdvertiserRole>
                    {
                        new UserAdvertiserRole {
                            UserId = userId
                        }
                    }
                },
            };

            MockUnitOfWorkRepository <Advertiser>().Setup(x => x.Queryable())
            .Returns(advertisers.ToAsyncEnumerable());

            // Act
            var retVal = Mock.Create <AdvertiserService>().GetAdvertisers(queryOptions).ToList();

            // Assert
            Assert.That(retVal, Is.Not.Null);
            Assert.That(retVal.Count, Is.EqualTo(3));
        }