From 5a53e2e695f75a4b332bf0cb7d6652e5164836d7 Mon Sep 17 00:00:00 2001 From: Brendan Golden Date: Mon, 7 Aug 2023 16:46:44 +0100 Subject: [PATCH] fix: was not correctly getting accounts if limited to just a single field returned --- src/bin/update_groups.rs | 37 +++++++++++-------------------------- 1 file changed, 11 insertions(+), 26 deletions(-) diff --git a/src/bin/update_groups.rs b/src/bin/update_groups.rs index 3e6857e..3eb8b23 100644 --- a/src/bin/update_groups.rs +++ b/src/bin/update_groups.rs @@ -26,7 +26,7 @@ async fn update(config: &Config) -> tide::Result<()> { } // pull from wolves csv - for user in from_csv(config).await.unwrap_or_default() { + for user in from_csv(&db).await.unwrap_or_default() { users_tmp.insert(user); } @@ -67,20 +67,17 @@ async fn update(config: &Config) -> tide::Result<()> { Ok(()) } -async fn from_csv(config: &Config) -> Result, Box> { - let db = db_init(config).await.unwrap(); - +async fn from_csv(db: &Pool) -> Result, Box> { let mut uids = HashSet::new(); - for record in get_wolves(&db).await { + for record in get_wolves(db).await { // only import users if it is actually active. if record.expiry < get_now_iso(true) { continue; } - - if let Some(uid) = account_mail_get_uid(&db, &record.email).await { + if let Some(uid) = account_mail_get_uid(db, &record.email).await { uids.insert(uid); - } else if let Some(uid) = account_id_get_uid(&db, &record.id_student).await { + } else if let Some(uid) = account_id_get_uid(db, &record.id_student).await { uids.insert(uid); } } @@ -91,22 +88,16 @@ async fn from_csv(config: &Config) -> Result, Box> { async fn account_mail_get_uid(db: &Pool, mail: &str) -> Option { match sqlx::query_as::<_, Accounts>( r#" - SELECT user + SELECT * FROM accounts WHERE mail == ? "#, ) .bind(mail) - .fetch_all(db) + .fetch_one(db) .await { - Ok(res) => { - if res.is_empty() { - None - } else { - Some(res[0].user.to_owned()) - } - } + Ok(res) => Some(res.user.to_owned()), Err(_) => None, } } @@ -114,22 +105,16 @@ async fn account_mail_get_uid(db: &Pool, mail: &str) -> Option { async fn account_id_get_uid(db: &Pool, id: &str) -> Option { match sqlx::query_as::<_, Accounts>( r#" - SELECT user + SELECT * FROM accounts WHERE student_id == ? "#, ) .bind(id) - .fetch_all(db) + .fetch_one(db) .await { - Ok(res) => { - if res.is_empty() { - None - } else { - Some(res[0].student_id.to_owned()) - } - } + Ok(res) => Some(res.student_id.to_owned()), Err(_) => None, } }