@@ -535,10 +535,10 @@ pub struct AlertRequest {
535535
536536impl AlertRequest {
537537 pub async fn into ( self ) -> Result < AlertConfig , AlertError > {
538- // let mut targets = Vec::new();
539- // for id in self.targets {
540- // targets.push( TARGETS.get_target_by_id(id).await?) ;
541- // }
538+ // Validate that all target IDs exist
539+ for id in & self . targets {
540+ TARGETS . get_target_by_id ( id) . await ?;
541+ }
542542 let config = AlertConfig {
543543 version : AlertVerison :: from ( CURRENT_ALERTS_VERSION ) ,
544544 id : Ulid :: new ( ) ,
@@ -575,10 +575,10 @@ pub struct AlertConfig {
575575
576576impl AlertConfig {
577577 pub async fn modify ( & mut self , alert : AlertRequest ) -> Result < ( ) , AlertError > {
578- // let mut targets = Vec::new();
579- // for id in alert.targets {
580- // targets.push (id);
581- // }
578+ // Validate that all target IDs exist
579+ for id in & alert. targets {
580+ TARGETS . get_target_by_id ( id) . await ? ;
581+ }
582582 self . title = alert. title ;
583583 self . stream = alert. stream ;
584584 self . alert_type = alert. alert_type ;
@@ -853,6 +853,8 @@ pub enum AlertError {
853853 FromStrError ( #[ from] FromStrError ) ,
854854 #[ error( "Invalid Target ID- {0}" ) ]
855855 InvalidTargetID ( String ) ,
856+ #[ error( "Target already exists" ) ]
857+ DuplicateTargetConfig ,
856858}
857859
858860impl actix_web:: ResponseError for AlertError {
@@ -871,6 +873,7 @@ impl actix_web::ResponseError for AlertError {
871873 Self :: InvalidAlertModifyRequest => StatusCode :: BAD_REQUEST ,
872874 Self :: FromStrError ( _) => StatusCode :: BAD_REQUEST ,
873875 Self :: InvalidTargetID ( _) => StatusCode :: BAD_REQUEST ,
876+ Self :: DuplicateTargetConfig => StatusCode :: BAD_REQUEST ,
874877 }
875878 }
876879
0 commit comments