Skip to content

Commit

Permalink
feat: add filtering capabilities to workspace search
Browse files Browse the repository at this point in the history
  • Loading branch information
speed2exe committed Nov 22, 2024
1 parent 3a0b210 commit 351dca6
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 4 deletions.
7 changes: 5 additions & 2 deletions libs/database/src/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -755,6 +755,7 @@ pub async fn update_updated_at_of_workspace_with_uid<'a, E: Executor<'a, Databas
pub async fn select_all_user_workspaces<'a, E: Executor<'a, Database = Postgres>>(
executor: E,
user_uuid: &Uuid,
name_filter: Option<&str>,
) -> Result<Vec<AFWorkspaceRow>, AppError> {
let workspaces = sqlx::query_as!(
AFWorkspaceRow,
Expand All @@ -776,9 +777,11 @@ pub async fn select_all_user_workspaces<'a, E: Executor<'a, Database = Postgres>
WHERE wm.uid = (
SELECT uid FROM public.af_user WHERE uuid = $1
)
AND COALESCE(w.is_initialized, true) = true;
AND COALESCE(w.is_initialized, true) = true
AND ($2::TEXT IS NULL OR w.workspace_name ILIKE $2::TEXT);
"#,
user_uuid
user_uuid,
name_filter.map(|filter| format!("%{}%", filter))
)
.fetch_all(executor)
.await?;
Expand Down
1 change: 1 addition & 0 deletions libs/shared-entity/src/dto/workspace_dto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ pub enum SpacePermission {

#[derive(Default, Debug, Deserialize, Serialize)]
pub struct QueryWorkspaceParam {
pub name_filter: Option<String>,
pub include_member_count: Option<bool>,
pub include_role: Option<bool>,
}
Expand Down
2 changes: 2 additions & 0 deletions src/api/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,13 +344,15 @@ async fn list_workspace_handler(
query: web::Query<QueryWorkspaceParam>,
) -> Result<JsonAppResponse<Vec<AFWorkspace>>> {
let QueryWorkspaceParam {
name_filter,
include_member_count,
include_role,
} = query.into_inner();

let workspaces = workspace::ops::get_all_user_workspaces(
&state.pg_pool,
&uuid,
name_filter.as_deref(),
include_member_count.unwrap_or(false),
include_role.unwrap_or(false),
)
Expand Down
2 changes: 1 addition & 1 deletion src/biz/user/user_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ pub async fn get_user_workspace_info(
let user_profile = AFUserProfile::try_from(row)?;

// Get all workspaces that the user can access to
let workspaces = select_all_user_workspaces(txn.deref_mut(), uuid)
let workspaces = select_all_user_workspaces(txn.deref_mut(), uuid, None)
.await?
.into_iter()
.flat_map(|row| AFWorkspace::try_from(row).ok())
Expand Down
3 changes: 2 additions & 1 deletion src/biz/workspace/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,10 +255,11 @@ pub async fn remove_reaction_on_comment(
pub async fn get_all_user_workspaces(
pg_pool: &PgPool,
user_uuid: &Uuid,
name_filter: Option<&str>,
include_member_count: bool,
include_role: bool,
) -> Result<Vec<AFWorkspace>, AppResponseError> {
let workspaces = select_all_user_workspaces(pg_pool, user_uuid).await?;
let workspaces = select_all_user_workspaces(pg_pool, user_uuid, name_filter).await?;
let mut workspaces = workspaces
.into_iter()
.flat_map(|row| {
Expand Down
18 changes: 18 additions & 0 deletions tests/workspace/invitation_crud.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ async fn invite_workspace_crud() {
// alice's view of the workspaces
let workspaces = alice_client
.get_workspaces_opt(QueryWorkspaceParam {
name_filter: None,
include_member_count: Some(true),
include_role: Some(true),
})
Expand All @@ -124,6 +125,7 @@ async fn invite_workspace_crud() {
// bob should see 2 workspaces, one is his own and the other is alice's
let workspaces = bob_client
.get_workspaces_opt(QueryWorkspaceParam {
name_filter: None,
include_member_count: Some(true),
include_role: Some(true),
})
Expand All @@ -148,4 +150,20 @@ async fn invite_workspace_crud() {
assert_eq!(bob_workspace.role, Some(AFRole::Owner));
}
}

{
// bob's filter workspaces by name
let workspaces = bob_client
.get_workspaces_opt(QueryWorkspaceParam {
name_filter: Some("alice".to_string()),
include_member_count: Some(true),
include_role: Some(true),
})
.await
.unwrap();
assert_eq!(workspaces.len(), 1);
let alice_workspace = &workspaces[0];
assert_eq!(alice_workspace.member_count, Some(2));
assert_eq!(alice_workspace.role, Some(AFRole::Member));
}
}

0 comments on commit 351dca6

Please sign in to comment.