diff --git a/Cargo.lock b/Cargo.lock index 786caec730..737cee80d9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2329,6 +2329,8 @@ dependencies = [ name = "enums" version = "0.0.1" dependencies = [ + "chrono", + "errors", "my-workspace-hack", "serde", "serde_derive", diff --git a/crates/_types/enums/Cargo.toml b/crates/_types/enums/Cargo.toml index 8825c821fb..9819ea0cf4 100644 --- a/crates/_types/enums/Cargo.toml +++ b/crates/_types/enums/Cargo.toml @@ -39,3 +39,7 @@ strum = { version = "0.26", features = ["derive"] } # Test serialization serde_json = "1.0.108" toml = "0.8.14" + +# Temporary for a test +errors = { path = "../../lib/errors" } +chrono = { version = "0.4.22", features = ["serde"] } diff --git a/crates/_types/enums/tests/list_available_api_tokens_for_user.rs b/crates/_types/enums/tests/list_available_api_tokens_for_user.rs new file mode 100644 index 0000000000..8d79dbae8d --- /dev/null +++ b/crates/_types/enums/tests/list_available_api_tokens_for_user.rs @@ -0,0 +1,58 @@ +use chrono::Utc; +use sqlx::MySqlPool; + +use errors::AnyhowResult; + +#[test] +fn test_that_this_compiles() { + // This test only exists as an integration test with the package. + // When we do upgrades of SQLx, the binding between sqlx, our macros, + // and our code can break down. Having this thin use case helps us + // deal with migration difficulties. + // + // Unfortunately this code requires access to a database or cached queries to compile, so + // we may want to rethink this strategy in the future. + assert_eq!(1, 1); +} + +/// Query available (non-deleted) API tokens for a user. +/// A user can only have five active tokens at a time, so we only return the five most recent. +pub async fn list_available_api_tokens_for_user( + user_token: &str, + pool: &MySqlPool, +) -> AnyhowResult<()> { + + let _records : Vec = sqlx::query_as!( + AvailableApiTokenInternal, + r#" +SELECT + true as testing, + internal_token + -- api_token + -- maybe_short_description + -- created_at, + -- updated_at +FROM api_tokens +WHERE + user_token = ? + AND deleted_at IS NULL +ORDER BY created_at DESC +LIMIT 5 + "#, + user_token, + ) + .fetch_all(pool) + .await?; + + Ok(()) +} + +#[derive(Debug)] +struct AvailableApiTokenInternal { + pub testing: i64, + pub internal_token: String, + //pub api_token: String, + //pub maybe_short_description: Option, + //pub created_at: chrono::DateTime, + //pub updated_at: chrono::DateTime, +}