gitdataai/libs/api/project/invitation.rs
2026-04-15 09:08:09 +08:00

169 lines
5.2 KiB
Rust

use super::PageQuery;
use crate::{ApiResponse, error::ApiError};
use actix_web::{HttpResponse, Result, web};
use models::projects::MemberRole;
use service::AppService;
use session::Session;
#[derive(serde::Deserialize, utoipa::ToSchema)]
pub struct InviteUserRequest {
pub email: String,
pub scope: MemberRole,
}
#[utoipa::path(
get,
path = "/api/projects/{project_name}/invitations",
params(
("project_name" = String, Path),
("page" = Option<u64>, Query),
("per_page" = Option<u64>, Query),
),
responses(
(status = 200, description = "List project invitations", body = ApiResponse<service::project::invitation::InvitationListResponse>),
(status = 401, description = "Unauthorized"),
(status = 403, description = "Forbidden"),
(status = 404, description = "Not found"),
),
tag = "Project"
)]
pub async fn project_invitations(
service: web::Data<AppService>,
session: Session,
path: web::Path<String>,
query: web::Query<PageQuery>,
) -> Result<HttpResponse, ApiError> {
let project_name = path.into_inner();
let resp = service
.project_get_invitations(project_name, query.page, query.per_page, &session)
.await?;
Ok(ApiResponse::ok(resp).to_response())
}
#[utoipa::path(
get,
path = "/api/projects/me/invitations",
responses(
(status = 200, description = "List my invitations", body = ApiResponse<service::project::invitation::InvitationListResponse>),
(status = 401, description = "Unauthorized"),
(status = 404, description = "Not found"),
),
tag = "Project"
)]
pub async fn project_my_invitations(
service: web::Data<AppService>,
session: Session,
query: web::Query<PageQuery>,
) -> Result<HttpResponse, ApiError> {
let resp = service
.project_get_my_invitations(query.page, query.per_page, &session)
.await?;
Ok(ApiResponse::ok(resp).to_response())
}
#[utoipa::path(
post,
path = "/api/projects/{project_name}/invitations",
params(("project_name" = String, Path)),
request_body = InviteUserRequest,
responses(
(status = 200, description = "Invite user to project"),
(status = 401, description = "Unauthorized"),
(status = 403, description = "Forbidden"),
(status = 404, description = "Not found"),
),
tag = "Project"
)]
pub async fn project_invite_user(
service: web::Data<AppService>,
session: Session,
path: web::Path<String>,
body: web::Json<InviteUserRequest>,
) -> Result<HttpResponse, ApiError> {
let project_name = path.into_inner();
service
.project_invite_user(
project_name,
body.email.clone(),
body.scope.clone(),
&session,
)
.await?;
Ok(ApiResponse::ok(serde_json::json!({ "success": true })).to_response())
}
#[utoipa::path(
post,
path = "/api/projects/{project_name}/invitations/accept",
params(("project_name" = String, Path)),
responses(
(status = 200, description = "Accept project invitation"),
(status = 401, description = "Unauthorized"),
(status = 404, description = "Not found"),
),
tag = "Project"
)]
pub async fn project_accept_invitation(
service: web::Data<AppService>,
session: Session,
path: web::Path<String>,
) -> Result<HttpResponse, ApiError> {
let project_name = path.into_inner();
service
.project_accept_invitation(project_name, &session)
.await?;
Ok(ApiResponse::ok(serde_json::json!({ "success": true })).to_response())
}
#[utoipa::path(
post,
path = "/api/projects/{project_name}/invitations/reject",
params(("project_name" = String, Path)),
responses(
(status = 200, description = "Reject project invitation"),
(status = 401, description = "Unauthorized"),
(status = 404, description = "Not found"),
),
tag = "Project"
)]
pub async fn project_reject_invitation(
service: web::Data<AppService>,
session: Session,
path: web::Path<String>,
) -> Result<HttpResponse, ApiError> {
let project_name = path.into_inner();
service
.project_reject_invitation(project_name, &session)
.await?;
Ok(ApiResponse::ok(serde_json::json!({ "success": true })).to_response())
}
#[utoipa::path(
delete,
path = "/api/projects/{project_name}/invitations/{user_id}",
params(
("project_name" = String, Path),
("user_id" = String, Path),
),
responses(
(status = 200, description = "Cancel project invitation"),
(status = 401, description = "Unauthorized"),
(status = 403, description = "Forbidden"),
(status = 404, description = "Not found"),
),
tag = "Project"
)]
pub async fn project_cancel_invitation(
service: web::Data<AppService>,
session: Session,
path: web::Path<(String, String)>,
) -> Result<HttpResponse, ApiError> {
let (project_name, user_id) = path.into_inner();
let user_uuid = uuid::Uuid::parse_str(&user_id)
.map_err(|_| service::error::AppError::BadRequest("Invalid UUID".to_string()))?;
service
.project_cancel_invitation(project_name, user_uuid, &session)
.await?;
Ok(ApiResponse::ok(serde_json::json!({ "success": true })).to_response())
}