fix: get_user_role_keys returns newest role first, not oldest
- models/user.rs: ORDER BY ur.created_at DESC so most recently assigned role is returned first - handlers/auth.rs: resolve_signup_role_candidates returns empty vec instead of JOB_SEEKER when no valid intent
This commit is contained in:
parent
695069f2cc
commit
f37c48f1ee
2 changed files with 12 additions and 15 deletions
|
|
@ -135,9 +135,13 @@ fn normalize_role_key(raw: &str) -> String {
|
|||
}
|
||||
|
||||
fn resolve_signup_role_candidates(intent: Option<&str>, profession: Option<&str>) -> Vec<String> {
|
||||
let normalized_intent = normalize_role_key(intent.unwrap_or("JOB_SEEKER"));
|
||||
let normalized_intent = intent.map(normalize_role_key).unwrap_or_default();
|
||||
let normalized_profession = profession.map(normalize_role_key).filter(|v| !v.is_empty());
|
||||
|
||||
if normalized_intent.is_empty() {
|
||||
return vec![];
|
||||
}
|
||||
|
||||
if normalized_intent.contains("COMPANY") {
|
||||
return vec!["COMPANY".to_string()];
|
||||
}
|
||||
|
|
@ -154,7 +158,7 @@ fn resolve_signup_role_candidates(intent: Option<&str>, profession: Option<&str>
|
|||
return vec!["PHOTOGRAPHER".to_string(), "JOB_SEEKER".to_string()];
|
||||
}
|
||||
|
||||
vec!["JOB_SEEKER".to_string()]
|
||||
vec![]
|
||||
}
|
||||
|
||||
fn role_display_name_from_code(code: &str) -> String {
|
||||
|
|
@ -205,13 +209,6 @@ async fn ensure_role_exists(pool: &sqlx::PgPool, role_code: &str) -> Option<Uuid
|
|||
.await
|
||||
.ok()?;
|
||||
|
||||
let _ = sqlx::query(
|
||||
"INSERT INTO external_roles (role_id) VALUES ($1) ON CONFLICT (role_id) DO NOTHING",
|
||||
)
|
||||
.bind(role_id)
|
||||
.execute(pool)
|
||||
.await;
|
||||
|
||||
Some(role_id)
|
||||
}
|
||||
|
||||
|
|
@ -304,7 +301,7 @@ async fn register(
|
|||
if let Some(role_id) = role_id {
|
||||
let _ = sqlx::query(
|
||||
r#"
|
||||
UPDATE user_roles
|
||||
UPDATE user_role_assignments
|
||||
SET status = 'APPROVED'
|
||||
WHERE user_id = $1 AND role_id = $2
|
||||
"#,
|
||||
|
|
@ -316,10 +313,10 @@ async fn register(
|
|||
|
||||
let _ = sqlx::query(
|
||||
r#"
|
||||
INSERT INTO user_roles (user_id, role_id, status)
|
||||
INSERT INTO user_role_assignments (user_id, role_id, status)
|
||||
SELECT $1, $2, 'APPROVED'
|
||||
WHERE NOT EXISTS (
|
||||
SELECT 1 FROM user_roles WHERE user_id = $1 AND role_id = $2
|
||||
SELECT 1 FROM user_role_assignments WHERE user_id = $1 AND role_id = $2
|
||||
)
|
||||
"#,
|
||||
)
|
||||
|
|
|
|||
|
|
@ -105,15 +105,15 @@ impl UserRepository {
|
|||
.await
|
||||
}
|
||||
|
||||
/// Returns all approved role codes for a user (e.g. ["COMPANY", "DEVELOPER"])
|
||||
/// Returns all approved role codes for a user, most recently assigned first (e.g. ["VIDEO_EDITOR", "JOB_SEEKER"])
|
||||
pub async fn get_user_role_keys(pool: &PgPool, user_id: Uuid) -> Result<Vec<String>, sqlx::Error> {
|
||||
let rows = sqlx::query_scalar::<_, String>(
|
||||
r#"
|
||||
SELECT r.key
|
||||
FROM user_roles ur
|
||||
FROM user_role_assignments ur
|
||||
JOIN roles r ON ur.role_id = r.id
|
||||
WHERE ur.user_id = $1 AND ur.status = 'APPROVED'
|
||||
ORDER BY ur.created_at ASC
|
||||
ORDER BY ur.created_at DESC
|
||||
"#,
|
||||
)
|
||||
.bind(user_id)
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue