Skip to content

Commit b6d0965

Browse files
committed
refactor: modify dataset configuration update to include filtering options
1 parent 3bf8896 commit b6d0965

File tree

2 files changed

+30
-8
lines changed

2 files changed

+30
-8
lines changed

server/src/handlers/organization_handler.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -434,8 +434,10 @@ pub async fn remove_user_from_org(
434434
}
435435
}))]
436436
pub struct UpdateAllOrgDatasetConfigsReqPayload {
437+
/// The configuration to provide a filter on what datasets to update.
438+
pub from_configuration: Option<serde_json::Value>,
437439
/// 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.
438-
pub dataset_config: serde_json::Value,
440+
pub to_configuration: serde_json::Value,
439441
}
440442

441443
/// Update All Dataset Configurations
@@ -469,9 +471,12 @@ pub async fn update_all_org_dataset_configs(
469471
return Ok(HttpResponse::Forbidden().finish());
470472
};
471473

472-
let new_dataset_config = req_payload.dataset_config.clone();
474+
let req_payload = req_payload.into_inner();
473475

474-
update_all_org_dataset_configs_query(organization_id, new_dataset_config, pool).await?;
476+
let new_dataset_config = req_payload.to_configuration.clone();
477+
let from_configuration = req_payload.from_configuration.clone();
478+
479+
update_all_org_dataset_configs_query(organization_id, new_dataset_config, from_configuration, pool).await?;
475480

476481
Ok(HttpResponse::NoContent().finish())
477482
}

server/src/operators/organization_operator.rs

+22-5
Original file line numberDiff line numberDiff line change
@@ -985,22 +985,39 @@ pub async fn delete_actual_organization_query(
985985

986986
Ok(())
987987
}
988-
989988
pub async fn update_all_org_dataset_configs_query(
990989
org_id: uuid::Uuid,
991990
new_config: serde_json::Value,
991+
from_config: Option<serde_json::Value>,
992992
pool: web::Data<Pool>,
993993
) -> Result<(), ServiceError> {
994-
let concat_configs_raw_query = sql_query(format!(
995-
"UPDATE datasets SET server_configuration = server_configuration || '{}' WHERE organization_id = '{}';",
994+
let mut concat_configs_raw_query = format!(
995+
"UPDATE datasets SET server_configuration = server_configuration || '{}' WHERE organization_id = '{}'",
996996
new_config.to_string().replace('\'', "''"), org_id
997-
));
997+
);
998+
999+
if let Some(from_config) = from_config {
1000+
let from_config_query = from_config.as_object().unwrap().iter().map(|(key, value)| {
1001+
format!(
1002+
"server_configuration::jsonb->>'{}' = '{}'",
1003+
key,
1004+
value.as_str().unwrap().replace('\'', "''")
1005+
)
1006+
}).collect::<Vec<String>>().join(" AND ");
1007+
1008+
concat_configs_raw_query.push_str(&format!(
1009+
" AND ({})",
1010+
from_config_query
1011+
));
1012+
}
1013+
1014+
concat_configs_raw_query.push(';');
9981015

9991016
let mut conn = pool.get().await.map_err(|_e| {
10001017
ServiceError::InternalServerError("Failed to get postgres connection".to_string())
10011018
})?;
10021019

1003-
concat_configs_raw_query
1020+
sql_query(concat_configs_raw_query)
10041021
.execute(&mut conn)
10051022
.await
10061023
.map_err(|e| {

0 commit comments

Comments
 (0)