post table data validating now steel
This commit is contained in:
@@ -5,8 +5,11 @@ use sqlx::postgres::PgArguments;
|
||||
use chrono::{DateTime, Utc};
|
||||
use common::proto::multieko2::tables_data::{PostTableDataRequest, PostTableDataResponse};
|
||||
use std::collections::HashMap;
|
||||
use std::sync::Arc;
|
||||
use tokio::runtime::Handle;
|
||||
|
||||
use crate::steel::server::execution::{self, Value};
|
||||
use crate::steel::server::functions::SteelContext;
|
||||
|
||||
pub async fn post_table_data(
|
||||
db_pool: &PgPool,
|
||||
@@ -26,7 +29,7 @@ pub async fn post_table_data(
|
||||
}
|
||||
|
||||
// Add trimmed non-empty values to data map
|
||||
if !trimmed.is_empty(){
|
||||
if !trimmed.is_empty() {
|
||||
data.insert(key, trimmed);
|
||||
}
|
||||
}
|
||||
@@ -72,7 +75,7 @@ pub async fn post_table_data(
|
||||
|
||||
// Get required relationships
|
||||
let required_links = sqlx::query!(
|
||||
r#"SELECT ltd.table_name
|
||||
r#"SELECT ltd.table_name
|
||||
FROM table_definition_links tdl
|
||||
JOIN table_definitions ltd ON tdl.linked_table_id = ltd.id
|
||||
WHERE tdl.source_table_id = $1 AND tdl.is_required = true"#,
|
||||
@@ -105,7 +108,6 @@ pub async fn post_table_data(
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
// Validate Steel scripts
|
||||
let scripts = sqlx::query!(
|
||||
"SELECT target_column, script FROM table_scripts WHERE table_definitions_id = $1",
|
||||
@@ -124,22 +126,41 @@ pub async fn post_table_data(
|
||||
format!("Script target column '{}' is required", target_column)
|
||||
))?;
|
||||
|
||||
// Execute the script using the owned String
|
||||
let script_result = execution::execute_script(script_record.script.clone(), "STRING") // Changed here
|
||||
.map_err(|e| Status::invalid_argument(
|
||||
format!("Script execution failed for '{}': {}", target_column, e)
|
||||
))?;
|
||||
// Create execution context
|
||||
let context = SteelContext {
|
||||
current_table: table_name.clone(),
|
||||
profile_id,
|
||||
row_data: data.clone(),
|
||||
db_pool: Arc::new(db_pool.clone()),
|
||||
runtime: Handle::current(),
|
||||
};
|
||||
|
||||
// Execute validation script
|
||||
let script_result = execution::execute_script(
|
||||
script_record.script,
|
||||
"STRINGS",
|
||||
Arc::new(db_pool.clone()),
|
||||
context,
|
||||
)
|
||||
.map_err(|e| Status::invalid_argument(
|
||||
format!("Script execution failed for '{}': {}", target_column, e)
|
||||
))?;
|
||||
|
||||
// Validate script output
|
||||
let Value::Strings(mut script_output) = script_result else {
|
||||
return Err(Status::internal("Script must return string values"));
|
||||
};
|
||||
|
||||
let expected_value = script_output.pop()
|
||||
.ok_or_else(|| Status::internal("Script returned no values"))?;
|
||||
|
||||
// Compare script result with user input
|
||||
let Value::String(expected_value) = script_result;
|
||||
if user_value != &expected_value {
|
||||
return Err(Status::invalid_argument(format!(
|
||||
"Validation failed for '{}'. Expected: '{}', Received: '{}'",
|
||||
"Validation failed for column '{}': Expected '{}', Got '{}'",
|
||||
target_column, expected_value, user_value
|
||||
)));
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
// Prepare SQL parameters
|
||||
let mut params = PgArguments::default();
|
||||
|
||||
Reference in New Issue
Block a user