diff --git a/src/bin/update_data.rs b/src/bin/update_data.rs index e43d7c0..3914fd7 100644 --- a/src/bin/update_data.rs +++ b/src/bin/update_data.rs @@ -83,7 +83,8 @@ async fn fetch_accounts(ctx: &Context) { #[derive(Debug, Deserialize)] pub struct SkynetResult { discord: String, - wolves_id: String, + id_wolves: String, + id_member: String, } async fn get_skynet(db: &Pool, config: &Config) { let url = format!("{}/ldap/discord?auth={}", &config.ldap_api, &config.auth); @@ -94,23 +95,46 @@ async fn get_skynet(db: &Pool, config: &Config) { } } async fn add_users_skynet(db: &Pool, server: &GuildId, user: &SkynetResult) { - match sqlx::query_as::<_, Accounts>( - " + if !user.id_wolves.is_empty() { + match sqlx::query_as::<_, Accounts>( + " UPDATE accounts SET discord = ? WHERE server = ? AND wolves_id = ? ", - ) - .bind(&user.discord) - .bind(*server.as_u64() as i64) - .bind(&user.wolves_id) - .fetch_optional(db) - .await - { - Ok(_) => {} - Err(e) => { - println!("Failure to insert into {} {:?}", server.as_u64(), user); - println!("{:?}", e); + ) + .bind(&user.discord) + .bind(*server.as_u64() as i64) + .bind(&user.id_wolves) + .fetch_optional(db) + .await + { + Ok(_) => {} + Err(e) => { + println!("Failure to insert into {} {:?}", server.as_u64(), user); + println!("{:?}", e); + } + } + } + if !user.id_member.is_empty() { + match sqlx::query_as::<_, Accounts>( + " + UPDATE accounts + SET discord = ? + WHERE server = ? AND id_member = ? + ", + ) + .bind(&user.discord) + .bind(*server.as_u64() as i64) + .bind(&user.id_member) + .fetch_optional(db) + .await + { + Ok(_) => {} + Err(e) => { + println!("Failure to insert into {} {:?}", server.as_u64(), user); + println!("{:?}", e); + } } } } @@ -141,7 +165,7 @@ async fn get_wolves(db: &Pool) { async fn add_users_wolves(db: &Pool, server: &GuildId, user: &WolvesResult) { match sqlx::query_as::<_, Accounts>( " - INSERT OR REPLACE INTO accounts (server, wolves_id, email, expiry) + INSERT OR REPLACE INTO accounts (server, id_wolves, email, expiry) VALUES (?1, ?2, ?3, ?4) ", ) diff --git a/src/lib.rs b/src/lib.rs index daa34e9..efc7ab1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -105,7 +105,8 @@ fn str_to_num(x: &str) -> T { #[derive(Debug, Clone, Deserialize, Serialize)] pub struct Accounts { pub server: GuildId, - pub wolves_id: String, + pub id_wolves: String, + pub id_member: String, pub email: String, pub expiry: String, pub discord: Option, @@ -119,7 +120,8 @@ impl<'r> FromRow<'r, SqliteRow> for Accounts { Ok(Self { server, - wolves_id: row.try_get("wolves_api")?, + id_wolves: row.try_get("id_wolves")?, + id_member: row.try_get("id_member")?, email: row.try_get("email")?, expiry: row.try_get("expiry")?, discord: row.try_get("discord")?, @@ -178,19 +180,20 @@ pub async fn db_init(config: &Config) -> Result, Error> { sqlx::query( "CREATE TABLE IF NOT EXISTS accounts ( server integer not null, - wolves_id text not null, + id_wolves text DEFAULT '', + id_member text DEFAULT '', email text not null, expiry text not null, discord text, minecraft text, - CONSTRAINT con_server_wolves PRIMARY KEY(server,wolves_id) + PRIMARY KEY(server,id_wolves,id_member) )", ) .execute(&pool) .await?; sqlx::query("CREATE INDEX IF NOT EXISTS index_server ON accounts (server)").execute(&pool).await?; - sqlx::query("CREATE INDEX IF NOT EXISTS index_wolves_id ON accounts (wolves_id)") + sqlx::query("CREATE INDEX IF NOT EXISTS index_id_wolves ON accounts (id_wolves)") .execute(&pool) .await?;