Skip to content

Commit

Permalink
fix: incorrect requester avatar url and workspace member count (#847)
Browse files Browse the repository at this point in the history
  • Loading branch information
khorshuheng authored Sep 29, 2024
1 parent 47a1aae commit 757d0e5
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 57 deletions.

This file was deleted.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 15 additions & 4 deletions libs/database/src/access_request.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::pg_row::{
AFAccessRequestStatusColumn, AFAccessRequestWithViewIdColumn, AFAccessRequesterColumn,
AFWorkspaceRow,
AFWorkspaceWithMemberCountRow,
};
use app_error::AppError;
use database_entity::dto::AccessRequestWithViewId;
Expand Down Expand Up @@ -54,6 +54,15 @@ pub async fn select_access_request_by_request_id<'a, E: Executor<'a, Database =
let access_request = sqlx::query_as!(
AFAccessRequestWithViewIdColumn,
r#"
WITH request_id_workspace_member_count AS (
SELECT
request_id,
COUNT(*) AS member_count
FROM af_access_request
JOIN af_workspace_member USING (workspace_id)
WHERE request_id = $1
GROUP BY request_id
)
SELECT
request_id,
view_id,
Expand All @@ -66,20 +75,22 @@ pub async fn select_access_request_by_request_id<'a, E: Executor<'a, Database =
af_workspace.workspace_type,
af_workspace.deleted_at,
af_workspace.workspace_name,
af_workspace.icon
) AS "workspace!: AFWorkspaceRow",
af_workspace.icon,
request_id_workspace_member_count.member_count
) AS "workspace!: AFWorkspaceWithMemberCountRow",
(
af_user.uuid,
af_user.name,
af_user.email,
af_user.metadata ->> 'avatar'
af_user.metadata ->> 'icon_url'
) AS "requester!: AFAccessRequesterColumn",
status AS "status: AFAccessRequestStatusColumn",
af_access_request.created_at AS created_at
FROM af_access_request
JOIN af_user USING (uid)
JOIN af_workspace USING (workspace_id)
JOIN af_user AS owner_profile ON af_workspace.owner_uid = owner_profile.uid
JOIN request_id_workspace_member_count USING (request_id)
WHERE request_id = $1
"#,
request_id,
Expand Down
45 changes: 44 additions & 1 deletion libs/database/src/pg_row.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,49 @@ impl TryFrom<AFWorkspaceRow> for AFWorkspace {
}
}

#[derive(Debug, Clone, FromRow, Serialize, Deserialize, sqlx::Type)]
pub struct AFWorkspaceWithMemberCountRow {
pub workspace_id: Uuid,
pub database_storage_id: Option<Uuid>,
pub owner_uid: Option<i64>,
pub owner_name: Option<String>,
pub created_at: Option<DateTime<Utc>>,
pub workspace_type: i32,
pub deleted_at: Option<DateTime<Utc>>,
pub workspace_name: Option<String>,
pub icon: Option<String>,
pub member_count: i64,
}

impl TryFrom<AFWorkspaceWithMemberCountRow> for AFWorkspace {
type Error = AppError;

fn try_from(value: AFWorkspaceWithMemberCountRow) -> Result<Self, Self::Error> {
let owner_uid = value
.owner_uid
.ok_or(AppError::Internal(anyhow!("Unexpected empty owner_uid")))?;
let database_storage_id = value
.database_storage_id
.ok_or(AppError::Internal(anyhow!("Unexpected empty workspace_id")))?;

let workspace_name = value.workspace_name.unwrap_or_default();
let created_at = value.created_at.unwrap_or_else(Utc::now);
let icon = value.icon.unwrap_or_default();

Ok(Self {
workspace_id: value.workspace_id,
database_storage_id,
owner_uid,
owner_name: value.owner_name.unwrap_or_default(),
workspace_type: value.workspace_type,
workspace_name,
created_at,
icon,
member_count: Some(value.member_count),
})
}
}

/// Represent the row of the af_user table
#[derive(Debug, FromRow, Deserialize, Serialize, Clone)]
pub struct AFUserRow {
Expand Down Expand Up @@ -563,7 +606,7 @@ impl From<AFAccessRequestMinimalColumn> for AccessRequestMinimal {
#[derive(Serialize, Deserialize, Debug)]
pub struct AFAccessRequestWithViewIdColumn {
pub request_id: Uuid,
pub workspace: AFWorkspaceRow,
pub workspace: AFWorkspaceWithMemberCountRow,
pub requester: AccessRequesterInfo,
pub view_id: Uuid,
pub status: AFAccessRequestStatusColumn,
Expand Down
4 changes: 4 additions & 0 deletions tests/workspace/access_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ async fn access_request_test() {
access_request_to_be_approved.workspace.workspace_id,
workspace_id
);
assert_eq!(
access_request_to_be_approved.workspace.member_count,
Some(1)
);
owner_client
.approve_access_request(access_request_id)
.await
Expand Down

0 comments on commit 757d0e5

Please sign in to comment.