aboutsummaryrefslogtreecommitdiff
path: root/src/jobs
diff options
context:
space:
mode:
Diffstat (limited to 'src/jobs')
-rw-r--r--src/jobs/mod.rs7
-rw-r--r--src/jobs/weekly_report.rs27
2 files changed, 20 insertions, 14 deletions
diff --git a/src/jobs/mod.rs b/src/jobs/mod.rs
index 17df58c..a718d93 100644
--- a/src/jobs/mod.rs
+++ b/src/jobs/mod.rs
@@ -1,14 +1,17 @@
mod weekly_report;
use sqlx::sqlite::SqlitePool;
-use tera::Tera;
use tokio::time::{sleep, Duration};
use crate::db;
use crate::model::config::Config;
use crate::model::job::Job;
-pub async fn start(config: Config, pool: SqlitePool, templates: Tera) {
+pub async fn start(
+ config: Config,
+ pool: SqlitePool,
+ templates: minijinja::Environment<'_>,
+) {
loop {
if db::jobs::should_run(&pool, Job::WeeklyReport).await {
log::info!("Starting weekly report job");
diff --git a/src/jobs/weekly_report.rs b/src/jobs/weekly_report.rs
index 0c10143..5058c52 100644
--- a/src/jobs/weekly_report.rs
+++ b/src/jobs/weekly_report.rs
@@ -1,6 +1,5 @@
use sqlx::sqlite::SqlitePool;
use std::collections::HashMap;
-use tera::{Context, Tera};
use crate::db;
use crate::mail;
@@ -10,9 +9,9 @@ use crate::payer;
pub async fn send(
config: &Config,
pool: &SqlitePool,
- templates: &Tera,
+ env: &minijinja::Environment<'_>,
) -> bool {
- match get_weekly_report(pool, templates).await {
+ match get_weekly_report(pool, env).await {
Ok(report) => {
let users = db::users::list(pool).await;
mail::send(
@@ -30,7 +29,10 @@ pub async fn send(
.await
}
Err(err) => {
- log::error!("Error preparing weekly report from template: {:?}", err);
+ log::error!(
+ "Error preparing weekly report from template: {:?}",
+ err
+ );
false
}
}
@@ -38,8 +40,8 @@ pub async fn send(
async fn get_weekly_report(
pool: &SqlitePool,
- templates: &Tera,
-) -> Result<String, tera::Error> {
+ env: &minijinja::Environment<'_>,
+) -> Result<String, minijinja::Error> {
let users = db::users::list(pool).await;
let incomes_from = db::incomes::defined_for_all(pool).await;
let user_incomes = match incomes_from {
@@ -53,10 +55,11 @@ async fn get_weekly_report(
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)
+ let template = env.get_template("report/report.j2")?;
+ template.render(minijinja::context!(
+ name => "John",
+ exceeding_payers => exceeding_payers,
+ payments => last_week_payments,
+ incomes => last_week_incomes
+ ))
}