feat: add get invitation by id
This commit is contained in:
parent
96896101d4
commit
f25066f51f
|
|
@ -0,0 +1,59 @@
|
||||||
|
{
|
||||||
|
"db_name": "PostgreSQL",
|
||||||
|
"query": "\n SELECT\n id AS invite_id,\n workspace_id,\n (SELECT workspace_name FROM public.af_workspace WHERE workspace_id = af_workspace_invitation.workspace_id),\n (SELECT email FROM public.af_user WHERE uid = af_workspace_invitation.inviter) AS inviter_email,\n (SELECT name FROM public.af_user WHERE uid = af_workspace_invitation.inviter) AS inviter_name,\n status,\n updated_at\n FROM public.af_workspace_invitation\n WHERE af_workspace_invitation.invitee_email = (SELECT email FROM public.af_user WHERE uuid = $1)\n AND id = $2\n ",
|
||||||
|
"describe": {
|
||||||
|
"columns": [
|
||||||
|
{
|
||||||
|
"ordinal": 0,
|
||||||
|
"name": "invite_id",
|
||||||
|
"type_info": "Uuid"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ordinal": 1,
|
||||||
|
"name": "workspace_id",
|
||||||
|
"type_info": "Uuid"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ordinal": 2,
|
||||||
|
"name": "workspace_name",
|
||||||
|
"type_info": "Text"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ordinal": 3,
|
||||||
|
"name": "inviter_email",
|
||||||
|
"type_info": "Text"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ordinal": 4,
|
||||||
|
"name": "inviter_name",
|
||||||
|
"type_info": "Text"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ordinal": 5,
|
||||||
|
"name": "status",
|
||||||
|
"type_info": "Int2"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ordinal": 6,
|
||||||
|
"name": "updated_at",
|
||||||
|
"type_info": "Timestamptz"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"parameters": {
|
||||||
|
"Left": [
|
||||||
|
"Uuid",
|
||||||
|
"Uuid"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"nullable": [
|
||||||
|
false,
|
||||||
|
false,
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
false,
|
||||||
|
false
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"hash": "6dd7f6db2d364cc37b1b46c611fe111d06e327edd9f3a98a4b0636bfe8fd6319"
|
||||||
|
}
|
||||||
|
|
@ -65,7 +65,6 @@ impl Client {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[instrument(level = "info", skip_all, err)]
|
|
||||||
pub async fn list_workspace_invitations(
|
pub async fn list_workspace_invitations(
|
||||||
&self,
|
&self,
|
||||||
status: Option<AFWorkspaceInvitationStatus>,
|
status: Option<AFWorkspaceInvitationStatus>,
|
||||||
|
|
@ -81,6 +80,21 @@ impl Client {
|
||||||
res.into_data()
|
res.into_data()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn get_workspace_invitation(
|
||||||
|
&self,
|
||||||
|
invite_uuid: &str,
|
||||||
|
) -> Result<AFWorkspaceInvitation, AppResponseError> {
|
||||||
|
let url = format!("{}/api/workspace/invite/{}", self.base_url, invite_uuid);
|
||||||
|
let resp = self
|
||||||
|
.http_client_with_auth(Method::GET, &url)
|
||||||
|
.await?
|
||||||
|
.send()
|
||||||
|
.await?;
|
||||||
|
log_request_id(&resp);
|
||||||
|
let res: AppResponse<AFWorkspaceInvitation> = AppResponse::from_response(resp).await?;
|
||||||
|
res.into_data()
|
||||||
|
}
|
||||||
|
|
||||||
pub async fn accept_workspace_invitation(
|
pub async fn accept_workspace_invitation(
|
||||||
&self,
|
&self,
|
||||||
invitation_id: &str,
|
invitation_id: &str,
|
||||||
|
|
|
||||||
|
|
@ -428,6 +428,33 @@ pub async fn select_workspace_invitations_for_user(
|
||||||
Ok(res)
|
Ok(res)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
pub async fn select_workspace_invitation_for_user(
|
||||||
|
pg_pool: &PgPool,
|
||||||
|
invitee_uuid: &Uuid,
|
||||||
|
invite_id: &Uuid,
|
||||||
|
) -> Result<AFWorkspaceInvitation, AppError> {
|
||||||
|
let res = sqlx::query_as!(
|
||||||
|
AFWorkspaceInvitation,
|
||||||
|
r#"
|
||||||
|
SELECT
|
||||||
|
id AS invite_id,
|
||||||
|
workspace_id,
|
||||||
|
(SELECT workspace_name FROM public.af_workspace WHERE workspace_id = af_workspace_invitation.workspace_id),
|
||||||
|
(SELECT email FROM public.af_user WHERE uid = af_workspace_invitation.inviter) AS inviter_email,
|
||||||
|
(SELECT name FROM public.af_user WHERE uid = af_workspace_invitation.inviter) AS inviter_name,
|
||||||
|
status,
|
||||||
|
updated_at
|
||||||
|
FROM public.af_workspace_invitation
|
||||||
|
WHERE af_workspace_invitation.invitee_email = (SELECT email FROM public.af_user WHERE uuid = $1)
|
||||||
|
AND id = $2
|
||||||
|
"#,
|
||||||
|
invitee_uuid,
|
||||||
|
invite_id,
|
||||||
|
).fetch_one(pg_pool).await?;
|
||||||
|
Ok(res)
|
||||||
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
#[instrument(level = "trace", skip(pool, email, role), err)]
|
#[instrument(level = "trace", skip(pool, email, role), err)]
|
||||||
pub async fn upsert_workspace_member(
|
pub async fn upsert_workspace_member(
|
||||||
|
|
|
||||||
|
|
@ -74,6 +74,9 @@ pub fn workspace_scope() -> Scope {
|
||||||
.service(
|
.service(
|
||||||
web::resource("/invite").route(web::get().to(get_workspace_invite_handler)), // show invites for user
|
web::resource("/invite").route(web::get().to(get_workspace_invite_handler)), // show invites for user
|
||||||
)
|
)
|
||||||
|
.service(
|
||||||
|
web::resource("/invite/{invite_id}").route(web::get().to(get_workspace_invite_by_id_handler)),
|
||||||
|
)
|
||||||
.service(
|
.service(
|
||||||
web::resource("/accept-invite/{invite_id}")
|
web::resource("/accept-invite/{invite_id}")
|
||||||
.route(web::post().to(post_accept_workspace_invite_handler)), // accept invitation to workspace
|
.route(web::post().to(post_accept_workspace_invite_handler)), // accept invitation to workspace
|
||||||
|
|
@ -308,6 +311,18 @@ async fn get_workspace_invite_handler(
|
||||||
Ok(AppResponse::Ok().with_data(res).into())
|
Ok(AppResponse::Ok().with_data(res).into())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn get_workspace_invite_by_id_handler(
|
||||||
|
user_uuid: UserUuid,
|
||||||
|
state: Data<AppState>,
|
||||||
|
invite_id: web::Path<Uuid>,
|
||||||
|
) -> Result<JsonAppResponse<AFWorkspaceInvitation>> {
|
||||||
|
let invite_id = invite_id.into_inner();
|
||||||
|
let res =
|
||||||
|
workspace::ops::get_workspace_invitations_for_user(&state.pg_pool, &user_uuid, &invite_id)
|
||||||
|
.await?;
|
||||||
|
Ok(AppResponse::Ok().with_data(res).into())
|
||||||
|
}
|
||||||
|
|
||||||
async fn post_accept_workspace_invite_handler(
|
async fn post_accept_workspace_invite_handler(
|
||||||
user_uuid: UserUuid,
|
user_uuid: UserUuid,
|
||||||
invite_id: web::Path<Uuid>,
|
invite_id: web::Path<Uuid>,
|
||||||
|
|
|
||||||
|
|
@ -450,6 +450,15 @@ pub async fn list_workspace_invitations_for_user(
|
||||||
Ok(invis)
|
Ok(invis)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn get_workspace_invitations_for_user(
|
||||||
|
pg_pool: &PgPool,
|
||||||
|
user_uuid: &Uuid,
|
||||||
|
invite_id: &Uuid,
|
||||||
|
) -> Result<AFWorkspaceInvitation, AppError> {
|
||||||
|
let invitation = select_workspace_invitation_for_user(pg_pool, user_uuid, invite_id).await?;
|
||||||
|
Ok(invitation)
|
||||||
|
}
|
||||||
|
|
||||||
// use in tests only
|
// use in tests only
|
||||||
pub async fn add_workspace_members_db_only(
|
pub async fn add_workspace_members_db_only(
|
||||||
pg_pool: &PgPool,
|
pg_pool: &PgPool,
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,7 @@ use shared_entity::dto::workspace_dto::{QueryWorkspaceParam, WorkspaceMemberInvi
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn invite_workspace_crud() {
|
async fn invite_workspace_crud() {
|
||||||
let (alice_client, _alice) = generate_unique_registered_user_client().await;
|
let (alice_client, alice) = generate_unique_registered_user_client().await;
|
||||||
let alice_workspace_id = alice_client
|
let alice_workspace_id = alice_client
|
||||||
.get_workspaces()
|
.get_workspaces()
|
||||||
.await
|
.await
|
||||||
|
|
@ -49,14 +49,19 @@ async fn invite_workspace_crud() {
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
assert_eq!(pending_invs.len(), 1);
|
assert_eq!(pending_invs.len(), 1);
|
||||||
|
let invite_id = pending_invs.first().unwrap().invite_id.to_string();
|
||||||
|
|
||||||
// accept invitation
|
// get invitation by id
|
||||||
let target_invite = pending_invs
|
let invitation = bob_client
|
||||||
.iter()
|
.get_workspace_invitation(&invite_id)
|
||||||
.find(|i| i.workspace_id == alice_workspace_id)
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
|
assert_eq!(invitation.workspace_id, alice_workspace_id);
|
||||||
|
assert_eq!(invitation.inviter_email, Some(alice.email));
|
||||||
|
|
||||||
bob_client
|
bob_client
|
||||||
.accept_workspace_invitation(target_invite.invite_id.to_string().as_str())
|
.accept_workspace_invitation(&invite_id)
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue