diff --git a/server/src/handlers/organization_handler.rs b/server/src/handlers/organization_handler.rs index 82006bfbae..96d4f0a7bf 100644 --- a/server/src/handlers/organization_handler.rs +++ b/server/src/handlers/organization_handler.rs @@ -434,8 +434,10 @@ pub async fn remove_user_from_org( } }))] pub struct UpdateAllOrgDatasetConfigsReqPayload { + /// The configuration to provide a filter on what datasets to update. + pub match_configuration: Option, /// The new configuration for all datasets in the organization. Only the specified keys in the configuration object will be changed per dataset such that you can preserve dataset unique values. - pub dataset_config: serde_json::Value, + pub to_configuration: serde_json::Value, } /// Update All Dataset Configurations @@ -469,9 +471,18 @@ pub async fn update_all_org_dataset_configs( return Ok(HttpResponse::Forbidden().finish()); }; - let new_dataset_config = req_payload.dataset_config.clone(); + let req_payload = req_payload.into_inner(); - update_all_org_dataset_configs_query(organization_id, new_dataset_config, pool).await?; + let new_dataset_config = req_payload.to_configuration.clone(); + let match_configuration = req_payload.match_configuration.clone(); + + update_all_org_dataset_configs_query( + organization_id, + new_dataset_config, + match_configuration, + pool, + ) + .await?; Ok(HttpResponse::NoContent().finish()) } diff --git a/server/src/operators/organization_operator.rs b/server/src/operators/organization_operator.rs index 1526479885..fb601d24e0 100644 --- a/server/src/operators/organization_operator.rs +++ b/server/src/operators/organization_operator.rs @@ -987,27 +987,47 @@ pub async fn delete_actual_organization_query( Ok(()) } - pub async fn update_all_org_dataset_configs_query( org_id: uuid::Uuid, new_config: serde_json::Value, + match_config: Option, pool: web::Data, ) -> Result<(), ServiceError> { - let concat_configs_raw_query = sql_query(format!( - "UPDATE datasets SET server_configuration = server_configuration || '{}' WHERE organization_id = '{}';", + let mut concat_configs_raw_query = format!( + "UPDATE datasets SET server_configuration = server_configuration || '{}' WHERE organization_id = '{}'", new_config.to_string().replace('\'', "''"), org_id - )); + ); + + if let Some(match_config) = match_config { + let match_config_query = match_config + .as_object() + .unwrap() + .iter() + .map(|(key, value)| { + format!( + "server_configuration::json->>'{}' = '{}'", + key, + value.as_str().unwrap().replace('\'', "''") + ) + }) + .collect::>() + .join(" AND "); + + concat_configs_raw_query.push_str(&format!(" AND ({})", match_config_query)); + } + + concat_configs_raw_query.push(';'); let mut conn = pool.get().await.map_err(|_e| { ServiceError::InternalServerError("Failed to get postgres connection".to_string()) })?; - concat_configs_raw_query + sql_query(concat_configs_raw_query) .execute(&mut conn) .await .map_err(|e| { log::error!( - "Error updating datasets in update_all_org_dataset_server_configs: {:?}", + "Error updating datasets in update_all_org_dataset_configs: {:?}", e ); ServiceError::BadRequest("Error updating datasets".to_string())