Skip to content

Commit

Permalink
chore(notifications): refactor email formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
UncleSamtoshi committed Feb 21, 2024
1 parent 74e9ef1 commit dbe95fa
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 18 deletions.
48 changes: 48 additions & 0 deletions core/notifications/src/messages/email_formatter.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
use handlebars::Handlebars;
use serde_json::json;

pub struct EmailFomatter<'a> {

Check warning on line 4 in core/notifications/src/messages/email_formatter.rs

View workflow job for this annotation

GitHub Actions / Spell Check with Typos

"Fomatter" should be "Formatter".
handlebars: Handlebars<'a>,
}

impl EmailFomatter<'_> {

Check warning on line 8 in core/notifications/src/messages/email_formatter.rs

View workflow job for this annotation

GitHub Actions / Spell Check with Typos

"Fomatter" should be "Formatter".
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 }

Check warning on line 23 in core/notifications/src/messages/email_formatter.rs

View workflow job for this annotation

GitHub Actions / Spell Check with Typos

"Fomatter" should be "Formatter".
}

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();

Check warning on line 43 in core/notifications/src/messages/email_formatter.rs

View workflow job for this annotation

GitHub Actions / Spell Check with Typos

"Fomatter" should be "Formatter".
let title = "title";
let body = "body";
email_formatter.generic_email_template(title, body);
}
}
23 changes: 5 additions & 18 deletions core/notifications/src/messages/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use handlebars::Handlebars;
use rust_i18n::t;

use crate::{notification_event::*, primitives::*};

mod email_formatter;
use email_formatter::EmailFomatter;

Check warning on line 6 in core/notifications/src/messages/mod.rs

View workflow job for this annotation

GitHub Actions / Spell Check with Typos

"Fomatter" should be "Formatter".

pub struct LocalizedPushMessage {
pub title: String,
pub body: String,
Expand Down Expand Up @@ -155,27 +157,12 @@ impl EmailMessages {
locale: &str,
_event: &IdentityVerificationApproved,
) -> Option<LocalizedEmail> {
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();

Check warning on line 160 in core/notifications/src/messages/mod.rs

View workflow job for this annotation

GitHub Actions / Spell Check with Typos

"Fomatter" should be "Formatter".

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,
Expand Down

0 comments on commit dbe95fa

Please sign in to comment.