|
| 1 | +use bencher_endpoint::{CorsResponse, Endpoint, Post, ResponseCreated}; |
| 2 | +use bencher_json::{JsonNewClaim, JsonOrganization, ResourceId}; |
| 3 | +use bencher_schema::{ |
| 4 | + conn_lock, |
| 5 | + context::ApiContext, |
| 6 | + model::{ |
| 7 | + organization::QueryOrganization, |
| 8 | + user::auth::{AuthUser, BearerToken}, |
| 9 | + }, |
| 10 | +}; |
| 11 | +use dropshot::{endpoint, HttpError, Path, RequestContext, TypedBody}; |
| 12 | +use schemars::JsonSchema; |
| 13 | +use serde::Deserialize; |
| 14 | + |
| 15 | +#[derive(Deserialize, JsonSchema)] |
| 16 | +pub struct OrgClaimParams { |
| 17 | + /// The slug or UUID for an organization. |
| 18 | + pub organization: ResourceId, |
| 19 | +} |
| 20 | + |
| 21 | +#[allow(clippy::no_effect_underscore_binding, clippy::unused_async)] |
| 22 | +#[endpoint { |
| 23 | + method = OPTIONS, |
| 24 | + path = "/v0/organizations/{organization}/claim", |
| 25 | + tags = ["organizations"] |
| 26 | +}] |
| 27 | +pub async fn org_claim_options( |
| 28 | + _rqctx: RequestContext<ApiContext>, |
| 29 | + _path_params: Path<OrgClaimParams>, |
| 30 | +) -> Result<CorsResponse, HttpError> { |
| 31 | + Ok(Endpoint::cors(&[Post.into()])) |
| 32 | +} |
| 33 | + |
| 34 | +/// Claim an organization |
| 35 | +/// |
| 36 | +/// Claim an organization. |
| 37 | +/// The user must be authenticated and the organization must be unclaimed. |
| 38 | +#[endpoint { |
| 39 | + method = POST, |
| 40 | + path = "/v0/organizations/{organization}/claim", |
| 41 | + tags = ["organizations"] |
| 42 | +}] |
| 43 | +pub async fn org_claim_post( |
| 44 | + rqctx: RequestContext<ApiContext>, |
| 45 | + bearer_token: BearerToken, |
| 46 | + path_params: Path<OrgClaimParams>, |
| 47 | + body: TypedBody<JsonNewClaim>, |
| 48 | +) -> Result<ResponseCreated<JsonOrganization>, HttpError> { |
| 49 | + let auth_user = AuthUser::from_token(rqctx.context(), bearer_token).await?; |
| 50 | + let json = post_inner( |
| 51 | + rqctx.context(), |
| 52 | + path_params.into_inner(), |
| 53 | + body.into_inner(), |
| 54 | + auth_user, |
| 55 | + ) |
| 56 | + .await?; |
| 57 | + Ok(Post::auth_response_created(json)) |
| 58 | +} |
| 59 | + |
| 60 | +async fn post_inner( |
| 61 | + context: &ApiContext, |
| 62 | + path_params: OrgClaimParams, |
| 63 | + _json_claim: JsonNewClaim, |
| 64 | + auth_user: AuthUser, |
| 65 | +) -> Result<JsonOrganization, HttpError> { |
| 66 | + let query_organization = |
| 67 | + QueryOrganization::from_resource_id(conn_lock!(context), &path_params.organization)?; |
| 68 | + query_organization.claim(context, &auth_user.user).await?; |
| 69 | + |
| 70 | + Ok(query_organization.into_json(conn_lock!(context))) |
| 71 | +} |
0 commit comments