Skip to content

Commit 9ff3cee

Browse files
committed
backend/send chargelog: add email templates
1 parent c2f340a commit 9ff3cee

File tree

3 files changed

+91
-2
lines changed

3 files changed

+91
-2
lines changed

backend/src/routes/send_chargelog_to_user.rs

Lines changed: 71 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use actix_web::{post, web, HttpRequest, HttpResponse, Responder};
2+
use askama::Template;
23
use futures_util::StreamExt;
34
use serde::{Deserialize, Serialize};
45
use utoipa::ToSchema;
@@ -20,6 +21,61 @@ pub struct SendChargelogSchema {
2021
pub chargelog: Vec<u8>,
2122
}
2223

24+
#[derive(Template)]
25+
#[template(path = "chargelog_de.html")]
26+
struct ChargelogDETemplate<'a> {
27+
name: &'a str,
28+
month: &'a str,
29+
filename: &'a str,
30+
}
31+
32+
#[derive(Template)]
33+
#[template(path = "chargelog_en.html")]
34+
struct ChargelogENTemplate<'a> {
35+
name: &'a str,
36+
month: &'a str,
37+
filename: &'a str,
38+
}
39+
40+
fn render_chargelog_email(
41+
user_name: &str,
42+
month: &str,
43+
filename: &str,
44+
lang: &str,
45+
) -> actix_web::Result<(String, &'static str)> {
46+
let (body, subject) = match lang {
47+
"de" | "de-DE" => {
48+
let template = ChargelogDETemplate {
49+
name: user_name,
50+
month,
51+
filename,
52+
};
53+
match template.render() {
54+
Ok(b) => (b, "Dein Ladelog"),
55+
Err(e) => {
56+
log::error!("Failed to render German chargelog email template for user '{}': {}", user_name, e);
57+
return Err(crate::error::Error::InternalError.into());
58+
}
59+
}
60+
}
61+
_ => {
62+
let template = ChargelogENTemplate {
63+
name: user_name,
64+
month,
65+
filename,
66+
};
67+
match template.render() {
68+
Ok(b) => (b, "Your Charge Log"),
69+
Err(e) => {
70+
log::error!("Failed to render English chargelog email template for user '{}': {}", user_name, e);
71+
return Err(crate::error::Error::InternalError.into());
72+
}
73+
}
74+
}
75+
};
76+
Ok((body, subject))
77+
}
78+
2379
#[utoipa::path(
2480
request_body = SendChargelogSchema,
2581
responses(
@@ -34,6 +90,7 @@ pub async fn send_chargelog(
3490
state: web::Data<AppState>,
3591
rate_limiter: web::Data<ChargerRateLimiter>,
3692
mut payload: web::Payload,
93+
#[cfg(not(test))] lang: crate::models::lang::Lang,
3794
) -> actix_web::Result<impl Responder> {
3895
let mut bytes = web::BytesMut::new();
3996
while let Some(chunk) = payload.next().await {
@@ -60,8 +117,20 @@ pub async fn send_chargelog(
60117
let user = parse_uuid(&payload.user_uuid)?;
61118
let user = get_user(&state, user).await?;
62119

63-
let subject = "Your Charger Log";
64-
let body = "Attached is your requested chargelog.".to_string();
120+
#[cfg(not(test))]
121+
let lang_str: String = lang.into();
122+
#[cfg(test)]
123+
let lang_str = String::from("en");
124+
125+
let month = chrono::Utc::now().format("%B %Y").to_string();
126+
127+
let (body, subject) = render_chargelog_email(
128+
&user.name,
129+
&month,
130+
&payload.filename,
131+
&lang_str,
132+
)?;
133+
65134
send_email_with_attachment(
66135
&user.email,
67136
subject,
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
</head>
5+
<body>
6+
<h3>Hallo {{name}},</h3>
7+
<p>Anbei findest du das Ladelog für den Monat {{month}}.</p>
8+
<p>Die Datei ist als Anhang dieser E-Mail beigefügt: {{filename}}</p>
9+
</body>
10+
</html>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
</head>
5+
<body>
6+
<h3>Hello {{name}},</h3>
7+
<p>Please find attached the charge log for the month of {{month}}.</p>
8+
<p>The file is attached to this email: {{filename}}</p>
9+
</body>
10+
</html>

0 commit comments

Comments
 (0)