Added the ExpensTypes in expense list API and get expenese filter API

This commit is contained in:
ashutosh.nehete 2025-10-04 12:25:02 +05:30
parent 658fa8cd23
commit d380dfebd2
2 changed files with 21 additions and 57 deletions

View File

@ -6,6 +6,7 @@
public List<Guid>? StatusIds { get; set; } public List<Guid>? StatusIds { get; set; }
public List<Guid>? CreatedByIds { get; set; } public List<Guid>? CreatedByIds { get; set; }
public List<Guid>? PaidById { get; set; } public List<Guid>? PaidById { get; set; }
public List<Guid>? ExpenseTypeIds { get; set; }
public bool IsTransactionDate { get; set; } = false; public bool IsTransactionDate { get; set; } = false;
public DateTime? StartDate { get; set; } public DateTime? StartDate { get; set; }
public DateTime? EndDate { get; set; } public DateTime? EndDate { get; set; }

View File

@ -187,6 +187,10 @@ namespace Marco.Pms.Services.Service
{ {
expensesQuery = expensesQuery.Where(e => expenseFilter.PaidById.Contains(e.PaidById)); 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. // Only allow filtering by 'CreatedBy' if the user has 'View All' permission.
if (expenseFilter.CreatedByIds?.Any() == true && hasViewAllPermissionTask.Result) if (expenseFilter.CreatedByIds?.Any() == true && hasViewAllPermissionTask.Result)
@ -391,66 +395,25 @@ namespace Marco.Pms.Services.Service
{ {
try try
{ {
// Task 1: Get all distinct projects associated with the tenant's expenses. var expenses = await _context.Expenses
var projectsTask = Task.Run(async () => .Include(e => e.PaidBy)
{ .Include(e => e.Project)
await using var dbContext = await _dbContextFactory.CreateDbContextAsync(); .Include(e => e.CreatedBy)
return await dbContext.Expenses .Include(e => e.Status)
.Where(e => e.TenantId == tenantId && e.Project != null) .Include(e => e.ExpensesType)
.Select(e => e.Project!) .Where(e => e.TenantId == tenantId)
.Distinct()
.Select(p => new { p.Id, Name = $"{p.Name}" })
.ToListAsync(); .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);
// Construct the final object from the results of the completed tasks. // Construct the final object from the results of the completed tasks.
return ApiResponse<object>.SuccessResponse(new var response = new
{ {
Projects = await projectsTask, Projects = expenses.Where(e => e.Project != null).Select(e => new { Id = e.Project!.Id, Name = e.Project.Name }).ToList(),
PaidBy = await paidByTask, PaidBy = expenses.Where(e => e.PaidBy != null).Select(e => new { Id = e.PaidBy!.Id, Name = $"{e.PaidBy.FirstName} {e.PaidBy.LastName}" }).ToList(),
CreatedBy = await createdByTask, CreatedBy = expenses.Where(e => e.CreatedBy != null).Select(e => new { Id = e.CreatedBy!.Id, Name = $"{e.CreatedBy.FirstName} {e.CreatedBy.LastName}" }).ToList(),
Status = await statusTask Status = expenses.Where(e => e.Status != null).Select(e => new { Id = e.Status!.Id, Name = e.Status.Name }).ToList(),
}, "Successfully fetched the filter list", 200); ExpensesType = expenses.Where(e => e.ExpensesType != null).Select(e => new { Id = e.ExpensesType!.Id, Name = e.ExpensesType.Name }).ToList()
};
return ApiResponse<object>.SuccessResponse(response, "Successfully fetched the filter list", 200);
} }
catch (Exception ex) catch (Exception ex)
{ {