fix: guest membership
This commit is contained in:
parent
80c03483ee
commit
9acb688379
2 changed files with 50 additions and 2 deletions
|
|
@ -35,12 +35,30 @@ pub async fn get_user_membership(
|
|||
}
|
||||
|
||||
/// Create a new membership (join a realm).
|
||||
/// This function is idempotent - if the membership already exists, it returns the existing id.
|
||||
pub async fn create_membership(
|
||||
pool: &PgPool,
|
||||
user_id: Uuid,
|
||||
realm_id: Uuid,
|
||||
role: RealmRole,
|
||||
) -> Result<Uuid, AppError> {
|
||||
// Check if membership already exists
|
||||
let existing: Option<(Uuid,)> = sqlx::query_as(
|
||||
r#"
|
||||
SELECT id FROM realm.memberships
|
||||
WHERE user_id = $1 AND realm_id = $2
|
||||
"#,
|
||||
)
|
||||
.bind(user_id)
|
||||
.bind(realm_id)
|
||||
.fetch_optional(pool)
|
||||
.await?;
|
||||
|
||||
if let Some((id,)) = existing {
|
||||
return Ok(id);
|
||||
}
|
||||
|
||||
// Create new membership
|
||||
let (membership_id,): (Uuid,) = sqlx::query_as(
|
||||
r#"
|
||||
INSERT INTO realm.memberships (realm_id, user_id, role)
|
||||
|
|
@ -54,7 +72,7 @@ pub async fn create_membership(
|
|||
.fetch_one(pool)
|
||||
.await?;
|
||||
|
||||
// Update member count on the realm
|
||||
// Only increment count for new memberships
|
||||
sqlx::query("UPDATE realm.realms SET member_count = member_count + 1 WHERE id = $1")
|
||||
.bind(realm_id)
|
||||
.execute(pool)
|
||||
|
|
@ -64,12 +82,30 @@ pub async fn create_membership(
|
|||
}
|
||||
|
||||
/// Create a new membership using a connection (for RLS support).
|
||||
/// This function is idempotent - if the membership already exists, it returns the existing id.
|
||||
pub async fn create_membership_conn(
|
||||
conn: &mut sqlx::PgConnection,
|
||||
user_id: Uuid,
|
||||
realm_id: Uuid,
|
||||
role: RealmRole,
|
||||
) -> Result<Uuid, AppError> {
|
||||
// Check if membership already exists
|
||||
let existing: Option<(Uuid,)> = sqlx::query_as(
|
||||
r#"
|
||||
SELECT id FROM realm.memberships
|
||||
WHERE user_id = $1 AND realm_id = $2
|
||||
"#,
|
||||
)
|
||||
.bind(user_id)
|
||||
.bind(realm_id)
|
||||
.fetch_optional(&mut *conn)
|
||||
.await?;
|
||||
|
||||
if let Some((id,)) = existing {
|
||||
return Ok(id);
|
||||
}
|
||||
|
||||
// Create new membership
|
||||
let (membership_id,): (Uuid,) = sqlx::query_as(
|
||||
r#"
|
||||
INSERT INTO realm.memberships (realm_id, user_id, role)
|
||||
|
|
@ -83,7 +119,7 @@ pub async fn create_membership_conn(
|
|||
.fetch_one(&mut *conn)
|
||||
.await?;
|
||||
|
||||
// Update member count on the realm
|
||||
// Only increment count for new memberships
|
||||
sqlx::query("UPDATE realm.realms SET member_count = member_count + 1 WHERE id = $1")
|
||||
.bind(realm_id)
|
||||
.execute(&mut *conn)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue