feat: modified to use the wolves api
This commit is contained in:
parent
4125ad634f
commit
49363d02aa
2 changed files with 62 additions and 129 deletions
|
@ -1,6 +1,6 @@
|
|||
use skynet_discord_bot::{db_init, get_config, get_server_config_bulk, Config, ServerMembers, Servers, Wolves};
|
||||
|
||||
use serde::Deserialize;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serenity::model::id::GuildId;
|
||||
use sqlx::{Pool, Sqlite};
|
||||
|
||||
|
@ -13,149 +13,75 @@ async fn main() {
|
|||
};
|
||||
|
||||
// handle wolves api here
|
||||
get_wolves_csv(&db, &config).await;
|
||||
// handle wolves api here
|
||||
get_wolves(&db).await;
|
||||
get_wolves(&db, &config).await;
|
||||
}
|
||||
|
||||
async fn get_wolves_csv(db: &Pool<Sqlite>, config: &Config) {
|
||||
if let Ok(accounts) = get_csv(config) {
|
||||
for account in accounts {
|
||||
add_users_wolves_csv(db, &config.skynet_server, &account).await;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, serde::Deserialize)]
|
||||
struct RecordCSV {
|
||||
#[serde(rename = "MemID")]
|
||||
mem_id: String,
|
||||
#[serde(rename = "Contact Email")]
|
||||
#[derive(Deserialize, Serialize, Debug)]
|
||||
struct WolvesResultUser {
|
||||
committee: String,
|
||||
wolves_id: String,
|
||||
first_name: String,
|
||||
last_name: String,
|
||||
email: String,
|
||||
#[serde(rename = "Expiry")]
|
||||
student_id: Option<String>,
|
||||
note: Option<String>,
|
||||
expiry: String,
|
||||
requested: String,
|
||||
approved: String,
|
||||
sitename: String,
|
||||
domain: String,
|
||||
}
|
||||
|
||||
impl From<&RecordCSV> for Wolves {
|
||||
fn from(input: &RecordCSV) -> Self {
|
||||
Self {
|
||||
id_wolves: input.mem_id.to_owned(),
|
||||
email: input.email.to_owned(),
|
||||
discord: None,
|
||||
minecraft: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&RecordCSV> for ServerMembers {
|
||||
fn from(input: &RecordCSV) -> Self {
|
||||
Self {
|
||||
server: Default::default(),
|
||||
id_wolves: input.mem_id.to_owned(),
|
||||
expiry: input.expiry.to_owned(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct Csv {
|
||||
wolves: Wolves,
|
||||
server_members: ServerMembers,
|
||||
}
|
||||
|
||||
fn get_csv(config: &Config) -> Result<Vec<Csv>, Box<dyn std::error::Error>> {
|
||||
let mut result = vec![];
|
||||
|
||||
let csv = format!("{}/{}", &config.home, &config.csv);
|
||||
if let Ok(mut rdr) = csv::Reader::from_path(csv) {
|
||||
for r in rdr.deserialize() {
|
||||
// Notice that we need to provide a type hint for automatic
|
||||
// deserialization.
|
||||
let record: RecordCSV = r?;
|
||||
if record.mem_id.is_empty() {
|
||||
continue;
|
||||
}
|
||||
|
||||
let mut tmp = ServerMembers::from(&record);
|
||||
tmp.server = config.skynet_server;
|
||||
result.push(Csv {
|
||||
wolves: Wolves::from(&record),
|
||||
server_members: tmp,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Ok(result)
|
||||
}
|
||||
async fn add_users_wolves_csv(db: &Pool<Sqlite>, server: &GuildId, user: &Csv) {
|
||||
match sqlx::query_as::<_, Wolves>(
|
||||
"
|
||||
INSERT INTO wolves (id_wolves, email)
|
||||
VALUES ($1, $2)
|
||||
ON CONFLICT(id_wolves) DO UPDATE SET email = $2
|
||||
",
|
||||
)
|
||||
.bind(&user.wolves.id_wolves)
|
||||
.bind(&user.wolves.email)
|
||||
.fetch_optional(db)
|
||||
.await
|
||||
{
|
||||
Ok(_) => {}
|
||||
Err(e) => {
|
||||
println!("Failure to insert into {} {:?}", server.as_u64(), user.wolves);
|
||||
println!("{:?}", e);
|
||||
}
|
||||
}
|
||||
|
||||
match sqlx::query_as::<_, ServerMembers>(
|
||||
"
|
||||
INSERT OR REPLACE INTO server_members (server, id_wolves, expiry)
|
||||
VALUES (?1, ?2, ?3)
|
||||
",
|
||||
)
|
||||
.bind(*server.as_u64() as i64)
|
||||
.bind(&user.server_members.id_wolves)
|
||||
.bind(&user.server_members.expiry)
|
||||
.fetch_optional(db)
|
||||
.await
|
||||
{
|
||||
Ok(_) => {}
|
||||
Err(e) => {
|
||||
println!("Failure to insert into {} {:?}", server.as_u64(), user.server_members);
|
||||
println!("{:?}", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct SkynetResult {
|
||||
discord: String,
|
||||
id_member: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
#[derive(Deserialize, Serialize, Debug)]
|
||||
struct WolvesResult {
|
||||
success: i8,
|
||||
result: Vec<WolvesResultUser>,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Serialize, Debug)]
|
||||
struct WolvesResultLocal {
|
||||
pub id_wolves: String,
|
||||
pub email: String,
|
||||
pub expiry: String,
|
||||
}
|
||||
async fn get_wolves(db: &Pool<Sqlite>) {
|
||||
async fn get_wolves(db: &Pool<Sqlite>, config: &Config) {
|
||||
for server_config in get_server_config_bulk(db).await {
|
||||
let Servers {
|
||||
server,
|
||||
//wolves_api,
|
||||
wolves_api,
|
||||
..
|
||||
} = server_config;
|
||||
|
||||
// get the data here
|
||||
let result: Vec<WolvesResult> = vec![];
|
||||
|
||||
for user in result {
|
||||
for user in get_wolves_sub(config, &wolves_api).await {
|
||||
add_users_wolves(db, &server, &user).await;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn add_users_wolves(db: &Pool<Sqlite>, server: &GuildId, user: &WolvesResult) {
|
||||
async fn get_wolves_sub(config: &Config, wolves_api: &str) -> Vec<WolvesResultUser> {
|
||||
if config.wolves_url.is_empty() {
|
||||
return vec![];
|
||||
}
|
||||
|
||||
// get wolves data
|
||||
if let Ok(mut res) = surf::post(&config.wolves_url).header("X-AM-Identity", wolves_api).await {
|
||||
if let Ok(WolvesResult {
|
||||
success,
|
||||
result,
|
||||
}) = res.body_json().await
|
||||
{
|
||||
if success != 1 {
|
||||
return vec![];
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
vec![]
|
||||
}
|
||||
|
||||
async fn add_users_wolves(db: &Pool<Sqlite>, server: &GuildId, user: &WolvesResultUser) {
|
||||
// expiry
|
||||
match sqlx::query_as::<_, Wolves>(
|
||||
"
|
||||
|
@ -164,7 +90,7 @@ async fn add_users_wolves(db: &Pool<Sqlite>, server: &GuildId, user: &WolvesResu
|
|||
ON CONFLICT(id_wolves) DO UPDATE SET email = $2
|
||||
",
|
||||
)
|
||||
.bind(&user.id_wolves)
|
||||
.bind(&user.wolves_id)
|
||||
.bind(&user.email)
|
||||
.fetch_optional(db)
|
||||
.await
|
||||
|
@ -183,7 +109,7 @@ async fn add_users_wolves(db: &Pool<Sqlite>, server: &GuildId, user: &WolvesResu
|
|||
",
|
||||
)
|
||||
.bind(*server.as_u64() as i64)
|
||||
.bind(&user.id_wolves)
|
||||
.bind(&user.wolves_id)
|
||||
.bind(&user.expiry)
|
||||
.fetch_optional(db)
|
||||
.await
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue