@@ -195,6 +195,8 @@ public async Task ProcessWeatherAlertSourceAsync(Guid sourceId, CancellationToke
195195 var existing = await _weatherAlertRepository . GetByExternalIdAndSourceIdAsync (
196196 alert . ExternalId , source . WeatherAlertSourceId ) ;
197197
198+ TruncateAlertFields ( alert ) ;
199+
198200 if ( existing == null )
199201 {
200202 // New alert
@@ -525,78 +527,31 @@ private static string FormatAlertMessageBody(WeatherAlert alert, Department depa
525527 {
526528 var sb = new System . Text . StringBuilder ( ) ;
527529
528- // Header
529- sb . AppendLine ( $ "=== WEATHER ALERT: { alert . Event ? . ToUpper ( ) } ===") ;
530- sb . AppendLine ( ) ;
530+ sb . AppendLine ( $ "WEATHER ALERT: { alert . Event ? . ToUpper ( ) } ") ;
531+ sb . AppendLine ( $ "Severity: { SeverityNames [ Math . Min ( alert . Severity , 4 ) ] } ") ;
531532
532- // Headline
533- if ( ! string . IsNullOrEmpty ( alert . Headline ) )
533+ if ( alert . ExpiresUtc . HasValue )
534534 {
535- sb . AppendLine ( alert . Headline ) ;
536- sb . AppendLine ( ) ;
535+ if ( department != null )
536+ sb . AppendLine ( $ "Expires: { alert . ExpiresUtc . Value . TimeConverter ( department ) : MM/dd/yyyy h:mm tt} ") ;
537+ else
538+ sb . AppendLine ( $ "Expires: { alert . ExpiresUtc . Value : yyyy-MM-dd HH:mm} UTC") ;
537539 }
538540
539- // Classification grid
540- sb . AppendLine ( "--- Alert Details ---" ) ;
541- sb . AppendLine ( $ "Severity: { SeverityNames [ Math . Min ( alert . Severity , 4 ) ] } ") ;
542- sb . AppendLine ( $ "Urgency: { UrgencyNames [ Math . Min ( alert . Urgency , 4 ) ] } ") ;
543- sb . AppendLine ( $ "Certainty: { CertaintyNames [ Math . Min ( alert . Certainty , 4 ) ] } ") ;
544- sb . AppendLine ( $ "Category: { CategoryNames [ Math . Min ( alert . AlertCategory , 4 ) ] } ") ;
545541 sb . AppendLine ( ) ;
546542
547- // Timing
548- sb . AppendLine ( "--- Timing ---" ) ;
549- if ( department != null )
550- {
551- sb . AppendLine ( $ "Effective: { alert . EffectiveUtc . TimeConverter ( department ) : MM/dd/yyyy h:mm tt} ") ;
552- if ( alert . OnsetUtc . HasValue )
553- sb . AppendLine ( $ "Onset: { alert . OnsetUtc . Value . TimeConverter ( department ) : MM/dd/yyyy h:mm tt} ") ;
554- if ( alert . ExpiresUtc . HasValue )
555- sb . AppendLine ( $ "Expires: { alert . ExpiresUtc . Value . TimeConverter ( department ) : MM/dd/yyyy h:mm tt} ") ;
556- }
557- else
558- {
559- sb . AppendLine ( $ "Effective: { alert . EffectiveUtc : yyyy-MM-dd HH:mm} UTC") ;
560- if ( alert . OnsetUtc . HasValue )
561- sb . AppendLine ( $ "Onset: { alert . OnsetUtc . Value : yyyy-MM-dd HH:mm} UTC") ;
562- if ( alert . ExpiresUtc . HasValue )
563- sb . AppendLine ( $ "Expires: { alert . ExpiresUtc . Value : yyyy-MM-dd HH:mm} UTC") ;
564- }
565- sb . AppendLine ( ) ;
566-
567- // Affected area
568- if ( ! string . IsNullOrEmpty ( alert . AreaDescription ) )
569- {
570- sb . AppendLine ( "--- Affected Area ---" ) ;
571- sb . AppendLine ( alert . AreaDescription ) ;
572- sb . AppendLine ( ) ;
573- }
574-
575- // Description
576- if ( ! string . IsNullOrEmpty ( alert . Description ) )
577- {
578- sb . AppendLine ( "--- Description ---" ) ;
579- sb . AppendLine ( alert . Description ) ;
580- sb . AppendLine ( ) ;
581- }
543+ if ( ! string . IsNullOrEmpty ( alert . Headline ) )
544+ sb . AppendLine ( alert . Headline ) ;
582545
583- // Instructions (critical for responders)
584546 if ( ! string . IsNullOrEmpty ( alert . Instruction ) )
585547 {
586- sb . AppendLine ( "--- INSTRUCTIONS ---" ) ;
587- sb . AppendLine ( alert . Instruction ) ;
588548 sb . AppendLine ( ) ;
549+ sb . AppendLine ( alert . Instruction ) ;
589550 }
590551
591- // Source info
592- if ( ! string . IsNullOrEmpty ( alert . Sender ) )
593- sb . AppendLine ( $ "Source: { alert . Sender } ") ;
594-
595- sb . AppendLine ( $ "Alert ID: { alert . ExternalId } ") ;
596552 sb . AppendLine ( ) ;
597- sb . AppendLine ( "This is an automated weather alert from the Resgrid Weather Alert System ." ) ;
553+ sb . AppendLine ( "View active weather alerts for full details ." ) ;
598554
599- // Respect the 4000 char body limit
600555 var body = sb . ToString ( ) ;
601556 if ( body . Length > 3950 )
602557 body = body . Substring ( 0 , 3947 ) + "..." ;
@@ -619,5 +574,24 @@ private static double CalculateDistanceMiles(double lat1, double lng1, double la
619574 private static double ToRadians ( double degrees ) => degrees * Math . PI / 180 ;
620575
621576 #endregion
577+
578+ private static void TruncateAlertFields ( WeatherAlert alert )
579+ {
580+ alert . ExternalId = Truncate ( alert . ExternalId , 500 ) ;
581+ alert . Sender = Truncate ( alert . Sender , 500 ) ;
582+ alert . Event = Truncate ( alert . Event , 500 ) ;
583+ alert . Headline = Truncate ( alert . Headline , 500 ) ;
584+ alert . AreaDescription = Truncate ( alert . AreaDescription , 500 ) ;
585+ alert . CenterGeoLocation = Truncate ( alert . CenterGeoLocation , 100 ) ;
586+ alert . ReferencesExternalId = Truncate ( alert . ReferencesExternalId , 500 ) ;
587+ }
588+
589+ private static string Truncate ( string value , int maxLength )
590+ {
591+ if ( string . IsNullOrEmpty ( value ) || value . Length <= maxLength )
592+ return value ;
593+
594+ return value . Substring ( 0 , maxLength ) ;
595+ }
622596 }
623597}
0 commit comments