Ejemplo n.º 1
0
        public async Task <IActionResult> Index(string sortColumn, string sortDirection, int pageSize = 0, int pageNumber = 0, string filters = null)
        {
            var scope = AuthenticationService.GetScope(User);

            var queryOptions = new CommissionSplitRuleQueryOptions(scope, sortColumn, sortDirection, pageSize, pageNumber, filters);

            var commissions = await CommissionSplitService.GetCommissionSplitRules(queryOptions);

            return(Ok(commissions));
        }
        public async Task GetCommissionSplitRules()
        {
            var options = TestHelper.GetDbContext("GetCommissionSplitRules");

            var user1 = TestHelper.InsertUserDetailed(options);
            var user2 = TestHelper.InsertUserDetailed(options, user1.Organisation);
            var user3 = TestHelper.InsertUserDetailed(options);

            var csr1 = new CommissionSplitRuleEntity
            {
                Id        = Guid.NewGuid(),
                UserId    = user1.User.Id,
                Name      = "Com Split Rule 1",
                IsDefault = true,
                Split     = new List <CommissionSplit>()
                {
                    new CommissionSplit()
                    {
                        UserId     = user1.User.Id,
                        Percentage = 100
                    }
                }
            };

            var csr2 = new CommissionSplitRuleEntity
            {
                Id        = Guid.NewGuid(),
                UserId    = user2.User.Id,
                Name      = "Com Split Rule 2",
                IsDefault = true,
                Split     = new List <CommissionSplit>()
                {
                    new CommissionSplit()
                    {
                        UserId     = user2.User.Id,
                        Percentage = 100
                    }
                }
            };

            var csr3 = new CommissionSplitRuleEntity
            {
                Id        = Guid.NewGuid(),
                UserId    = user3.User.Id,
                Name      = "Com Split Rule 3",
                IsDefault = true,
                Split     = new List <CommissionSplit>()
                {
                    new CommissionSplit()
                    {
                        UserId     = user3.User.Id,
                        Percentage = 100
                    }
                }
            };

            var csr4 = new CommissionSplitRuleEntity
            {
                Id        = Guid.NewGuid(),
                UserId    = user1.User.Id,
                Name      = "Com Split Rule 4",
                IsDefault = false,
                Split     = new List <CommissionSplit>()
                {
                    new CommissionSplit()
                    {
                        UserId     = user1.User.Id,
                        Percentage = 100
                    }
                }
            };

            using (var context = new DataContext(options))
            {
                context.CommissionSplitRule.Add(csr1);
                context.CommissionSplitRule.Add(csr2);
                context.CommissionSplitRule.Add(csr3);
                context.CommissionSplitRule.Add(csr4);

                context.SaveChanges();
            }

            using (var context = new DataContext(options))
            {
                var auditService = new AuditServiceMock();
                var service      = new CommissionSplitService(context, auditService);

                //When
                var scope        = TestHelper.GetScopeOptions(user1);
                var queryOptions = new CommissionSplitRuleQueryOptions(scope, "", "", 0, 0);
                var rules        = await service.GetCommissionSplitRules(queryOptions);

                //Then
                Assert.Equal(3, rules.TotalItems);
                Assert.Equal(3, rules.Items.Count());

                var items  = rules.Items.ToList();
                var actual = items[0];
                Assert.Equal(csr1.Id, actual.Id);
                Assert.Equal(csr1.Name, actual.Name);
                Assert.Equal(csr1.UserId, actual.UserId);
                Assert.Equal(csr1.IsDefault, actual.IsDefault);
                Assert.Equal(csr1.Split, actual.Split);

                actual = items[1];
                Assert.Equal(csr2.Id, actual.Id);

                actual = items[2];
                Assert.Equal(csr4.Id, actual.Id);
                Assert.Equal(csr4.IsDefault, actual.IsDefault);

                //Filter check (user id)
                scope        = TestHelper.GetScopeOptions(user1);
                queryOptions = new CommissionSplitRuleQueryOptions(scope, "", "", 0, 0);
                queryOptions.UserId.Add(user2.User.Id);
                rules = await service.GetCommissionSplitRules(queryOptions);

                Assert.Single(rules.Items);

                actual = rules.Items.First();
                Assert.Equal(csr2.Id, actual.Id);


                //Check scope
                scope        = TestHelper.GetScopeOptions(user3, Scope.Organisation);
                queryOptions = new CommissionSplitRuleQueryOptions(scope, "", "", 0, 0);
                rules        = await service.GetCommissionSplitRules(queryOptions);

                Assert.Single(rules.Items);

                actual = rules.Items.First();
                Assert.Equal(csr3.Id, actual.Id);
            }
        }