private CommissionAllocationEntity MapModelToEntity(CommissionAllocationEdit model, CommissionAllocationEntity entity = null) { if (entity == null) { entity = new CommissionAllocationEntity(); } entity.FromClientId = model.FromClientId.Value; entity.ToClientId = model.ToClientId.Value; return(entity); }
public async Task DeleteCommissionAllocation() { var options = TestHelper.GetDbContext("DeleteCommissionAllocation"); var user1 = TestHelper.InsertUserDetailed(options); var client1 = TestHelper.InsertClient(options, user1.Organisation); var client2 = TestHelper.InsertClient(options, user1.Organisation); var client3 = TestHelper.InsertClient(options, user1.Organisation); var user2 = TestHelper.InsertUserDetailed(options); var ca1 = new CommissionAllocationEntity { Id = Guid.NewGuid(), FromClientId = client1.Client.Id, ToClientId = client2.Client.Id }; var cap1 = new CommissionAllocationPolicyEntity { Id = Guid.NewGuid(), CommissionAllocationId = ca1.Id, PolicyId = Guid.NewGuid() }; var ca2 = new CommissionAllocationEntity { Id = Guid.NewGuid(), FromClientId = client1.Client.Id, ToClientId = client3.Client.Id }; var cap2 = new CommissionAllocationPolicyEntity { Id = Guid.NewGuid(), CommissionAllocationId = ca2.Id, PolicyId = Guid.NewGuid() }; using (var context = new DataContext(options)) { context.CommissionAllocation.Add(ca1); context.CommissionAllocation.Add(ca2); context.CommissionAllocationPolicy.Add(cap1); context.CommissionAllocationPolicy.Add(cap2); context.SaveChanges(); } using (var context = new DataContext(options)) { var auditService = new AuditServiceMock(); var service = new CommissionAllocationService(context, auditService); //When var scope = TestHelper.GetScopeOptions(user1); var result = await service.DeleteCommissionAllocation(scope, ca2.Id); //Then Assert.True(result.Success); var actual = await context.CommissionAllocation.FindAsync(ca2.Id); Assert.Null(actual); var anyPolicies = await context.CommissionAllocationPolicy.Where(c => c.CommissionAllocationId == ca2.Id).AnyAsync(); Assert.False(anyPolicies); //Out of scope scope = TestHelper.GetScopeOptions(user2, Scope.User); result = await service.DeleteCommissionAllocation(scope, ca1.Id); Assert.False(result.Success); actual = await context.CommissionAllocation.FindAsync(ca1.Id); Assert.NotNull(actual); } }
public async Task UpdateCommissionAllocation() { var options = TestHelper.GetDbContext("UpdateCommissionAllocation"); var user1 = TestHelper.InsertUserDetailed(options); var client1 = TestHelper.InsertClient(options, user1.Organisation); var client2 = TestHelper.InsertClient(options, user1.Organisation); var client3 = TestHelper.InsertClient(options, user1.Organisation); var client4 = TestHelper.InsertClient(options, user1.Organisation); var user2 = TestHelper.InsertUserDetailed(options); var policy1 = new PolicyEntity { Id = Guid.NewGuid(), ClientId = client1.Client.Id, UserId = user1.User.Id }; var policy2 = new PolicyEntity { Id = Guid.NewGuid(), ClientId = client2.Client.Id, UserId = user1.User.Id }; var policy3 = new PolicyEntity { Id = Guid.NewGuid(), ClientId = client2.Client.Id, UserId = user1.User.Id }; var ca1 = new CommissionAllocationEntity { Id = Guid.NewGuid(), FromClientId = client1.Client.Id, ToClientId = client2.Client.Id }; var cap1 = new CommissionAllocationPolicyEntity { Id = Guid.NewGuid(), CommissionAllocationId = ca1.Id, PolicyId = Guid.NewGuid() }; var ca2 = new CommissionAllocationEntity { Id = Guid.NewGuid(), FromClientId = client1.Client.Id, ToClientId = client3.Client.Id }; var cap2 = new CommissionAllocationPolicyEntity { Id = Guid.NewGuid(), CommissionAllocationId = ca2.Id, PolicyId = policy1.Id }; using (var context = new DataContext(options)) { context.Policy.Add(policy1); context.Policy.Add(policy2); context.Policy.Add(policy3); context.CommissionAllocation.Add(ca1); context.CommissionAllocation.Add(ca2); context.CommissionAllocationPolicy.Add(cap2); context.CommissionAllocationPolicy.Add(cap1); context.SaveChanges(); } using (var context = new DataContext(options)) { var ca2Updated = new CommissionAllocationEdit { Id = ca2.Id, FromClientId = client2.Client.Id, ToClientId = client4.Client.Id, PolicyIds = new List <Guid>() { policy2.Id, policy3.Id } }; var auditService = new AuditServiceMock(); var service = new CommissionAllocationService(context, auditService); //When var scope = TestHelper.GetScopeOptions(user1); var result = await service.UpdateCommissionAllocation(scope, ca2Updated); //Then Assert.True(result.Success); var actual = await context.CommissionAllocation.FindAsync(ca2Updated.Id); Assert.Equal(ca2Updated.Id, actual.Id); Assert.Equal(ca2Updated.FromClientId, actual.FromClientId); Assert.Equal(ca2Updated.ToClientId, actual.ToClientId); var policyIds = await context.CommissionAllocationPolicy.Where(p => p.CommissionAllocationId == actual.Id).Select(p => p.PolicyId).ToListAsync(); Assert.Equal(ca2Updated.PolicyIds, policyIds); //Out of scope scope = TestHelper.GetScopeOptions(user2, Scope.User); result = await service.UpdateCommissionAllocation(scope, ca2Updated); Assert.False(result.Success); Assert.Equal("'Client' does not exist.", result.ValidationFailures.First().ErrorMessage); } }
public async Task GetCommissionAllocations() { var options = TestHelper.GetDbContext("GetCommissionAllocations"); var user1 = TestHelper.InsertUserDetailed(options); var client1 = TestHelper.InsertClient(options, user1.Organisation); var client2 = TestHelper.InsertClient(options, user1.Organisation); var client3 = TestHelper.InsertClient(options, user1.Organisation); var user2 = TestHelper.InsertUserDetailed(options); var client4 = TestHelper.InsertClient(options, user2.Organisation); var client5 = TestHelper.InsertClient(options, user2.Organisation); var ca1 = new CommissionAllocationEntity { Id = Guid.NewGuid(), FromClientId = client1.Client.Id, ToClientId = client2.Client.Id }; var cap1 = new CommissionAllocationPolicyEntity { Id = Guid.NewGuid(), CommissionAllocationId = ca1.Id, PolicyId = Guid.NewGuid() }; var ca2 = new CommissionAllocationEntity { Id = Guid.NewGuid(), FromClientId = client1.Client.Id, ToClientId = client3.Client.Id }; var cap2 = new CommissionAllocationPolicyEntity { Id = Guid.NewGuid(), CommissionAllocationId = ca2.Id, PolicyId = Guid.NewGuid() }; var ca3 = new CommissionAllocationEntity { Id = Guid.NewGuid(), FromClientId = client4.Client.Id, ToClientId = client5.Client.Id }; var cap3 = new CommissionAllocationPolicyEntity { Id = Guid.NewGuid(), CommissionAllocationId = ca3.Id, PolicyId = Guid.NewGuid() }; using (var context = new DataContext(options)) { context.CommissionAllocation.Add(ca1); context.CommissionAllocation.Add(ca3); context.CommissionAllocation.Add(ca2); context.CommissionAllocationPolicy.Add(cap2); context.CommissionAllocationPolicy.Add(cap1); context.CommissionAllocationPolicy.Add(cap3); context.SaveChanges(); } using (var context = new DataContext(options)) { var auditService = new AuditServiceMock(); var service = new CommissionAllocationService(context, auditService); //When var scope = TestHelper.GetScopeOptions(user1); var queryOptions = new CommissionAllocationQueryOptions(scope, "", "", 0, 0); var allocations = await service.GetCommissionAllocations(queryOptions); //Then Assert.Equal(2, allocations.TotalItems); Assert.Equal(2, allocations.Items.Count()); var items = allocations.Items.ToList(); var actual = items[0]; Assert.Equal(ca1.Id, actual.Id); Assert.Equal(ca1.FromClientId, actual.FromClientId); Assert.Equal(ca1.ToClientId, actual.ToClientId); Assert.Equal(1, actual.PolicyIdCount); Assert.Equal(client1.Client.FirstName, actual.FromClientFirstName); Assert.Equal(client1.Client.LastName, actual.FromClientLastName); actual = items[1]; Assert.Equal(ca2.Id, actual.Id); //Check scope scope = TestHelper.GetScopeOptions(user2, Scope.Organisation); queryOptions = new CommissionAllocationQueryOptions(scope, "", "", 0, 0); allocations = await service.GetCommissionAllocations(queryOptions); Assert.Single(allocations.Items); actual = allocations.Items.First(); Assert.Equal(ca3.Id, actual.Id); } }
public async Task GetCommissionAllocation() { var options = TestHelper.GetDbContext("GetCommissionAllocation"); var user1 = TestHelper.InsertUserDetailed(options); var client1 = TestHelper.InsertClient(options, user1.Organisation); var client2 = TestHelper.InsertClient(options, user1.Organisation); var client3 = TestHelper.InsertClient(options, user1.Organisation); var user2 = TestHelper.InsertUserDetailed(options); var ca1 = new CommissionAllocationEntity { Id = Guid.NewGuid(), FromClientId = client1.Client.Id, ToClientId = client2.Client.Id }; var cap1 = new CommissionAllocationPolicyEntity { Id = Guid.NewGuid(), CommissionAllocationId = ca1.Id, PolicyId = Guid.NewGuid() }; var ca2 = new CommissionAllocationEntity { Id = Guid.NewGuid(), FromClientId = client1.Client.Id, ToClientId = client3.Client.Id }; var cap2 = new CommissionAllocationPolicyEntity { Id = Guid.NewGuid(), CommissionAllocationId = ca2.Id, PolicyId = Guid.NewGuid() }; using (var context = new DataContext(options)) { context.CommissionAllocation.Add(ca1); context.CommissionAllocation.Add(ca2); context.CommissionAllocationPolicy.Add(cap2); context.CommissionAllocationPolicy.Add(cap1); context.SaveChanges(); } using (var context = new DataContext(options)) { var auditService = new AuditServiceMock(); var service = new CommissionAllocationService(context, auditService); //When var scope = TestHelper.GetScopeOptions(user1); var allocation = await service.GetCommissionAllocation(scope, ca2.Id); //Then Assert.Equal(ca2.Id, allocation.Id); Assert.Equal(ca2.FromClientId, allocation.FromClientId); Assert.Equal(ca2.ToClientId, allocation.ToClientId); Assert.Equal(new List <Guid>() { cap2.PolicyId }, allocation.PolicyIds); //Check scope scope = TestHelper.GetScopeOptions(user2, Scope.Organisation); allocation = await service.GetCommissionAllocation(scope, ca2.Id); Assert.Null(allocation); } }
public async Task GetClientRevenueData_Allocations() { var options = await CreateDatabaseSqlServer(); var company = TestHelper.InsertCompany(options); var commissionType1 = TestHelper.InsertCommissionType(options, PolicyType.POLICY_TYPE_INVESTMENT, CommissionEarningsType.EARNINGS_TYPE_MONTHLY_ANNUITY); var user1 = TestHelper.InsertUserDetailed(options); var client1 = TestHelper.InsertClient(options, user1.Organisation); var client2 = TestHelper.InsertClient(options, user1.Organisation); var client3 = TestHelper.InsertClient(options, user1.Organisation); var thisMonth = DateTime.Now.Date; var cs1 = new CommissionStatementEntity { Id = Guid.NewGuid(), CompanyId = company.Id, AmountIncludingVAT = 0, VAT = 0, Date = thisMonth, Processed = true, OrganisationId = user1.Organisation.Id }; //------------------------------------------------------------------------ var policy1 = new PolicyEntity { Id = Guid.NewGuid(), Number = Guid.NewGuid().ToString(), CompanyId = company.Id, ClientId = client1.Client.Id, UserId = user1.User.Id }; var commission1 = new CommissionEntity { Id = Guid.NewGuid(), PolicyId = policy1.Id, UserId = policy1.UserId, CommissionTypeId = commissionType1.Id, AmountIncludingVAT = 100, VAT = 0, CommissionStatementId = cs1.Id }; //------------------------------------------------------------------------ var policy2 = new PolicyEntity { Id = Guid.NewGuid(), Number = Guid.NewGuid().ToString(), CompanyId = company.Id, ClientId = client2.Client.Id, UserId = user1.User.Id }; var commission2 = new CommissionEntity { Id = Guid.NewGuid(), PolicyId = policy2.Id, UserId = policy2.UserId, CommissionTypeId = commissionType1.Id, AmountIncludingVAT = 200, VAT = 0, CommissionStatementId = cs1.Id }; //------------------------------------------------------------------------ var policy3a = new PolicyEntity { Id = Guid.NewGuid(), Number = Guid.NewGuid().ToString(), CompanyId = company.Id, ClientId = client3.Client.Id, UserId = user1.User.Id }; var commission3a = new CommissionEntity { Id = Guid.NewGuid(), PolicyId = policy3a.Id, UserId = policy3a.UserId, CommissionTypeId = commissionType1.Id, AmountIncludingVAT = 300, VAT = 0, CommissionStatementId = cs1.Id }; var policy3b = new PolicyEntity { Id = Guid.NewGuid(), Number = Guid.NewGuid().ToString(), CompanyId = company.Id, ClientId = client3.Client.Id, UserId = user1.User.Id }; var commission3b = new CommissionEntity { Id = Guid.NewGuid(), PolicyId = policy3b.Id, UserId = policy3b.UserId, CommissionTypeId = commissionType1.Id, AmountIncludingVAT = 400, VAT = 0, CommissionStatementId = cs1.Id }; var policy3c = new PolicyEntity { Id = Guid.NewGuid(), Number = Guid.NewGuid().ToString(), CompanyId = company.Id, ClientId = client3.Client.Id, UserId = user1.User.Id }; var commission3c = new CommissionEntity { Id = Guid.NewGuid(), PolicyId = policy3c.Id, UserId = policy3c.UserId, CommissionTypeId = commissionType1.Id, AmountIncludingVAT = 500, VAT = 0, CommissionStatementId = cs1.Id }; //------------------------------------------------------------------------ //Allocate Client1, Policy1 to Client2. var allocation1 = new CommissionAllocationEntity { Id = Guid.NewGuid(), FromClientId = client1.Client.Id, ToClientId = client2.Client.Id }; var cap1 = new CommissionAllocationPolicyEntity { Id = Guid.NewGuid(), CommissionAllocationId = allocation1.Id, PolicyId = policy1.Id }; //Allocate Client3, Policy3b to Client2. var allocation2 = new CommissionAllocationEntity { Id = Guid.NewGuid(), FromClientId = client3.Client.Id, ToClientId = client2.Client.Id }; var cap2 = new CommissionAllocationPolicyEntity { Id = Guid.NewGuid(), CommissionAllocationId = allocation2.Id, PolicyId = policy3b.Id }; using (var context = new DataContext(options)) { context.Policy.Add(policy2); context.Policy.Add(policy1); context.Policy.Add(policy3a); context.Policy.Add(policy3b); context.Policy.Add(policy3c); context.CommissionStatement.Add(cs1); context.Commission.Add(commission3a); context.Commission.Add(commission1); context.Commission.Add(commission2); context.Commission.Add(commission3b); context.Commission.Add(commission3c); context.CommissionAllocation.Add(allocation1); context.CommissionAllocation.Add(allocation2); context.CommissionAllocationPolicy.Add(cap1); context.CommissionAllocationPolicy.Add(cap2); context.SaveChanges(); } using (var context = new DataContext(options)) { var service = new CommissionReportService(context); //When var scope = TestHelper.GetScopeOptions(user1); var queryOptions = new ClientRevenueQueryOptions(scope, "MonthlyAnnuityMonth", "asc", 0, 0, $"YearEnding={thisMonth.Year};MonthEnding={thisMonth.Month}"); var data = await service.GetClientRevenueData(queryOptions); //Then var results = data.Items.ToList(); Assert.Equal(3, data.TotalItems); var resultsCount = results.Count(); Assert.Equal(3, resultsCount); var actual = results[0]; Assert.Equal(client1.Client.Id, actual.ClientId); Assert.Equal(100, actual.MonthlyAnnuityMonth); Assert.Equal(0, actual.AllocationsCount); actual = results[1]; Assert.Equal(client2.Client.Id, actual.ClientId); Assert.Equal(700, actual.MonthlyAnnuityMonth); //200 + 100 (pol1) + 400 (pol3b) Assert.Equal(2, actual.AllocationsCount); actual = results[2]; Assert.Equal(client3.Client.Id, actual.ClientId); Assert.Equal(1200, actual.MonthlyAnnuityMonth); //300 + 400 + 500 Assert.Equal(0, actual.AllocationsCount); } }