Exemplo n.º 1
0
        public async Task <IActionResult> Insert([FromBody] CommissionSplitRulePolicy commissionSplitRulePolicy)
        {
            var scope = AuthenticationService.GetScope(User);

            var result = await CommissionSplitRulePolicyService.InsertCommissionSplitRulePolicy(scope, commissionSplitRulePolicy);

            if (!result.Success)
            {
                return(BadRequest(result.ValidationFailures));
            }

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

            var user1   = TestHelper.InsertUserDetailed(options);
            var client1 = TestHelper.InsertClient(options, user1.Organisation);

            var user2 = TestHelper.InsertUserDetailed(options);

            var policy1 = new PolicyEntity
            {
                Id        = Guid.NewGuid(),
                CompanyId = Guid.NewGuid(),
                ClientId  = client1.Client.Id,
                UserId    = user1.User.Id,
                Number    = "123465",
            };

            var policy2 = new PolicyEntity
            {
                Id        = Guid.NewGuid(),
                CompanyId = Guid.NewGuid(),
                ClientId  = client1.Client.Id,
                UserId    = user1.User.Id,
                Number    = "123465",
            };

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

            var csrp1 = new CommissionSplitRulePolicyEntity
            {
                Id       = Guid.NewGuid(),
                PolicyId = policy1.Id,
                CommissionSplitRuleId = Guid.NewGuid()
            };

            var csrp2 = new CommissionSplitRulePolicyEntity
            {
                Id       = Guid.NewGuid(),
                PolicyId = policy1.Id,
                CommissionSplitRuleId = Guid.NewGuid()
            };

            using (var context = new DataContext(options))
            {
                context.Policy.Add(policy1);
                context.Policy.Add(policy2);

                context.CommissionSplitRule.Add(csr2);

                context.CommissionSplitRulePolicy.Add(csrp1);
                context.CommissionSplitRulePolicy.Add(csrp2);

                context.SaveChanges();
            }

            using (var context = new DataContext(options))
            {
                var csrp2Updated = new CommissionSplitRulePolicy
                {
                    Id       = csrp2.Id,
                    PolicyId = policy2.Id,
                    CommissionSplitRuleId = csr2.Id
                };

                var auditService           = new AuditServiceMock();
                var commissionSplitService = new CommissionSplitService(context, auditService);
                var service = new CommissionSplitRulePolicyService(context, commissionSplitService, auditService);

                //When
                var scope  = TestHelper.GetScopeOptions(user1);
                var result = await service.UpdateCommissionSplitRulePolicy(scope, csrp2Updated);

                //Then
                Assert.True(result.Success);

                var actual = await context.CommissionSplitRulePolicy.FindAsync(csrp2Updated.Id);

                Assert.Equal(csrp2Updated.Id, actual.Id);
                Assert.Equal(csrp2Updated.PolicyId, actual.PolicyId);
                Assert.Equal(csrp2Updated.CommissionSplitRuleId, actual.CommissionSplitRuleId);

                //Out of scope
                scope  = TestHelper.GetScopeOptions(user2, Scope.User);
                result = await service.InsertCommissionSplitRulePolicy(scope, csrp2Updated);

                Assert.False(result.Success);
                Assert.Equal("'Policy' does not exist.", result.ValidationFailures.First().ErrorMessage);
            }
        }
        public async Task InsertCommissionSplitRulePolicy_IsDefault_NotInserted()
        {
            var options = TestHelper.GetDbContext("InsertCommissionSplitRulePolicy_IsDefault_NotInserted");

            var user1   = TestHelper.InsertUserDetailed(options);
            var client1 = TestHelper.InsertClient(options, user1.Organisation);

            var user2 = TestHelper.InsertUserDetailed(options);

            var policy1 = new PolicyEntity
            {
                Id        = Guid.NewGuid(),
                CompanyId = Guid.NewGuid(),
                ClientId  = client1.Client.Id,
                UserId    = user1.User.Id,
                Number    = "123465",
            };

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

            using (var context = new DataContext(options))
            {
                context.Policy.Add(policy1);
                context.CommissionSplitRule.Add(csr1);

                context.SaveChanges();
            }

            using (var context = new DataContext(options))
            {
                var csrp1 = new CommissionSplitRulePolicy
                {
                    Id       = Guid.NewGuid(),
                    PolicyId = policy1.Id,
                    CommissionSplitRuleId = csr1.Id
                };

                var auditService           = new AuditServiceMock();
                var commissionSplitService = new CommissionSplitService(context, auditService);
                var service = new CommissionSplitRulePolicyService(context, commissionSplitService, auditService);

                //When
                var scope  = TestHelper.GetScopeOptions(user1);
                var result = await service.InsertCommissionSplitRulePolicy(scope, csrp1);

                //Then
                Assert.True(result.Success);

                var actual = await context.CommissionSplitRulePolicy.FirstOrDefaultAsync();

                Assert.Null(actual);
            }
        }