feat: now able to get the memebr_id from just email

This commit is contained in:
silver 2024-11-09 02:23:46 +00:00
parent 733827c3e6
commit 7a6421469c
Signed by: silver
GPG key ID: 36F93D61BAD3FD7D
3 changed files with 402 additions and 22 deletions

View file

@ -19,6 +19,7 @@ use sqlx::{Pool, Sqlite};
pub mod link {
use super::*;
use serde::{Deserialize, Serialize};
pub async fn run(command: &ApplicationCommandInteraction, ctx: &Context) -> String {
let db_lock = {
@ -61,7 +62,32 @@ 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();
// see if the user actually exists
let id = match get_user(&config, 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,
};
@ -234,6 +260,59 @@ pub mod link {
.fetch_optional(db)
.await
}
#[derive(Serialize, Deserialize, Debug)]
#[serde(untagged)]
pub enum WolvesResultUserResult {
B(bool),
S(String),
}
#[derive(Deserialize, Serialize, Debug)]
struct WolvesResultUser {
success: i64,
result: WolvesResultUserResult,
}
async fn get_user(config: &Config, email: &str) -> Option<i64> {
let url = format!("{}/get_id_from_email", &config.wolves_url);
match reqwest::Client::new()
.post(&url)
.form(&[("email", email)])
.header("X-AM-Identity", &config.wolves_api)
.send()
.await
{
Ok(x) => {
if let Ok(y) = x.json::<WolvesResultUser>().await {
// this is the only time we will get a positive response, the None at the end catches everything else
if let WolvesResultUserResult::S(z) = y.result {
if let Ok(id) = z.parse::<i64>() {
return Some(id);
}
}
}
}
Err(e) => {
dbg!(e);
}
}
None
}
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)
",
)
.bind(id_wolves)
.bind(email)
.fetch_optional(db)
.await
}
}
pub mod verify {