use anyhow::Result; use diesel::{ExpressionMethods, QueryDsl}; use diesel_async::RunQueryDsl; use uuid::Uuid; use crate::pool::get_pg_pool; use crate::schema::users_to_organizations; /// Gets the organization ID for a user /// /// # Arguments /// * `user_id` - The UUID of the user /// /// # Returns /// * `Result>` - The organization ID if found pub async fn get_user_organization_id(user_id: &Uuid) -> Result> { let mut conn = get_pg_pool().get().await?; let result = match users_to_organizations::table .filter(users_to_organizations::user_id.eq(user_id)) .filter(users_to_organizations::deleted_at.is_null()) .select(users_to_organizations::organization_id) .first::(&mut conn) .await { Ok(organization_id) => Some(organization_id), Err(diesel::NotFound) => None, Err(e) => return Err(e.into()), }; Ok(result) }