diff --git a/src/bin/update_data.rs b/src/bin/update_data.rs index 471e323..df2f95c 100644 --- a/src/bin/update_data.rs +++ b/src/bin/update_data.rs @@ -1,4 +1,4 @@ -use skynet_discord_bot::{db_init, get_config, Accounts, Config, DataBase}; +use skynet_discord_bot::{db_init, get_config, Accounts, Config, DataBase, get_server_config_bulk, Servers}; use std::{process, sync::Arc}; use serde::Deserialize; @@ -59,11 +59,6 @@ impl EventHandler for Handler { } } -#[derive(Debug, Deserialize)] -pub struct DiscordResult { - discord: String, - wolves_id: String, -} async fn fetch_accounts(ctx: &Context) { let config_lock = { let data_read = ctx.data.read().await; @@ -79,17 +74,27 @@ async fn fetch_accounts(ctx: &Context) { let db = db_lock.read().await; // handle wolves api here + get_wolves(&db).await; // get from skynet for the compsoc server only + get_skynet(&db, &config).await; + +} + +#[derive(Debug, Deserialize)] +pub struct SkynetResult { + discord: String, + wolves_id: String, +} +async fn get_skynet(db: &Pool, config: &Config){ let url = format!("{}/ldap/discord?auth={}", &config.ldap_api, &config.auth); - if let Ok(result) = surf::get(url).recv_json::>().await { + if let Ok(result) = surf::get(url).recv_json::>().await { for user in result { add_users_skynet(&db, &config.skynet_server, &user).await; } } } - -pub async fn add_users_skynet(db: &Pool, server: &GuildId, user: &DiscordResult) { +async fn add_users_skynet(db: &Pool, server: &GuildId, user: &SkynetResult) { match sqlx::query_as::<_, Accounts>( " UPDATE accounts @@ -110,3 +115,48 @@ pub async fn add_users_skynet(db: &Pool, server: &GuildId, user: &Discor } } } + +#[derive(Debug, Deserialize)] +struct WolvesResult { + pub id_wolves: String, + pub email: String, + pub expiry: String, +} +async fn get_wolves(db: &Pool){ + for server_config in get_server_config_bulk(db).await { + let Servers { + server, + wolves_api, + .. + } = server_config; + + // get the data here + let mut result: Vec = vec![]; + + for user in result { + add_users_wolves(&db, &server, &user).await; + } + } +} + +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) + VALUES (?1, ?2, ?3, ?4) + ", + ) + .bind(*server.as_u64() as i64) + .bind(&user.id_wolves) + .bind(&user.email) + .bind(&user.expiry) + .fetch_optional(db) + .await + { + Ok(_) => {} + Err(e) => { + println!("Failure to insert into {} {:?}", server.as_u64(), user); + println!("{:?}", e); + } + } +} \ No newline at end of file