From 92ce2d2a86acb9e6c9c664a21b845d777f548a7a Mon Sep 17 00:00:00 2001 From: Ashwin Kumar Sivakumar Date: Tue, 21 Jul 2026 04:30:34 +0530 Subject: [PATCH] fix: notification insert used wrong column name; log silent email failures MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Found while investigating "notifications/emails not working on approve or job posting": 1. Real bug: apps/companies/src/handlers/mod.rs::view_contact (company viewing an applicant's contact info) inserted into notifications using column name `notification_type`, which has never existed — the column is `type`. This INSERT has been failing outright every time a company views a contact. 2. Root cause for approvals specifically: verifications/approval_requests never existed until earlier this session (see 20260718200000_create_verifications_and_approvals) — every admin approve/reject action was failing at the DB layer before it ever reached the notification/email code, so nothing in this area could have worked regardless of the email/notification logic itself. 3. Observability gap: every `state.mail.send_*_email(...)` call site silently discarded its Result (`let _ = ...`), so if the SMTP/ Zeptomail provider is unconfigured (crates/email::Mailer already logs a clear warning at startup for that, but callers gave no per-send signal) or a send fails for any other reason, there was no way to see it happen. Added `tracing::error!` logging on failure for every job/approval-related email: job submitted, job approved, job rejected, requirement approved, profile approval approved/rejected, requirement submitted. Doesn't change delivery — if the environment has no EMAIL_PROVIDER/SMTP_*/ZEPTOMAIL_* configured, sends still fail, but that failure is now visible in logs instead of silent. In-app notifications for approvals were already schema-correct (job/profile/requirement approve+reject all insert into notifications with the right columns) — the two real defects were #1 and #2 above. --- apps/companies/src/handlers/mod.rs | 7 ++++--- apps/customers/src/handlers.rs | 5 +++-- apps/users/src/handlers/approvals.rs | 12 +++++++++--- apps/users/src/handlers/verifications.rs | 8 ++++++-- 4 files changed, 22 insertions(+), 10 deletions(-) diff --git a/apps/companies/src/handlers/mod.rs b/apps/companies/src/handlers/mod.rs index 5dafc80..c182d74 100644 --- a/apps/companies/src/handlers/mod.rs +++ b/apps/companies/src/handlers/mod.rs @@ -326,9 +326,10 @@ async fn submit_job( match JobRepository::update_status(&state.pool, job.id, "PENDING_APPROVAL").await { Ok(updated) => { - // Fire email to company user (ignore failures) if let Ok(user) = UserRepository::get_by_id(&state.pool, auth.user_id).await { - let _ = state.mail.send_job_submitted_email(&user.email, &format!("{} {}", user.first_name.unwrap_or_default(), user.last_name.unwrap_or_default()), &updated.title).await; + if let Err(e) = state.mail.send_job_submitted_email(&user.email, &format!("{} {}", user.first_name.unwrap_or_default(), user.last_name.unwrap_or_default()), &updated.title).await { + tracing::error!("Failed to send job-submitted email to {}: {:?}", user.email, e); + } } // Create verification case so the request appears in Verification Management first. @@ -714,7 +715,7 @@ async fn view_contact( let _ = sqlx::query( r#" - INSERT INTO notifications (user_id, title, body, notification_type, reference_id) + INSERT INTO notifications (user_id, title, body, type, reference_id) VALUES ($1, $2, $3, $4, $5) "# ) diff --git a/apps/customers/src/handlers.rs b/apps/customers/src/handlers.rs index 4ed7806..3637cdd 100644 --- a/apps/customers/src/handlers.rs +++ b/apps/customers/src/handlers.rs @@ -267,9 +267,10 @@ async fn submit_requirement( match RequirementRepository::update_status(&state.pool, req.id, "PENDING_APPROVAL").await { Ok(updated) => { - // Fire email to customer (ignore failures) if let Ok(user) = UserRepository::get_by_id(&state.pool, auth.user_id).await { - let _ = state.mail.send_requirement_submitted_email(&user.email, &format!("{} {}", user.first_name.unwrap_or_default(), user.last_name.unwrap_or_default()), &updated.title).await; + if let Err(e) = state.mail.send_requirement_submitted_email(&user.email, &format!("{} {}", user.first_name.unwrap_or_default(), user.last_name.unwrap_or_default()), &updated.title).await { + tracing::error!("Failed to send requirement-submitted email to {}: {:?}", user.email, e); + } } // Create verification case so this request enters Verification Management first. diff --git a/apps/users/src/handlers/approvals.rs b/apps/users/src/handlers/approvals.rs index 1b78d23..1e354e3 100644 --- a/apps/users/src/handlers/approvals.rs +++ b/apps/users/src/handlers/approvals.rs @@ -456,7 +456,9 @@ async fn approve_job( .await; if let Ok(Some((name, email, user_uuid))) = company_info { - let _ = state.mail.send_job_approved_email(&email, &name, &existing.title).await; + if let Err(e) = state.mail.send_job_approved_email(&email, &name, &existing.title).await { + tracing::error!("Failed to send job-approved email to {}: {:?}", email, e); + } // Send in-app notification to company sqlx::query( @@ -521,7 +523,9 @@ async fn reject_job( if let Ok(Some((name, email, user_uuid))) = company_info { let r = payload.reason.as_deref().unwrap_or("Rejected by admin"); - let _ = state.mail.send_job_rejected_email(&email, &name, &existing.title, r).await; + if let Err(e) = state.mail.send_job_rejected_email(&email, &name, &existing.title, r).await { + tracing::error!("Failed to send job-rejected email to {}: {:?}", email, e); + } // Send in-app notification to company sqlx::query( @@ -594,7 +598,9 @@ async fn approve_requirement( if let Some(user_id) = req.created_by_user_id { if let Ok(user) = UserRepository::get_by_id(&state.pool, user_id).await { let name = format!("{} {}", user.first_name.unwrap_or_default(), user.last_name.unwrap_or_default()); - let _ = state.mail.send_requirement_approved_email(&user.email, &name, &req.title).await; + if let Err(e) = state.mail.send_requirement_approved_email(&user.email, &name, &req.title).await { + tracing::error!("Failed to send requirement-approved email to {}: {:?}", user.email, e); + } } } diff --git a/apps/users/src/handlers/verifications.rs b/apps/users/src/handlers/verifications.rs index cbd2ba7..2747e3a 100644 --- a/apps/users/src/handlers/verifications.rs +++ b/apps/users/src/handlers/verifications.rs @@ -172,7 +172,9 @@ async fn trigger_rejection( if let Ok(user) = db::models::user::UserRepository::get_by_id(&state.pool, user_id).await { let display = role_key_to_display(&role_key); let user_name = format!("{} {}", user.first_name.unwrap_or_default(), user.last_name.unwrap_or_default()); - let _ = state.mail.send_approval_rejected_email(&user.email, &user_name, &display, reason_str).await; + if let Err(e) = state.mail.send_approval_rejected_email(&user.email, &user_name, &display, reason_str).await { + tracing::error!("Failed to send approval-rejected email to {}: {:?}", user.email, e); + } } // Send in-app notification @@ -226,7 +228,9 @@ async fn approve_verification( let display = role_key_to_display(&v.role_key); let user_name = format!("{} {}", user.first_name.unwrap_or_default(), user.last_name.unwrap_or_default()); // Use a "verification passed" notification instead of final approval - let _ = state.mail.send_approval_approved_email(&user.email, &user_name, &display).await; + if let Err(e) = state.mail.send_approval_approved_email(&user.email, &user_name, &display).await { + tracing::error!("Failed to send approval-approved email to {}: {:?}", user.email, e); + } } // Send in-app notification - profile verified, pending final approval