feat: moved the timestamp function to lib

This commit is contained in:
silver 2023-07-30 01:32:01 +01:00
parent 4ce3e94c36
commit 72776a967a
4 changed files with 17 additions and 13 deletions

View file

@ -1,4 +1,3 @@
use chrono::{DateTime, SecondsFormat, Utc};
use lettre::{ use lettre::{
message::{header, MultiPart, SinglePart}, message::{header, MultiPart, SinglePart},
transport::smtp::{authentication::Credentials, response::Response}, transport::smtp::{authentication::Credentials, response::Response},
@ -6,14 +5,13 @@ use lettre::{
}; };
use maud::html; use maud::html;
use rand::{distributions::Alphanumeric, thread_rng, Rng}; 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}; use sqlx::{Pool, Sqlite};
#[async_std::main] #[async_std::main]
async fn main() { 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();
let now = Utc::now();
if let Ok(records) = read_csv(&config) { if let Ok(records) = read_csv(&config) {
for record in records { for record in records {
@ -26,7 +24,7 @@ async fn main() {
let auth = generate_auth(); let auth = generate_auth();
match send_mail(&config, &record, &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(_) => {} Ok(_) => {}
Err(e) => { Err(e) => {
println!("Unable to save to db {} {e:?}", &record.email); println!("Unable to save to db {} {e:?}", &record.email);
@ -164,7 +162,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>, now: DateTime<Utc>, record: &Record, auth: &str) -> Result<Option<AccountsNew>, sqlx::Error> { async fn save_to_db(db: &Pool<Sqlite>, record: &Record, 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) 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<Sqlite>, now: DateTime<Utc>, record: &Record, auth
) )
.bind(record.email.to_owned()) .bind(record.email.to_owned())
.bind(auth.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.expiry.to_owned())
.bind(record.name_first.to_owned()) .bind(record.name_first.to_owned())
.bind(record.name_second.to_owned()) .bind(record.name_second.to_owned())

View file

@ -1,7 +1,6 @@
use chrono::{Datelike, Utc};
use dotenvy::dotenv; use dotenvy::dotenv;
use ldap3::{LdapConn, Mod}; 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 sqlx::{Pool, Sqlite};
use std::{collections::HashSet, env, error::Error}; use std::{collections::HashSet, env, error::Error};
@ -132,12 +131,9 @@ async fn from_csv(config: &Config) -> Result<HashSet<String>, Box<dyn Error>> {
let records = read_csv(config)?; let records = read_csv(config)?;
let now = Utc::now();
let today = format!("{}-{:02}-{:02}", now.year(), now.month(), now.day());
for record in records { for record in records {
// only import users if it is actually active. // only import users if it is actually active.
if record.expiry < today { if record.expiry < get_now_iso(true) {
continue; continue;
} }

View file

@ -6,6 +6,7 @@ use sqlx::{Error, Pool, Sqlite};
use std::env; use std::env;
use std::str::FromStr; use std::str::FromStr;
use std::time::{SystemTime, UNIX_EPOCH}; use std::time::{SystemTime, UNIX_EPOCH};
use chrono::{Datelike, SecondsFormat, Utc};
use tide::prelude::*; use tide::prelude::*;
#[derive(Debug, Deserialize, Serialize, sqlx::FromRow)] #[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)] #[derive(Clone)]
pub struct State { pub struct State {
pub db: Pool<Sqlite>, pub db: Pool<Sqlite>,

View file

@ -150,7 +150,7 @@ async fn db_pending_clear_expired(pool: &Pool<Sqlite>) -> Result<Vec<AccountsPen
WHERE expiry < ? WHERE expiry < ?
"#, "#,
) )
.bind(now) .bind(get_now_iso(true))
.fetch_all(pool) .fetch_all(pool)
.await .await
} }