shoc-backend/SeaHaven.DataServices/Implementation/DashboardDataService.cs

40 lines
1.4 KiB
C#

using Data.SeaHavenIndustries;
using Microsoft.EntityFrameworkCore;
using SeaHaven.DataServices.Interfaces;
namespace SeaHaven.DataServices.Implementation
{
public class DashboardDataService : IDashboardDataService
{
private readonly ApplicationDbContext _context;
public DashboardDataService(ApplicationDbContext context)
{
_context = context;
}
public async Task<DashboardCounts> GetWorkOrderCountsAsync(
int? accountId,
CancellationToken cancellationToken)
{
var query = _context.workOrders.Where(w => w.istemplate != true);
if (accountId is int scopedAccountId)
{
query = query.Where(w => w.AccountId == scopedAccountId);
}
var total = await query.CountAsync(cancellationToken);
var open = await query.CountAsync(w => w.Status == "Open" || w.Status == "In Progress" || w.Status == "On Hold", cancellationToken);
var notDispatched = await query.CountAsync(w => (w.Status == "Open") && (w.AssignTo == null || w.AssignTo == ""), cancellationToken);
var completed = await query.CountAsync(w => w.Status == "Done", cancellationToken);
return new DashboardCounts
{
Total = total,
Open = open,
NotDispatched = notDispatched,
Completed = completed
};
}
}
}