use uuid::Uuid; use super::{ QueryConversionError, ReferenceRelation, ReferenceViolationKind, RepositoryError, dtos::Upsert, }; use crate::{ database::{Constraint, DatabaseError, dtos::UserAppSecretRow, user_app_secrets as db}, domain::UserAppSecret, repository::DecodeError, }; pub struct Query { pub app_id: Uuid, pub limit: Option, pub offset: Option, } pub async fn list<'e, E>(executor: E, query: Query) -> Result, RepositoryError> where E: sqlx::Executor<'e, Database = sqlx::Postgres>, { let query = db::Query { app_id: query.app_id, limit: query .limit .map(|limit| i64::try_from(limit).map_err(|_| QueryConversionError::Limit(limit))) .transpose()?, offset: query .offset .map(|offset| i64::try_from(offset).map_err(|_| QueryConversionError::Offset(offset))) .transpose()?, }; let secrets = db::list(executor, query) .await? .into_iter() .map(decode) .collect::, _>>()?; Ok(secrets) } pub async fn get_by_id<'e, E>( executor: E, app_id: Uuid, id: Uuid, ) -> Result, RepositoryError> where E: sqlx::Executor<'e, Database = sqlx::Postgres>, { let secret = db::get_by_id(executor, app_id, id) .await? .map(decode) .transpose()?; Ok(secret) } pub async fn upsert<'e, E>( executor: E, secret: UserAppSecret, ) -> Result, RepositoryError> where E: sqlx::Executor<'e, Database = sqlx::Postgres>, { let secret = db::upsert(executor, encode(secret)) .await .map_err(classify_upsert_error)? .try_map(decode)? .into(); Ok(secret) } fn classify_upsert_error(error: DatabaseError) -> RepositoryError { match &error { DatabaseError::ForeignKeyViolation { constraint: Constraint::UserAppSecretApp, .. } => RepositoryError::ReferenceViolation { relation: ReferenceRelation::UserApp, kind: ReferenceViolationKind::Missing, source: error, }, _ => RepositoryError::Database(error), } } pub async fn verify<'e, E>( executor: E, app_id: Uuid, secret_hash: &[u8], ) -> Result where E: sqlx::Executor<'e, Database = sqlx::Postgres>, { let result = db::verify(executor, app_id, secret_hash).await?; Ok(result) } pub async fn delete_by_id<'e, E>( executor: E, app_id: Uuid, id: Uuid, ) -> Result, RepositoryError> where E: sqlx::Executor<'e, Database = sqlx::Postgres>, { let secret = db::delete_by_id(executor, app_id, id) .await? .map(decode) .transpose()?; Ok(secret) } fn decode(row: UserAppSecretRow) -> Result { Ok(UserAppSecret::new( row.id, row.app_id, row.secret_hash, row.expires_at, row.created_at, )?) } fn encode(secret: UserAppSecret) -> UserAppSecretRow { UserAppSecretRow { id: secret.id(), app_id: secret.app_id(), secret_hash: secret.secret_hash().clone(), expires_at: secret.expires_at(), created_at: secret.created_at(), } } #[cfg(test)] mod tests { use chrono::{Duration, Utc}; use super::*; fn row() -> UserAppSecretRow { let created_at = Utc::now(); UserAppSecretRow { id: Uuid::new_v4(), app_id: Uuid::new_v4(), secret_hash: vec![0; 32], expires_at: created_at + Duration::days(1), created_at, } } fn foreign_key_violation(constraint: Constraint) -> DatabaseError { DatabaseError::ForeignKeyViolation { constraint, source: sqlx::Error::Protocol("test database cause".to_owned()), } } #[test] fn classifies_user_app_secret_upsert_foreign_key_violation() { let error = classify_upsert_error(foreign_key_violation(Constraint::UserAppSecretApp)); assert!(matches!( error, RepositoryError::ReferenceViolation { relation: ReferenceRelation::UserApp, kind: ReferenceViolationKind::Missing, .. } )); } #[test] fn keeps_unknown_upsert_foreign_keys_generic() { let error = classify_upsert_error(foreign_key_violation(Constraint::Unnamed)); assert!(matches!(error, RepositoryError::Database(_))); } #[test] fn round_trips_through_rows() { let secret = decode(row()).unwrap(); let round_trip = decode(encode(secret.clone())).unwrap(); assert_eq!(round_trip, secret); } }