diff --git a/apps/users/src/handlers/auth.rs b/apps/users/src/handlers/auth.rs index f517068..cce3a4f 100644 --- a/apps/users/src/handlers/auth.rs +++ b/apps/users/src/handlers/auth.rs @@ -316,12 +316,25 @@ async fn register( payload.intent.as_deref(), payload.profession.as_deref(), ); + + // Log role assignment for debugging + tracing::info!( + user_id = %user.id, + email = %email, + intent = ?payload.intent, + profession = ?payload.profession, + candidates = ?role_candidates, + "Assigning roles after registration" + ); + for role_key in role_candidates { let role_id = ensure_role_exists(&state.pool, &role_key).await; if let Some(role_id) = role_id { // For demo accounts, auto-approve the role immediately let status = if is_demo_account { "APPROVED" } else { "PENDING" }; - let _ = sqlx::query( + + // Try to update existing assignment first + let update_result = sqlx::query( r#" UPDATE user_role_assignments SET status = $3 @@ -333,8 +346,13 @@ async fn register( .bind(status) .execute(&state.pool) .await; + + if let Err(e) = &update_result { + tracing::warn!(error = %e, "Failed to update role assignment"); + } - let _ = sqlx::query( + // Insert if not exists (UPSERT pattern) + let insert_result = sqlx::query( r#" INSERT INTO user_role_assignments (user_id, role_id, status) SELECT $1, $2, $3 @@ -348,7 +366,14 @@ async fn register( .bind(status) .execute(&state.pool) .await; - break; + + if let Err(e) = &insert_result { + tracing::error!(error = %e, "Failed to insert role assignment"); + } else { + tracing::info!(role_key = %role_key, status = %status, "Role assigned successfully"); + } + } else { + tracing::warn!(role_key = %role_key, "Role not found in database"); } }