1
0
Fork 0
forked from 2sys/phonograph
phonograph/interim-models/src/workspace_user_perm.rs

105 lines
2.4 KiB
Rust
Raw Normal View History

use derive_builder::Builder;
use serde::{Deserialize, Serialize};
2025-10-22 00:43:53 -07:00
use sqlx::query_as;
use uuid::Uuid;
use crate::client::AppDbClient;
/// Assigns an access control permission on a workspace to a user. These are
/// derived from the permission grants of the workspace's backing database.
#[derive(Clone, Debug, Deserialize, Serialize)]
2025-10-22 00:43:53 -07:00
pub struct WorkspaceMembership {
/// Primary key (defaults to UUIDv7).
pub id: Uuid,
/// Workspace to which the permission belongs.
pub workspace_id: Uuid,
/// **Synthesized field** generated by joining to the `workspaces` table.
pub workspace_name: String,
/// User to which the permission belongs.
pub user_id: Uuid,
}
2025-10-22 00:43:53 -07:00
impl WorkspaceMembership {
/// Construct a single-field query to fetch workspace permissions assigned
/// to a user.
pub fn belonging_to_user(id: Uuid) -> BelongingToUserQuery {
BelongingToUserQuery { id }
}
/// Build an insert statement to create a new object.
pub fn insert() -> InsertBuilder {
InsertBuilder::default()
}
}
#[derive(Clone, Debug)]
pub struct BelongingToUserQuery {
id: Uuid,
}
impl BelongingToUserQuery {
pub async fn fetch_all(
self,
app_db: &mut AppDbClient,
2025-10-22 00:43:53 -07:00
) -> Result<Vec<WorkspaceMembership>, sqlx::Error> {
query_as!(
2025-10-22 00:43:53 -07:00
WorkspaceMembership,
r#"
select
p.id as id,
p.workspace_id as workspace_id,
p.user_id as user_id,
w.name as workspace_name
2025-10-22 00:43:53 -07:00
from workspace_memberships as p
inner join workspaces as w
on w.id = p.workspace_id
where p.user_id = $1
"#,
self.id,
)
.fetch_all(app_db.get_conn())
.await
}
}
#[derive(Builder, Clone, Debug)]
pub struct Insert {
workspace_id: Uuid,
user_id: Uuid,
}
impl Insert {
2025-10-22 00:43:53 -07:00
pub async fn execute(
self,
app_db: &mut AppDbClient,
) -> Result<WorkspaceMembership, sqlx::Error> {
query_as!(
2025-10-22 00:43:53 -07:00
WorkspaceMembership,
r#"
with p as (
2025-10-22 00:43:53 -07:00
insert into workspace_memberships (workspace_id, user_id) values ($1, $2)
returning
id,
workspace_id,
2025-10-22 00:43:53 -07:00
user_id
)
select
p.id as id,
p.workspace_id as workspace_id,
p.user_id as user_id,
w.name as workspace_name
2025-10-22 00:43:53 -07:00
from workspace_memberships as p
inner join workspaces as w
on w.id = p.workspace_id
"#,
self.workspace_id,
self.user_id,
)
.fetch_one(app_db.get_conn())
.await
}
}