diff --git a/geneit_backend/src/controllers/user_controller.rs b/geneit_backend/src/controllers/user_controller.rs index bebbf8b..c4b089e 100644 --- a/geneit_backend/src/controllers/user_controller.rs +++ b/geneit_backend/src/controllers/user_controller.rs @@ -115,7 +115,7 @@ pub async fn request_delete_account(remote_ip: RemoteIP, token: LoginToken) -> H } #[derive(serde::Deserialize)] -pub struct DeleteTokenBody { +pub struct DeleteAccountTokenBody { token: String, } @@ -125,8 +125,17 @@ struct CheckDeleteTokenResponse { } /// Check delete account token -pub async fn check_delete_token(req: Json) -> HttpResult { - let token = users_service::get_by_account_delete_token(&req.token).await?; +pub async fn check_delete_token(req: Json) -> HttpResult { + let user = users_service::get_by_account_delete_token(&req.token).await?; - Ok(HttpResponse::Ok().json(CheckDeleteTokenResponse { email: token.email })) + Ok(HttpResponse::Ok().json(CheckDeleteTokenResponse { email: user.email })) +} + +/// Delete account +pub async fn delete_account(req: Json) -> HttpResult { + let user = users_service::get_by_account_delete_token(&req.token).await?; + + users_service::delete_account(&user).await?; + + Ok(HttpResponse::Accepted().finish()) } diff --git a/geneit_backend/src/main.rs b/geneit_backend/src/main.rs index a9b2b5f..4587b5f 100644 --- a/geneit_backend/src/main.rs +++ b/geneit_backend/src/main.rs @@ -70,6 +70,10 @@ async fn main() -> std::io::Result<()> { "/user/check_delete_token", web::post().to(user_controller::check_delete_token), ) + .route( + "/user/delete_account", + web::post().to(user_controller::delete_account), + ) }) .bind(AppConfig::get().listen_address.as_str())? .run() diff --git a/geneit_backend/src/services/users_service.rs b/geneit_backend/src/services/users_service.rs index 52544d7..70eea27 100644 --- a/geneit_backend/src/services/users_service.rs +++ b/geneit_backend/src/services/users_service.rs @@ -171,6 +171,18 @@ pub async fn delete_not_validated_accounts() -> anyhow::Result<()> { }) } +/// Delete account +pub async fn delete_account(user: &User) -> anyhow::Result<()> { + log::info!("Delete account #{:?}", user.id()); + + // TODO : remove families memberships + + db_connection::execute(|conn| { + diesel::delete(users::dsl::users.filter(users::dsl::id.eq(user.id))).execute(conn)?; + Ok(()) + }) +} + /// Mark account as validated pub async fn validate_account(user: &mut User) -> anyhow::Result<()> { if user.time_activate > 0 {