feat: backport changes from the #17-automate-onboarding-mk-ii branch
This commit is contained in:
parent
93359698f0
commit
37ea38f516
4 changed files with 433 additions and 96 deletions
|
@ -59,7 +59,34 @@ pub mod link {
|
|||
// check if email exists
|
||||
let details = match get_server_member_email(&db, email).await {
|
||||
None => {
|
||||
return "Please check it matches (including case) your preferred contact on https://ulwolves.ie/memberships/profile and that you are fully paid up.".to_string()
|
||||
let invalid_user = "Please check it matches (including case) your preferred contact on https://ulwolves.ie/memberships/profile and that you are fully paid up.".to_string();
|
||||
|
||||
let wolves = wolves_oxidised::Client::new(&config.wolves_url, Some(&config.wolves_api));
|
||||
|
||||
// see if the user actually exists
|
||||
let id = match wolves.get_member(email).await {
|
||||
None => {
|
||||
return invalid_user;
|
||||
}
|
||||
Some(x) => x,
|
||||
};
|
||||
|
||||
// save teh user id and email to teh db
|
||||
match save_to_db_user(&db, id, email).await {
|
||||
Ok(x) => x,
|
||||
Err(x) => {
|
||||
dbg!(x);
|
||||
return "Error: unable to save user to teh database, contact Computer Society".to_string();
|
||||
}
|
||||
};
|
||||
|
||||
// pull it back out (technically could do it in previous step but more explicit)
|
||||
match get_server_member_email(&db, email).await {
|
||||
None => {
|
||||
return "Error: failed to read user from database.".to_string();
|
||||
}
|
||||
Some(x) => x,
|
||||
}
|
||||
}
|
||||
Some(x) => x,
|
||||
};
|
||||
|
@ -232,6 +259,20 @@ pub mod link {
|
|||
.fetch_optional(db)
|
||||
.await
|
||||
}
|
||||
|
||||
async fn save_to_db_user(db: &Pool<Sqlite>, id_wolves: i64, email: &str) -> Result<Option<Wolves>, sqlx::Error> {
|
||||
sqlx::query_as::<_, Wolves>(
|
||||
"
|
||||
INSERT INTO wolves (id_wolves, email)
|
||||
VALUES ($1, $2)
|
||||
ON CONFLICT(id_wolves) DO UPDATE SET email = $2
|
||||
",
|
||||
)
|
||||
.bind(id_wolves)
|
||||
.bind(email)
|
||||
.fetch_optional(db)
|
||||
.await
|
||||
}
|
||||
}
|
||||
|
||||
pub mod verify {
|
||||
|
|
68
src/lib.rs
68
src/lib.rs
|
@ -38,6 +38,8 @@ pub struct Config {
|
|||
|
||||
// wolves API base for clubs/socs
|
||||
pub wolves_url: String,
|
||||
// API key for accessing more general resources
|
||||
pub wolves_api: String,
|
||||
}
|
||||
impl TypeMapKey for Config {
|
||||
type Value = Arc<RwLock<Config>>;
|
||||
|
@ -63,6 +65,7 @@ pub fn get_config() -> Config {
|
|||
mail_user: "".to_string(),
|
||||
mail_pass: "".to_string(),
|
||||
wolves_url: "".to_string(),
|
||||
wolves_api: "".to_string(),
|
||||
};
|
||||
|
||||
if let Ok(x) = env::var("DATABASE_HOME") {
|
||||
|
@ -92,6 +95,9 @@ pub fn get_config() -> Config {
|
|||
if let Ok(x) = env::var("WOLVES_URL") {
|
||||
config.wolves_url = x.trim().to_string();
|
||||
}
|
||||
if let Ok(x) = env::var("WOLVES_API") {
|
||||
config.wolves_api = x.trim().to_string();
|
||||
}
|
||||
|
||||
config
|
||||
}
|
||||
|
@ -526,36 +532,8 @@ pub mod get_data {
|
|||
use super::*;
|
||||
use crate::set_roles::update_server;
|
||||
use std::collections::BTreeMap;
|
||||
use wolves_oxidised::WolvesUser;
|
||||
|
||||
#[derive(Deserialize, Serialize, Debug)]
|
||||
struct WolvesResultUser {
|
||||
committee: String,
|
||||
member_id: String,
|
||||
first_name: String,
|
||||
last_name: String,
|
||||
contact_email: String,
|
||||
opt_in_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>,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Serialize, Debug)]
|
||||
struct WolvesResultLocal {
|
||||
pub id_wolves: String,
|
||||
pub email: String,
|
||||
pub expiry: String,
|
||||
}
|
||||
pub async fn get_wolves(ctx: &Context) {
|
||||
let db_lock = {
|
||||
let data_read = ctx.data.read().await;
|
||||
|
@ -569,6 +547,9 @@ pub mod get_data {
|
|||
};
|
||||
let config = config_lock.read().await;
|
||||
|
||||
// set up teh client
|
||||
let wolves = wolves_oxidised::Client::new(&config.wolves_url, Some(&config.wolves_api));
|
||||
|
||||
for server_config in get_server_config_bulk(&db).await {
|
||||
let Servers {
|
||||
server,
|
||||
|
@ -581,7 +562,7 @@ pub mod get_data {
|
|||
|
||||
// list of users that need to be updated for this server
|
||||
let mut user_to_update = vec![];
|
||||
for user in get_wolves_sub(&config, wolves_api).await {
|
||||
for user in wolves.get_members(wolves_api).await {
|
||||
let id = user.member_id.parse::<u64>().unwrap_or_default();
|
||||
match existing.get(&(id as i64)) {
|
||||
None => {
|
||||
|
@ -627,30 +608,7 @@ pub mod get_data {
|
|||
.unwrap_or_default()
|
||||
}
|
||||
|
||||
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>, user: &WolvesResultUser) {
|
||||
async fn add_users_wolves(db: &Pool<Sqlite>, user: &WolvesUser) {
|
||||
// expiry
|
||||
match sqlx::query_as::<_, Wolves>(
|
||||
"
|
||||
|
@ -671,7 +629,7 @@ pub mod get_data {
|
|||
}
|
||||
}
|
||||
}
|
||||
async fn add_users_server_members(db: &Pool<Sqlite>, server: &GuildId, user: &WolvesResultUser) {
|
||||
async fn add_users_server_members(db: &Pool<Sqlite>, server: &GuildId, user: &WolvesUser) {
|
||||
match sqlx::query_as::<_, ServerMembers>(
|
||||
"
|
||||
INSERT OR REPLACE INTO server_members (server, id_wolves, expiry)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue