Aktualisiere Benachrichtigungsabfragen: Ersetze character_id durch user_id in den Benachrichtigungs-INSERT-Abfragen und optimiere die Abfragen zur Auswahl des Benutzers.

This commit is contained in:
Torsten Schulz (local)
2025-12-16 08:24:46 +01:00
parent ae90166adb
commit b45990c1b6

View File

@@ -38,14 +38,8 @@ ON CONFLICT (region_id) DO UPDATE SET weather_type_id = EXCLUDED.weather_type_id
"#; "#;
pub const QUERY_INSERT_NOTIFICATION: &str = r#" pub const QUERY_INSERT_NOTIFICATION: &str = r#"
INSERT INTO falukant_log.notification (character_id, tr, shown, created_at, updated_at) INSERT INTO falukant_log.notification (user_id, tr, shown, created_at, updated_at)
VALUES ( VALUES ($1, $2, FALSE, NOW(), NOW());
(SELECT c.id FROM falukant_data.character c WHERE c.user_id = $1 ORDER BY c.id LIMIT 1),
$2,
FALSE,
NOW(),
NOW()
);
"#; "#;
// Product pricing // Product pricing
@@ -270,7 +264,7 @@ SELECT po.id AS office_id, pot.name AS office_name, po.region_id, rt.label_tr AS
FROM falukant_data.political_office po FROM falukant_data.political_office po
JOIN falukant_type.political_office_type pot ON pot.id = po.office_type_id JOIN falukant_type.political_office_type pot ON pot.id = po.office_type_id
JOIN falukant_data.region r ON r.id = po.region_id JOIN falukant_data.region r ON r.id = po.region_id
JOIN falukant_type.region_type rt ON rt.id = r.region_type_id JOIN falukant_type.region rt ON rt.id = r.region_type_id
WHERE po.holder_id = $1 AND (po.end_date IS NULL OR po.end_date > NOW()); WHERE po.holder_id = $1 AND (po.end_date IS NULL OR po.end_date > NOW());
"#; "#;
@@ -286,10 +280,10 @@ SELECT COALESCE(SUM(tax_percent),0.0) AS total_percent FROM ancestors;
pub const QUERY_CUMULATIVE_TAX_WITH_EXEMPT: &str = r#" pub const QUERY_CUMULATIVE_TAX_WITH_EXEMPT: &str = r#"
WITH RECURSIVE ancestors AS ( WITH RECURSIVE ancestors AS (
SELECT r.id, r.parent_id, CASE WHEN rt.label_tr = ANY($2::text[]) THEN 0.0 ELSE COALESCE(r.tax_percent,0.0) END AS tax_percent SELECT r.id, r.parent_id, CASE WHEN rt.label_tr = ANY($2::text[]) THEN 0.0 ELSE COALESCE(r.tax_percent,0.0) END AS tax_percent
FROM falukant_data.region r JOIN falukant_type.region_type rt ON rt.id = r.region_type_id WHERE r.id = $1 FROM falukant_data.region r JOIN falukant_type.region rt ON rt.id = r.region_type_id WHERE r.id = $1
UNION ALL UNION ALL
SELECT r.id, r.parent_id, CASE WHEN rt.label_tr = ANY($2::text[]) THEN 0.0 ELSE COALESCE(r.tax_percent,0.0) END SELECT r.id, r.parent_id, CASE WHEN rt.label_tr = ANY($2::text[]) THEN 0.0 ELSE COALESCE(r.tax_percent,0.0) END
FROM falukant_data.region r JOIN falukant_type.region_type rt ON rt.id = r.region_type_id JOIN ancestors a ON r.id = a.parent_id FROM falukant_data.region r JOIN falukant_type.region rt ON rt.id = r.region_type_id JOIN ancestors a ON r.id = a.parent_id
) )
SELECT COALESCE(SUM(tax_percent),0.0) AS total_percent FROM ancestors; SELECT COALESCE(SUM(tax_percent),0.0) AS total_percent FROM ancestors;
"#; "#;
@@ -920,32 +914,35 @@ pub const QUERY_USERS_IN_CITIES_OF_REGIONS: &str = r#"
pub const QUERY_NOTIFY_OFFICE_EXPIRATION: &str = r#" pub const QUERY_NOTIFY_OFFICE_EXPIRATION: &str = r#"
INSERT INTO falukant_log.notification INSERT INTO falukant_log.notification
(character_id, tr, created_at, updated_at) (user_id, tr, created_at, updated_at)
SELECT SELECT
po.character_id, ch.user_id,
'notify_office_expiring', 'notify_office_expiring',
NOW(), NOW(),
NOW() NOW()
FROM falukant_data.political_office AS po FROM falukant_data.political_office AS po
JOIN falukant_type.political_office_type AS pot JOIN falukant_type.political_office_type AS pot
ON po.office_type_id = pot.id ON po.office_type_id = pot.id
WHERE (po.created_at + (pot.term_length * INTERVAL '1 day')) JOIN falukant_data.character AS ch
ON ch.id = po.character_id
WHERE ch.user_id IS NOT NULL
AND (po.created_at + (pot.term_length * INTERVAL '1 day'))
BETWEEN (NOW() + INTERVAL '2 days') BETWEEN (NOW() + INTERVAL '2 days')
AND (NOW() + INTERVAL '2 days' + INTERVAL '1 second'); AND (NOW() + INTERVAL '2 days' + INTERVAL '1 second');
"#; "#;
pub const QUERY_NOTIFY_ELECTION_CREATED: &str = r#" pub const QUERY_NOTIFY_ELECTION_CREATED: &str = r#"
INSERT INTO falukant_log.notification INSERT INTO falukant_log.notification
(character_id, tr, created_at, updated_at) (user_id, tr, created_at, updated_at)
VALUES VALUES
((SELECT c.id FROM falukant_data.character c WHERE c.user_id = $1 ORDER BY c.id LIMIT 1), 'notify_election_created', NOW(), NOW()); ($1, 'notify_election_created', NOW(), NOW());
"#; "#;
pub const QUERY_NOTIFY_OFFICE_FILLED: &str = r#" pub const QUERY_NOTIFY_OFFICE_FILLED: &str = r#"
INSERT INTO falukant_log.notification INSERT INTO falukant_log.notification
(character_id, tr, created_at, updated_at) (user_id, tr, created_at, updated_at)
VALUES VALUES
($1, 'notify_office_filled', NOW(), NOW()); ((SELECT user_id FROM falukant_data.character WHERE id = $1), 'notify_office_filled', NOW(), NOW());
"#; "#;
pub const QUERY_GET_USERS_WITH_EXPIRING_OFFICES: &str = r#" pub const QUERY_GET_USERS_WITH_EXPIRING_OFFICES: &str = r#"