diff --git a/src/backend/mail.rs b/src/backend/mail.rs index 04f59b8..a51bfa4 100644 --- a/src/backend/mail.rs +++ b/src/backend/mail.rs @@ -19,7 +19,6 @@ cfg_if! { if #[cfg(feature = "ssr")] { use lettre::transport::smtp::client::{Tls, TlsParameters}; use lettre::transport::smtp::authentication::Credentials; use lettre::transport::smtp::extension::ClientId; - use std::ops::Add; use std::sync::mpsc::Sender; use std::sync::mpsc; @@ -81,15 +80,12 @@ cfg_if! { if #[cfg(feature = "ssr")] { fn replace_body_vars(text: String, reservation: &ResSumWithItems) -> String { text .replace("#date#", &reservation.summary.date.format("%d. %m. %Y").to_string()) - .replace("#summary#", &{ - let mut sum = "".to_string(); - for p in &reservation.reservations { - sum = sum.add(&format!("{}: {} - {}", - p.property.name, - p.reservation.from.format("%H:%M").to_string(), p.reservation.to.format("%H:%M").to_string())); - } - sum - } + .replace("#summary#", &reservation.reservations.iter() + .map(|p| format!("{}: {} - {}", + p.property.name, + p.reservation.from.format("%H:%M"), p.reservation.to.format("%H:%M"))) + .collect::>() + .join("\n") ) } @@ -267,19 +263,23 @@ mod tests { use chrono::{NaiveDate, NaiveTime}; use crate::backend::data::{Reservation, ResProperty, ResWithProperty}; - fn reservation() -> ResSumWithItems { + fn item(name: &str, from: (u32, u32), to: (u32, u32)) -> ResWithProperty { let mut property = ResProperty::default(); - property.name = "Court 1".to_string(); + property.name = name.to_string(); - let mut reservation = ResSumWithItems::default(); - reservation.summary.date = NaiveDate::from_ymd_opt(2030, 3, 5).unwrap(); - reservation.reservations.push(ResWithProperty { + ResWithProperty { reservation: Reservation::new( - NaiveTime::from_hms_opt(8, 0, 0).unwrap(), - NaiveTime::from_hms_opt(9, 30, 0).unwrap(), + NaiveTime::from_hms_opt(from.0, from.1, 0).unwrap(), + NaiveTime::from_hms_opt(to.0, to.1, 0).unwrap(), 1), property - }); + } + } + + fn reservation() -> ResSumWithItems { + let mut reservation = ResSumWithItems::default(); + reservation.summary.date = NaiveDate::from_ymd_opt(2030, 3, 5).unwrap(); + reservation.reservations.push(item("Court 1", (8, 0), (9, 30))); reservation } @@ -304,6 +304,32 @@ mod tests { assert_eq!(mail.text, "Date: 05. 03. 2030, items: Court 1: 08:00 - 09:30."); } + #[test] + fn mail_message_summary_has_one_item_per_line() { + let mut reservation = reservation(); + reservation.reservations.push(item("Court 2", (10, 0), (11, 0))); + reservation.reservations.push(item("Sauna", (18, 0), (20, 0))); + + let mail = MailMessage::new( + "admin@example.com".to_string(), + "customer@example.com".to_string(), + message("Booking", "Items:\n#summary#\nBye"), + &reservation); + + assert_eq!(mail.text, "Items:\nCourt 1: 08:00 - 09:30\nCourt 2: 10:00 - 11:00\nSauna: 18:00 - 20:00\nBye"); + } + + #[test] + fn mail_message_summary_is_empty_without_items() { + let mail = MailMessage::new( + "admin@example.com".to_string(), + "customer@example.com".to_string(), + message("Booking", "Items: #summary#."), + &ResSumWithItems::default()); + + assert_eq!(mail.text, "Items: ."); + } + #[test] fn mail_message_without_variables_is_unchanged() { let mail = MailMessage::new(