feat: added the function to get teh data and updated teh times tehy run at #35

Merged
silver merged 6 commits from #10_wolves_database into main 2023-08-05 21:36:30 +00:00
4 changed files with 64 additions and 59 deletions
Showing only changes of commit 58bf1e80fd - Show all commits

View file

@ -4,7 +4,7 @@ use lettre::{
Message, SmtpTransport, Transport, Message, SmtpTransport, Transport,
}; };
use maud::html; use maud::html;
use skynet_ldap_backend::{db_init, get_config, get_now_iso, random_string, read_csv, Accounts, AccountsNew, Config, Record}; use skynet_ldap_backend::{db_init, get_config, get_now_iso, get_wolves, random_string, AccountWolves, Accounts, AccountsNew, Config};
use sqlx::{Pool, Sqlite}; use sqlx::{Pool, Sqlite};
#[async_std::main] #[async_std::main]
@ -12,31 +12,29 @@ async fn main() {
let config = get_config(); let config = get_config();
let db = db_init(&config).await.unwrap(); let db = db_init(&config).await.unwrap();
if let Ok(records) = read_csv(&config) { for record in get_wolves(&db).await {
for record in records { // skynet emails not permitted
// skynet emails not permitted if record.email.trim().ends_with("@skynet.ie") {
if record.email.trim().ends_with("@skynet.ie") { continue;
continue; }
}
// check if the email is already in the db // check if the email is already in the db
if !check(&db, &record.email).await { if !check(&db, &record.email).await {
continue; continue;
} }
// generate a auth key // generate a auth key
let auth = random_string(50); let auth = random_string(50);
match send_mail(&config, &record, &auth) { match send_mail(&config, &record, &auth) {
Ok(_) => match save_to_db(&db, &record, &auth).await { Ok(_) => match save_to_db(&db, &record, &auth).await {
Ok(_) => {} Ok(_) => {}
Err(e) => {
println!("Unable to save to db {} {e:?}", &record.email);
}
},
Err(e) => { Err(e) => {
println!("Unable to send mail to {} {e:?}", &record.email); println!("Unable to save to db {} {e:?}", &record.email);
} }
},
Err(e) => {
println!("Unable to send mail to {} {e:?}", &record.email);
} }
} }
} }
@ -75,7 +73,7 @@ async fn check_pending(db: &Pool<Sqlite>, mail: &str) -> bool {
} }
// using https://github.com/lettre/lettre/blob/57886c367d69b4d66300b322c94bd910b1eca364/examples/maud_html.rs // using https://github.com/lettre/lettre/blob/57886c367d69b4d66300b322c94bd910b1eca364/examples/maud_html.rs
fn send_mail(config: &Config, record: &Record, auth: &str) -> Result<Response, lettre::transport::smtp::Error> { fn send_mail(config: &Config, record: &AccountWolves, auth: &str) -> Result<Response, lettre::transport::smtp::Error> {
let recipient = &record.name_first; let recipient = &record.name_first;
let mail = &record.email; let mail = &record.email;
let url_base = "https://sso.skynet.ie"; let url_base = "https://sso.skynet.ie";
@ -180,7 +178,7 @@ fn send_mail(config: &Config, record: &Record, auth: &str) -> Result<Response, l
mailer.send(&email) mailer.send(&email)
} }
async fn save_to_db(db: &Pool<Sqlite>, record: &Record, auth: &str) -> Result<Option<AccountsNew>, sqlx::Error> { async fn save_to_db(db: &Pool<Sqlite>, record: &AccountWolves, auth: &str) -> Result<Option<AccountsNew>, sqlx::Error> {
sqlx::query_as::<_, AccountsNew>( sqlx::query_as::<_, AccountsNew>(
" "
INSERT OR REPLACE INTO accounts_new (mail, auth_code, date_iso, date_expiry, name_first, name_surname, id_student) INSERT OR REPLACE INTO accounts_new (mail, auth_code, date_iso, date_expiry, name_first, name_surname, id_student)

View file

@ -1,5 +1,5 @@
use ldap3::{LdapConn, Mod}; use ldap3::{LdapConn, Mod};
use skynet_ldap_backend::{db_init, get_config, get_now_iso, read_csv, Accounts, Config}; use skynet_ldap_backend::{db_init, get_config, get_now_iso, get_wolves, Accounts, Config};
use sqlx::{Pool, Sqlite}; use sqlx::{Pool, Sqlite};
use std::{collections::HashSet, env, error::Error}; use std::{collections::HashSet, env, error::Error};
@ -116,9 +116,7 @@ async fn from_csv(config: &Config) -> Result<HashSet<String>, Box<dyn Error>> {
let mut uids = HashSet::new(); let mut uids = HashSet::new();
let records = read_csv(config)?; for record in get_wolves(&db).await {
for record in records {
// only import users if it is actually active. // only import users if it is actually active.
if record.expiry < get_now_iso(true) { if record.expiry < get_now_iso(true) {
continue; continue;

View file

@ -1,20 +1,19 @@
use skynet_ldap_backend::{db_init, get_config, Config}; use skynet_ldap_backend::{db_init, get_config, AccountWolves, Config};
use sqlx::{Pool, Sqlite}; use sqlx::{Pool, Sqlite};
use tide::prelude::{Serialize, Deserialize};
#[async_std::main] #[async_std::main]
async fn main() -> tide::Result<()> { async fn main() -> tide::Result<()> {
let config = get_config(); let config = get_config();
let db = db_init(&config).await.unwrap(); let db = db_init(&config).await.unwrap();
let mut records = vec![]; let mut records = vec![];
if let Ok(accounts) = get_csv(&config){ if let Ok(accounts) = get_csv(&config) {
for account in accounts { for account in accounts {
records.push(AccountWolves::from(account)); records.push(AccountWolves::from(account));
} }
} }
for account in records { for account in records {
update_account(&db, &account).await; update_account(&db, &account).await;
} }
@ -22,16 +21,6 @@ async fn main() -> tide::Result<()> {
Ok(()) Ok(())
} }
#[derive(Debug, Deserialize, Serialize, sqlx::FromRow)]
pub struct AccountWolves {
pub id_wolves: String,
pub id_student: String,
pub email: String,
pub expiry: String,
pub name_first: String,
pub name_second: String,
}
#[derive(Debug, serde::Deserialize)] #[derive(Debug, serde::Deserialize)]
struct RecordCSV { struct RecordCSV {
#[serde(rename = "MemID")] #[serde(rename = "MemID")]
@ -49,7 +38,7 @@ struct RecordCSV {
} }
impl From<RecordCSV> for AccountWolves { impl From<RecordCSV> for AccountWolves {
fn from(input: RecordCSV) -> Self { fn from(input: RecordCSV) -> Self {
AccountWolves{ AccountWolves {
id_wolves: input.mem_id, id_wolves: input.mem_id,
id_student: input.id_student, id_student: input.id_student,
email: input.email, email: input.email,
@ -78,21 +67,20 @@ fn get_csv(config: &Config) -> Result<Vec<RecordCSV>, Box<dyn std::error::Error>
Ok(records) Ok(records)
} }
async fn update_account(db: &Pool<Sqlite>, account: &AccountWolves) {
async fn update_account(db: &Pool<Sqlite>, account: &AccountWolves){
sqlx::query_as::<_, AccountWolves>( sqlx::query_as::<_, AccountWolves>(
" "
INSERT OR REPLACE INTO accounts_wolves (id_wolves, id_student, email, expiry, name_first, name_second) INSERT OR REPLACE INTO accounts_wolves (id_wolves, id_student, email, expiry, name_first, name_second)
VALUES (?1, ?2, ?3, ?4, ?5, ?6) VALUES (?1, ?2, ?3, ?4, ?5, ?6)
", ",
) )
.bind(&account.id_wolves) .bind(&account.id_wolves)
.bind(&account.id_student) .bind(&account.id_student)
.bind(&account.email) .bind(&account.email)
.bind(&account.expiry) .bind(&account.expiry)
.bind(&account.name_first) .bind(&account.name_first)
.bind(&account.name_second) .bind(&account.name_second)
.fetch_optional(db) .fetch_optional(db)
.await .await
.ok(); .ok();
} }

View file

@ -14,6 +14,16 @@ use std::{
}; };
use tide::prelude::*; use tide::prelude::*;
#[derive(Debug, Deserialize, Serialize, sqlx::FromRow)]
pub struct AccountWolves {
pub id_wolves: String,
pub id_student: String,
pub email: String,
pub expiry: String,
pub name_first: String,
pub name_second: String,
}
#[derive(Debug, Clone, Deserialize, Serialize, sqlx::FromRow)] #[derive(Debug, Clone, Deserialize, Serialize, sqlx::FromRow)]
pub struct AccountsNew { pub struct AccountsNew {
pub mail: String, pub mail: String,
@ -54,10 +64,9 @@ pub async fn db_init(config: &Config) -> Result<Pool<Sqlite>, Error> {
name_surname integer not null name_surname integer not null
)", )",
) )
.execute(&pool) .execute(&pool)
.await?; .await?;
sqlx::query( sqlx::query(
"CREATE TABLE IF NOT EXISTS accounts_new ( "CREATE TABLE IF NOT EXISTS accounts_new (
mail text primary key, mail text primary key,
@ -264,3 +273,15 @@ async fn update_accounts(pool: &Pool<Sqlite>, config: &Config) {
pub fn random_string(len: usize) -> String { pub fn random_string(len: usize) -> String {
thread_rng().sample_iter(&Alphanumeric).take(len).map(char::from).collect() thread_rng().sample_iter(&Alphanumeric).take(len).map(char::from).collect()
} }
pub async fn get_wolves(db: &Pool<Sqlite>) -> Vec<AccountWolves> {
sqlx::query_as::<_, AccountWolves>(
r#"
SELECT *
FROM accounts_wolves
"#,
)
.fetch_all(db)
.await
.unwrap_or(vec![])
}