Enhance money history logging: Implement best-effort insert into money_history for UI visibility across BaseWorker, EventsWorker, and UndergroundWorker, improving tracking of monetary changes.

This commit is contained in:
Torsten Schulz (local)
2025-12-09 08:19:42 +01:00
parent e8c8c5edb1
commit d740dbbf73
3 changed files with 53 additions and 0 deletions

View File

@@ -234,6 +234,24 @@ impl BaseWorker {
);
let _ = conn.query(&sql)?;
// Best-effort: insert a money history entry so the UI/history views
// can show the change even if the DB-function doesn't write it.
// We don't want to fail the whole operation if this insert fails,
// so log errors and continue.
let history_sql = format!(
"INSERT INTO falukant_log.money_history (user_id, change, action, created_at) VALUES ({uid}, {money}::numeric, '{act}', NOW());",
uid = uid_i32,
money = money_str,
act = escaped_action
);
if let Err(err) = conn.query(&history_sql) {
eprintln!(
"[BaseWorker] Warning: inserting money_history failed for user {}: {}",
uid_i32, err
);
}
Ok(())
}
}

View File

@@ -1009,6 +1009,23 @@ impl EventsWorker {
conn.prepare("update_money_event", QUERY_UPDATE_MONEY)?;
let _ = conn.execute("update_money_event", &[&user_id, &change, &action])?;
// Best-effort money_history insert for UI/history visibility.
let money_str = format!("{:.2}", change);
fn escape_sql_literal(s: &str) -> String { s.replace('\'', "''") }
let escaped_action = escape_sql_literal(&action);
let history_sql = format!(
"INSERT INTO falukant_log.money_history (user_id, change, action, created_at) VALUES ({uid}, {money}::numeric, '{act}', NOW());",
uid = user_id,
money = money_str,
act = escaped_action
);
if let Err(err) = conn.query(&history_sql) {
eprintln!(
"[EventsWorker] Warning: inserting money_history failed for user {}: {}",
user_id, err
);
}
Ok(change)
}

View File

@@ -962,6 +962,24 @@ fn change_falukant_user_money(
let p2: &(dyn ToSql + Sync) = &money_change;
let p3: &(dyn ToSql + Sync) = &action;
conn.execute("ug_update_money", &[p1, p2, p3])?;
// Best-effort insert into money_history for UI visibility
let money_str = format!("{:.2}", money_change);
fn escape_sql_literal(s: &str) -> String { s.replace('\'', "''") }
let escaped_action = escape_sql_literal(action);
let history_sql = format!(
"INSERT INTO falukant_log.money_history (user_id, change, action, created_at) VALUES ({uid}, {money}::numeric, '{act}', NOW());",
uid = falukant_user_id,
money = money_str,
act = escaped_action
);
if let Err(err) = conn.query(&history_sql) {
eprintln!(
"[UndergroundWorker] Warning: inserting money_history failed for user {}: {}",
falukant_user_id, err
);
}
Ok(())
}