From d380dfebd2923db3c1f09faaba00555d16ac8f62 Mon Sep 17 00:00:00 2001 From: "ashutosh.nehete" Date: Sat, 4 Oct 2025 12:25:02 +0530 Subject: [PATCH] Added the ExpensTypes in expense list API and get expenese filter API --- Marco.Pms.Model/Filters/ExpensesFilter.cs | 1 + Marco.Pms.Services/Service/ExpensesService.cs | 77 +++++-------------- 2 files changed, 21 insertions(+), 57 deletions(-) diff --git a/Marco.Pms.Model/Filters/ExpensesFilter.cs b/Marco.Pms.Model/Filters/ExpensesFilter.cs index bd24ab8..597820d 100644 --- a/Marco.Pms.Model/Filters/ExpensesFilter.cs +++ b/Marco.Pms.Model/Filters/ExpensesFilter.cs @@ -6,6 +6,7 @@ public List? StatusIds { get; set; } public List? CreatedByIds { get; set; } public List? PaidById { get; set; } + public List? ExpenseTypeIds { get; set; } public bool IsTransactionDate { get; set; } = false; public DateTime? StartDate { get; set; } public DateTime? EndDate { get; set; } diff --git a/Marco.Pms.Services/Service/ExpensesService.cs b/Marco.Pms.Services/Service/ExpensesService.cs index d913fa1..28da818 100644 --- a/Marco.Pms.Services/Service/ExpensesService.cs +++ b/Marco.Pms.Services/Service/ExpensesService.cs @@ -187,6 +187,10 @@ namespace Marco.Pms.Services.Service { expensesQuery = expensesQuery.Where(e => expenseFilter.PaidById.Contains(e.PaidById)); } + if (expenseFilter.ExpenseTypeIds?.Any() == true) + { + expensesQuery = expensesQuery.Where(e => expenseFilter.ExpenseTypeIds.Contains(e.ExpensesTypeId)); + } // Only allow filtering by 'CreatedBy' if the user has 'View All' permission. if (expenseFilter.CreatedByIds?.Any() == true && hasViewAllPermissionTask.Result) @@ -391,66 +395,25 @@ namespace Marco.Pms.Services.Service { try { - // Task 1: Get all distinct projects associated with the tenant's expenses. - var projectsTask = Task.Run(async () => - { - await using var dbContext = await _dbContextFactory.CreateDbContextAsync(); - return await dbContext.Expenses - .Where(e => e.TenantId == tenantId && e.Project != null) - .Select(e => e.Project!) - .Distinct() - .Select(p => new { p.Id, Name = $"{p.Name}" }) - .ToListAsync(); - }); - - // Task 2: Get all distinct users who paid for the tenant's expenses. - var paidByTask = Task.Run(async () => - { - await using var dbContext = await _dbContextFactory.CreateDbContextAsync(); - return await dbContext.Expenses - .Where(e => e.TenantId == tenantId && e.PaidBy != null) - .Select(e => e.PaidBy!) - .Distinct() - .Select(u => new { u.Id, Name = $"{u.FirstName} {u.LastName}" }) - .ToListAsync(); - }); - - // Task 3: Get all distinct users who created the tenant's expenses. - var createdByTask = Task.Run(async () => - { - await using var dbContext = await _dbContextFactory.CreateDbContextAsync(); - return await dbContext.Expenses - .Where(e => e.TenantId == tenantId && e.CreatedBy != null) - .Select(e => e.CreatedBy!) - .Distinct() - .Select(u => new { u.Id, Name = $"{u.FirstName} {u.LastName}" }) - .ToListAsync(); - }); - - // Task 4: Get all distinct statuses associated with the tenant's expenses. - var statusTask = Task.Run(async () => - { - await using var dbContext = await _dbContextFactory.CreateDbContextAsync(); - return await dbContext.Expenses - .Where(e => e.TenantId == tenantId && e.Status != null) - .Select(e => e.Status!) - .Distinct() - .Select(s => new { s.Id, s.Name }) - .ToListAsync(); - }); - - // Execute all four queries concurrently. The total wait time will be determined - // by the longest-running query, not the sum of all four. - await Task.WhenAll(projectsTask, paidByTask, createdByTask, statusTask); + var expenses = await _context.Expenses + .Include(e => e.PaidBy) + .Include(e => e.Project) + .Include(e => e.CreatedBy) + .Include(e => e.Status) + .Include(e => e.ExpensesType) + .Where(e => e.TenantId == tenantId) + .ToListAsync(); // Construct the final object from the results of the completed tasks. - return ApiResponse.SuccessResponse(new + var response = new { - Projects = await projectsTask, - PaidBy = await paidByTask, - CreatedBy = await createdByTask, - Status = await statusTask - }, "Successfully fetched the filter list", 200); + Projects = expenses.Where(e => e.Project != null).Select(e => new { Id = e.Project!.Id, Name = e.Project.Name }).ToList(), + PaidBy = expenses.Where(e => e.PaidBy != null).Select(e => new { Id = e.PaidBy!.Id, Name = $"{e.PaidBy.FirstName} {e.PaidBy.LastName}" }).ToList(), + CreatedBy = expenses.Where(e => e.CreatedBy != null).Select(e => new { Id = e.CreatedBy!.Id, Name = $"{e.CreatedBy.FirstName} {e.CreatedBy.LastName}" }).ToList(), + Status = expenses.Where(e => e.Status != null).Select(e => new { Id = e.Status!.Id, Name = e.Status.Name }).ToList(), + ExpensesType = expenses.Where(e => e.ExpensesType != null).Select(e => new { Id = e.ExpensesType!.Id, Name = e.ExpensesType.Name }).ToList() + }; + return ApiResponse.SuccessResponse(response, "Successfully fetched the filter list", 200); } catch (Exception ex) {