33using System . Linq ;
44using System . Threading ;
55using System . Threading . Tasks ;
6+ using Newtonsoft . Json ;
67using Resgrid . Framework ;
78using Resgrid . Model ;
89using Resgrid . Model . Helpers ;
@@ -319,15 +320,27 @@ public async Task SendPendingNotificationsAsync(CancellationToken ct = default)
319320 continue ;
320321 }
321322
322- // Get the auto-message severity threshold setting
323- var thresholdSetting = await _departmentSettingsRepository . GetDepartmentSettingByIdTypeAsync (
324- departmentId , DepartmentSettingTypes . WeatherAlertAutoMessageSeverity ) ;
323+ // Load per-severity schedule (if configured)
324+ List < AutoMessageSeveritySchedule > schedule = null ;
325+ var scheduleSetting = await _departmentSettingsRepository . GetDepartmentSettingByIdTypeAsync (
326+ departmentId , DepartmentSettingTypes . WeatherAlertAutoMessageSchedule ) ;
327+ if ( scheduleSetting != null && ! string . IsNullOrWhiteSpace ( scheduleSetting . Setting ) )
328+ {
329+ try { schedule = JsonConvert . DeserializeObject < List < AutoMessageSeveritySchedule > > ( scheduleSetting . Setting ) ; }
330+ catch { }
331+ }
325332
326- int threshold = ( int ) WeatherAlertSeverity . Severe ; // Default: Severe=1
327- if ( thresholdSetting != null && int . TryParse ( thresholdSetting . Setting , out var parsed ) )
328- threshold = parsed ;
333+ // Fall back to legacy threshold if no schedule configured
334+ int legacyThreshold = ( int ) WeatherAlertSeverity . Severe ;
335+ if ( schedule == null || schedule . Count == 0 )
336+ {
337+ var thresholdSetting = await _departmentSettingsRepository . GetDepartmentSettingByIdTypeAsync (
338+ departmentId , DepartmentSettingTypes . WeatherAlertAutoMessageSeverity ) ;
339+ if ( thresholdSetting != null && int . TryParse ( thresholdSetting . Setting , out var parsed ) )
340+ legacyThreshold = parsed ;
341+ }
329342
330- // Load department for sender info
343+ // Load department for sender info and time conversion
331344 Department department = null ;
332345 try
333346 {
@@ -340,9 +353,8 @@ public async Task SendPendingNotificationsAsync(CancellationToken ct = default)
340353
341354 foreach ( var alert in group )
342355 {
343- // Only send notifications for alerts meeting severity threshold
344- // Lower enum value = higher severity (Extreme=0, Severe=1, etc.)
345- if ( alert . Severity <= threshold )
356+ bool shouldSend = ShouldSendAutoMessage ( alert . Severity , schedule , legacyThreshold , department ) ;
357+ if ( shouldSend )
346358 {
347359 try
348360 {
@@ -594,5 +606,50 @@ private static string Truncate(string value, int maxLength)
594606
595607 return value . Substring ( 0 , maxLength ) ;
596608 }
609+
610+ private static bool ShouldSendAutoMessage ( int severity , List < AutoMessageSeveritySchedule > schedule , int legacyThreshold , Department department )
611+ {
612+ if ( schedule != null && schedule . Count > 0 )
613+ {
614+ var entry = schedule . FirstOrDefault ( s => s . Severity == severity ) ;
615+
616+ // Severity not in schedule — don't send
617+ if ( entry == null || ! entry . Enabled )
618+ return false ;
619+
620+ // Check time window (StartHour == 0 && EndHour == 0 means 24h/always)
621+ if ( entry . StartHour == 0 && entry . EndHour == 0 )
622+ return true ;
623+
624+ // Get department local time
625+ var now = DateTime . UtcNow ;
626+ if ( department != null )
627+ now = now . TimeConverter ( department ) ;
628+
629+ int currentHour = now . Hour ;
630+
631+ if ( entry . StartHour <= entry . EndHour )
632+ {
633+ // Same-day window: e.g. 6-18
634+ return currentHour >= entry . StartHour && currentHour < entry . EndHour ;
635+ }
636+ else
637+ {
638+ // Overnight window: e.g. 18-6 (6pm to 6am)
639+ return currentHour >= entry . StartHour || currentHour < entry . EndHour ;
640+ }
641+ }
642+
643+ // Legacy: simple severity threshold
644+ return severity <= legacyThreshold ;
645+ }
646+
647+ private class AutoMessageSeveritySchedule
648+ {
649+ public int Severity { get ; set ; }
650+ public bool Enabled { get ; set ; }
651+ public int StartHour { get ; set ; }
652+ public int EndHour { get ; set ; }
653+ }
597654 }
598655}
0 commit comments