Skip to content

Commit

Permalink
Add set email endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
augustuswm committed Nov 19, 2024
1 parent 90cffd1 commit 7ab0f59
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 4 deletions.
43 changes: 42 additions & 1 deletion v-api/src/endpoints/api_user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ use uuid::Uuid;
use v_model::{
permissions::{Caller, Permission, PermissionStorage, Permissions},
storage::{ApiUserProviderFilter, ListPagination},
AccessGroupId, ApiKeyId, ApiUser, ApiUserProvider, NewApiKey, NewApiUser, UserId,
AccessGroupId, ApiKeyId, ApiUser, ApiUserContactEmail, ApiUserProvider, NewApiKey, NewApiUser,
UserId,
};

use crate::{
Expand Down Expand Up @@ -245,6 +246,46 @@ where
))
}

#[derive(Debug, Clone, Deserialize, JsonSchema)]
pub struct ApiUserEmailUpdateParams {
email: String,
}

#[instrument(skip(rqctx, body), err(Debug))]
pub async fn set_api_user_contact_email_op<T>(
rqctx: &RequestContext<impl ApiContext<AppPermissions = T>>,
path: ApiUserPath,
body: ApiUserEmailUpdateParams,
) -> Result<HttpResponseOk<ApiUserContactEmail>, HttpError>
where
T: VAppPermission + PermissionStorage,
{
let (ctx, caller) = rqctx.as_ctx().await?;
set_api_user_contact_email_inner(ctx, caller, path, body).await
}

#[instrument(skip(ctx, body))]
pub async fn set_api_user_contact_email_inner<T>(
ctx: &VContext<T>,
caller: Caller<T>,
path: ApiUserPath,
body: ApiUserEmailUpdateParams,
) -> Result<HttpResponseOk<ApiUserContactEmail>, HttpError>
where
T: VAppPermission + PermissionStorage,
{
tracing::info!("Setting contact email for user");

let email = ctx
.user
.set_api_user_contact_email(&caller, path.user_id, &body.email)
.await?;

tracing::info!("Set contact email for user");

Ok(HttpResponseOk(email))
}

#[derive(Debug, Clone, Deserialize, JsonSchema)]
pub struct ApiKeyCreateParams<T> {
permissions: Option<Permissions<T>>,
Expand Down
21 changes: 18 additions & 3 deletions v-api/src/endpoints/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@ mod macros {
RequestContext, TypedBody, Body,
};
use http::Response;
use v_model::{Mapper, OAuthClient, OAuthClientRedirectUri, OAuthClientSecret, AccessGroup, ApiUser, MagicLink, MagicLinkRedirectUri, MagicLinkSecret};
use v_model::{Mapper, ApiUserContactEmail, OAuthClient, OAuthClientRedirectUri, OAuthClientSecret, AccessGroup, ApiUser, MagicLink, MagicLinkRedirectUri, MagicLinkSecret};

use v_api::endpoints::{
api_user::{
add_api_user_to_group_op, create_api_user_op, create_api_user_token_op,
delete_api_user_token_op, get_api_user_op, get_api_user_token_op, get_self_op,
link_provider_op, list_api_user_tokens_op, remove_api_user_from_group_op,
link_provider_op, list_api_user_tokens_op, remove_api_user_from_group_op, set_api_user_contact_email_op,
update_api_user_op, AddGroupBody, ApiKeyCreateParams, ApiKeyResponse, ApiUserPath,
ApiUserProviderLinkPayload, ApiUserRemoveGroupPath, ApiUserTokenPath,
ApiUserUpdateParams, GetUserResponse, InitialApiKeyResponse,
ApiUserUpdateParams, GetUserResponse, InitialApiKeyResponse, ApiUserEmailUpdateParams
},
api_user_provider::{
create_link_token_op, ApiUserLinkRequestPayload, ApiUserLinkRequestResponse,
Expand Down Expand Up @@ -492,6 +492,19 @@ mod macros {
delete_api_user_token_op(&rqctx, path.into_inner()).await
}

/// Set the contact email for a user
#[endpoint {
method = PUT,
path = "/api-user/{user_id}/contact/email",
}]
pub async fn set_api_user_contact_email(
rqctx: RequestContext<$context_type>,
path: Path<ApiUserPath>,
body: TypedBody<ApiUserEmailUpdateParams>,
) -> Result<HttpResponseOk<ApiUserContactEmail>, HttpError> {
set_api_user_contact_email_op(&rqctx, path.into_inner(), body.into_inner()).await
}

/// Add a user to a group
#[endpoint {
method = POST,
Expand Down Expand Up @@ -654,6 +667,8 @@ mod macros {
.expect("Failed to register endpoint");
$api.register(update_api_user)
.expect("Failed to register endpoint");
$api.register(set_api_user_contact_email)
.expect("Failed to register endpoint");
$api.register(list_api_user_tokens)
.expect("Failed to register endpoint");
$api.register(get_api_user_token)
Expand Down

0 comments on commit 7ab0f59

Please sign in to comment.