Trial status endpoint for the workspace banner
C# · ASP.NET Core · beginner · modification
Adds the trial status endpoint the web app's trial banner reads. `TrialStatus.For` (new, `Workspaces/TrialStatus.cs`) turns a workspace's `TrialEndsOn` and today's date into `active`, `daysLeft` and `endsOn`; a workspace with no trial gets an inactive status with no end date. `WorkspaceEndpoints.cs` maps `GET /trial` in the existing workspace group (so it inherits sign-in and the membership filter), reads only `TrialEndsOn`, takes today's UTC date from the injected `TimeProvider` and returns the status, or 404 if the workspace row is not found. `TrialStatusTests.cs` adds xUnit tests for `TrialStatus.For`.
ASP.NET Core 10 minimal API on .NET 10 (C# 14, nullable reference types enabled, implicit usings), EF Core 10 with Npgsql, xUnit for tests. Existing and unchanged: `Program.cs` registers `AppDbContext`, `TimeProvider.System` as a singleton and the auth scheme, and calls `app.MapWorkspaceEndpoints()`; `AppDbContext.Workspaces` is the `Workspace` entity (`int Id`, `string Name`, `string Plan`, `DateOnly? TrialEndsOn` mapped to a Postgres `date` column, set by the signup flow and cleared by the billing webhook when the workspace pays, both unchanged); `WorkspaceSummary` is an existing record; `WorkspaceMemberFilter` answers itself unless the signed-in user is a member of the workspace in the path: 401 when signed out, 404 when no workspace has the path's id, 403 when the user is not a member. JSON uses the ASP.NET Core web defaults (camelCase names, `DateOnly` as `"yyyy-MM-dd"`). The banner (web app) ships separately and only reads this endpoint.
Requirements
- New endpoint `GET /api/workspaces/{workspaceId}/trial` returns `{ "active", "daysLeft", "endsOn" }` so the web app can show a trial banner ("3 days left in your trial" or "Your trial has ended").
- A workspace's trial runs through its `TrialEndsOn` date, inclusive: the trial is active on every day up to and including `TrialEndsOn`, and it ends when that day ends. `active` is `true` on those days and `false` from the day after.
- `daysLeft` counts the trial days left including today: 1 on the last day, 2 on the day before, and so on; it is 0 when the trial is not active. `endsOn` echoes `TrialEndsOn`.
- A workspace with no trial (`TrialEndsOn` is `null`, a paid workspace) answers `active: false`, `daysLeft: 0`, `endsOn: null`.
- "Today" is the current UTC calendar date, read from the injected `TimeProvider` (trial dates are UTC calendar dates). Unchanged: only signed-in members of the workspace in the path reach the group's endpoints (the membership filter answers 401 when signed out, 404 when the workspace does not exist and 403 when the user is not a member); like `GET /api/workspaces/{workspaceId}`, the endpoint answers 404 if the workspace row is not found.
Files touched
- src/Acme.Api/Workspaces/WorkspaceEndpoints.cs
- src/Acme.Api/Workspaces/TrialStatus.cs
- tests/Acme.Api.Tests/TrialStatusTests.cs