300 lines
9.9 KiB
C#
300 lines
9.9 KiB
C#
using System.ComponentModel.DataAnnotations;
|
|
using System.Text.Json;
|
|
using maintenance.Data;
|
|
using maintenance.Services.Pages;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.AspNetCore.Mvc.RazorPages;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace maintenance.Pages.Admin.Templates;
|
|
|
|
[Authorize(Policy = "AdminOnly")]
|
|
public sealed class EditModel : PageModel
|
|
{
|
|
private readonly AppDbContext _dbContext;
|
|
|
|
public EditModel(AppDbContext dbContext)
|
|
{
|
|
_dbContext = dbContext;
|
|
}
|
|
|
|
[BindProperty]
|
|
public InputModel Input { get; set; } = new();
|
|
|
|
public string PreviewUrl { get; private set; } = "/maintenance";
|
|
|
|
public sealed class InputModel
|
|
{
|
|
public Guid Id { get; set; }
|
|
|
|
public string Key { get; set; } = string.Empty;
|
|
|
|
[StringLength(100)]
|
|
[RegularExpression("^[a-z0-9-]*$")]
|
|
[Display(Name = "Stub (optional)")]
|
|
public string? Stub { get; set; }
|
|
|
|
[Required]
|
|
[StringLength(200)]
|
|
public string DisplayName { get; set; } = string.Empty;
|
|
|
|
[Required]
|
|
[RegularExpression("^(maintenance|error)$")]
|
|
public string PageKind { get; set; } = "maintenance";
|
|
|
|
public bool IsDefaultFallback { get; set; }
|
|
public bool IsEnabled { get; set; }
|
|
public bool OverrideWindow { get; set; }
|
|
public int Priority { get; set; }
|
|
[Display(Name = "Start ET")]
|
|
public DateTime? StartUtc { get; set; }
|
|
[Display(Name = "End ET")]
|
|
public DateTime? EndUtc { get; set; }
|
|
public string? RecurrenceRule { get; set; }
|
|
|
|
[Required]
|
|
[StringLength(200)]
|
|
public string ErrorName { get; set; } = string.Empty;
|
|
public string? ShortDescription { get; set; }
|
|
[Required]
|
|
public string Description { get; set; } = string.Empty;
|
|
public string? InstructionsHeader { get; set; }
|
|
public string? InstructionSteps { get; set; }
|
|
public bool ShowInstructions { get; set; } = true;
|
|
public bool ShowSupport { get; set; } = true;
|
|
[Required]
|
|
public string SupportEmail { get; set; } = "maxsupport@max.gov";
|
|
[Required]
|
|
public string SupportPhone { get; set; } = "202-395-6860";
|
|
[Required]
|
|
public string SupportHours { get; set; } = "8:00AM - 8:00PM Weekdays, 9:00AM - 6:00PM Weekends (ET)";
|
|
[Required]
|
|
public string ErrorCode { get; set; } = "eag";
|
|
}
|
|
|
|
public async Task<IActionResult> OnGetAsync(Guid id)
|
|
{
|
|
var page = await _dbContext.ManagedPages
|
|
.AsNoTracking()
|
|
.FirstOrDefaultAsync(t => t.Id == id, HttpContext.RequestAborted);
|
|
|
|
if (page is null)
|
|
{
|
|
return NotFound();
|
|
}
|
|
|
|
var content = Deserialize(page.ContentJson);
|
|
|
|
Input = new InputModel
|
|
{
|
|
Id = page.Id,
|
|
Key = page.Key,
|
|
Stub = page.Stub,
|
|
DisplayName = page.DisplayName,
|
|
PageKind = page.PageKind,
|
|
IsDefaultFallback = page.IsDefaultFallback,
|
|
IsEnabled = page.IsEnabled,
|
|
OverrideWindow = page.OverrideWindow,
|
|
Priority = page.Priority,
|
|
StartUtc = ConvertUtcToEasternLocal(page.StartUtc),
|
|
EndUtc = ConvertUtcToEasternLocal(page.EndUtc),
|
|
RecurrenceRule = page.RecurrenceRule,
|
|
ErrorName = content.ErrorName,
|
|
ShortDescription = content.ShortDescription,
|
|
Description = content.Description,
|
|
InstructionsHeader = content.InstructionsHeader,
|
|
InstructionSteps = content.InstructionSteps.Count == 0 ? null : string.Join('|', content.InstructionSteps),
|
|
ShowInstructions = content.ShowInstructions,
|
|
ShowSupport = content.ShowSupport,
|
|
SupportEmail = content.SupportEmail,
|
|
SupportPhone = content.SupportPhone,
|
|
SupportHours = content.SupportHours,
|
|
ErrorCode = content.ErrorCode
|
|
};
|
|
|
|
PreviewUrl = BuildPreviewUrl(page.PageKind, page.Key, page.Stub);
|
|
return Page();
|
|
}
|
|
|
|
public async Task<IActionResult> OnPostAsync()
|
|
{
|
|
if (!ModelState.IsValid)
|
|
{
|
|
PreviewUrl = BuildPreviewUrl(Input.PageKind, Input.Key, Input.Stub);
|
|
return Page();
|
|
}
|
|
|
|
var page = await _dbContext.ManagedPages
|
|
.FirstOrDefaultAsync(t => t.Id == Input.Id, HttpContext.RequestAborted);
|
|
|
|
if (page is null)
|
|
{
|
|
return NotFound();
|
|
}
|
|
|
|
page.DisplayName = Input.DisplayName.Trim();
|
|
page.PageKind = Input.PageKind.Trim().ToLowerInvariant();
|
|
page.Stub = NormalizeStub(Input.Stub);
|
|
page.NormalizedStub = page.Stub;
|
|
page.IsDefaultFallback = Input.IsDefaultFallback;
|
|
page.IsEnabled = Input.IsEnabled;
|
|
page.OverrideWindow = Input.OverrideWindow;
|
|
page.Priority = Input.Priority;
|
|
if (!string.IsNullOrWhiteSpace(page.NormalizedStub))
|
|
{
|
|
var duplicateStubExists = await _dbContext.ManagedPages
|
|
.AnyAsync(t =>
|
|
t.Id != page.Id &&
|
|
t.PageKind == page.PageKind &&
|
|
t.NormalizedStub == page.NormalizedStub,
|
|
HttpContext.RequestAborted);
|
|
|
|
if (duplicateStubExists)
|
|
{
|
|
ModelState.AddModelError("Input.Stub", "A page with this stub already exists for this page kind.");
|
|
PreviewUrl = BuildPreviewUrl(Input.PageKind, Input.Key, Input.Stub);
|
|
return Page();
|
|
}
|
|
}
|
|
|
|
var hasInvalidEasternTime = false;
|
|
if (!TryConvertEasternLocalToUtc(Input.StartUtc, out var startUtc))
|
|
{
|
|
ModelState.AddModelError("Input.StartUtc", "The selected Start ET is invalid due to daylight saving time.");
|
|
hasInvalidEasternTime = true;
|
|
}
|
|
|
|
if (!TryConvertEasternLocalToUtc(Input.EndUtc, out var endUtc))
|
|
{
|
|
ModelState.AddModelError("Input.EndUtc", "The selected End ET is invalid due to daylight saving time.");
|
|
hasInvalidEasternTime = true;
|
|
}
|
|
|
|
if (hasInvalidEasternTime)
|
|
{
|
|
PreviewUrl = BuildPreviewUrl(Input.PageKind, Input.Key, Input.Stub);
|
|
return Page();
|
|
}
|
|
|
|
page.StartUtc = startUtc;
|
|
page.EndUtc = endUtc;
|
|
page.RecurrenceRule = string.IsNullOrWhiteSpace(Input.RecurrenceRule) ? null : Input.RecurrenceRule.Trim();
|
|
page.ContentJson = JsonSerializer.Serialize(new PageContentModel
|
|
{
|
|
ErrorName = Input.ErrorName.Trim(),
|
|
ShortDescription = string.IsNullOrWhiteSpace(Input.ShortDescription) ? null : Input.ShortDescription.Trim(),
|
|
Description = Input.Description.Trim(),
|
|
InstructionsHeader = string.IsNullOrWhiteSpace(Input.InstructionsHeader) ? null : Input.InstructionsHeader.Trim(),
|
|
InstructionSteps = ParseSteps(Input.InstructionSteps),
|
|
ShowInstructions = Input.ShowInstructions,
|
|
ShowSupport = Input.ShowSupport,
|
|
SupportEmail = Input.SupportEmail.Trim(),
|
|
SupportPhone = Input.SupportPhone.Trim(),
|
|
SupportHours = Input.SupportHours.Trim(),
|
|
ErrorCode = Input.ErrorCode.Trim()
|
|
});
|
|
page.LastUpdatedAtUtc = DateTime.UtcNow;
|
|
page.LastUpdatedBy = User.Identity?.Name ?? "unknown";
|
|
|
|
await _dbContext.SaveChangesAsync(HttpContext.RequestAborted);
|
|
|
|
return RedirectToPage("./Index");
|
|
}
|
|
|
|
private static PageContentModel Deserialize(string json)
|
|
{
|
|
try
|
|
{
|
|
return JsonSerializer.Deserialize<PageContentModel>(json) ?? new PageContentModel();
|
|
}
|
|
catch
|
|
{
|
|
return new PageContentModel();
|
|
}
|
|
}
|
|
|
|
private static List<string> ParseSteps(string? steps)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(steps))
|
|
{
|
|
return [];
|
|
}
|
|
|
|
return steps.Split('|', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries)
|
|
.Where(x => !string.IsNullOrWhiteSpace(x))
|
|
.ToList();
|
|
}
|
|
|
|
private static string BuildPreviewUrl(string kind, string key, string? stub = null)
|
|
{
|
|
if (!string.IsNullOrWhiteSpace(stub))
|
|
{
|
|
return $"/{kind}?stub={Uri.EscapeDataString(stub.Trim())}";
|
|
}
|
|
|
|
return $"/{kind}?page={Uri.EscapeDataString(key)}";
|
|
}
|
|
|
|
private static string? NormalizeStub(string? stub)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(stub))
|
|
{
|
|
return null;
|
|
}
|
|
|
|
return stub.Trim().ToLowerInvariant();
|
|
}
|
|
|
|
private static bool TryConvertEasternLocalToUtc(DateTime? easternLocal, out DateTime? utc)
|
|
{
|
|
utc = null;
|
|
|
|
if (!easternLocal.HasValue)
|
|
{
|
|
return true;
|
|
}
|
|
|
|
var localValue = DateTime.SpecifyKind(easternLocal.Value, DateTimeKind.Unspecified);
|
|
var easternTimeZone = ResolveEasternTimeZone();
|
|
|
|
if (easternTimeZone.IsInvalidTime(localValue))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
utc = TimeZoneInfo.ConvertTimeToUtc(localValue, easternTimeZone);
|
|
return true;
|
|
}
|
|
|
|
private static DateTime? ConvertUtcToEasternLocal(DateTime? utc)
|
|
{
|
|
if (!utc.HasValue)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
var utcValue = utc.Value.Kind switch
|
|
{
|
|
DateTimeKind.Utc => utc.Value,
|
|
DateTimeKind.Local => utc.Value.ToUniversalTime(),
|
|
_ => DateTime.SpecifyKind(utc.Value, DateTimeKind.Utc)
|
|
};
|
|
|
|
var eastern = TimeZoneInfo.ConvertTimeFromUtc(utcValue, ResolveEasternTimeZone());
|
|
return DateTime.SpecifyKind(eastern, DateTimeKind.Unspecified);
|
|
}
|
|
|
|
private static TimeZoneInfo ResolveEasternTimeZone()
|
|
{
|
|
try
|
|
{
|
|
return TimeZoneInfo.FindSystemTimeZoneById("America/New_York");
|
|
}
|
|
catch (TimeZoneNotFoundException)
|
|
{
|
|
return TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");
|
|
}
|
|
}
|
|
}
|