Skip to content

Commit

Permalink
refactor(public-api): emailer to email notification with messages
Browse files Browse the repository at this point in the history
  • Loading branch information
sergeysova committed Mar 16, 2020
1 parent ef6286e commit 276764f
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 23 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions public-api/src/generated.rs
@@ -1,3 +1,5 @@
#![allow(dead_code)]

pub mod api {
use actix_swagger::{Answer, Api};
use actix_web::{
Expand Down
10 changes: 4 additions & 6 deletions public-api/src/services/email.rs
@@ -1,4 +1,4 @@
use authmenow_public_app::contracts::{Emailer, RegisterEmailer};
use authmenow_public_app::contracts::{EmailMessage, EmailNotification};

#[derive(Clone)]
pub struct Email {}
Expand All @@ -9,11 +9,9 @@ impl Email {
}
}

impl Emailer for Email {
fn send(&self, email: String, content: String) -> bool {
println!("EMAIL: send {} to {}", content, email);
impl EmailNotification for Email {
fn send(&self, email: String, message: EmailMessage) -> bool {
println!("EMAIL: send {:?} to {}", message, email);
true
}
}

impl RegisterEmailer for Email {}
1 change: 1 addition & 0 deletions public-app/Cargo.toml
Expand Up @@ -10,3 +10,4 @@ chrono = { version = "0.4.10", features = [] }
uuid = { version = "0.7.4", features = ["v4"] }
validator = "0.10.0"
validator_derive = "0.10.0"
serde = { version = "1.0", features = ["derive"] }
20 changes: 13 additions & 7 deletions public-app/src/contracts/emailer.rs
@@ -1,10 +1,16 @@
pub trait Emailer {
fn send(&self, email: String, content: String) -> bool;
use serde::Serialize;

pub trait EmailNotification {
fn send(&self, email: String, content: EmailMessage) -> bool;
}

pub trait RegisterEmailer: Emailer {
fn confirmation_code(&self, email: String, code: String) -> bool {
let content = format!("Enter this code {}", code);
self.send(email, content)
}
#[derive(Debug, Serialize)]
pub enum EmailMessage {
RegisterConfirmation {
code: String,
},
RegisterFinished {
first_name: String,
last_name: String,
},
}
33 changes: 23 additions & 10 deletions public-app/src/registrator.rs
@@ -1,6 +1,6 @@
use crate::contracts::{
RegisterEmailer, RegisterUserError, RequestsRepo, SaveRegisterRequestError, SecureGenerator,
UnexpectedDatabaseError, UserRegisterForm, UserRepo,
EmailMessage, EmailNotification, RegisterUserError, RequestsRepo, SaveRegisterRequestError,
SecureGenerator, UnexpectedDatabaseError, UserRegisterForm, UserRepo,
};
use crate::models::RegisterRequest;
use crate::App;
Expand Down Expand Up @@ -61,7 +61,7 @@ impl<DB, E, G> Registrator for App<DB, E, G>
where
DB: UserRepo + RequestsRepo,
G: SecureGenerator,
E: RegisterEmailer,
E: EmailNotification,
{
fn create_register_request(
&self,
Expand Down Expand Up @@ -93,7 +93,12 @@ where
break result;
}?;

self.emailer.confirmation_code(form.email, request.code);
self.emailer.send(
form.email,
EmailMessage::RegisterConfirmation {
code: request.code.clone(),
},
);

Ok(RequestCreated {
expires_at: request.expires_at,
Expand All @@ -117,6 +122,14 @@ where
last_name: form.last_name,
})?;

self.emailer.send(
created_user.email.clone(),
EmailMessage::RegisterFinished {
first_name: created_user.first_name,
last_name: created_user.last_name,
},
);

self.db
.register_requests_delete_all_for_email(created_user.email)?;

Expand Down Expand Up @@ -148,6 +161,12 @@ impl From<RegisterUserError> for RegisterConfirmError {
}
}

impl From<validator::ValidationErrors> for RegisterConfirmError {
fn from(_: validator::ValidationErrors) -> Self {
Self::InvalidForm
}
}

impl From<SaveRegisterRequestError> for RegisterRequestError {
fn from(_: SaveRegisterRequestError) -> Self {
// Now all errors from request errors converted to Unexpected
Expand All @@ -156,12 +175,6 @@ impl From<SaveRegisterRequestError> for RegisterRequestError {
}
}

impl From<validator::ValidationErrors> for RegisterConfirmError {
fn from(_: validator::ValidationErrors) -> Self {
Self::InvalidForm
}
}

impl From<validator::ValidationErrors> for RegisterRequestError {
fn from(_: validator::ValidationErrors) -> Self {
Self::InvalidForm
Expand Down

0 comments on commit 276764f

Please sign in to comment.