diff --git a/src/bin/new_users.rs b/src/bin/new_users.rs index 9e887a0..54b7ac4 100644 --- a/src/bin/new_users.rs +++ b/src/bin/new_users.rs @@ -1,4 +1,3 @@ -use chrono::{DateTime, SecondsFormat, Utc}; use lettre::{ message::{header, MultiPart, SinglePart}, transport::smtp::{authentication::Credentials, response::Response}, @@ -6,14 +5,13 @@ use lettre::{ }; use maud::html; use rand::{distributions::Alphanumeric, thread_rng, Rng}; -use skynet_ldap_backend::{db_init, get_config, read_csv, Accounts, AccountsNew, Config, Record}; +use skynet_ldap_backend::{db_init, get_config, read_csv, Accounts, AccountsNew, Config, Record, get_now_iso}; use sqlx::{Pool, Sqlite}; #[async_std::main] async fn main() { let config = get_config(); let db = db_init(&config).await.unwrap(); - let now = Utc::now(); if let Ok(records) = read_csv(&config) { for record in records { @@ -26,7 +24,7 @@ async fn main() { let auth = generate_auth(); match send_mail(&config, &record, &auth) { - Ok(_) => match save_to_db(&db, now, &record, &auth).await { + Ok(_) => match save_to_db(&db, &record, &auth).await { Ok(_) => {} Err(e) => { println!("Unable to save to db {} {e:?}", &record.email); @@ -164,7 +162,7 @@ fn send_mail(config: &Config, record: &Record, auth: &str) -> Result, now: DateTime, record: &Record, auth: &str) -> Result, sqlx::Error> { +async fn save_to_db(db: &Pool, record: &Record, auth: &str) -> Result, sqlx::Error> { sqlx::query_as::<_, AccountsNew>( " INSERT OR REPLACE INTO accounts_new (mail, auth_code, date_iso, date_expiry, name_first, name_surname) @@ -173,7 +171,7 @@ async fn save_to_db(db: &Pool, now: DateTime, record: &Record, auth ) .bind(record.email.to_owned()) .bind(auth.to_owned()) - .bind(now.to_rfc3339_opts(SecondsFormat::Millis, true)) + .bind(get_now_iso(false)) .bind(record.expiry.to_owned()) .bind(record.name_first.to_owned()) .bind(record.name_second.to_owned()) diff --git a/src/bin/update_groups.rs b/src/bin/update_groups.rs index 61983dc..275c329 100644 --- a/src/bin/update_groups.rs +++ b/src/bin/update_groups.rs @@ -1,7 +1,6 @@ -use chrono::{Datelike, Utc}; use dotenvy::dotenv; use ldap3::{LdapConn, Mod}; -use skynet_ldap_backend::{db_init, get_config, read_csv, Accounts, Config}; +use skynet_ldap_backend::{db_init, get_config, read_csv, Accounts, Config, get_now_iso}; use sqlx::{Pool, Sqlite}; use std::{collections::HashSet, env, error::Error}; @@ -132,12 +131,9 @@ async fn from_csv(config: &Config) -> Result, Box> { let records = read_csv(config)?; - let now = Utc::now(); - let today = format!("{}-{:02}-{:02}", now.year(), now.month(), now.day()); - for record in records { // only import users if it is actually active. - if record.expiry < today { + if record.expiry < get_now_iso(true) { continue; } diff --git a/src/lib.rs b/src/lib.rs index e395452..f363e03 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -6,6 +6,7 @@ use sqlx::{Error, Pool, Sqlite}; use std::env; use std::str::FromStr; use std::time::{SystemTime, UNIX_EPOCH}; +use chrono::{Datelike, SecondsFormat, Utc}; use tide::prelude::*; #[derive(Debug, Deserialize, Serialize, sqlx::FromRow)] @@ -111,6 +112,15 @@ pub fn get_now() -> i64 { } } +pub fn get_now_iso(short: bool) -> String { + let now = Utc::now(); + if short { + format!("{}-{:02}-{:02}", now.year(), now.month(), now.day()) + } else { + now.to_rfc3339_opts(SecondsFormat::Millis, true) + } +} + #[derive(Clone)] pub struct State { pub db: Pool, diff --git a/src/methods/account_new.rs b/src/methods/account_new.rs index c4899d0..e1c850d 100644 --- a/src/methods/account_new.rs +++ b/src/methods/account_new.rs @@ -150,7 +150,7 @@ async fn db_pending_clear_expired(pool: &Pool) -> Result