diff --git a/src/worker/sql.rs b/src/worker/sql.rs index d0481dd..4949a03 100644 --- a/src/worker/sql.rs +++ b/src/worker/sql.rs @@ -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#" -INSERT INTO falukant_log.notification (character_id, tr, shown, created_at, updated_at) -VALUES ( - (SELECT c.id FROM falukant_data.character c WHERE c.user_id = $1 ORDER BY c.id LIMIT 1), - $2, - FALSE, - NOW(), - NOW() -); +INSERT INTO falukant_log.notification (user_id, tr, shown, created_at, updated_at) +VALUES ($1, $2, FALSE, NOW(), NOW()); "#; // 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 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_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()); "#; @@ -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#" 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 - 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 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; "#; @@ -919,33 +913,36 @@ pub const QUERY_USERS_IN_CITIES_OF_REGIONS: &str = r#" "#; pub const QUERY_NOTIFY_OFFICE_EXPIRATION: &str = r#" - INSERT INTO falukant_log.notification - (character_id, tr, created_at, updated_at) - SELECT - po.character_id, - 'notify_office_expiring', - NOW(), - NOW() - FROM falukant_data.political_office AS po - JOIN falukant_type.political_office_type AS pot - ON po.office_type_id = pot.id - WHERE (po.created_at + (pot.term_length * INTERVAL '1 day')) - BETWEEN (NOW() + INTERVAL '2 days') - AND (NOW() + INTERVAL '2 days' + INTERVAL '1 second'); + INSERT INTO falukant_log.notification + (user_id, tr, created_at, updated_at) + SELECT + ch.user_id, + 'notify_office_expiring', + NOW(), + NOW() + FROM falukant_data.political_office AS po + JOIN falukant_type.political_office_type AS pot + ON po.office_type_id = pot.id + 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') + AND (NOW() + INTERVAL '2 days' + INTERVAL '1 second'); "#; pub const QUERY_NOTIFY_ELECTION_CREATED: &str = r#" INSERT INTO falukant_log.notification - (character_id, tr, created_at, updated_at) + (user_id, tr, created_at, updated_at) 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#" INSERT INTO falukant_log.notification - (character_id, tr, created_at, updated_at) + (user_id, tr, created_at, updated_at) 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#"