Implement overproduction notification handling in ProduceWorker: Add logic to check for existing notifications and update them if necessary, or create a new notification if none exist. Introduce SQL queries for finding and updating overproduction notifications to enhance database interactions.

This commit is contained in:
Torsten Schulz (local)
2026-01-28 15:06:48 +01:00
parent 710a2a62b2
commit 4b4d84b15c
8 changed files with 5170 additions and 2 deletions

View File

@@ -14,6 +14,8 @@ use crate::worker::sql::{
QUERY_INSERT_INVENTORY,
QUERY_INSERT_UPDATE_PRODUCTION_LOG,
QUERY_ADD_OVERPRODUCTION_NOTIFICATION,
QUERY_FIND_OVERPRODUCTION_NOTIFICATION,
QUERY_UPDATE_OVERPRODUCTION_NOTIFICATION,
};
/// Abbildet eine abgeschlossene Produktion aus der Datenbank.
@@ -402,9 +404,15 @@ impl ProduceWorker {
.get()
.map_err(|e| crate::db::DbError::new(format!("DB-Verbindung fehlgeschlagen: {e}")))?;
// Prüfe, ob bereits eine unangezeigte Überproduktions-Benachrichtigung für diesen User/Branch existiert
conn.prepare(
"add_overproduction_notification",
QUERY_ADD_OVERPRODUCTION_NOTIFICATION,
"find_overproduction_notification",
QUERY_FIND_OVERPRODUCTION_NOTIFICATION,
)?;
let existing_rows = conn.execute(
"find_overproduction_notification",
&[&user_id, &branch_id],
)?;
// Zusätzlich zur Menge die Branch-ID in der Payload mitschicken, damit
@@ -414,6 +422,35 @@ impl ProduceWorker {
remaining_quantity, branch_id
);
if !existing_rows.is_empty() {
// Aktualisiere bestehende Benachrichtigung
// PostgreSQL aggregiert die Werte automatisch in der UPDATE-Query
conn.prepare(
"update_overproduction_notification",
QUERY_UPDATE_OVERPRODUCTION_NOTIFICATION,
)?;
// Versuche Update; wenn es fehlschlägt (z.B. ungültiges JSON), erstelle neue Benachrichtigung
match conn.execute(
"update_overproduction_notification",
&[&user_id, &branch_id, &remaining_quantity],
) {
Ok(_) => return Ok(()),
Err(err) => {
eprintln!(
"[ProduceWorker] Fehler beim Aktualisieren der Overproduction-Notification für User {} Branch {}: {}. Erstelle neue Benachrichtigung.",
user_id, branch_id, err
);
// Fallback: Erstelle neue Benachrichtigung
}
}
}
// Erstelle neue Benachrichtigung, wenn keine existiert oder Parsing fehlschlug
conn.prepare(
"add_overproduction_notification",
QUERY_ADD_OVERPRODUCTION_NOTIFICATION,
)?;
conn.execute(
"add_overproduction_notification",
&[&user_id, &notification],

View File

@@ -1452,6 +1452,31 @@ pub const QUERY_ADD_OVERPRODUCTION_NOTIFICATION: &str = r#"
) VALUES ($1, $2, FALSE, NOW(), NOW());
"#;
pub const QUERY_UPDATE_OVERPRODUCTION_NOTIFICATION: &str = r#"
UPDATE falukant_log.notification
SET tr = jsonb_set(
tr::jsonb,
'{value}',
to_jsonb(COALESCE((tr::jsonb->>'value')::int, 0) + $3)
)::text,
updated_at = NOW()
WHERE user_id = $1
AND shown = FALSE
AND tr::text LIKE '%"tr":"production.overproduction"%'
AND (tr::jsonb->>'branch_id')::int = $2;
"#;
pub const QUERY_FIND_OVERPRODUCTION_NOTIFICATION: &str = r#"
SELECT id, tr
FROM falukant_log.notification
WHERE user_id = $1
AND shown = FALSE
AND tr::text LIKE '%"tr":"production.overproduction"%'
AND (tr::jsonb->>'branch_id')::int = $2
ORDER BY created_at DESC
LIMIT 1;
"#;
// Aliases for personal variants (keeps original prepared statement names used in events.worker)
pub const QUERY_REDUCE_INVENTORY_PERSONAL: &str = QUERY_REDUCE_INVENTORY;
pub const QUERY_DELETE_INVENTORY_PERSONAL: &str = QUERY_DELETE_INVENTORY;