diff --git a/core/notifications/src/messages/email_formatter.rs b/core/notifications/src/messages/email_formatter.rs new file mode 100644 index 00000000000..13271fdc1c8 --- /dev/null +++ b/core/notifications/src/messages/email_formatter.rs @@ -0,0 +1,48 @@ +use handlebars::Handlebars; +use serde_json::json; + +pub struct EmailFomatter<'a> { + handlebars: Handlebars<'a>, +} + +impl EmailFomatter<'_> { + pub fn new() -> Self { + let mut handlebars = Handlebars::new(); + // add error handling + handlebars + .register_template_file("identification", "./templates/identification.hbs") + .expect("Template should be present"); + handlebars + .register_template_file("base", "./templates/layouts/base.hbs") + .expect("Template should be present"); + handlebars + .register_template_file("styles", "./templates/partials/styles.hbs") + .expect("Template should be present"); + + // add error handling + EmailFomatter { handlebars } + } + + pub fn generic_email_template(&self, subject: &str, body: &str) -> String { + let data = json!({ + "subject": subject, + "body": body, + }); + self.handlebars + .render("identification", &data) + .expect("Template should be present") + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_generic_email_template() { + let email_formatter = EmailFomatter::new(); + let title = "title"; + let body = "body"; + email_formatter.generic_email_template(title, body); + } +} diff --git a/core/notifications/src/messages/mod.rs b/core/notifications/src/messages/mod.rs index eb8342e5d19..8f648d5b77b 100644 --- a/core/notifications/src/messages/mod.rs +++ b/core/notifications/src/messages/mod.rs @@ -1,8 +1,10 @@ -use handlebars::Handlebars; use rust_i18n::t; use crate::{notification_event::*, primitives::*}; +mod email_formatter; +use email_formatter::EmailFomatter; + pub struct LocalizedPushMessage { pub title: String, pub body: String, @@ -155,27 +157,12 @@ impl EmailMessages { locale: &str, _event: &IdentityVerificationApproved, ) -> Option { - let mut handlebars = Handlebars::new(); - // add error handling - handlebars - .register_template_file("identification", "./templates/identification.hbs") - .ok()?; - handlebars - .register_template_file("base", "./templates/layouts/base.hbs") - .ok()?; - handlebars - .register_template_file("styles", "./templates/partials/styles.hbs") - .ok()?; + let email_formatter = EmailFomatter::new(); let title = t!("identity_verification_approved.title", locale = locale).to_string(); let body = t!("identity_verification_approved.body", locale = locale).to_string(); - let data = serde_json::json!({ - "subject": &title, - "body": &body - }); - // add error handling - let body = handlebars.render("identification", &data).ok()?; + let body = email_formatter.generic_email_template(&title, &body); Some(LocalizedEmail { subject: title,