2025-10-06 10:30:17 -03:00
|
|
|
use log::{error, info};
|
|
|
|
|
use rhai::Dynamic;
|
|
|
|
|
use rhai::Engine;
|
|
|
|
|
use serde_json::{json, Value};
|
2025-10-11 12:29:03 -03:00
|
|
|
use diesel::prelude::*;
|
2025-10-06 10:30:17 -03:00
|
|
|
|
2025-10-06 20:49:38 -03:00
|
|
|
use crate::shared::models::TriggerKind;
|
2025-10-06 10:30:17 -03:00
|
|
|
use crate::shared::state::AppState;
|
2025-10-11 12:29:03 -03:00
|
|
|
use crate::shared::models::UserSession;
|
2025-10-06 10:30:17 -03:00
|
|
|
|
2025-10-11 12:29:03 -03:00
|
|
|
pub fn on_keyword(state: &AppState, user: UserSession, engine: &mut Engine) {
|
|
|
|
|
let state_clone = state.clone();
|
2025-10-06 10:30:17 -03:00
|
|
|
|
|
|
|
|
engine
|
|
|
|
|
.register_custom_syntax(
|
2025-10-11 12:29:03 -03:00
|
|
|
["ON", "$ident$", "OF", "$string$"],
|
2025-10-06 10:30:17 -03:00
|
|
|
true,
|
|
|
|
|
{
|
|
|
|
|
move |context, inputs| {
|
|
|
|
|
let trigger_type = context.eval_expression_tree(&inputs[0])?.to_string();
|
|
|
|
|
let table = context.eval_expression_tree(&inputs[1])?.to_string();
|
|
|
|
|
let script_name = format!("{}_{}.rhai", table, trigger_type.to_lowercase());
|
|
|
|
|
|
|
|
|
|
let kind = match trigger_type.to_uppercase().as_str() {
|
|
|
|
|
"UPDATE" => TriggerKind::TableUpdate,
|
|
|
|
|
"INSERT" => TriggerKind::TableInsert,
|
|
|
|
|
"DELETE" => TriggerKind::TableDelete,
|
|
|
|
|
_ => return Err(format!("Invalid trigger type: {}", trigger_type).into()),
|
|
|
|
|
};
|
|
|
|
|
|
2025-10-11 12:29:03 -03:00
|
|
|
let conn = state_clone.conn.lock().unwrap().clone();
|
|
|
|
|
let result = execute_on_trigger(&conn, kind, &table, &script_name)
|
|
|
|
|
.map_err(|e| format!("DB error: {}", e))?;
|
2025-10-06 10:30:17 -03:00
|
|
|
|
|
|
|
|
if let Some(rows_affected) = result.get("rows_affected") {
|
|
|
|
|
Ok(Dynamic::from(rows_affected.as_i64().unwrap_or(0)))
|
|
|
|
|
} else {
|
|
|
|
|
Err("No rows affected".into())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
.unwrap();
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-11 12:29:03 -03:00
|
|
|
pub fn execute_on_trigger(
|
|
|
|
|
conn: &PgConnection,
|
2025-10-06 10:30:17 -03:00
|
|
|
kind: TriggerKind,
|
|
|
|
|
table: &str,
|
|
|
|
|
script_name: &str,
|
|
|
|
|
) -> Result<Value, String> {
|
|
|
|
|
info!(
|
|
|
|
|
"Starting execute_on_trigger with kind: {:?}, table: {}, script_name: {}",
|
|
|
|
|
kind, table, script_name
|
|
|
|
|
);
|
|
|
|
|
|
2025-10-11 12:29:03 -03:00
|
|
|
use crate::shared::models::system_automations;
|
|
|
|
|
|
|
|
|
|
let new_automation = (
|
|
|
|
|
system_automations::kind.eq(kind as i32),
|
|
|
|
|
system_automations::target.eq(table),
|
|
|
|
|
system_automations::script_name.eq(script_name),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
let result = diesel::insert_into(system_automations::table)
|
|
|
|
|
.values(&new_automation)
|
|
|
|
|
.execute(&mut conn.clone())
|
|
|
|
|
.map_err(|e| {
|
|
|
|
|
error!("SQL execution error: {}", e);
|
|
|
|
|
e.to_string()
|
|
|
|
|
})?;
|
2025-10-06 10:30:17 -03:00
|
|
|
|
|
|
|
|
Ok(json!({
|
|
|
|
|
"command": "on_trigger",
|
|
|
|
|
"trigger_type": format!("{:?}", kind),
|
|
|
|
|
"table": table,
|
|
|
|
|
"script_name": script_name,
|
2025-10-11 12:29:03 -03:00
|
|
|
"rows_affected": result
|
2025-10-06 10:30:17 -03:00
|
|
|
}))
|
|
|
|
|
}
|