fix: was not correctly getting accounts if limited to just a single field returned

This commit is contained in:
silver 2023-08-07 16:46:44 +01:00
parent 224451292d
commit 5a53e2e695

View file

@ -26,7 +26,7 @@ async fn update(config: &Config) -> tide::Result<()> {
} }
// pull from wolves csv // 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); users_tmp.insert(user);
} }
@ -67,20 +67,17 @@ async fn update(config: &Config) -> tide::Result<()> {
Ok(()) Ok(())
} }
async fn from_csv(config: &Config) -> Result<HashSet<String>, Box<dyn Error>> { async fn from_csv(db: &Pool<Sqlite>) -> Result<HashSet<String>, Box<dyn Error>> {
let db = db_init(config).await.unwrap();
let mut uids = HashSet::new(); 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. // only import users if it is actually active.
if record.expiry < get_now_iso(true) { if record.expiry < get_now_iso(true) {
continue; 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); 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); uids.insert(uid);
} }
} }
@ -91,22 +88,16 @@ async fn from_csv(config: &Config) -> Result<HashSet<String>, Box<dyn Error>> {
async fn account_mail_get_uid(db: &Pool<Sqlite>, mail: &str) -> Option<String> { async fn account_mail_get_uid(db: &Pool<Sqlite>, mail: &str) -> Option<String> {
match sqlx::query_as::<_, Accounts>( match sqlx::query_as::<_, Accounts>(
r#" r#"
SELECT user SELECT *
FROM accounts FROM accounts
WHERE mail == ? WHERE mail == ?
"#, "#,
) )
.bind(mail) .bind(mail)
.fetch_all(db) .fetch_one(db)
.await .await
{ {
Ok(res) => { Ok(res) => Some(res.user.to_owned()),
if res.is_empty() {
None
} else {
Some(res[0].user.to_owned())
}
}
Err(_) => None, Err(_) => None,
} }
} }
@ -114,22 +105,16 @@ async fn account_mail_get_uid(db: &Pool<Sqlite>, mail: &str) -> Option<String> {
async fn account_id_get_uid(db: &Pool<Sqlite>, id: &str) -> Option<String> { async fn account_id_get_uid(db: &Pool<Sqlite>, id: &str) -> Option<String> {
match sqlx::query_as::<_, Accounts>( match sqlx::query_as::<_, Accounts>(
r#" r#"
SELECT user SELECT *
FROM accounts FROM accounts
WHERE student_id == ? WHERE student_id == ?
"#, "#,
) )
.bind(id) .bind(id)
.fetch_all(db) .fetch_one(db)
.await .await
{ {
Ok(res) => { Ok(res) => Some(res.student_id.to_owned()),
if res.is_empty() {
None
} else {
Some(res[0].student_id.to_owned())
}
}
Err(_) => None, Err(_) => None,
} }
} }