Propagate user app ownership conflict.
This commit is contained in:
@@ -12,6 +12,22 @@ impl<T> Upsert<T> {
|
||||
}
|
||||
}
|
||||
|
||||
pub enum GuardedUpsert<T> {
|
||||
Created(T),
|
||||
Updated(T),
|
||||
Rejected,
|
||||
}
|
||||
|
||||
impl<T> GuardedUpsert<T> {
|
||||
pub fn try_map<U, E>(self, f: impl FnOnce(T) -> Result<U, E>) -> Result<GuardedUpsert<U>, E> {
|
||||
Ok(match self {
|
||||
Self::Created(value) => GuardedUpsert::Created(f(value)?),
|
||||
Self::Updated(value) => GuardedUpsert::Updated(f(value)?),
|
||||
Self::Rejected => GuardedUpsert::Rejected,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub enum Delete {
|
||||
Deleted,
|
||||
NotFound,
|
||||
|
||||
@@ -2,7 +2,7 @@ use uuid::Uuid;
|
||||
|
||||
use super::{
|
||||
DatabaseError,
|
||||
dtos::{Delete, Upsert, UserAppRow},
|
||||
dtos::{Delete, GuardedUpsert, UserAppRow},
|
||||
};
|
||||
|
||||
pub struct Query {
|
||||
@@ -60,7 +60,7 @@ where
|
||||
pub async fn upsert<'e, E>(
|
||||
executor: E,
|
||||
app: UserAppRow,
|
||||
) -> Result<Upsert<UserAppRow>, DatabaseError>
|
||||
) -> Result<GuardedUpsert<UserAppRow>, DatabaseError>
|
||||
where
|
||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||
{
|
||||
@@ -70,6 +70,7 @@ where
|
||||
VALUES ($1, $2)
|
||||
ON CONFLICT (id) DO UPDATE SET
|
||||
id = EXCLUDED.id
|
||||
WHERE user_apps.user_id = $2
|
||||
RETURNING WITH (OLD AS old_row, NEW AS new_row)
|
||||
old_row.id AS "old_id?",
|
||||
new_row.id AS "id!",
|
||||
@@ -78,19 +79,23 @@ where
|
||||
app.id,
|
||||
app.user_id,
|
||||
)
|
||||
.fetch_one(executor)
|
||||
.fetch_optional(executor)
|
||||
.await
|
||||
.map_err(DatabaseError::from)?;
|
||||
|
||||
let Some(row) = row else {
|
||||
return Ok(GuardedUpsert::Rejected);
|
||||
};
|
||||
|
||||
let app = UserAppRow {
|
||||
id: row.id,
|
||||
user_id: row.user_id,
|
||||
};
|
||||
|
||||
Ok(if row.old_id.is_some() {
|
||||
Upsert::Updated(app)
|
||||
GuardedUpsert::Updated(app)
|
||||
} else {
|
||||
Upsert::Created(app)
|
||||
GuardedUpsert::Created(app)
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -50,6 +50,15 @@ impl IntoResponse for AppError {
|
||||
Self::Repository(RepositoryError::Decode(error)) => {
|
||||
tracing::error!(%error, "invalid stored data");
|
||||
}
|
||||
Self::Repository(RepositoryError::OwnershipConflict {
|
||||
relation: ReferenceRelation::UserApp,
|
||||
}) => {
|
||||
tracing::warn!("user app ownership conflict");
|
||||
return error_response(StatusCode::CONFLICT, "User app belongs to another user");
|
||||
}
|
||||
Self::Repository(RepositoryError::OwnershipConflict { relation }) => {
|
||||
tracing::error!(%relation, "unhandled ownership conflict");
|
||||
}
|
||||
Self::Repository(RepositoryError::ReferenceViolation {
|
||||
relation: ReferenceRelation::User,
|
||||
kind: ReferenceViolationKind::Missing,
|
||||
|
||||
@@ -21,6 +21,9 @@ pub enum RepositoryError {
|
||||
#[error(transparent)]
|
||||
Decode(#[from] DecodeError),
|
||||
|
||||
#[error("{relation} is owned by another user")]
|
||||
OwnershipConflict { relation: ReferenceRelation },
|
||||
|
||||
#[error("reference violation ({kind}: {relation})")]
|
||||
ReferenceViolation {
|
||||
relation: ReferenceRelation,
|
||||
|
||||
@@ -1,8 +1,12 @@
|
||||
use uuid::Uuid;
|
||||
|
||||
use super::{QueryConversionError, RepositoryError, dtos::Upsert};
|
||||
use super::{QueryConversionError, ReferenceRelation, RepositoryError, dtos::Upsert};
|
||||
use crate::{
|
||||
database::{Constraint, DatabaseError, dtos::UserAppRow, user_apps as db},
|
||||
database::{
|
||||
Constraint, DatabaseError,
|
||||
dtos::{GuardedUpsert, UserAppRow},
|
||||
user_apps as db,
|
||||
},
|
||||
domain::UserApp,
|
||||
repository::DecodeError,
|
||||
};
|
||||
@@ -58,13 +62,23 @@ pub async fn upsert<'e, E>(executor: E, app: UserApp) -> Result<Upsert<UserApp>,
|
||||
where
|
||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||
{
|
||||
let app = db::upsert(executor, encode(app))
|
||||
let outcome = db::upsert(executor, encode(app))
|
||||
.await
|
||||
.map_err(classify_upsert_error)?
|
||||
.try_map(decode)?
|
||||
.into();
|
||||
.map_err(classify_upsert_error)?;
|
||||
|
||||
Ok(app)
|
||||
map_upsert(outcome)
|
||||
}
|
||||
|
||||
fn map_upsert(outcome: GuardedUpsert<UserAppRow>) -> Result<Upsert<UserApp>, RepositoryError> {
|
||||
Ok(match outcome.try_map(decode)? {
|
||||
GuardedUpsert::Created(app) => Upsert::Created(app),
|
||||
GuardedUpsert::Updated(app) => Upsert::Updated(app),
|
||||
GuardedUpsert::Rejected => {
|
||||
return Err(RepositoryError::OwnershipConflict {
|
||||
relation: ReferenceRelation::UserApp,
|
||||
});
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
fn classify_upsert_error(error: DatabaseError) -> RepositoryError {
|
||||
@@ -138,6 +152,41 @@ mod tests {
|
||||
assert!(matches!(error, RepositoryError::Database(_)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn maps_created_upsert() {
|
||||
let row = row();
|
||||
let id = row.id;
|
||||
|
||||
let Ok(Upsert::Created(app)) = map_upsert(GuardedUpsert::Created(row)) else {
|
||||
panic!("expected created upsert");
|
||||
};
|
||||
assert_eq!(app.id, id);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn maps_updated_upsert() {
|
||||
let row = row();
|
||||
let id = row.id;
|
||||
|
||||
let Ok(Upsert::Updated(app)) = map_upsert(GuardedUpsert::Updated(row)) else {
|
||||
panic!("expected updated upsert");
|
||||
};
|
||||
assert_eq!(app.id, id);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn maps_rejected_upsert_to_ownership_conflict() {
|
||||
let Err(error) = map_upsert(GuardedUpsert::Rejected) else {
|
||||
panic!("expected ownership conflict");
|
||||
};
|
||||
assert!(matches!(
|
||||
error,
|
||||
RepositoryError::OwnershipConflict {
|
||||
relation: ReferenceRelation::UserApp,
|
||||
}
|
||||
));
|
||||
}
|
||||
|
||||
fn row() -> UserAppRow {
|
||||
UserAppRow {
|
||||
id: Uuid::new_v4(),
|
||||
|
||||
Reference in New Issue
Block a user