From 281e61d45c9cbe3fe5e3bdebb325d09a290b89ee Mon Sep 17 00:00:00 2001 From: Zack Fu Zi Xiang Date: Wed, 15 May 2024 15:14:35 +0800 Subject: [PATCH] feat: user billing portal --- .../src/af_cloud/impls/user/cloud_service_impl.rs | 9 +++++++++ frontend/rust-lib/flowy-user-pub/src/cloud.rs | 4 ++++ frontend/rust-lib/flowy-user/src/entities/workspace.rs | 6 ++++++ frontend/rust-lib/flowy-user/src/event_handler.rs | 9 +++++++++ frontend/rust-lib/flowy-user/src/event_map.rs | 4 ++++ .../src/user_manager/manager_user_workspace.rs | 10 ++++++++++ 6 files changed, 42 insertions(+) diff --git a/frontend/rust-lib/flowy-server/src/af_cloud/impls/user/cloud_service_impl.rs b/frontend/rust-lib/flowy-server/src/af_cloud/impls/user/cloud_service_impl.rs index 6b406ba2a3..ff4e065bc3 100644 --- a/frontend/rust-lib/flowy-server/src/af_cloud/impls/user/cloud_service_impl.rs +++ b/frontend/rust-lib/flowy-server/src/af_cloud/impls/user/cloud_service_impl.rs @@ -541,6 +541,15 @@ where }) }) } + + fn get_billing_portal_url(&self) -> FutureResult { + let try_get_client = self.server.try_get_client(); + FutureResult::new(async move { + let client = try_get_client?; + let url = client.get_portal_session_link().await?; + Ok(url) + }) + } } async fn get_admin_client(client: &Arc) -> FlowyResult { diff --git a/frontend/rust-lib/flowy-user-pub/src/cloud.rs b/frontend/rust-lib/flowy-user-pub/src/cloud.rs index 7d56bd4d16..0400c8ba51 100644 --- a/frontend/rust-lib/flowy-user-pub/src/cloud.rs +++ b/frontend/rust-lib/flowy-user-pub/src/cloud.rs @@ -289,6 +289,10 @@ pub trait UserCloudService: Send + Sync + 'static { fn get_workspace_usage(&self, workspace_id: String) -> FutureResult { FutureResult::new(async { Err(FlowyError::not_support()) }) } + + fn get_billing_portal_url(&self) -> FutureResult { + FutureResult::new(async { Err(FlowyError::not_support()) }) + } } pub type UserUpdateReceiver = tokio::sync::mpsc::Receiver; diff --git a/frontend/rust-lib/flowy-user/src/entities/workspace.rs b/frontend/rust-lib/flowy-user/src/entities/workspace.rs index c723c04c71..babcbbdb49 100644 --- a/frontend/rust-lib/flowy-user/src/entities/workspace.rs +++ b/frontend/rust-lib/flowy-user/src/entities/workspace.rs @@ -318,3 +318,9 @@ pub struct WorkspaceUsagePB { #[pb(index = 4)] pub total_blob_bytes_limit: u64, } + +#[derive(Debug, ProtoBuf, Default, Clone)] +pub struct BillingPortalPB { + #[pb(index = 1)] + pub url: String, +} diff --git a/frontend/rust-lib/flowy-user/src/event_handler.rs b/frontend/rust-lib/flowy-user/src/event_handler.rs index c8c90afaa1..0e225e6416 100644 --- a/frontend/rust-lib/flowy-user/src/event_handler.rs +++ b/frontend/rust-lib/flowy-user/src/event_handler.rs @@ -826,3 +826,12 @@ pub async fn get_workspace_usage_handler( total_blob_bytes_limit: workspace_usage.total_blob_bytes_limit as u64, }) } + +#[tracing::instrument(level = "debug", skip_all, err)] +pub async fn get_billing_portal_handler( + manager: AFPluginState>, +) -> DataResult { + let manager = upgrade_manager(manager)?; + let url = manager.get_billing_portal_url().await?; + data_result_ok(BillingPortalPB { url }) +} diff --git a/frontend/rust-lib/flowy-user/src/event_map.rs b/frontend/rust-lib/flowy-user/src/event_map.rs index 7f17bc54f9..1fa03d7604 100644 --- a/frontend/rust-lib/flowy-user/src/event_map.rs +++ b/frontend/rust-lib/flowy-user/src/event_map.rs @@ -76,6 +76,7 @@ pub fn init(user_manager: Weak) -> AFPlugin { .event(UserEvent::GetWorkspaceSubscriptions, get_workspace_subscriptions_handler) .event(UserEvent::CancelWorkspaceSubscription, cancel_workspace_subscription_handler) .event(UserEvent::GetWorkspaceUsage, get_workspace_usage_handler) + .event(UserEvent::GetBillingPortal, get_billing_portal_handler) } #[derive(Clone, Copy, PartialEq, Eq, Debug, Display, Hash, ProtoBuf_Enum, Flowy_Event)] @@ -247,6 +248,9 @@ pub enum UserEvent { #[event(input = "UserWorkspaceIdPB", output = "WorkspaceUsagePB")] GetWorkspaceUsage = 54, + + #[event(output = "BillingPortalPB")] + GetBillingPortal = 55, } pub trait UserStatusCallback: Send + Sync + 'static { diff --git a/frontend/rust-lib/flowy-user/src/user_manager/manager_user_workspace.rs b/frontend/rust-lib/flowy-user/src/user_manager/manager_user_workspace.rs index fc067b4abc..9f110f3aa2 100644 --- a/frontend/rust-lib/flowy-user/src/user_manager/manager_user_workspace.rs +++ b/frontend/rust-lib/flowy-user/src/user_manager/manager_user_workspace.rs @@ -460,6 +460,16 @@ impl UserManager { .await?; Ok(workspace_usage) } + + #[instrument(level = "info", skip(self), err)] + pub async fn get_billing_portal_url(&self) -> FlowyResult { + let url = self + .cloud_services + .get_user_service()? + .get_billing_portal_url() + .await?; + Ok(url) + } } /// This method is used to save one user workspace to the SQLite database