use super::{CommunityRole, PgPool, Uuid}; /// Ensure a user has a membership in a community with the given role. /// Creates membership if none exists, does nothing if already a member. #[tracing::instrument(skip_all)] pub async fn ensure_membership_with_role( pool: &PgPool, user_id: Uuid, community_id: Uuid, role: CommunityRole, ) -> Result<(), sqlx::Error> { sqlx::query!( "INSERT INTO memberships (user_id, community_id, role) VALUES ($1, $2, $3) ON CONFLICT (user_id, community_id) DO NOTHING", user_id, community_id, role.as_str(), ) .execute(pool) .await?; Ok(()) } /// Ensure a user has a membership in a community. Creates a 'member' role if none exists. #[tracing::instrument(skip_all)] pub async fn ensure_membership( pool: &PgPool, user_id: Uuid, community_id: Uuid, ) -> Result<(), sqlx::Error> { sqlx::query!( "INSERT INTO memberships (user_id, community_id, role) VALUES ($1, $2, 'member') ON CONFLICT (user_id, community_id) DO NOTHING", user_id, community_id, ) .execute(pool) .await?; Ok(()) }