using Api.SeaHavenIndustries.Options; using Microsoft.Extensions.Options; namespace Api.SeaHavenIndustries.Middleware { /// Adds Sunset/Deprecation headers to legacy WorkOrder REST routes. public class LegacyDeprecationMiddleware { private static readonly HashSet LegacyPathSegments = new(StringComparer.OrdinalIgnoreCase) { "GetWorkOrderList", "AddWorkorder", "EditWorkorder", "ChangeStatus", "ChangeAssignment", "DeleteWorkorder", "GetWorkorderById" }; private readonly RequestDelegate _next; private readonly LegacyEndpointOptions _options; public LegacyDeprecationMiddleware(RequestDelegate next, IOptions options) { _next = next; _options = options.Value; } public async Task InvokeAsync(HttpContext context) { if (_options.DeprecationEnabled) { var path = context.Request.Path.Value ?? ""; var isLegacyWo = (path.Contains("/WorkOrder", StringComparison.OrdinalIgnoreCase) || path.Contains("/workorders", StringComparison.OrdinalIgnoreCase)) && LegacyPathSegments.Any(seg => path.Contains(seg, StringComparison.OrdinalIgnoreCase)); if (isLegacyWo) { context.Response.OnStarting(() => { context.Response.Headers["Deprecation"] = "true"; if (!string.IsNullOrWhiteSpace(_options.SunsetDate) && DateTimeOffset.TryParse(_options.SunsetDate, out var sunset)) { context.Response.Headers["Sunset"] = sunset.UtcDateTime.ToString("r"); } context.Response.Headers["Link"] = "; rel=\"successor-version\""; return Task.CompletedTask; }); } } await _next(context); } } }