Skip to content

Commit

Permalink
Mail logic : 1st part
Browse files Browse the repository at this point in the history
Implementing the basics with front, back, cli usages
  • Loading branch information
nag763 committed Jan 5, 2024
1 parent 54e44a3 commit d042081
Show file tree
Hide file tree
Showing 22 changed files with 588 additions and 31 deletions.
133 changes: 133 additions & 0 deletions Cargo.lock

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-- Add down migration script here
ALTER TABLE CHATTER
DROP COLUMN email;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-- Add up migration script here
ALTER TABLE CHATTER
ADD COLUMN email TEXT UNIQUE;
33 changes: 32 additions & 1 deletion tchatchers_back/src/api/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ use axum_extra::extract::CookieJar;
use tchatchers_core::api_response::ApiGenericResponse;
use tchatchers_core::async_message::AsyncMessage;
use tchatchers_core::authorization_token::AuthorizationToken;
use tchatchers_core::mail::mail::Mail;
use tchatchers_core::mail::template::WelcomeMailContent;
use tchatchers_core::refresh_token::RefreshToken;
use tchatchers_core::report::Report;
use tchatchers_core::serializable_token::SerializableToken;
Expand Down Expand Up @@ -45,8 +47,30 @@ pub async fn create_user(
return Err(ApiGenericResponse::SimilarLoginExists);
}

if let Some(mail) = &new_user.email {
if User::email_exists(mail, &state.pg_pool).await? {
return Err(ApiGenericResponse::SimilarEmailExists);
}
}

new_user.insert(&state.pg_pool).await?;
Ok(ApiGenericResponse::UserCreated)
if state.mails_enabled {
if let Some(email) = new_user.email {
if let Some(mail) = Mail::new(
email,
WelcomeMailContent {
name: new_user.name,
},
) {
mail.send()
.await
.map_err(|_e| ApiGenericResponse::MailingError)?;
}
}
Ok(ApiGenericResponse::CreationMailSent)
} else {
Ok(ApiGenericResponse::UserCreated)
}
}

/// Check whether a login exists or not.
Expand Down Expand Up @@ -252,6 +276,13 @@ pub async fn update_user(
let payload_bytes = payload.bytes().await?;
let user: UpdatableUser = postcard::from_bytes(&payload_bytes)?;
user.validate()?;

if let Some(mail) = &user.email {
if User::email_exists_except_self(mail, user.id, &state.pg_pool).await? {
return Err(ApiGenericResponse::SimilarEmailExists);
}
}

let mut tasks: JoinSet<Result<(), ApiGenericResponse>> = JoinSet::new();
if jwt.user_id != user.id {
return Err(ApiGenericResponse::UnsifficentPriviledges);
Expand Down
7 changes: 7 additions & 0 deletions tchatchers_back/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ pub struct AppState {
session_pool: bb8::Pool<RedisConnectionManager>,
/// Redis async pool.
async_pool: bb8::Pool<RedisConnectionManager>,
// Global variable to indicate whether mails are enabled or not.
mails_enabled: bool,
}

#[tokio::main]
Expand All @@ -80,6 +82,10 @@ async fn main() -> anyhow::Result<()> {
.init();

let jwt_secret = std::env::var("JWT_SECRET").expect("No jwt secret has been defined");
let mails_enabled: bool = std::env::var("MAILS_ENABLED")
.expect("No email enabled configuration passed")
.parse()
.unwrap();
let refresh_token_secret = std::env::var("REFRESH_TOKEN_SECRET")
.expect("No refresh token signature key has been defined");
let (pg_pool, session_pool, async_pool) = join!(
Expand All @@ -101,6 +107,7 @@ async fn main() -> anyhow::Result<()> {
pg_pool,
session_pool,
async_pool,
mails_enabled,
};

let app = Router::new()
Expand Down
Loading

0 comments on commit d042081

Please sign in to comment.