public async Task <IActionResult> Update(Guid commissionSplitRulePolicyId, [FromBody] CommissionSplitRulePolicy commissionSplitRulePolicy) { commissionSplitRulePolicy.Id = commissionSplitRulePolicyId; var scope = AuthenticationService.GetScope(User); var result = await CommissionSplitRulePolicyService.UpdateCommissionSplitRulePolicy(scope, commissionSplitRulePolicy); if (!result.Success) { return(BadRequest(result.ValidationFailures)); } return(Ok(result)); }
public async Task UpdateCommissionSplitRule_IsDefault_Deleted() { var options = TestHelper.GetDbContext("UpdateCommissionSplitRule_IsDefault_Deleted"); var user1 = TestHelper.InsertUserDetailed(options); var client1 = TestHelper.InsertClient(options, user1.Organisation); var policy1 = 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 = true, //Is Default 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.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 = policy1.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.Null(actual); actual = await context.CommissionSplitRulePolicy.FindAsync(csrp1.Id); Assert.NotNull(actual); } }
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); } }