revamp
This commit is contained in:
@@ -0,0 +1,10 @@
|
||||
namespace maintenance.Services.Pages;
|
||||
|
||||
public interface IManagedPageService
|
||||
{
|
||||
Task<ResolvedPage> ResolveAsync(
|
||||
string pageKind,
|
||||
string? requestedPageKey = null,
|
||||
DateTime? nowUtc = null,
|
||||
CancellationToken cancellationToken = default);
|
||||
}
|
||||
@@ -0,0 +1,140 @@
|
||||
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,
|
||||
DateTime? nowUtc = null,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
var normalizedKind = NormalizeKind(pageKind);
|
||||
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(requestedPageKey))
|
||||
{
|
||||
selected = pages.FirstOrDefault(p =>
|
||||
p.Key.Equals(requestedPageKey, StringComparison.OrdinalIgnoreCase) &&
|
||||
IsWindowActive(p, utcNow));
|
||||
}
|
||||
|
||||
selected ??= pages.FirstOrDefault(p => p.OverrideWindow && IsWindowActive(p, utcNow));
|
||||
selected ??= pages.FirstOrDefault(p => !p.IsDefaultFallback && IsWindowActive(p, utcNow));
|
||||
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 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
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
namespace maintenance.Services.Pages;
|
||||
|
||||
public sealed class PageContentModel
|
||||
{
|
||||
public string ErrorName { get; set; } = "System Error";
|
||||
public string? ShortDescription { get; set; }
|
||||
public string Description { get; set; } = "A system error has occurred. Please contact support.";
|
||||
public string? InstructionsHeader { get; set; }
|
||||
public List<string> InstructionSteps { get; set; } = [];
|
||||
public bool ShowInstructions { get; set; } = true;
|
||||
public bool ShowSupport { get; set; } = true;
|
||||
public string SupportEmail { get; set; } = "maxsupport@max.gov";
|
||||
public string SupportPhone { get; set; } = "202-395-6860";
|
||||
public string SupportHours { get; set; } =
|
||||
"8:00AM - 8:00PM Weekdays, 9:00AM - 6:00PM Weekends (ET)";
|
||||
public string ErrorCode { get; set; } = "eag";
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
namespace maintenance.Services.Pages;
|
||||
|
||||
public sealed class ResolvedPage
|
||||
{
|
||||
public string PageKey { get; init; } = string.Empty;
|
||||
public string PageKind { get; init; } = string.Empty;
|
||||
public bool IsDefaultFallback { get; init; }
|
||||
public PageContentModel Content { get; init; } = new();
|
||||
}
|
||||
Reference in New Issue
Block a user