feat: now using teh "wolves-oxidised" library to get the member data
This commit is contained in:
parent
0b397369d1
commit
faa6233ecb
3 changed files with 343 additions and 276 deletions
|
@ -1,7 +1,7 @@
|
|||
use ldap3::{LdapConn, Scope, SearchEntry};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use skynet_ldap_backend::{db_init, get_config, AccountWolves, Accounts, Config};
|
||||
use sqlx::{Pool, Sqlite};
|
||||
use wolves_oxidised::WolvesUser;
|
||||
|
||||
#[async_std::main]
|
||||
async fn main() -> tide::Result<()> {
|
||||
|
@ -15,7 +15,8 @@ async fn main() -> tide::Result<()> {
|
|||
}
|
||||
|
||||
async fn update_wolves(config: &Config, db: &Pool<Sqlite>) {
|
||||
for account in get_wolves(config).await {
|
||||
let wolves = wolves_oxidised::Client::new(&config.wolves_url, None);
|
||||
for account in wolves.get_members(&config.wolves_key).await {
|
||||
update_account(db, &account).await;
|
||||
}
|
||||
}
|
||||
|
@ -96,91 +97,20 @@ async fn update_ldap(config: &Config, db: &Pool<Sqlite>) {
|
|||
// done with ldap
|
||||
ldap.unbind().unwrap();
|
||||
}
|
||||
impl From<&WolvesResultUser> for AccountWolves {
|
||||
fn from(input: &WolvesResultUser) -> Self {
|
||||
let id_student = match input.student_id.to_owned() {
|
||||
None => {
|
||||
Some("00000000".to_string())
|
||||
}
|
||||
Some(x) => {
|
||||
Some(x)
|
||||
}
|
||||
};
|
||||
|
||||
AccountWolves {
|
||||
id_wolves: input.member_id.parse::<i64>().unwrap_or(0),
|
||||
id_student,
|
||||
email: input.contact_email.to_owned(),
|
||||
expiry: input.expiry.to_owned(),
|
||||
name_first: Some(input.first_name.to_owned()),
|
||||
name_second: Some(input.last_name.to_owned()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Serialize, Debug)]
|
||||
struct WolvesResultUser {
|
||||
committee: String,
|
||||
member_id: String,
|
||||
first_name: String,
|
||||
last_name: String,
|
||||
contact_email: String,
|
||||
student_id: Option<String>,
|
||||
note: Option<String>,
|
||||
expiry: String,
|
||||
requested: String,
|
||||
approved: String,
|
||||
sitename: String,
|
||||
domain: String,
|
||||
}
|
||||
#[derive(Deserialize, Serialize, Debug)]
|
||||
struct WolvesResult {
|
||||
success: i8,
|
||||
result: Vec<WolvesResultUser>,
|
||||
}
|
||||
|
||||
async fn get_wolves(config: &Config) -> Vec<AccountWolves> {
|
||||
if config.wolves_key.is_empty() {
|
||||
return vec![];
|
||||
}
|
||||
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", &config.wolves_key).await {
|
||||
match res.body_json().await {
|
||||
Ok(WolvesResult {
|
||||
success,
|
||||
result,
|
||||
}) => {
|
||||
if success != 1 {
|
||||
return vec![];
|
||||
}
|
||||
return result.iter().map(AccountWolves::from).collect::<Vec<AccountWolves>>();
|
||||
}
|
||||
Err(e) => {
|
||||
println!("{:?}", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
vec![]
|
||||
}
|
||||
|
||||
async fn update_account(db: &Pool<Sqlite>, account: &AccountWolves) {
|
||||
async fn update_account(db: &Pool<Sqlite>, account: &WolvesUser) {
|
||||
sqlx::query_as::<_, AccountWolves>(
|
||||
"
|
||||
INSERT OR REPLACE INTO accounts_wolves (id_wolves, id_student, email, expiry, name_first, name_second)
|
||||
VALUES (?1, ?2, ?3, ?4, ?5, ?6)
|
||||
",
|
||||
)
|
||||
.bind(account.id_wolves)
|
||||
.bind(&account.id_student)
|
||||
.bind(&account.email)
|
||||
.bind(&account.member_id)
|
||||
.bind(&account.student_id)
|
||||
.bind(&account.contact_email)
|
||||
.bind(&account.expiry)
|
||||
.bind(&account.name_first)
|
||||
.bind(&account.name_second)
|
||||
.bind(&account.first_name)
|
||||
.bind(&account.last_name)
|
||||
.fetch_optional(db)
|
||||
.await
|
||||
.ok();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue