//! # Users service use crate::app_config::AppConfig; use crate::connections::db_connection; use crate::models::{NewUser, User, UserID}; use crate::schema::users; use crate::services::mail_service; use crate::utils::string_utils::rand_str; use crate::utils::time_utils::time; use diesel::prelude::*; /// Get the information of the user pub async fn get_by_id(id: UserID) -> anyhow::Result { db_connection::execute(|conn| Ok(users::table.filter(users::dsl::id.eq(id.0)).first(conn)?)) } /// Create a new account pub async fn create_account(name: &str, email: &str) -> anyhow::Result { db_connection::execute(|conn| { let res = diesel::insert_into(users::table) .values(&NewUser { name: name.trim(), email: email.trim(), time_create: time() as i64, }) .get_result(conn)?; Ok(res) }) } /// Check if an email address is already associated with an account pub async fn exists_email(email: &str) -> anyhow::Result { db_connection::execute(|conn| { let count: i64 = users::table .filter(users::email.eq(email)) .count() .get_result(conn)?; Ok(count != 0) }) } /// Request password reset pub async fn request_reset_password(user: &mut User) -> anyhow::Result<()> { // If required, regenerate reset token if user.reset_password_token.is_none() || user.time_gen_reset_token as u64 + 3600 * 2 < time() { user.reset_password_token = Some(rand_str(149)); user.time_gen_reset_token = time() as i64; db_connection::execute(|conn| { Ok( diesel::update(users::dsl::users.filter(users::dsl::id.eq(user.id))) .set(( users::dsl::time_gen_reset_token.eq(user.time_gen_reset_token), users::dsl::reset_password_token.eq(user.reset_password_token.clone()), )) .execute(conn)?, ) })?; } // Send mail mail_service::send_mail( &user.email, "Réinitialisation de votre mot de passe", format!( "Bonjour, \n\n\ Vous pouvez réinitialiser le mot de passe de votre compte à l'adresse suivante : {} \n\n\ Ce lien est valide durant 24 heures.\n\n\ Cordialement,\n\n\ L'équipe de GeneIT", AppConfig::get() .get_password_reset_url(user.reset_password_token.as_deref().unwrap_or("")) ), ) .await?; Ok(()) }