mirror of
https://github.com/Sea-Haven-Industries/shoc-backend.git
synced 2026-10-04 16:02:11 +00:00
feat(dashboard): count open unassigned work orders for the period (SH-392)
GET /dashboard/stats now returns unassigned: open (not Completed or Canceled) work orders with no dispatcher in the selected period, within the caller's server-derived account scope. It uses the same filters as the Unassigned board search, so the Dashboard number equals the list it drills into. A dispatcher-scoped Dashboard has no unassigned work and returns 0.
This commit is contained in:
parent
9d5798437d
commit
4876fa2717
7 changed files with 229 additions and 1 deletions
|
|
@ -40,7 +40,8 @@ public class DashboardControllerTests
|
||||||
{
|
{
|
||||||
ScheduledTomorrow = 3,
|
ScheduledTomorrow = 3,
|
||||||
PendingUplifts = 5,
|
PendingUplifts = 5,
|
||||||
AvetaPending = 7
|
AvetaPending = 7,
|
||||||
|
Unassigned = 11
|
||||||
});
|
});
|
||||||
|
|
||||||
var result = await NewController(service).GetStats(
|
var result = await NewController(service).GetStats(
|
||||||
|
|
@ -50,5 +51,6 @@ public class DashboardControllerTests
|
||||||
((int)Prop(body, "scheduledTomorrow")).Should().Be(3);
|
((int)Prop(body, "scheduledTomorrow")).Should().Be(3);
|
||||||
((int)Prop(body, "pendingUplifts")).Should().Be(5);
|
((int)Prop(body, "pendingUplifts")).Should().Be(5);
|
||||||
((int)Prop(body, "avetaPending")).Should().Be(7);
|
((int)Prop(body, "avetaPending")).Should().Be(7);
|
||||||
|
((int)Prop(body, "unassigned")).Should().Be(11);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
176
Api.SeaHavenIndustries.Tests/DashboardUnassignedTests.cs
Normal file
176
Api.SeaHavenIndustries.Tests/DashboardUnassignedTests.cs
Normal file
|
|
@ -0,0 +1,176 @@
|
||||||
|
using System.Security.Claims;
|
||||||
|
using Data.SeaHavenIndustries;
|
||||||
|
using Data.SeaHavenIndustries.Enums;
|
||||||
|
using FluentAssertions;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using SeaHaven.DataServices.Implementation;
|
||||||
|
using SeaHaven.Services.DTOs;
|
||||||
|
using SeaHaven.Services.Helpers;
|
||||||
|
using SeaHaven.Services.Implementation;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace Api.SeaHavenIndustries.Tests;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// SH-392: the Dashboard "Unassigned" count is open work orders with no
|
||||||
|
/// dispatcher in the selected period, and it equals the Work Orders list the
|
||||||
|
/// tile drills into (GET /board/search, Dispatcher = Unassigned).
|
||||||
|
/// </summary>
|
||||||
|
public class DashboardUnassignedTests
|
||||||
|
{
|
||||||
|
private static readonly DateOnly From = new(2026, 9, 21);
|
||||||
|
private static readonly DateOnly To = new(2026, 9, 25);
|
||||||
|
|
||||||
|
private static readonly List<LifecycleStatus> OpenStatuses = Enum.GetValues<LifecycleStatus>()
|
||||||
|
.Where(s => s != LifecycleStatus.Completed && s != LifecycleStatus.Canceled)
|
||||||
|
.ToList();
|
||||||
|
|
||||||
|
private static ApplicationDbContext NewContext()
|
||||||
|
{
|
||||||
|
var options = new DbContextOptionsBuilder<ApplicationDbContext>()
|
||||||
|
.UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString())
|
||||||
|
.Options;
|
||||||
|
return new ApplicationDbContext(options);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static WorkOrderAccountResolver Resolver(ApplicationDbContext ctx)
|
||||||
|
=> new(new AccountDataService(ctx), new LocationDataService(ctx));
|
||||||
|
|
||||||
|
private static DashboardService NewDashboard(ApplicationDbContext ctx)
|
||||||
|
=> new(new DashboardDataService(ctx), Resolver(ctx));
|
||||||
|
|
||||||
|
private static WorkOrderAdvancedSearchService NewSearch(ApplicationDbContext ctx)
|
||||||
|
=> new(new WorkOrderAdvancedSearchDataService(ctx), Resolver(ctx));
|
||||||
|
|
||||||
|
private static ClaimsPrincipal AccountUser(int accountId, string role = "Admin", string userId = "admin-1")
|
||||||
|
=> new(new ClaimsIdentity(new[]
|
||||||
|
{
|
||||||
|
new Claim(SeaHavenClaimTypes.AccountId, accountId.ToString()),
|
||||||
|
new Claim(ClaimTypes.NameIdentifier, userId),
|
||||||
|
new Claim(ClaimTypes.Role, role)
|
||||||
|
}, "test"));
|
||||||
|
|
||||||
|
private static WorkOrder Wo(
|
||||||
|
int id,
|
||||||
|
DateTime? scheduled,
|
||||||
|
string? assignTo = null,
|
||||||
|
LifecycleStatus status = LifecycleStatus.Scheduled,
|
||||||
|
DateOnly? targetWeek = null,
|
||||||
|
int accountId = 1)
|
||||||
|
=> new()
|
||||||
|
{
|
||||||
|
Id = id,
|
||||||
|
AccountId = accountId,
|
||||||
|
InternalWONumber = $"2000000{id:0000}",
|
||||||
|
AssignTo = assignTo,
|
||||||
|
ScheduledDate = scheduled,
|
||||||
|
ScheduleWeekOnly = targetWeek.HasValue,
|
||||||
|
TargetWeek = targetWeek,
|
||||||
|
LifecycleStatus = status,
|
||||||
|
istemplate = false
|
||||||
|
};
|
||||||
|
|
||||||
|
private static void Seed(ApplicationDbContext ctx)
|
||||||
|
{
|
||||||
|
ctx.workOrders.AddRange(
|
||||||
|
Wo(1, new DateTime(2026, 9, 22)), // counted
|
||||||
|
Wo(2, new DateTime(2026, 9, 23), assignTo: ""), // counted: blank assignee
|
||||||
|
Wo(3, null, targetWeek: new DateOnly(2026, 9, 21)), // counted: week-only in range
|
||||||
|
Wo(4, new DateTime(2026, 9, 24), assignTo: "disp-1"), // assigned
|
||||||
|
Wo(5, new DateTime(2026, 9, 24), status: LifecycleStatus.Completed),
|
||||||
|
Wo(6, new DateTime(2026, 9, 24), status: LifecycleStatus.Canceled),
|
||||||
|
Wo(7, new DateTime(2026, 8, 4)), // out of range
|
||||||
|
Wo(8, null, status: LifecycleStatus.Incomplete), // undated
|
||||||
|
Wo(9, new DateTime(2026, 9, 22), accountId: 2), // other tenant
|
||||||
|
Wo(10, null, status: LifecycleStatus.Incomplete, accountId: 2)); // other tenant, undated
|
||||||
|
ctx.SaveChanges();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task GetStatsAsync_Range_CountsOpenUnassignedScheduledInPeriod()
|
||||||
|
{
|
||||||
|
using var ctx = NewContext();
|
||||||
|
Seed(ctx);
|
||||||
|
|
||||||
|
var stats = await NewDashboard(ctx).GetStatsAsync(
|
||||||
|
AccountUser(1), new DashboardStatsQueryDTO { DateFrom = From, DateTo = To }, CancellationToken.None);
|
||||||
|
|
||||||
|
stats.Unassigned.Should().Be(3);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task GetStatsAsync_AllTime_CountsEveryOpenUnassignedIncludingUndated()
|
||||||
|
{
|
||||||
|
using var ctx = NewContext();
|
||||||
|
Seed(ctx);
|
||||||
|
|
||||||
|
var stats = await NewDashboard(ctx).GetStatsAsync(
|
||||||
|
AccountUser(1), new DashboardStatsQueryDTO(), CancellationToken.None);
|
||||||
|
|
||||||
|
// 1, 2, 3, 7 (out of any week but open) and 8 (undated).
|
||||||
|
stats.Unassigned.Should().Be(5);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task GetStatsAsync_Unassigned_NeverCountsAnotherTenantsWorkOrders()
|
||||||
|
{
|
||||||
|
using var ctx = NewContext();
|
||||||
|
Seed(ctx);
|
||||||
|
|
||||||
|
var accountTwo = await NewDashboard(ctx).GetStatsAsync(
|
||||||
|
AccountUser(2), new DashboardStatsQueryDTO(), CancellationToken.None);
|
||||||
|
var accountTwoInRange = await NewDashboard(ctx).GetStatsAsync(
|
||||||
|
AccountUser(2), new DashboardStatsQueryDTO { DateFrom = From, DateTo = To }, CancellationToken.None);
|
||||||
|
|
||||||
|
accountTwo.Unassigned.Should().Be(2);
|
||||||
|
accountTwoInRange.Unassigned.Should().Be(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task GetStatsAsync_DispatcherScope_HasNoUnassignedWork()
|
||||||
|
{
|
||||||
|
using var ctx = NewContext();
|
||||||
|
Seed(ctx);
|
||||||
|
|
||||||
|
var dispatcher = await NewDashboard(ctx).GetStatsAsync(
|
||||||
|
AccountUser(1, role: "Dispatcher", userId: "disp-1"),
|
||||||
|
new DashboardStatsQueryDTO(),
|
||||||
|
CancellationToken.None);
|
||||||
|
var adminPickingDispatcher = await NewDashboard(ctx).GetStatsAsync(
|
||||||
|
AccountUser(1),
|
||||||
|
new DashboardStatsQueryDTO { DispatcherId = "disp-1" },
|
||||||
|
CancellationToken.None);
|
||||||
|
|
||||||
|
dispatcher.Unassigned.Should().Be(0);
|
||||||
|
adminPickingDispatcher.Unassigned.Should().Be(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Theory]
|
||||||
|
[InlineData(true)]
|
||||||
|
[InlineData(false)]
|
||||||
|
public async Task GetStatsAsync_UnassignedMatchesTheDrillDownList(bool withRange)
|
||||||
|
{
|
||||||
|
using var ctx = NewContext();
|
||||||
|
Seed(ctx);
|
||||||
|
var user = AccountUser(1);
|
||||||
|
|
||||||
|
var stats = await NewDashboard(ctx).GetStatsAsync(
|
||||||
|
user,
|
||||||
|
withRange ? new DashboardStatsQueryDTO { DateFrom = From, DateTo = To } : new DashboardStatsQueryDTO(),
|
||||||
|
CancellationToken.None);
|
||||||
|
|
||||||
|
// What the Work Orders board sends for the tile's drill-down link.
|
||||||
|
var list = await NewSearch(ctx).SearchAsync(new WorkOrderAdvancedSearchQueryDto
|
||||||
|
{
|
||||||
|
DatePreset = WorkOrderAdvancedSearchDatePreset.Custom,
|
||||||
|
DateFrom = withRange ? From : new DateOnly(2000, 1, 1),
|
||||||
|
DateTo = withRange ? To : new DateOnly(2099, 12, 31),
|
||||||
|
IncludeDateless = !withRange,
|
||||||
|
Dispatchers = new List<string> { "__unassigned__" },
|
||||||
|
Statuses = OpenStatuses,
|
||||||
|
PageSize = 200
|
||||||
|
}, user, "admin-1");
|
||||||
|
|
||||||
|
stats.Unassigned.Should().Be(list.TotalCount);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -33,6 +33,7 @@ namespace Api.SeaHavenIndustries.Controllers
|
||||||
scheduledTomorrow = stats.ScheduledTomorrow,
|
scheduledTomorrow = stats.ScheduledTomorrow,
|
||||||
pendingUplifts = stats.PendingUplifts,
|
pendingUplifts = stats.PendingUplifts,
|
||||||
avetaPending = stats.AvetaPending,
|
avetaPending = stats.AvetaPending,
|
||||||
|
unassigned = stats.Unassigned,
|
||||||
breakdown = stats.Breakdown,
|
breakdown = stats.Breakdown,
|
||||||
dueCount = stats.DueCount,
|
dueCount = stats.DueCount,
|
||||||
completedDueCount = stats.CompletedDueCount,
|
completedDueCount = stats.CompletedDueCount,
|
||||||
|
|
|
||||||
|
|
@ -39,6 +39,34 @@ namespace SeaHaven.DataServices.Implementation
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static readonly IReadOnlyList<LifecycleStatus> OpenLifecycleStatuses = Enum
|
||||||
|
.GetValues<LifecycleStatus>()
|
||||||
|
.Where(s => s != LifecycleStatus.Completed && s != LifecycleStatus.Canceled)
|
||||||
|
.ToList();
|
||||||
|
|
||||||
|
public async Task<int> CountOpenUnassignedAsync(
|
||||||
|
int? accountId,
|
||||||
|
string? dispatcherId,
|
||||||
|
DateOnly? dateFrom,
|
||||||
|
DateOnly? dateTo,
|
||||||
|
CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
// A dispatcher-scoped Dashboard has no unassigned work by definition.
|
||||||
|
if (dispatcherId != null)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
var query = WorkOrderBoardQueryFilters.ApplyBaseScope(_context.workOrders.AsNoTracking());
|
||||||
|
if (accountId is int scopedAccountId)
|
||||||
|
query = WorkOrderBoardQueryFilters.ApplyAccountScope(query, scopedAccountId);
|
||||||
|
query = WorkOrderBoardQueryFilters.ApplyDispatcherFilter(
|
||||||
|
query, new[] { "__unassigned__" }, myWorkOrders: false, currentUserId: null);
|
||||||
|
query = WorkOrderBoardQueryFilters.ApplyStatusFilter(query, OpenLifecycleStatuses);
|
||||||
|
if (dateFrom is DateOnly from && dateTo is DateOnly to)
|
||||||
|
query = WorkOrderBoardQueryFilters.ApplyDateRangeFilter(query, from, to);
|
||||||
|
|
||||||
|
return await query.CountAsync(cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
public async Task<IReadOnlyList<DashboardWorkOrder>> GetDashboardWorkOrdersAsync(
|
public async Task<IReadOnlyList<DashboardWorkOrder>> GetDashboardWorkOrdersAsync(
|
||||||
int? accountId,
|
int? accountId,
|
||||||
string? dispatcherId,
|
string? dispatcherId,
|
||||||
|
|
|
||||||
|
|
@ -14,6 +14,18 @@ namespace SeaHaven.DataServices.Interfaces
|
||||||
DateOnly today,
|
DateOnly today,
|
||||||
CancellationToken cancellationToken);
|
CancellationToken cancellationToken);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Open work orders with no dispatcher in the period, counted with the
|
||||||
|
/// same filters as GET /board/search for Dispatcher=Unassigned (SH-392),
|
||||||
|
/// so the Dashboard number matches the list it drills into.
|
||||||
|
/// </summary>
|
||||||
|
Task<int> CountOpenUnassignedAsync(
|
||||||
|
int? accountId,
|
||||||
|
string? dispatcherId,
|
||||||
|
DateOnly? dateFrom,
|
||||||
|
DateOnly? dateTo,
|
||||||
|
CancellationToken cancellationToken);
|
||||||
|
|
||||||
Task<IReadOnlyList<DashboardWorkOrder>> GetDashboardWorkOrdersAsync(
|
Task<IReadOnlyList<DashboardWorkOrder>> GetDashboardWorkOrdersAsync(
|
||||||
int? accountId,
|
int? accountId,
|
||||||
string? dispatcherId,
|
string? dispatcherId,
|
||||||
|
|
|
||||||
|
|
@ -106,6 +106,8 @@ namespace SeaHaven.Services.DTOs
|
||||||
public int ScheduledTomorrow { get; set; }
|
public int ScheduledTomorrow { get; set; }
|
||||||
public int PendingUplifts { get; set; }
|
public int PendingUplifts { get; set; }
|
||||||
public int AvetaPending { get; set; }
|
public int AvetaPending { get; set; }
|
||||||
|
/// <summary>Open work orders with no dispatcher in the selected period (SH-392).</summary>
|
||||||
|
public int Unassigned { get; set; }
|
||||||
public DashboardBreakdownDTO Breakdown { get; set; } = new();
|
public DashboardBreakdownDTO Breakdown { get; set; } = new();
|
||||||
public int DueCount { get; set; }
|
public int DueCount { get; set; }
|
||||||
public int CompletedDueCount { get; set; }
|
public int CompletedDueCount { get; set; }
|
||||||
|
|
|
||||||
|
|
@ -43,6 +43,12 @@ namespace SeaHaven.Services.Implementation
|
||||||
query.DateTo,
|
query.DateTo,
|
||||||
cancellationToken);
|
cancellationToken);
|
||||||
var metrics = DashboardMetrics.Calculate(workOrders, DashboardBusinessTime.Today());
|
var metrics = DashboardMetrics.Calculate(workOrders, DashboardBusinessTime.Today());
|
||||||
|
var unassigned = await _dataService.CountOpenUnassignedAsync(
|
||||||
|
accountId,
|
||||||
|
dispatcherId,
|
||||||
|
query.DateFrom,
|
||||||
|
query.DateTo,
|
||||||
|
cancellationToken);
|
||||||
|
|
||||||
var useMetrics = query.DateFrom.HasValue && query.DateTo.HasValue || dispatcherId is not null;
|
var useMetrics = query.DateFrom.HasValue && query.DateTo.HasValue || dispatcherId is not null;
|
||||||
|
|
||||||
|
|
@ -55,6 +61,7 @@ namespace SeaHaven.Services.Implementation
|
||||||
ScheduledTomorrow = kpis.ScheduledTomorrow,
|
ScheduledTomorrow = kpis.ScheduledTomorrow,
|
||||||
PendingUplifts = kpis.PendingUplifts,
|
PendingUplifts = kpis.PendingUplifts,
|
||||||
AvetaPending = kpis.AvetaPending,
|
AvetaPending = kpis.AvetaPending,
|
||||||
|
Unassigned = unassigned,
|
||||||
Breakdown = metrics.Breakdown,
|
Breakdown = metrics.Breakdown,
|
||||||
DueCount = metrics.DueCount,
|
DueCount = metrics.DueCount,
|
||||||
CompletedDueCount = metrics.CompletedDueCount,
|
CompletedDueCount = metrics.CompletedDueCount,
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue