Files
expenses/server/src/repository/user_app_secrets.rs
T
2026-10-26 12:31:04 +01:00

174 lines
4.5 KiB
Rust

use uuid::Uuid;
use super::{
QueryConversionError, ReferenceRelation, ReferenceViolationKind, RepositoryError, dtos::Upsert,
};
use crate::{
database::{self, Constraint, DatabaseError, dtos::UserAppSecretRow},
domain::{HashedSecret, UserAppSecret},
repository::DecodeError,
};
pub struct Query {
pub limit: Option<u64>,
pub offset: Option<u64>,
}
pub async fn list<'e, E>(
executor: E,
user_id: Uuid,
app_id: Uuid,
query: Query,
) -> Result<Vec<UserAppSecret>, RepositoryError>
where
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
{
let query = database::user_app_secrets::Query {
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 = database::user_app_secrets::list(executor, user_id, app_id, query)
.await?
.into_iter()
.map(decode)
.collect::<Result<Vec<_>, _>>()?;
Ok(secrets)
}
pub async fn get_by_id<'e, E>(
executor: E,
user_id: Uuid,
app_id: Uuid,
id: Uuid,
) -> Result<Option<UserAppSecret>, RepositoryError>
where
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
{
let secret = database::user_app_secrets::get_by_id(executor, user_id, app_id, id)
.await?
.map(decode)
.transpose()?;
Ok(secret)
}
pub async fn upsert<'e, E>(
executor: E,
user_id: Uuid,
secret: UserAppSecret,
) -> Result<Option<Upsert<UserAppSecret>>, RepositoryError>
where
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
{
let secret = database::user_app_secrets::upsert(executor, user_id, encode(secret))
.await
.map_err(classify_upsert_error)?
.map(|upserted| upserted.try_map(decode))
.transpose()?
.map(Into::into);
Ok(secret)
}
pub async fn verify<'e, E>(
executor: E,
app_id: Uuid,
secret_hash: &HashedSecret,
) -> Result<bool, RepositoryError>
where
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
{
let is_valid = database::user_app_secrets::verify(executor, app_id, secret_hash.hash()).await?;
Ok(is_valid)
}
pub async fn delete_by_id<'e, E>(
executor: E,
user_id: Uuid,
app_id: Uuid,
id: Uuid,
) -> Result<Option<UserAppSecret>, RepositoryError>
where
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
{
let secret = database::user_app_secrets::delete_by_id(executor, user_id, app_id, id)
.await?
.map(decode)
.transpose()?;
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),
}
}
fn decode(row: UserAppSecretRow) -> Result<UserAppSecret, DecodeError> {
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 super::*;
#[test]
fn classifies_upsert_error_app_foreign_key_violation() {
let error = classify_upsert_error(DatabaseError::ForeignKeyViolation {
constraint: Constraint::UserAppSecretApp,
source: sqlx::Error::Protocol("test database cause".to_owned()),
});
assert!(matches!(
error,
RepositoryError::ReferenceViolation {
relation: ReferenceRelation::UserApp,
kind: ReferenceViolationKind::Missing,
..
}
));
}
#[test]
fn classifies_upsert_error_unknown_foreign_key_violation() {
let error = classify_upsert_error(DatabaseError::ForeignKeyViolation {
constraint: Constraint::Unnamed,
source: sqlx::Error::Protocol("test database cause".to_owned()),
});
assert!(matches!(error, RepositoryError::Database(_)));
}
}