|
1 | 1 | use axum::{Extension, Json, extract::State, http::StatusCode}; |
2 | 2 | use serde::{Deserialize, Serialize}; |
3 | 3 | use uuid::Uuid; |
4 | | -use tracing::{error, info, info_span}; |
5 | | -use utoipa::{ToSchema, openapi::info}; |
| 4 | +use tracing::{error, info}; |
| 5 | +use utoipa::{ToSchema}; |
6 | 6 |
|
7 | 7 | use crate::{ |
8 | | - AppState, auth::service as auth, error::AppError, models::{NewRefreshToken, User}, schema::email_verification_tokens, utils::{ |
| 8 | + AppState, |
| 9 | + auth::service as auth, |
| 10 | + error::AppError, |
| 11 | + models::{User}, |
| 12 | + utils::{ |
9 | 13 | enums::Role, response::{ApiResponse, EmptyData}, validation::validate_email |
10 | 14 | } |
11 | 15 | }; |
@@ -88,10 +92,6 @@ impl From<User> for UserResponse { |
88 | 92 | } |
89 | 93 | } |
90 | 94 |
|
91 | | -#[derive(Deserialize, ToSchema)] |
92 | | -pub struct DeleteAccountRequest { |
93 | | - pub token: String, |
94 | | -} |
95 | 95 |
|
96 | 96 | #[derive(Deserialize, ToSchema)] |
97 | 97 | pub struct RefreshTokenRequest { |
@@ -465,8 +465,44 @@ pub async fn delete_account( |
465 | 465 | ) -> Result<ApiResponse<EmptyData>, AppError> { |
466 | 466 |
|
467 | 467 | let mut conn = state.pool.get()?; |
468 | | -info!("Deleting account for user: {}", &user.id); |
| 468 | + info!("Deleting account for user: {}", &user.id); |
469 | 469 | auth::soft_delete_account(&mut conn, &user.id)?; |
| 470 | + |
| 471 | + let mail_service = state.mail_service.clone(); |
| 472 | + let user_email = user.email.clone(); |
| 473 | + let user_name = format!("{} {}", user.first_name, user.last_name); |
| 474 | + |
| 475 | + // Send email asynchronously |
| 476 | + tokio::spawn(async move { |
| 477 | + let subject = "Your HealthBridge account has been deleted"; |
| 478 | + let html_body = format!( |
| 479 | + r#" |
| 480 | + <p>Hi {},</p> |
| 481 | + <p>Your HealthBridge account has been successfully deleted.</p> |
| 482 | + <p>If this action was not initiated by you, please contact our support team immediately.</p> |
| 483 | + <p>— HealthBridge Team</p> |
| 484 | + "#, |
| 485 | + user_name |
| 486 | + ); |
| 487 | + |
| 488 | + let text_body = format!( |
| 489 | + "Hi {},\n\nYour HealthBridge account has been successfully deleted.\nIf this wasn’t you, please contact support.\n\n— HealthBridge Team", |
| 490 | + user_name |
| 491 | + ); |
| 492 | + |
| 493 | + if let Err(e) = mail_service |
| 494 | + .send_notification( |
| 495 | + &user_email, |
| 496 | + subject, |
| 497 | + &html_body, |
| 498 | + Some(&text_body), |
| 499 | + ) |
| 500 | + .await |
| 501 | + { |
| 502 | + error!("Failed to send account deletion email: {:?}", e); |
| 503 | + } |
| 504 | + }); |
| 505 | + |
470 | 506 | Ok(ApiResponse::message_only( |
471 | 507 | StatusCode::OK, |
472 | 508 | "Account deleted successfully", |
|
0 commit comments