176 lines
5.4 KiB
C#
176 lines
5.4 KiB
C#
using System.Text.Json;
|
|
using maintenance.Data;
|
|
using maintenance.Data.Entities;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace maintenance.Services.Pages;
|
|
|
|
public sealed class ManagedPageService : IManagedPageService
|
|
{
|
|
private readonly AppDbContext _dbContext;
|
|
private static readonly JsonSerializerOptions JsonOptions = new()
|
|
{
|
|
PropertyNameCaseInsensitive = true
|
|
};
|
|
|
|
public ManagedPageService(AppDbContext dbContext)
|
|
{
|
|
_dbContext = dbContext;
|
|
}
|
|
|
|
public async Task<ResolvedPage> ResolveAsync(
|
|
string pageKind,
|
|
string? requestedPageKey = null,
|
|
string? requestedStub = null,
|
|
DateTime? nowUtc = null,
|
|
CancellationToken cancellationToken = default)
|
|
{
|
|
var normalizedKind = NormalizeKind(pageKind);
|
|
var normalizedStub = NormalizeStub(requestedStub);
|
|
var utcNow = nowUtc ?? DateTime.UtcNow;
|
|
|
|
var pages = await _dbContext.ManagedPages
|
|
.AsNoTracking()
|
|
.Where(p => p.PageKind == normalizedKind && p.IsEnabled)
|
|
.OrderByDescending(p => p.OverrideWindow)
|
|
.ThenByDescending(p => p.Priority)
|
|
.ThenBy(p => p.Key)
|
|
.ToListAsync(cancellationToken);
|
|
|
|
ManagedPageEntity? selected = null;
|
|
|
|
if (!string.IsNullOrWhiteSpace(normalizedStub))
|
|
{
|
|
selected = pages.FirstOrDefault(p =>
|
|
p.NormalizedStub == normalizedStub &&
|
|
IsWindowActive(p, utcNow));
|
|
}
|
|
|
|
if (!string.IsNullOrWhiteSpace(requestedPageKey))
|
|
{
|
|
selected ??= pages.FirstOrDefault(p =>
|
|
p.Key.Equals(requestedPageKey, StringComparison.OrdinalIgnoreCase) &&
|
|
IsWindowActive(p, utcNow));
|
|
}
|
|
|
|
if (selected is null)
|
|
{
|
|
var activeNonFallback = pages
|
|
.Where(p => !p.IsDefaultFallback && IsWindowActive(p, utcNow))
|
|
.ToList();
|
|
|
|
// When no stub is provided and multiple pages are active in the same window,
|
|
// the match is ambiguous — fall through to the default fallback instead of
|
|
// picking randomly. A stub must be used to target a specific page.
|
|
bool isAmbiguous = activeNonFallback.Count > 1 && string.IsNullOrWhiteSpace(normalizedStub);
|
|
|
|
if (!isAmbiguous)
|
|
{
|
|
selected = activeNonFallback.FirstOrDefault(p => p.OverrideWindow)
|
|
?? activeNonFallback.FirstOrDefault();
|
|
}
|
|
}
|
|
|
|
selected ??= pages.FirstOrDefault(p => p.IsDefaultFallback);
|
|
|
|
if (selected is null)
|
|
{
|
|
return new ResolvedPage
|
|
{
|
|
PageKind = normalizedKind,
|
|
PageKey = normalizedKind == "maintenance" ? "generic-maintenance" : "generic-error",
|
|
IsDefaultFallback = true,
|
|
Content = DefaultContent(normalizedKind)
|
|
};
|
|
}
|
|
|
|
return new ResolvedPage
|
|
{
|
|
PageKind = normalizedKind,
|
|
PageKey = selected.Key,
|
|
IsDefaultFallback = selected.IsDefaultFallback,
|
|
Content = DeserializeContent(selected.ContentJson, normalizedKind)
|
|
};
|
|
}
|
|
|
|
private static string NormalizeKind(string pageKind)
|
|
{
|
|
return pageKind.Equals("error", StringComparison.OrdinalIgnoreCase) ? "error" : "maintenance";
|
|
}
|
|
|
|
private static string? NormalizeStub(string? stub)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(stub))
|
|
{
|
|
return null;
|
|
}
|
|
|
|
return stub.Trim().ToLowerInvariant();
|
|
}
|
|
|
|
private static bool IsWindowActive(ManagedPageEntity page, DateTime utcNow)
|
|
{
|
|
// Initial behavior:
|
|
// - If no window set => active.
|
|
// - If one-time window set => active only inside start/end.
|
|
// - RecurrenceRule parsing is future enhancement; currently treated as active when no one-time bounds fail.
|
|
if (page.StartUtc is null && page.EndUtc is null)
|
|
{
|
|
return true;
|
|
}
|
|
|
|
if (page.StartUtc.HasValue && utcNow < page.StartUtc.Value)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
if (page.EndUtc.HasValue && utcNow > page.EndUtc.Value)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
private static PageContentModel DeserializeContent(string contentJson, string kind)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(contentJson))
|
|
{
|
|
return DefaultContent(kind);
|
|
}
|
|
|
|
try
|
|
{
|
|
return JsonSerializer.Deserialize<PageContentModel>(contentJson, JsonOptions)
|
|
?? DefaultContent(kind);
|
|
}
|
|
catch
|
|
{
|
|
return DefaultContent(kind);
|
|
}
|
|
}
|
|
|
|
private static PageContentModel DefaultContent(string kind)
|
|
{
|
|
if (kind == "maintenance")
|
|
{
|
|
return new PageContentModel
|
|
{
|
|
ErrorName = "Application Unavailable",
|
|
Description =
|
|
"MAX applications may be down for scheduled maintenance 2AM-8AM Sunday morning. If this appears at another time, contact support.",
|
|
ErrorCode = "eau",
|
|
ShowInstructions = false
|
|
};
|
|
}
|
|
|
|
return new PageContentModel
|
|
{
|
|
ErrorName = "System Error",
|
|
Description = "A system error has occurred. Please contact MAX Support.",
|
|
ErrorCode = "eag",
|
|
ShowInstructions = false
|
|
};
|
|
}
|
|
}
|