feat: backend updated to have support for both teh current csv and future API

Also tweeked #21 a tad
This commit is contained in:
silver 2023-09-16 16:28:28 +01:00
parent e4e7171eac
commit 347988e113
5 changed files with 30 additions and 18 deletions

View file

@ -123,12 +123,13 @@ struct RecordCSV {
impl From<RecordCSV> for AccountWolves {
fn from(input: RecordCSV) -> Self {
AccountWolves {
id_wolves: input.mem_id,
id_student: input.id_student,
id_wolves: "".to_string(),
id_member: input.mem_id,
id_student: if input.id_student.is_empty() { None } else { Some(input.id_student) },
email: input.email,
expiry: input.expiry,
name_first: input.name_first,
name_second: input.name_second,
name_first: if input.name_first.is_empty() { None } else { Some(input.name_first) },
name_second: if input.name_second.is_empty() { None } else { Some(input.name_second) },
}
}
}
@ -156,11 +157,12 @@ fn get_csv(config: &Config) -> Result<Vec<RecordCSV>, Box<dyn std::error::Error>
async fn update_account(db: &Pool<Sqlite>, account: &AccountWolves) {
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)
INSERT OR REPLACE INTO accounts_wolves (id_wolves, id_member, id_student, email, expiry, name_first, name_second)
VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7)
",
)
.bind(&account.id_wolves)
.bind(&account.id_member)
.bind(&account.id_student)
.bind(&account.email)
.bind(&account.expiry)

View file

@ -77,8 +77,10 @@ async fn from_csv(db: &Pool<Sqlite>) -> Result<HashSet<String>, Box<dyn Error>>
}
if let Some(uid) = account_mail_get_uid(db, &record.email).await {
uids.insert(uid);
} else if let Some(uid) = account_id_get_uid(db, &record.id_student).await {
uids.insert(uid);
} else if let Some(id_student) = record.id_student {
if let Some(uid) = account_id_get_uid(db, &id_student).await {
uids.insert(uid);
}
}
}

View file

@ -17,11 +17,12 @@ use tide::prelude::*;
#[derive(Debug, Deserialize, Serialize, sqlx::FromRow)]
pub struct AccountWolves {
pub id_wolves: String,
pub id_student: String,
pub id_member: String,
pub id_student: Option<String>,
pub email: String,
pub expiry: String,
pub name_first: String,
pub name_second: String,
pub name_first: Option<String>,
pub name_second: Option<String>,
}
#[derive(Debug, Clone, Deserialize, Serialize, sqlx::FromRow)]
@ -70,12 +71,14 @@ pub async fn db_init(config: &Config) -> Result<Pool<Sqlite>, Error> {
sqlx::query(
"CREATE TABLE IF NOT EXISTS accounts_wolves (
id_wolves text primary key,
id_student text not null,
id_wolves text DEFAULT '',
id_member text DEFAULT '',
id_student text,
email text not null,
expiry text not null,
name_first text not null,
name_second integer not null
name_first text,
name_second text,
PRIMARY KEY (id_wolves, id_member)
)",
)
.execute(&pool)

View file

@ -109,7 +109,10 @@ pub mod email {
// using https://github.com/lettre/lettre/blob/57886c367d69b4d66300b322c94bd910b1eca364/examples/maud_html.rs
fn send_mail(config: &Config, record: &AccountWolves, auth: &str) -> Result<smtp::response::Response, smtp::Error> {
let recipient = &record.name_first;
let recipient = match &record.name_first {
None => String::from("New User"),
Some(x) => x.to_owned(),
};
let mail = &record.email;
let url_base = "https://account.skynet.ie";
let link_new = format!("{url_base}/register?auth={auth}");

View file

@ -35,7 +35,8 @@ pub mod account {
#[derive(Debug, Deserialize, Serialize)]
pub struct DiscordResult {
discord: String,
wolves_id: String,
id_wolves: String,
id_member: String,
}
pub async fn get_discord_users(db: &Pool<Sqlite>) -> Vec<DiscordResult> {
@ -58,7 +59,8 @@ pub mod account {
if !accounts.is_empty() {
let tmp = DiscordResult {
discord,
wolves_id: accounts[0].id_wolves.to_owned(),
id_wolves: accounts[0].id_wolves.to_owned(),
id_member: accounts[0].id_member.to_owned(),
};
result.push(tmp);