Refactor notification handling in Workers: Centralize notification insertion and frontend updates by replacing direct SQL queries with dedicated functions. This improves code maintainability and reduces redundancy across character creation, events, and transport processing.

This commit is contained in:
Torsten Schulz (local)
2025-12-22 13:58:17 +01:00
parent a82d554494
commit fce7400303
7 changed files with 183 additions and 47 deletions

45
src/worker/notify.rs Normal file
View File

@@ -0,0 +1,45 @@
use crate::db::{ConnectionPool, DbConnection, DbError};
use crate::message_broker::MessageBroker;
use crate::worker::sql::QUERY_INSERT_NOTIFICATION_FULL;
/// Schreibt eine Notification in `falukant_log.notification`.
///
/// - `tr` ist ein (i.d.R. JSON-)String, den das Frontend parst.
/// - `character_id` ist optional (NULL).
pub fn insert_notification(
pool: &ConnectionPool,
user_id: i32,
tr: &str,
character_id: Option<i32>,
) -> Result<(), DbError> {
let mut conn = pool
.get()
.map_err(|e| DbError::new(format!("DB-Verbindung fehlgeschlagen: {e}")))?;
insert_notification_conn(&mut conn, user_id, tr, character_id)?;
Ok(())
}
/// Variante für bestehende DB-Verbindungen (spart Connect/Prepare in Loops).
pub fn insert_notification_conn(
conn: &mut DbConnection,
user_id: i32,
tr: &str,
character_id: Option<i32>,
) -> Result<(), DbError> {
conn.prepare("insert_notification_full", QUERY_INSERT_NOTIFICATION_FULL)?;
conn.execute(
"insert_notification_full",
&[&user_id, &tr, &character_id],
)?;
Ok(())
}
/// Informiert das Frontend, dass sich der Status geändert hat (z.B. Branches neu laden).
pub fn publish_update_status(broker: &MessageBroker, user_id: i32) {
broker.publish(format!(
r#"{{"event":"falukantUpdateStatus","user_id":{}}}"#,
user_id
));
}