2025-08-04 13:59:42 -07:00
|
|
|
use derive_builder::Builder;
|
2025-09-14 16:19:44 -04:00
|
|
|
use redact::Secret;
|
2025-08-04 13:59:42 -07:00
|
|
|
use sqlx::query_as;
|
2025-09-14 16:19:44 -04:00
|
|
|
use url::Url;
|
2025-08-04 13:59:42 -07:00
|
|
|
use uuid::Uuid;
|
|
|
|
|
|
|
|
|
|
use crate::client::AppDbClient;
|
|
|
|
|
|
2025-09-14 16:19:44 -04:00
|
|
|
/// A workspace is 1:1 with a Postgres "database".
|
2025-08-04 13:59:42 -07:00
|
|
|
#[derive(Clone, Debug)]
|
2025-09-14 16:19:44 -04:00
|
|
|
pub struct Workspace {
|
|
|
|
|
/// Primary key (defaults to UUIDv7).
|
2025-08-04 13:59:42 -07:00
|
|
|
pub id: Uuid,
|
2025-09-14 16:19:44 -04:00
|
|
|
|
|
|
|
|
/// Human friendly name for the workspace.
|
2025-08-04 13:59:42 -07:00
|
|
|
pub name: String,
|
2025-09-14 16:19:44 -04:00
|
|
|
|
|
|
|
|
/// `postgresql://` URL of the instance and database hosting this workspace.
|
2025-10-07 06:23:50 +00:00
|
|
|
// TODO: Encrypt values in Postgres using `pgp_sym_encrypt()`.
|
2025-09-14 16:19:44 -04:00
|
|
|
pub url: Secret<String>,
|
|
|
|
|
|
|
|
|
|
/// ID of the user account that created this workspace.
|
2025-08-04 13:59:42 -07:00
|
|
|
pub owner_id: Uuid,
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-14 16:19:44 -04:00
|
|
|
impl Workspace {
|
|
|
|
|
/// Build an insert statement to create a new workspace.
|
|
|
|
|
pub fn insert() -> InsertableWorkspaceBuilder {
|
|
|
|
|
InsertableWorkspaceBuilder::default()
|
2025-08-04 13:59:42 -07:00
|
|
|
}
|
|
|
|
|
|
2025-09-14 16:19:44 -04:00
|
|
|
/// Build a single-field query by workspace ID.
|
2025-08-04 13:59:42 -07:00
|
|
|
pub fn with_id(id: Uuid) -> WithIdQuery {
|
|
|
|
|
WithIdQuery { id }
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-14 16:19:44 -04:00
|
|
|
/// Build a query for workspaces filtered by a user's Phono permissions.
|
2025-08-04 13:59:42 -07:00
|
|
|
pub fn with_permission_in<I: IntoIterator<Item = &'static str>>(
|
|
|
|
|
perms: I,
|
|
|
|
|
) -> WithPermissionInQueryPartial {
|
|
|
|
|
let perms: Vec<String> = perms.into_iter().map(ToOwned::to_owned).collect();
|
|
|
|
|
WithPermissionInQueryPartial { perms }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub struct WithPermissionInQueryPartial {
|
|
|
|
|
perms: Vec<String>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl WithPermissionInQueryPartial {
|
|
|
|
|
pub fn for_user(self, user_id: Uuid) -> WithPermissionInQuery {
|
|
|
|
|
WithPermissionInQuery {
|
|
|
|
|
perms: self.perms,
|
|
|
|
|
user_id,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub struct WithPermissionInQuery {
|
|
|
|
|
perms: Vec<String>,
|
|
|
|
|
user_id: Uuid,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl WithPermissionInQuery {
|
2025-09-14 16:19:44 -04:00
|
|
|
pub async fn fetch_all(self, app_db: &mut AppDbClient) -> Result<Vec<Workspace>, sqlx::Error> {
|
2025-08-04 13:59:42 -07:00
|
|
|
query_as!(
|
2025-09-14 16:19:44 -04:00
|
|
|
Workspace,
|
2025-08-04 13:59:42 -07:00
|
|
|
"
|
2025-09-14 16:19:44 -04:00
|
|
|
select workspaces.*
|
|
|
|
|
from workspaces inner join workspace_user_perms as p
|
|
|
|
|
on p.workspace_id = workspaces.id
|
2025-08-04 13:59:42 -07:00
|
|
|
where p.user_id = $1 and perm = ANY($2)
|
|
|
|
|
",
|
|
|
|
|
self.user_id,
|
|
|
|
|
self.perms.as_slice(),
|
|
|
|
|
)
|
|
|
|
|
.fetch_all(&mut *app_db.conn)
|
|
|
|
|
.await
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub struct WithIdQuery {
|
|
|
|
|
id: Uuid,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl WithIdQuery {
|
|
|
|
|
pub async fn fetch_optional(
|
|
|
|
|
self,
|
|
|
|
|
app_db: &mut AppDbClient,
|
2025-09-14 16:19:44 -04:00
|
|
|
) -> Result<Option<Workspace>, sqlx::Error> {
|
|
|
|
|
query_as!(
|
|
|
|
|
Workspace,
|
|
|
|
|
"select * from workspaces where id = $1",
|
|
|
|
|
&self.id
|
|
|
|
|
)
|
|
|
|
|
.fetch_optional(&mut *app_db.conn)
|
|
|
|
|
.await
|
2025-08-04 13:59:42 -07:00
|
|
|
}
|
|
|
|
|
|
2025-09-14 16:19:44 -04:00
|
|
|
pub async fn fetch_one(self, app_db: &mut AppDbClient) -> Result<Workspace, sqlx::Error> {
|
|
|
|
|
query_as!(
|
|
|
|
|
Workspace,
|
|
|
|
|
"select * from workspaces where id = $1",
|
|
|
|
|
&self.id
|
|
|
|
|
)
|
|
|
|
|
.fetch_one(&mut *app_db.conn)
|
|
|
|
|
.await
|
2025-08-04 13:59:42 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Builder)]
|
2025-09-14 16:19:44 -04:00
|
|
|
pub struct InsertableWorkspace {
|
|
|
|
|
url: Url,
|
2025-08-04 13:59:42 -07:00
|
|
|
owner_id: Uuid,
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-14 16:19:44 -04:00
|
|
|
impl InsertableWorkspace {
|
|
|
|
|
pub async fn insert(self, app_db: &mut AppDbClient) -> Result<Workspace, sqlx::Error> {
|
2025-08-04 13:59:42 -07:00
|
|
|
query_as!(
|
2025-09-14 16:19:44 -04:00
|
|
|
Workspace,
|
2025-08-04 13:59:42 -07:00
|
|
|
"
|
2025-09-14 16:19:44 -04:00
|
|
|
insert into workspaces
|
|
|
|
|
(url, owner_id)
|
|
|
|
|
values ($1, $2)
|
2025-08-04 13:59:42 -07:00
|
|
|
returning *
|
|
|
|
|
",
|
2025-09-14 16:19:44 -04:00
|
|
|
self.url.to_string(),
|
2025-08-04 13:59:42 -07:00
|
|
|
self.owner_id
|
|
|
|
|
)
|
|
|
|
|
.fetch_one(&mut *app_db.conn)
|
|
|
|
|
.await
|
|
|
|
|
}
|
|
|
|
|
}
|