Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions server/src/handlers/organization_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<serde_json::Value>,
/// 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
Expand Down Expand Up @@ -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())
}
Expand Down
32 changes: 26 additions & 6 deletions server/src/operators/organization_operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<serde_json::Value>,
pool: web::Data<Pool>,
) -> 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::<Vec<String>>()
.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())
Expand Down