Check account membership on DB level.
This commit is contained in:
+3
-2
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"db_name": "PostgreSQL",
|
"db_name": "PostgreSQL",
|
||||||
"query": "\n SELECT id, account_id, user_id\n FROM account_members\n WHERE account_id = $1\n ORDER BY id\n LIMIT $2\n OFFSET $3\n ",
|
"query": "\n SELECT id, account_id, user_id\n FROM account_members\n WHERE account_id IN (SELECT id FROM accessible_accounts($1))\n AND account_id = $2\n ORDER BY id\n LIMIT $3\n OFFSET $4\n ",
|
||||||
"describe": {
|
"describe": {
|
||||||
"columns": [
|
"columns": [
|
||||||
{
|
{
|
||||||
@@ -39,6 +39,7 @@
|
|||||||
],
|
],
|
||||||
"parameters": {
|
"parameters": {
|
||||||
"Left": [
|
"Left": [
|
||||||
|
"Uuid",
|
||||||
"Uuid",
|
"Uuid",
|
||||||
"Int8",
|
"Int8",
|
||||||
"Int8"
|
"Int8"
|
||||||
@@ -50,5 +51,5 @@
|
|||||||
false
|
false
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"hash": "d4855797f718e952505eff69420fe7884714c2e6f5f10215c38c2a5571bbcd9a"
|
"hash": "88aa0cea999dcc4ddb0217535b7b3aa13e5d64a0914b2eab27a71c12c37043e9"
|
||||||
}
|
}
|
||||||
+3
-2
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"db_name": "PostgreSQL",
|
"db_name": "PostgreSQL",
|
||||||
"query": "\n DELETE FROM account_members\n WHERE account_id = $1 AND id = $2\n RETURNING id, account_id, user_id\n ",
|
"query": "\n DELETE FROM account_members\n WHERE account_id IN (SELECT id FROM accessible_accounts($1))\n AND account_id = $2\n AND id = $3\n RETURNING id, account_id, user_id\n ",
|
||||||
"describe": {
|
"describe": {
|
||||||
"columns": [
|
"columns": [
|
||||||
{
|
{
|
||||||
@@ -39,6 +39,7 @@
|
|||||||
],
|
],
|
||||||
"parameters": {
|
"parameters": {
|
||||||
"Left": [
|
"Left": [
|
||||||
|
"Uuid",
|
||||||
"Uuid",
|
"Uuid",
|
||||||
"Uuid"
|
"Uuid"
|
||||||
]
|
]
|
||||||
@@ -49,5 +50,5 @@
|
|||||||
false
|
false
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"hash": "e2bb42012ebde4620c1a6a3b7802d3b07ea759310f7f20feb9929d87509c4c34"
|
"hash": "890fe7a4664375bbe9095f5ecf1d629e87fb8c84450fae7ba1fd3b145ffbcf0a"
|
||||||
}
|
}
|
||||||
+3
-2
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"db_name": "PostgreSQL",
|
"db_name": "PostgreSQL",
|
||||||
"query": "\n INSERT INTO account_members (id, account_id, user_id)\n VALUES ($1, $2, $3)\n ON CONFLICT (id, account_id) DO UPDATE SET\n user_id = EXCLUDED.user_id\n RETURNING WITH (OLD AS old_row, NEW AS new_row)\n old_row.id AS \"old_id?\",\n COALESCE(old_row.account_id, new_row.account_id) AS \"old_account_id!\",\n COALESCE(old_row.user_id, new_row.user_id) AS \"old_user_id!\",\n new_row.id AS \"id!\",\n new_row.account_id AS \"account_id!\",\n new_row.user_id AS \"user_id!\"\n ",
|
"query": "\n INSERT INTO account_members (id, account_id, user_id)\n SELECT $2, $3, $4\n FROM accessible_accounts($1) AS accounts\n WHERE accounts.id = $3\n ON CONFLICT (id, account_id) DO UPDATE SET\n user_id = EXCLUDED.user_id\n RETURNING WITH (OLD AS old_row, NEW AS new_row)\n old_row.id AS \"old_id?\",\n COALESCE(old_row.account_id, new_row.account_id) AS \"old_account_id!\",\n COALESCE(old_row.user_id, new_row.user_id) AS \"old_user_id!\",\n new_row.id AS \"id!\",\n new_row.account_id AS \"account_id!\",\n new_row.user_id AS \"user_id!\"\n ",
|
||||||
"describe": {
|
"describe": {
|
||||||
"columns": [
|
"columns": [
|
||||||
{
|
{
|
||||||
@@ -62,6 +62,7 @@
|
|||||||
],
|
],
|
||||||
"parameters": {
|
"parameters": {
|
||||||
"Left": [
|
"Left": [
|
||||||
|
"Uuid",
|
||||||
"Uuid",
|
"Uuid",
|
||||||
"Uuid",
|
"Uuid",
|
||||||
"Uuid"
|
"Uuid"
|
||||||
@@ -76,5 +77,5 @@
|
|||||||
false
|
false
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"hash": "0782e5945f71e1541ead35a500ce23d6241853c39b99a575b75b3795b54493d9"
|
"hash": "9de36a6a53617c842abadaee6e54796d3c632e5b11d001de507b9039664fce38"
|
||||||
}
|
}
|
||||||
+54
@@ -0,0 +1,54 @@
|
|||||||
|
{
|
||||||
|
"db_name": "PostgreSQL",
|
||||||
|
"query": "\n INSERT INTO account_members (id, account_id, user_id)\n VALUES ($1, $2, $3)\n RETURNING id, account_id, user_id\n ",
|
||||||
|
"describe": {
|
||||||
|
"columns": [
|
||||||
|
{
|
||||||
|
"ordinal": 0,
|
||||||
|
"name": "id",
|
||||||
|
"type_info": "Uuid",
|
||||||
|
"origin": {
|
||||||
|
"Table": {
|
||||||
|
"table": "account_members",
|
||||||
|
"name": "id"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ordinal": 1,
|
||||||
|
"name": "account_id",
|
||||||
|
"type_info": "Uuid",
|
||||||
|
"origin": {
|
||||||
|
"Table": {
|
||||||
|
"table": "account_members",
|
||||||
|
"name": "account_id"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ordinal": 2,
|
||||||
|
"name": "user_id",
|
||||||
|
"type_info": "Uuid",
|
||||||
|
"origin": {
|
||||||
|
"Table": {
|
||||||
|
"table": "account_members",
|
||||||
|
"name": "user_id"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"parameters": {
|
||||||
|
"Left": [
|
||||||
|
"Uuid",
|
||||||
|
"Uuid",
|
||||||
|
"Uuid"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"nullable": [
|
||||||
|
false,
|
||||||
|
false,
|
||||||
|
false
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"hash": "a39efb08903f954f52d1f70d8f8cbc4450ff9c1e11f9d6be51a6b29b7f35594e"
|
||||||
|
}
|
||||||
+41
@@ -0,0 +1,41 @@
|
|||||||
|
{
|
||||||
|
"db_name": "PostgreSQL",
|
||||||
|
"query": "\n\t\tSELECT id, name\n\t\tFROM accounts\n\t\tWHERE id = $2\n\t\tAND id IN (SELECT id FROM accessible_accounts($1))\n\t\tFOR NO KEY UPDATE\n\t\t",
|
||||||
|
"describe": {
|
||||||
|
"columns": [
|
||||||
|
{
|
||||||
|
"ordinal": 0,
|
||||||
|
"name": "id",
|
||||||
|
"type_info": "Uuid",
|
||||||
|
"origin": {
|
||||||
|
"Table": {
|
||||||
|
"table": "accounts",
|
||||||
|
"name": "id"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ordinal": 1,
|
||||||
|
"name": "name",
|
||||||
|
"type_info": "Text",
|
||||||
|
"origin": {
|
||||||
|
"Table": {
|
||||||
|
"table": "accounts",
|
||||||
|
"name": "name"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"parameters": {
|
||||||
|
"Left": [
|
||||||
|
"Uuid",
|
||||||
|
"Uuid"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"nullable": [
|
||||||
|
false,
|
||||||
|
false
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"hash": "ccf33164a136215b0ef86da0ed5221088a3fc976be89c502abb63570e0c2380c"
|
||||||
|
}
|
||||||
+3
-2
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"db_name": "PostgreSQL",
|
"db_name": "PostgreSQL",
|
||||||
"query": "\n SELECT id, account_id, user_id\n FROM account_members\n WHERE account_id = $1 AND id = $2\n ",
|
"query": "\n SELECT id, account_id, user_id\n FROM account_members\n WHERE account_id IN (SELECT id FROM accessible_accounts($1))\n AND account_id = $2\n AND id = $3\n ",
|
||||||
"describe": {
|
"describe": {
|
||||||
"columns": [
|
"columns": [
|
||||||
{
|
{
|
||||||
@@ -39,6 +39,7 @@
|
|||||||
],
|
],
|
||||||
"parameters": {
|
"parameters": {
|
||||||
"Left": [
|
"Left": [
|
||||||
|
"Uuid",
|
||||||
"Uuid",
|
"Uuid",
|
||||||
"Uuid"
|
"Uuid"
|
||||||
]
|
]
|
||||||
@@ -49,5 +50,5 @@
|
|||||||
false
|
false
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"hash": "fec880b0ff2fddb5e5a835abed62e7e85e8c7838740a6a41cb50a677c8bcdd11"
|
"hash": "d5a336eff7f8927f93074d8b8524e7232fed59f715420b864b11938c05f65560"
|
||||||
}
|
}
|
||||||
@@ -11,7 +11,11 @@ pub struct Query {
|
|||||||
pub offset: Option<i64>,
|
pub offset: Option<i64>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn list<'e, E>(executor: E, query: Query) -> Result<Vec<AccountMemberRow>, DatabaseError>
|
pub async fn list<'e, E>(
|
||||||
|
executor: E,
|
||||||
|
user_id: Uuid,
|
||||||
|
query: Query,
|
||||||
|
) -> Result<Vec<AccountMemberRow>, DatabaseError>
|
||||||
where
|
where
|
||||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||||
{
|
{
|
||||||
@@ -20,11 +24,13 @@ where
|
|||||||
r#"
|
r#"
|
||||||
SELECT id, account_id, user_id
|
SELECT id, account_id, user_id
|
||||||
FROM account_members
|
FROM account_members
|
||||||
WHERE account_id = $1
|
WHERE account_id IN (SELECT id FROM accessible_accounts($1))
|
||||||
|
AND account_id = $2
|
||||||
ORDER BY id
|
ORDER BY id
|
||||||
LIMIT $2
|
LIMIT $3
|
||||||
OFFSET $3
|
OFFSET $4
|
||||||
"#,
|
"#,
|
||||||
|
user_id,
|
||||||
query.account_id,
|
query.account_id,
|
||||||
query.limit,
|
query.limit,
|
||||||
query.offset,
|
query.offset,
|
||||||
@@ -36,6 +42,7 @@ where
|
|||||||
|
|
||||||
pub async fn get_by_id<'e, E>(
|
pub async fn get_by_id<'e, E>(
|
||||||
executor: E,
|
executor: E,
|
||||||
|
user_id: Uuid,
|
||||||
account_id: Uuid,
|
account_id: Uuid,
|
||||||
id: Uuid,
|
id: Uuid,
|
||||||
) -> Result<Option<AccountMemberRow>, DatabaseError>
|
) -> Result<Option<AccountMemberRow>, DatabaseError>
|
||||||
@@ -47,8 +54,11 @@ where
|
|||||||
r#"
|
r#"
|
||||||
SELECT id, account_id, user_id
|
SELECT id, account_id, user_id
|
||||||
FROM account_members
|
FROM account_members
|
||||||
WHERE account_id = $1 AND id = $2
|
WHERE account_id IN (SELECT id FROM accessible_accounts($1))
|
||||||
|
AND account_id = $2
|
||||||
|
AND id = $3
|
||||||
"#,
|
"#,
|
||||||
|
user_id,
|
||||||
account_id,
|
account_id,
|
||||||
id,
|
id,
|
||||||
)
|
)
|
||||||
@@ -57,17 +67,46 @@ where
|
|||||||
.map_err(DatabaseError::from)
|
.map_err(DatabaseError::from)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn upsert<'e, E>(
|
/// Inserts a member without checking account access. Only meant for adding the first member of a
|
||||||
|
/// newly created account, where no member exists yet to grant access. Use [upsert] for regular
|
||||||
|
/// member insertion or updating existing members.
|
||||||
|
pub async fn insert_initial<'e, E>(
|
||||||
executor: E,
|
executor: E,
|
||||||
member: AccountMemberRow,
|
member: AccountMemberRow,
|
||||||
) -> Result<Upsert<AccountMemberRow>, DatabaseError>
|
) -> Result<AccountMemberRow, DatabaseError>
|
||||||
|
where
|
||||||
|
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||||
|
{
|
||||||
|
sqlx::query_as!(
|
||||||
|
AccountMemberRow,
|
||||||
|
r#"
|
||||||
|
INSERT INTO account_members (id, account_id, user_id)
|
||||||
|
VALUES ($1, $2, $3)
|
||||||
|
RETURNING id, account_id, user_id
|
||||||
|
"#,
|
||||||
|
member.id,
|
||||||
|
member.account_id,
|
||||||
|
member.user_id,
|
||||||
|
)
|
||||||
|
.fetch_one(executor)
|
||||||
|
.await
|
||||||
|
.map_err(DatabaseError::from)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn upsert<'e, E>(
|
||||||
|
executor: E,
|
||||||
|
user_id: Uuid,
|
||||||
|
member: AccountMemberRow,
|
||||||
|
) -> Result<Option<Upsert<AccountMemberRow>>, DatabaseError>
|
||||||
where
|
where
|
||||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||||
{
|
{
|
||||||
let row = sqlx::query!(
|
let row = sqlx::query!(
|
||||||
r#"
|
r#"
|
||||||
INSERT INTO account_members (id, account_id, user_id)
|
INSERT INTO account_members (id, account_id, user_id)
|
||||||
VALUES ($1, $2, $3)
|
SELECT $2, $3, $4
|
||||||
|
FROM accessible_accounts($1) AS accounts
|
||||||
|
WHERE accounts.id = $3
|
||||||
ON CONFLICT (id, account_id) DO UPDATE SET
|
ON CONFLICT (id, account_id) DO UPDATE SET
|
||||||
user_id = EXCLUDED.user_id
|
user_id = EXCLUDED.user_id
|
||||||
RETURNING WITH (OLD AS old_row, NEW AS new_row)
|
RETURNING WITH (OLD AS old_row, NEW AS new_row)
|
||||||
@@ -78,14 +117,20 @@ where
|
|||||||
new_row.account_id AS "account_id!",
|
new_row.account_id AS "account_id!",
|
||||||
new_row.user_id AS "user_id!"
|
new_row.user_id AS "user_id!"
|
||||||
"#,
|
"#,
|
||||||
|
user_id,
|
||||||
member.id,
|
member.id,
|
||||||
member.account_id,
|
member.account_id,
|
||||||
member.user_id,
|
member.user_id,
|
||||||
)
|
)
|
||||||
.fetch_one(executor)
|
.fetch_optional(executor)
|
||||||
.await
|
.await
|
||||||
.map_err(DatabaseError::from)?;
|
.map_err(DatabaseError::from)?;
|
||||||
|
|
||||||
|
// Nothing is written when the account is not accessible.
|
||||||
|
let Some(row) = row else {
|
||||||
|
return Ok(None);
|
||||||
|
};
|
||||||
|
|
||||||
// `old_id` indicates whether there was a previous row before the upsert, other old fields are
|
// `old_id` indicates whether there was a previous row before the upsert, other old fields are
|
||||||
// coalesced with the new row's values in case there was no previous row. This enables static
|
// coalesced with the new row's values in case there was no previous row. This enables static
|
||||||
// typing on the previous row instead of having to check nullability for each old field in code.
|
// typing on the previous row instead of having to check nullability for each old field in code.
|
||||||
@@ -95,18 +140,19 @@ where
|
|||||||
user_id: row.old_user_id,
|
user_id: row.old_user_id,
|
||||||
});
|
});
|
||||||
|
|
||||||
Ok(Upsert {
|
Ok(Some(Upsert {
|
||||||
previous,
|
previous,
|
||||||
current: AccountMemberRow {
|
current: AccountMemberRow {
|
||||||
id: row.id,
|
id: row.id,
|
||||||
account_id: row.account_id,
|
account_id: row.account_id,
|
||||||
user_id: row.user_id,
|
user_id: row.user_id,
|
||||||
},
|
},
|
||||||
})
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn delete_by_id<'e, E>(
|
pub async fn delete_by_id<'e, E>(
|
||||||
executor: E,
|
executor: E,
|
||||||
|
user_id: Uuid,
|
||||||
account_id: Uuid,
|
account_id: Uuid,
|
||||||
id: Uuid,
|
id: Uuid,
|
||||||
) -> Result<Option<AccountMemberRow>, DatabaseError>
|
) -> Result<Option<AccountMemberRow>, DatabaseError>
|
||||||
@@ -117,9 +163,12 @@ where
|
|||||||
AccountMemberRow,
|
AccountMemberRow,
|
||||||
r#"
|
r#"
|
||||||
DELETE FROM account_members
|
DELETE FROM account_members
|
||||||
WHERE account_id = $1 AND id = $2
|
WHERE account_id IN (SELECT id FROM accessible_accounts($1))
|
||||||
|
AND account_id = $2
|
||||||
|
AND id = $3
|
||||||
RETURNING id, account_id, user_id
|
RETURNING id, account_id, user_id
|
||||||
"#,
|
"#,
|
||||||
|
user_id,
|
||||||
account_id,
|
account_id,
|
||||||
id,
|
id,
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -62,6 +62,34 @@ where
|
|||||||
.map_err(DatabaseError::from)
|
.map_err(DatabaseError::from)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Like [`get_by_id`], but also locks the account row until the end of the transaction, so the
|
||||||
|
/// executor must be a transaction. Callers locking the same account are serialized, while
|
||||||
|
/// inserting members into the account is not blocked.
|
||||||
|
pub async fn get_by_id_locking<'e, E>(
|
||||||
|
executor: E,
|
||||||
|
user_id: Uuid,
|
||||||
|
id: Uuid,
|
||||||
|
) -> Result<Option<AccountRow>, DatabaseError>
|
||||||
|
where
|
||||||
|
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||||
|
{
|
||||||
|
sqlx::query_as!(
|
||||||
|
AccountRow,
|
||||||
|
r#"
|
||||||
|
SELECT id, name
|
||||||
|
FROM accounts
|
||||||
|
WHERE id = $2
|
||||||
|
AND id IN (SELECT id FROM accessible_accounts($1))
|
||||||
|
FOR NO KEY UPDATE
|
||||||
|
"#,
|
||||||
|
user_id,
|
||||||
|
id,
|
||||||
|
)
|
||||||
|
.fetch_optional(executor)
|
||||||
|
.await
|
||||||
|
.map_err(DatabaseError::from)
|
||||||
|
}
|
||||||
|
|
||||||
pub async fn get_by_ids<'e, E>(
|
pub async fn get_by_ids<'e, E>(
|
||||||
executor: E,
|
executor: E,
|
||||||
user_id: Uuid,
|
user_id: Uuid,
|
||||||
|
|||||||
@@ -15,7 +15,11 @@ pub struct Query {
|
|||||||
pub offset: Option<u64>,
|
pub offset: Option<u64>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn list<'e, E>(executor: E, query: Query) -> Result<Vec<AccountMember>, RepositoryError>
|
pub async fn list<'e, E>(
|
||||||
|
executor: E,
|
||||||
|
user_id: Uuid,
|
||||||
|
query: Query,
|
||||||
|
) -> Result<Vec<AccountMember>, RepositoryError>
|
||||||
where
|
where
|
||||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||||
{
|
{
|
||||||
@@ -31,7 +35,7 @@ where
|
|||||||
.transpose()?,
|
.transpose()?,
|
||||||
};
|
};
|
||||||
|
|
||||||
let members = db::list(executor, query)
|
let members = db::list(executor, user_id, query)
|
||||||
.await?
|
.await?
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(decode)
|
.map(decode)
|
||||||
@@ -42,13 +46,14 @@ where
|
|||||||
|
|
||||||
pub async fn get_by_id<'e, E>(
|
pub async fn get_by_id<'e, E>(
|
||||||
executor: E,
|
executor: E,
|
||||||
|
user_id: Uuid,
|
||||||
account_id: Uuid,
|
account_id: Uuid,
|
||||||
id: Uuid,
|
id: Uuid,
|
||||||
) -> Result<Option<AccountMember>, RepositoryError>
|
) -> Result<Option<AccountMember>, RepositoryError>
|
||||||
where
|
where
|
||||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||||
{
|
{
|
||||||
let member = db::get_by_id(executor, account_id, id)
|
let member = db::get_by_id(executor, user_id, account_id, id)
|
||||||
.await?
|
.await?
|
||||||
.map(decode)
|
.map(decode)
|
||||||
.transpose()?;
|
.transpose()?;
|
||||||
@@ -58,20 +63,37 @@ where
|
|||||||
|
|
||||||
pub async fn upsert<'e, E>(
|
pub async fn upsert<'e, E>(
|
||||||
executor: E,
|
executor: E,
|
||||||
|
user_id: Uuid,
|
||||||
member: AccountMember,
|
member: AccountMember,
|
||||||
) -> Result<Upsert<AccountMember>, RepositoryError>
|
) -> Result<Option<Upsert<AccountMember>>, RepositoryError>
|
||||||
where
|
where
|
||||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||||
{
|
{
|
||||||
let member = db::upsert(executor, encode(member))
|
let member = db::upsert(executor, user_id, encode(member))
|
||||||
.await
|
.await
|
||||||
.map_err(classify_upsert_error)?
|
.map_err(classify_upsert_error)?
|
||||||
.try_map(decode)?
|
.map(|upserted| upserted.try_map(decode))
|
||||||
.into();
|
.transpose()?
|
||||||
|
.map(Into::into);
|
||||||
|
|
||||||
Ok(member)
|
Ok(member)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Adds the first member of a newly created account, without checking account access.
|
||||||
|
pub async fn insert_initial<'e, E>(
|
||||||
|
executor: E,
|
||||||
|
member: AccountMember,
|
||||||
|
) -> Result<AccountMember, RepositoryError>
|
||||||
|
where
|
||||||
|
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||||
|
{
|
||||||
|
let row = db::insert_initial(executor, encode(member))
|
||||||
|
.await
|
||||||
|
.map_err(classify_upsert_error)?;
|
||||||
|
|
||||||
|
Ok(decode(row)?)
|
||||||
|
}
|
||||||
|
|
||||||
fn classify_upsert_error(error: DatabaseError) -> RepositoryError {
|
fn classify_upsert_error(error: DatabaseError) -> RepositoryError {
|
||||||
match &error {
|
match &error {
|
||||||
DatabaseError::ForeignKeyViolation {
|
DatabaseError::ForeignKeyViolation {
|
||||||
@@ -103,13 +125,14 @@ fn classify_upsert_error(error: DatabaseError) -> RepositoryError {
|
|||||||
|
|
||||||
pub async fn delete_by_id<'e, E>(
|
pub async fn delete_by_id<'e, E>(
|
||||||
executor: E,
|
executor: E,
|
||||||
|
user_id: Uuid,
|
||||||
account_id: Uuid,
|
account_id: Uuid,
|
||||||
id: Uuid,
|
id: Uuid,
|
||||||
) -> Result<Option<AccountMember>, RepositoryError>
|
) -> Result<Option<AccountMember>, RepositoryError>
|
||||||
where
|
where
|
||||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||||
{
|
{
|
||||||
let member = db::delete_by_id(executor, account_id, id)
|
let member = db::delete_by_id(executor, user_id, account_id, id)
|
||||||
.await?
|
.await?
|
||||||
.map(decode)
|
.map(decode)
|
||||||
.transpose()?;
|
.transpose()?;
|
||||||
|
|||||||
@@ -62,6 +62,22 @@ where
|
|||||||
Ok(account)
|
Ok(account)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn get_by_id_locking<'e, E>(
|
||||||
|
executor: E,
|
||||||
|
user_id: Uuid,
|
||||||
|
id: Uuid,
|
||||||
|
) -> Result<Option<Account>, RepositoryError>
|
||||||
|
where
|
||||||
|
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||||
|
{
|
||||||
|
let account = db::get_by_id_locking(executor, user_id, id)
|
||||||
|
.await?
|
||||||
|
.map(decode)
|
||||||
|
.transpose()?;
|
||||||
|
|
||||||
|
Ok(account)
|
||||||
|
}
|
||||||
|
|
||||||
pub async fn get_by_ids<'e, E>(
|
pub async fn get_by_ids<'e, E>(
|
||||||
executor: E,
|
executor: E,
|
||||||
user_id: Uuid,
|
user_id: Uuid,
|
||||||
|
|||||||
@@ -44,8 +44,8 @@ async fn list_account_members(
|
|||||||
};
|
};
|
||||||
|
|
||||||
let mut tx = pool.begin().await.map_err(RepositoryError::from)?;
|
let mut tx = pool.begin().await.map_err(RepositoryError::from)?;
|
||||||
ensure_account_ownership(&mut tx, auth.user_id(), account_id).await?;
|
ensure_account_access(&mut tx, auth.user_id(), account_id).await?;
|
||||||
let members = repository::account_members::list(&mut *tx, query)
|
let members = repository::account_members::list(&mut *tx, auth.user_id(), query)
|
||||||
.await?
|
.await?
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(AccountMemberResponse::from)
|
.map(AccountMemberResponse::from)
|
||||||
@@ -62,12 +62,10 @@ async fn get_account_member(
|
|||||||
State(pool): State<PgPool>,
|
State(pool): State<PgPool>,
|
||||||
Path((account_id, member_id)): Path<(Uuid, Uuid)>,
|
Path((account_id, member_id)): Path<(Uuid, Uuid)>,
|
||||||
) -> Result<Json<AccountMemberResponse>, AppError> {
|
) -> Result<Json<AccountMemberResponse>, AppError> {
|
||||||
let mut tx = pool.begin().await.map_err(RepositoryError::from)?;
|
let member =
|
||||||
ensure_account_ownership(&mut tx, auth.user_id(), account_id).await?;
|
repository::account_members::get_by_id(&pool, auth.user_id(), account_id, member_id)
|
||||||
let member = repository::account_members::get_by_id(&mut *tx, account_id, member_id)
|
|
||||||
.await?
|
.await?
|
||||||
.ok_or(AppError::NotFound)?;
|
.ok_or(AppError::NotFound)?;
|
||||||
tx.commit().await.map_err(RepositoryError::from)?;
|
|
||||||
|
|
||||||
Ok(Json(member.into()))
|
Ok(Json(member.into()))
|
||||||
}
|
}
|
||||||
@@ -82,10 +80,9 @@ async fn upsert_account_member(
|
|||||||
) -> Result<(StatusCode, Json<AccountMemberResponse>), AppError> {
|
) -> Result<(StatusCode, Json<AccountMemberResponse>), AppError> {
|
||||||
let member = member.into_domain(member_id, account_id)?;
|
let member = member.into_domain(member_id, account_id)?;
|
||||||
|
|
||||||
let mut tx = pool.begin().await.map_err(RepositoryError::from)?;
|
let upserted = repository::account_members::upsert(&pool, auth.user_id(), member)
|
||||||
ensure_account_ownership(&mut tx, auth.user_id(), account_id).await?;
|
.await?
|
||||||
let upserted = repository::account_members::upsert(&mut *tx, member).await?;
|
.ok_or(AppError::NotFound)?;
|
||||||
tx.commit().await.map_err(RepositoryError::from)?;
|
|
||||||
|
|
||||||
let status = if upserted.previous.is_some() {
|
let status = if upserted.previous.is_some() {
|
||||||
StatusCode::OK
|
StatusCode::OK
|
||||||
@@ -103,10 +100,15 @@ async fn delete_account_member(
|
|||||||
State(pool): State<PgPool>,
|
State(pool): State<PgPool>,
|
||||||
Path((account_id, member_id)): Path<(Uuid, Uuid)>,
|
Path((account_id, member_id)): Path<(Uuid, Uuid)>,
|
||||||
) -> Result<StatusCode, AppError> {
|
) -> Result<StatusCode, AppError> {
|
||||||
|
let user_id = auth.user_id();
|
||||||
|
|
||||||
let mut tx = pool.begin().await.map_err(RepositoryError::from)?;
|
let mut tx = pool.begin().await.map_err(RepositoryError::from)?;
|
||||||
ensure_account_ownership(&mut tx, auth.user_id(), account_id).await?;
|
repository::accounts::get_by_id_locking(&mut *tx, user_id, account_id)
|
||||||
|
.await?
|
||||||
|
.ok_or(AppError::NotFound)?;
|
||||||
let members = repository::account_members::list(
|
let members = repository::account_members::list(
|
||||||
&mut *tx,
|
&mut *tx,
|
||||||
|
user_id,
|
||||||
repository::account_members::Query {
|
repository::account_members::Query {
|
||||||
account_id,
|
account_id,
|
||||||
limit: Some(2),
|
limit: Some(2),
|
||||||
@@ -120,7 +122,7 @@ async fn delete_account_member(
|
|||||||
return Err(AppError::LastAccountMemberRemoval);
|
return Err(AppError::LastAccountMemberRemoval);
|
||||||
}
|
}
|
||||||
let deleted =
|
let deleted =
|
||||||
repository::account_members::delete_by_id(&mut *tx, account_id, member_id).await?;
|
repository::account_members::delete_by_id(&mut *tx, user_id, account_id, member_id).await?;
|
||||||
tx.commit().await.map_err(RepositoryError::from)?;
|
tx.commit().await.map_err(RepositoryError::from)?;
|
||||||
|
|
||||||
deleted.ok_or(AppError::NotFound)?;
|
deleted.ok_or(AppError::NotFound)?;
|
||||||
@@ -128,7 +130,7 @@ async fn delete_account_member(
|
|||||||
Ok(StatusCode::NO_CONTENT)
|
Ok(StatusCode::NO_CONTENT)
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn ensure_account_ownership(
|
async fn ensure_account_access(
|
||||||
connection: &mut sqlx::PgConnection,
|
connection: &mut sqlx::PgConnection,
|
||||||
user_id: Uuid,
|
user_id: Uuid,
|
||||||
account_id: Uuid,
|
account_id: Uuid,
|
||||||
|
|||||||
@@ -89,7 +89,7 @@ async fn upsert_account(
|
|||||||
account_id: id,
|
account_id: id,
|
||||||
user_id,
|
user_id,
|
||||||
};
|
};
|
||||||
repository::account_members::upsert(&mut *tx, member).await?;
|
repository::account_members::insert_initial(&mut *tx, member).await?;
|
||||||
}
|
}
|
||||||
|
|
||||||
tx.commit()
|
tx.commit()
|
||||||
|
|||||||
Reference in New Issue
Block a user