diff options
author | Joris | 2021-01-03 13:40:40 +0100 |
---|---|---|
committer | Joris | 2021-01-03 13:54:20 +0100 |
commit | 11052951b74b9ad4b6a9412ae490086235f9154b (patch) | |
tree | 64526ac926c1bf470ea113f6cac8a33158684e8d /src/jobs | |
parent | 371449b0e312a03162b78797b83dee9d81706669 (diff) |
Rewrite in Rust
Diffstat (limited to 'src/jobs')
-rw-r--r-- | src/jobs/jobs.rs | 28 | ||||
-rw-r--r-- | src/jobs/mod.rs | 2 | ||||
-rw-r--r-- | src/jobs/weekly_report.rs | 55 |
3 files changed, 85 insertions, 0 deletions
diff --git a/src/jobs/jobs.rs b/src/jobs/jobs.rs new file mode 100644 index 0000000..3e54624 --- /dev/null +++ b/src/jobs/jobs.rs @@ -0,0 +1,28 @@ +use sqlx::sqlite::SqlitePool; +use tera::Tera; +use tokio::time::{delay_for, Duration}; + +use crate::db; +use crate::jobs::weekly_report; +use crate::model::config::Config; +use crate::model::job::Job; + +pub async fn start(config: Config, pool: SqlitePool, templates: Tera) -> () { + loop { + if db::jobs::should_run(&pool, Job::WeeklyReport).await { + info!("Starting weekly report job"); + if weekly_report::send(&config, &pool, &templates).await { + db::jobs::actualize_last_execution(&pool, Job::WeeklyReport) + .await; + } + } + if db::jobs::should_run(&pool, Job::MonthlyPayment).await { + info!("Starting monthly payment job"); + db::payments::create_monthly_payments(&pool).await; + db::jobs::actualize_last_execution(&pool, Job::MonthlyPayment) + .await; + } + // Sleeping 8 hours + delay_for(Duration::from_secs(8 * 60 * 60)).await; + } +} diff --git a/src/jobs/mod.rs b/src/jobs/mod.rs new file mode 100644 index 0000000..be2ddac --- /dev/null +++ b/src/jobs/mod.rs @@ -0,0 +1,2 @@ +pub mod jobs; +mod weekly_report; diff --git a/src/jobs/weekly_report.rs b/src/jobs/weekly_report.rs new file mode 100644 index 0000000..819d30b --- /dev/null +++ b/src/jobs/weekly_report.rs @@ -0,0 +1,55 @@ +use sqlx::sqlite::SqlitePool; +use std::collections::HashMap; +use tera::{Context, Tera}; + +use crate::db; +use crate::mail; +use crate::model::config::Config; +use crate::payer; + +pub async fn send( + config: &Config, + pool: &SqlitePool, + templates: &Tera, +) -> bool { + match get_weekly_report(pool, templates).await { + Ok(report) => { + let users = db::users::list(pool).await; + mail::send( + config, + users.into_iter().map(|u| (u.email, u.name)).collect(), + "Budget — rapport hebdomadaire".to_string(), + report, + ) + } + Err(err) => { + error!("Error preparing weekly report from template: {:?}", err); + false + } + } +} + +async fn get_weekly_report( + pool: &SqlitePool, + templates: &Tera, +) -> Result<String, tera::Error> { + let users = db::users::list(pool).await; + let incomes_from = db::incomes::defined_for_all(pool).await; + let user_incomes = match incomes_from { + Some(from) => db::incomes::cumulative(pool, from).await, + None => HashMap::new(), + }; + let user_payments = db::payments::repartition(pool).await; + let exceeding_payers = + payer::exceeding(&users, &user_incomes, &user_payments); + + let last_week_payments = db::payments::last_week(pool).await; + let last_week_incomes = db::incomes::last_week(pool).await; + + let mut context = Context::new(); + context.insert("exceeding_payers", &exceeding_payers); + context.insert("payments", &last_week_payments); + context.insert("incomes", &last_week_incomes); + + templates.render("report/report.j2", &context) +} |