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
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<HashSet<String>, Box<dyn Error>> {
let db = db_init(config).await.unwrap();
async fn from_csv(db: &Pool<Sqlite>) -> Result<HashSet<String>, Box<dyn Error>> {
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<HashSet<String>, Box<dyn Error>> {
async fn account_mail_get_uid(db: &Pool<Sqlite>, mail: &str) -> Option<String> {
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<Sqlite>, mail: &str) -> Option<String> {
async fn account_id_get_uid(db: &Pool<Sqlite>, id: &str) -> Option<String> {
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,
}
}