public async Task Index() { var commission = new OneAdvisor.Model.Commission.Model.Commission.Commission() { Id = Guid.NewGuid(), AmountIncludingVAT = 100, VAT = 20, CommissionStatementId = Guid.NewGuid(), CommissionTypeId = Guid.NewGuid(), PolicyId = Guid.NewGuid(), PolicyNumber = "123456", UserId = Guid.NewGuid() }; var pagedItems = new PagedCommissions() { TotalItems = 1, SumAmountIncludingVAT = 300, SumVAT = 400, Items = new List <OneAdvisor.Model.Commission.Model.Commission.Commission>() { commission } }; var service = new Mock <ICommissionService>(); var authService = TestHelper.MockAuthenticationService(Scope.Branch); CommissionQueryOptions queryOptions = null; service.Setup(c => c.GetCommissions(It.IsAny <CommissionQueryOptions>())) .Callback((CommissionQueryOptions options) => queryOptions = options) .ReturnsAsync(pagedItems); var controller = new CommissionsController(service.Object, authService.Object); var result = await controller.Index("policyNumber", "desc", 15, 2, $"commissionTypeId={commission.CommissionTypeId}"); Assert.Equal(Scope.Branch, queryOptions.Scope.Scope); Assert.Equal("policyNumber", queryOptions.SortOptions.Column); Assert.Equal(SortDirection.Descending, queryOptions.SortOptions.Direction); Assert.Equal(15, queryOptions.PageOptions.Size); Assert.Equal(2, queryOptions.PageOptions.Number); Assert.Equal(commission.CommissionTypeId, queryOptions.CommissionTypeId.Single()); var okResult = Assert.IsType <OkObjectResult>(result); var returnValue = Assert.IsType <PagedCommissions>(okResult.Value); Assert.Same(pagedItems, returnValue); }
public async Task <PagedCommissions> GetCommissions(CommissionQueryOptions queryOptions) { var userQuery = ScopeQuery.GetUserEntityQuery(_context, queryOptions.Scope); var query = from commission in _context.Commission join user in userQuery on commission.UserId equals user.Id join policy in _context.Policy on commission.PolicyId equals policy.Id join client in _context.Client on policy.ClientId equals client.Id join statement in _context.CommissionStatement on commission.CommissionStatementId equals statement.Id select new OneAdvisor.Model.Commission.Model.Commission.Commission() { Id = commission.Id, CommissionStatementId = commission.CommissionStatementId, PolicyId = commission.PolicyId, CommissionTypeId = commission.CommissionTypeId, AmountIncludingVAT = commission.AmountIncludingVAT, VAT = commission.VAT, UserId = commission.UserId, BranchId = user.BranchId, PolicyNumber = policy.Number, CommissionStatementDate = statement.Date, PolicyCompanyId = policy.CompanyId, PolicyClientLastName = client.LastName, PolicyClientInitials = client.Initials, PolicyClientDateOfBirth = client.DateOfBirth, SplitGroupId = commission.SplitGroupId, PolicyTypeId = policy.PolicyTypeId, }; //Apply filters ---------------------------------------------------------------------------------------- if (queryOptions.CommissionStatementId.HasValue) { query = query.Where(c => c.CommissionStatementId == queryOptions.CommissionStatementId); } if (queryOptions.UserId.Any()) { query = query.Where(c => queryOptions.UserId.Contains(c.UserId)); } if (queryOptions.BranchId.Any()) { query = query.Where(c => queryOptions.BranchId.Contains(c.BranchId)); } if (queryOptions.CommissionTypeId.Any()) { query = query.Where(c => queryOptions.CommissionTypeId.Contains(c.CommissionTypeId)); } if (queryOptions.PolicyCompanyId.Any()) { query = query.Where(c => queryOptions.PolicyCompanyId.Contains(c.PolicyCompanyId)); } if (!string.IsNullOrWhiteSpace(queryOptions.PolicyNumber)) { query = query.Where(m => EF.Functions.Like(m.PolicyNumber, queryOptions.PolicyNumber)); } if (!string.IsNullOrWhiteSpace(queryOptions.PolicyClientLastName)) { query = query.Where(m => EF.Functions.Like(m.PolicyClientLastName, queryOptions.PolicyClientLastName)); } if (queryOptions.StartDate.HasValue) { query = query.Where(c => c.CommissionStatementDate >= queryOptions.StartDate.Value.Date); } if (queryOptions.EndDate.HasValue) { query = query.Where(c => c.CommissionStatementDate <= queryOptions.EndDate.Value.Date); } //------------------------------------------------------------------------------------------------------ var pagedItems = new PagedCommissions(); //Get total items pagedItems.TotalItems = await query.CountAsync(); //Aggregations var aggQuery = from commission in query select new { SumAmountIncludingVAT = query.Select(c => (decimal?)c.AmountIncludingVAT).Sum(), SumVAT = query.Select(c => (decimal?)c.VAT).Sum(), }; var aggregates = await aggQuery.FirstOrDefaultAsync(); if (aggregates != null) { pagedItems.SumAmountIncludingVAT = aggregates.SumAmountIncludingVAT.Value; pagedItems.SumVAT = aggregates.SumVAT.Value; } //Ordering query = query.OrderBy(queryOptions.SortOptions.Column, queryOptions.SortOptions.Direction); //Paging pagedItems.Items = await query.TakePage(queryOptions.PageOptions.Number, queryOptions.PageOptions.Size).ToListAsync(); return(pagedItems); }