using SeaHaven.Services.DTOs; namespace SeaHaven.Services.Helpers { public static class NotificationFeedOrdering { /// /// Drops empty sections, orders items by severity then recency, and orders sections by /// their highest severity with the fixed reason order breaking ties. /// public static IReadOnlyList Order( IEnumerable<(string Reason, int Count, IReadOnlyList Items)> sections) { return sections .Where(section => section.Items.Count > 0) .Select(section => { var items = section.Items .OrderByDescending(item => NotificationSeverities.Rank(item.Severity)) .ThenByDescending(item => item.TriggeredAt) .ThenBy(item => item.Id, StringComparer.Ordinal) .ToList(); return new NotificationSectionDto { Reason = section.Reason, Label = NotificationReasons.Labels[section.Reason], Severity = items[0].Severity, Count = section.Count, Items = items }; }) .OrderByDescending(section => NotificationSeverities.Rank(section.Severity)) .ThenBy(section => ReasonIndex(section.Reason)) .ToList(); } private static int ReasonIndex(string reason) { for (var index = 0; index < NotificationReasons.Order.Count; index++) { if (NotificationReasons.Order[index] == reason) return index; } return NotificationReasons.Order.Count; } } public static class NotificationBusinessTime { /// Business-calendar date of in the dashboard's time zone. public static DateOnly Today(DateTime utcNow) { var timezone = TimeZoneInfo.FindSystemTimeZoneById(DashboardBusinessTime.TimeZoneId); return DateOnly.FromDateTime(TimeZoneInfo.ConvertTimeFromUtc(utcNow, timezone)); } } }