From 276764f08f8dd117ccd011c06be194cb171c1291 Mon Sep 17 00:00:00 2001 From: Sergey Sova Date: Mon, 16 Mar 2020 14:31:03 +0300 Subject: [PATCH] refactor(public-api): emailer to email notification with messages --- Cargo.lock | 1 + public-api/src/generated.rs | 2 ++ public-api/src/services/email.rs | 10 ++++----- public-app/Cargo.toml | 1 + public-app/src/contracts/emailer.rs | 20 +++++++++++------ public-app/src/registrator.rs | 33 ++++++++++++++++++++--------- 6 files changed, 44 insertions(+), 23 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 8122a0b..231616c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -339,6 +339,7 @@ name = "authmenow-public-app" version = "0.1.0" dependencies = [ "chrono 0.4.10 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.104 (registry+https://github.com/rust-lang/crates.io-index)", "uuid 0.7.4 (registry+https://github.com/rust-lang/crates.io-index)", "validator 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)", "validator_derive 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/public-api/src/generated.rs b/public-api/src/generated.rs index a14d011..13a2ea7 100644 --- a/public-api/src/generated.rs +++ b/public-api/src/generated.rs @@ -1,3 +1,5 @@ +#![allow(dead_code)] + pub mod api { use actix_swagger::{Answer, Api}; use actix_web::{ diff --git a/public-api/src/services/email.rs b/public-api/src/services/email.rs index 1b03326..ae94fa1 100644 --- a/public-api/src/services/email.rs +++ b/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 {} @@ -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 {} diff --git a/public-app/Cargo.toml b/public-app/Cargo.toml index 4c2a7e1..a74f0fa 100644 --- a/public-app/Cargo.toml +++ b/public-app/Cargo.toml @@ -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"] } diff --git a/public-app/src/contracts/emailer.rs b/public-app/src/contracts/emailer.rs index c17cfb3..4174b96 100644 --- a/public-app/src/contracts/emailer.rs +++ b/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, + }, } diff --git a/public-app/src/registrator.rs b/public-app/src/registrator.rs index 30e4774..ee6855c 100644 --- a/public-app/src/registrator.rs +++ b/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; @@ -61,7 +61,7 @@ impl Registrator for App where DB: UserRepo + RequestsRepo, G: SecureGenerator, - E: RegisterEmailer, + E: EmailNotification, { fn create_register_request( &self, @@ -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, @@ -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)?; @@ -148,6 +161,12 @@ impl From for RegisterConfirmError { } } +impl From for RegisterConfirmError { + fn from(_: validator::ValidationErrors) -> Self { + Self::InvalidForm + } +} + impl From for RegisterRequestError { fn from(_: SaveRegisterRequestError) -> Self { // Now all errors from request errors converted to Unexpected @@ -156,12 +175,6 @@ impl From for RegisterRequestError { } } -impl From for RegisterConfirmError { - fn from(_: validator::ValidationErrors) -> Self { - Self::InvalidForm - } -} - impl From for RegisterRequestError { fn from(_: validator::ValidationErrors) -> Self { Self::InvalidForm