From 00312f6309462f50fdeb5a11f26a7d406533c2e0 Mon Sep 17 00:00:00 2001 From: Zack Date: Mon, 15 Jul 2024 22:00:19 +0800 Subject: [PATCH] chore: update api for billing (#5735) --- frontend/rust-lib/Cargo.lock | 24 +++++++------- frontend/rust-lib/Cargo.toml | 4 +-- .../af_cloud/impls/user/cloud_service_impl.rs | 31 +++++++++++++++++-- frontend/rust-lib/flowy-user-pub/src/cloud.rs | 9 ++++++ .../flowy-user/src/entities/workspace.rs | 13 ++++++++ .../rust-lib/flowy-user/src/event_handler.rs | 16 ++++++++++ frontend/rust-lib/flowy-user/src/event_map.rs | 6 +++- .../user_manager/manager_user_workspace.rs | 16 ++++++++++ 8 files changed, 102 insertions(+), 17 deletions(-) diff --git a/frontend/rust-lib/Cargo.lock b/frontend/rust-lib/Cargo.lock index 63e428742f..c1d6e89661 100644 --- a/frontend/rust-lib/Cargo.lock +++ b/frontend/rust-lib/Cargo.lock @@ -163,7 +163,7 @@ checksum = "b3d1d046238990b9cf5bcde22a3fb3584ee5cf65fb2765f454ed428c7a0063da" [[package]] name = "app-error" version = "0.1.0" -source = "git+https://github.com/AppFlowy-IO/AppFlowy-Cloud?rev=e0e7950ae1a3129928120505337c37988bf2235e#e0e7950ae1a3129928120505337c37988bf2235e" +source = "git+https://github.com/AppFlowy-IO/AppFlowy-Cloud?rev=2a145c2f8c6293bc2da12b8322386ee2731a7c2d#2a145c2f8c6293bc2da12b8322386ee2731a7c2d" dependencies = [ "anyhow", "bincode", @@ -183,7 +183,7 @@ dependencies = [ [[package]] name = "appflowy-ai-client" version = "0.1.0" -source = "git+https://github.com/AppFlowy-IO/AppFlowy-Cloud?rev=e0e7950ae1a3129928120505337c37988bf2235e#e0e7950ae1a3129928120505337c37988bf2235e" +source = "git+https://github.com/AppFlowy-IO/AppFlowy-Cloud?rev=2a145c2f8c6293bc2da12b8322386ee2731a7c2d#2a145c2f8c6293bc2da12b8322386ee2731a7c2d" dependencies = [ "anyhow", "bytes", @@ -698,7 +698,7 @@ dependencies = [ [[package]] name = "client-api" version = "0.2.0" -source = "git+https://github.com/AppFlowy-IO/AppFlowy-Cloud?rev=e0e7950ae1a3129928120505337c37988bf2235e#e0e7950ae1a3129928120505337c37988bf2235e" +source = "git+https://github.com/AppFlowy-IO/AppFlowy-Cloud?rev=2a145c2f8c6293bc2da12b8322386ee2731a7c2d#2a145c2f8c6293bc2da12b8322386ee2731a7c2d" dependencies = [ "again", "anyhow", @@ -748,7 +748,7 @@ dependencies = [ [[package]] name = "client-api-entity" version = "0.1.0" -source = "git+https://github.com/AppFlowy-IO/AppFlowy-Cloud?rev=e0e7950ae1a3129928120505337c37988bf2235e#e0e7950ae1a3129928120505337c37988bf2235e" +source = "git+https://github.com/AppFlowy-IO/AppFlowy-Cloud?rev=2a145c2f8c6293bc2da12b8322386ee2731a7c2d#2a145c2f8c6293bc2da12b8322386ee2731a7c2d" dependencies = [ "collab-entity", "collab-rt-entity", @@ -760,7 +760,7 @@ dependencies = [ [[package]] name = "client-websocket" version = "0.1.0" -source = "git+https://github.com/AppFlowy-IO/AppFlowy-Cloud?rev=e0e7950ae1a3129928120505337c37988bf2235e#e0e7950ae1a3129928120505337c37988bf2235e" +source = "git+https://github.com/AppFlowy-IO/AppFlowy-Cloud?rev=2a145c2f8c6293bc2da12b8322386ee2731a7c2d#2a145c2f8c6293bc2da12b8322386ee2731a7c2d" dependencies = [ "futures-channel", "futures-util", @@ -969,7 +969,7 @@ dependencies = [ [[package]] name = "collab-rt-entity" version = "0.1.0" -source = "git+https://github.com/AppFlowy-IO/AppFlowy-Cloud?rev=e0e7950ae1a3129928120505337c37988bf2235e#e0e7950ae1a3129928120505337c37988bf2235e" +source = "git+https://github.com/AppFlowy-IO/AppFlowy-Cloud?rev=2a145c2f8c6293bc2da12b8322386ee2731a7c2d#2a145c2f8c6293bc2da12b8322386ee2731a7c2d" dependencies = [ "anyhow", "bincode", @@ -994,7 +994,7 @@ dependencies = [ [[package]] name = "collab-rt-protocol" version = "0.1.0" -source = "git+https://github.com/AppFlowy-IO/AppFlowy-Cloud?rev=e0e7950ae1a3129928120505337c37988bf2235e#e0e7950ae1a3129928120505337c37988bf2235e" +source = "git+https://github.com/AppFlowy-IO/AppFlowy-Cloud?rev=2a145c2f8c6293bc2da12b8322386ee2731a7c2d#2a145c2f8c6293bc2da12b8322386ee2731a7c2d" dependencies = [ "anyhow", "async-trait", @@ -1311,7 +1311,7 @@ checksum = "c2e66c9d817f1720209181c316d28635c050fa304f9c79e47a520882661b7308" [[package]] name = "database-entity" version = "0.1.0" -source = "git+https://github.com/AppFlowy-IO/AppFlowy-Cloud?rev=e0e7950ae1a3129928120505337c37988bf2235e#e0e7950ae1a3129928120505337c37988bf2235e" +source = "git+https://github.com/AppFlowy-IO/AppFlowy-Cloud?rev=2a145c2f8c6293bc2da12b8322386ee2731a7c2d#2a145c2f8c6293bc2da12b8322386ee2731a7c2d" dependencies = [ "anyhow", "app-error", @@ -2618,7 +2618,7 @@ dependencies = [ [[package]] name = "gotrue" version = "0.1.0" -source = "git+https://github.com/AppFlowy-IO/AppFlowy-Cloud?rev=e0e7950ae1a3129928120505337c37988bf2235e#e0e7950ae1a3129928120505337c37988bf2235e" +source = "git+https://github.com/AppFlowy-IO/AppFlowy-Cloud?rev=2a145c2f8c6293bc2da12b8322386ee2731a7c2d#2a145c2f8c6293bc2da12b8322386ee2731a7c2d" dependencies = [ "anyhow", "futures-util", @@ -2635,7 +2635,7 @@ dependencies = [ [[package]] name = "gotrue-entity" version = "0.1.0" -source = "git+https://github.com/AppFlowy-IO/AppFlowy-Cloud?rev=e0e7950ae1a3129928120505337c37988bf2235e#e0e7950ae1a3129928120505337c37988bf2235e" +source = "git+https://github.com/AppFlowy-IO/AppFlowy-Cloud?rev=2a145c2f8c6293bc2da12b8322386ee2731a7c2d#2a145c2f8c6293bc2da12b8322386ee2731a7c2d" dependencies = [ "anyhow", "app-error", @@ -3000,7 +3000,7 @@ dependencies = [ [[package]] name = "infra" version = "0.1.0" -source = "git+https://github.com/AppFlowy-IO/AppFlowy-Cloud?rev=e0e7950ae1a3129928120505337c37988bf2235e#e0e7950ae1a3129928120505337c37988bf2235e" +source = "git+https://github.com/AppFlowy-IO/AppFlowy-Cloud?rev=2a145c2f8c6293bc2da12b8322386ee2731a7c2d#2a145c2f8c6293bc2da12b8322386ee2731a7c2d" dependencies = [ "anyhow", "bytes", @@ -5101,7 +5101,7 @@ dependencies = [ [[package]] name = "shared-entity" version = "0.1.0" -source = "git+https://github.com/AppFlowy-IO/AppFlowy-Cloud?rev=e0e7950ae1a3129928120505337c37988bf2235e#e0e7950ae1a3129928120505337c37988bf2235e" +source = "git+https://github.com/AppFlowy-IO/AppFlowy-Cloud?rev=2a145c2f8c6293bc2da12b8322386ee2731a7c2d#2a145c2f8c6293bc2da12b8322386ee2731a7c2d" dependencies = [ "anyhow", "app-error", diff --git a/frontend/rust-lib/Cargo.toml b/frontend/rust-lib/Cargo.toml index 3fab944af0..18ecddd5d1 100644 --- a/frontend/rust-lib/Cargo.toml +++ b/frontend/rust-lib/Cargo.toml @@ -97,8 +97,8 @@ validator = { version = "0.16.1", features = ["derive"] } # Run the script.add_workspace_members: # scripts/tool/update_client_api_rev.sh new_rev_id # ⚠️⚠️⚠️️ -client-api = { git = "https://github.com/AppFlowy-IO/AppFlowy-Cloud", rev = "e0e7950ae1a3129928120505337c37988bf2235e" } -client-api-entity = { git = "https://github.com/AppFlowy-IO/AppFlowy-Cloud", rev = "e0e7950ae1a3129928120505337c37988bf2235e" } +client-api = { git = "https://github.com/AppFlowy-IO/AppFlowy-Cloud", rev = "2a145c2f8c6293bc2da12b8322386ee2731a7c2d" } +client-api-entity = { git = "https://github.com/AppFlowy-IO/AppFlowy-Cloud", rev = "2a145c2f8c6293bc2da12b8322386ee2731a7c2d" } [profile.dev] opt-level = 1 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 f741f61715..b71ffe064a 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 @@ -3,7 +3,8 @@ use std::sync::Arc; use anyhow::anyhow; use client_api::entity::billing_dto::{ - RecurringInterval, SubscriptionPlan, WorkspaceSubscriptionStatus, WorkspaceUsageAndLimit, + RecurringInterval, SetSubscriptionRecurringInterval, SubscriptionCancelRequest, SubscriptionPlan, + WorkspaceSubscriptionStatus, WorkspaceUsageAndLimit, }; use client_api::entity::workspace_dto::{ CreateWorkspaceParam, PatchWorkspaceParam, WorkspaceMemberChangeset, WorkspaceMemberInvitation, @@ -554,7 +555,13 @@ where let try_get_client = self.server.try_get_client(); FutureResult::new(async move { let client = try_get_client?; - client.cancel_subscription(&workspace_id, &plan).await?; + client + .cancel_subscription(&SubscriptionCancelRequest { + workspace_id, + plan, + sync: true, + }) + .await?; Ok(()) }) } @@ -580,6 +587,26 @@ where }) } + fn update_workspace_subscription_payment_period( + &self, + workspace_id: String, + plan: SubscriptionPlan, + recurring_interval: RecurringInterval, + ) -> FutureResult<(), FlowyError> { + let try_get_client = self.server.try_get_client(); + FutureResult::new(async move { + let client = try_get_client?; + client + .set_subscription_recurring_interval(&SetSubscriptionRecurringInterval { + workspace_id, + plan, + recurring_interval, + }) + .await?; + Ok(()) + }) + } + fn get_workspace_setting( &self, workspace_id: &str, diff --git a/frontend/rust-lib/flowy-user-pub/src/cloud.rs b/frontend/rust-lib/flowy-user-pub/src/cloud.rs index 1f565b18c6..5c5be4733e 100644 --- a/frontend/rust-lib/flowy-user-pub/src/cloud.rs +++ b/frontend/rust-lib/flowy-user-pub/src/cloud.rs @@ -318,6 +318,15 @@ pub trait UserCloudService: Send + Sync + 'static { FutureResult::new(async { Err(FlowyError::not_support()) }) } + fn update_workspace_subscription_payment_period( + &self, + workspace_id: String, + plan: SubscriptionPlan, + recurring_interval: RecurringInterval, + ) -> FutureResult<(), FlowyError> { + FutureResult::new(async { Err(FlowyError::not_support()) }) + } + fn get_workspace_setting( &self, workspace_id: &str, diff --git a/frontend/rust-lib/flowy-user/src/entities/workspace.rs b/frontend/rust-lib/flowy-user/src/entities/workspace.rs index dabda55184..5e03cc386e 100644 --- a/frontend/rust-lib/flowy-user/src/entities/workspace.rs +++ b/frontend/rust-lib/flowy-user/src/entities/workspace.rs @@ -662,3 +662,16 @@ impl From for WorkspaceSubscriptionStatusPB { } } } + +#[derive(ProtoBuf, Default, Clone, Validate)] +pub struct UpdateWorkspaceSubscriptionPaymentPeriodPB { + #[pb(index = 1)] + #[validate(custom = "required_not_empty_str")] + pub workspace_id: String, + + #[pb(index = 2)] + pub plan: SubscriptionPlanPB, + + #[pb(index = 3)] + pub recurring_interval: RecurringIntervalPB, +} diff --git a/frontend/rust-lib/flowy-user/src/event_handler.rs b/frontend/rust-lib/flowy-user/src/event_handler.rs index 8883626c40..b003ee7a1d 100644 --- a/frontend/rust-lib/flowy-user/src/event_handler.rs +++ b/frontend/rust-lib/flowy-user/src/event_handler.rs @@ -841,6 +841,22 @@ pub async fn invalidate_workspace_subscription_info_cache_handler( .await } +#[tracing::instrument(level = "debug", skip_all, err)] +pub async fn update_workspace_subscription_payment_period_handler( + params: AFPluginData, + manager: AFPluginState>, +) -> FlowyResult<()> { + let params = params.try_into_inner().unwrap(); + let manager = upgrade_manager(manager).unwrap(); + manager + .update_workspace_subscription_payment_period( + params.workspace_id, + params.plan.into(), + params.recurring_interval.into(), + ) + .await +} + #[tracing::instrument(level = "debug", skip_all, err)] pub async fn get_workspace_member_info( param: AFPluginData, diff --git a/frontend/rust-lib/flowy-user/src/event_map.rs b/frontend/rust-lib/flowy-user/src/event_map.rs index 0eb9745beb..ea04f35dfb 100644 --- a/frontend/rust-lib/flowy-user/src/event_map.rs +++ b/frontend/rust-lib/flowy-user/src/event_map.rs @@ -70,11 +70,12 @@ pub fn init(user_manager: Weak) -> AFPlugin { .event(UserEvent::AcceptWorkspaceInvitation, accept_workspace_invitations_handler) // Billing .event(UserEvent::SubscribeWorkspace, subscribe_workspace_handler) - .event(UserEvent::GetWorkspaceSubscriptionInfo, get_workspace_subscription_info_handler) + .event(UserEvent::GetWorkspaceSubscriptionInfo, get_workspace_subscription_info_handler) .event(UserEvent::CancelWorkspaceSubscription, cancel_workspace_subscription_handler) .event(UserEvent::GetWorkspaceUsage, get_workspace_usage_handler) .event(UserEvent::GetBillingPortal, get_billing_portal_handler) .event(UserEvent::InvalidateWorkspaceSubscriptionInfoCache, invalidate_workspace_subscription_info_cache_handler) + .event(UserEvent::UpdateWorkspaceSubscriptionPaymentPeriod, update_workspace_subscription_payment_period_handler) // Workspace Setting .event(UserEvent::UpdateWorkspaceSetting, update_workspace_setting) .event(UserEvent::GetWorkspaceSetting, get_workspace_setting) @@ -265,6 +266,9 @@ pub enum UserEvent { #[event(input = "UserWorkspaceIdPB")] InvalidateWorkspaceSubscriptionInfoCache = 60, + + #[event(input = "UpdateWorkspaceSubscriptionPaymentPeriodPB")] + UpdateWorkspaceSubscriptionPaymentPeriod = 61, } 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 7a6dc27f48..9058af2e44 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 @@ -1,4 +1,5 @@ use chrono::{Duration, NaiveDateTime, Utc}; +use client_api::entity::billing_dto::RecurringInterval; use client_api::entity::billing_dto::{SubscriptionPlan, WorkspaceUsageAndLimit}; use std::convert::TryFrom; @@ -523,6 +524,21 @@ impl UserManager { delete_workspace_subscription_from_cache(db, &workspace_id) } + #[instrument(level = "info", skip(self), err)] + pub async fn update_workspace_subscription_payment_period( + &self, + workspace_id: String, + plan: SubscriptionPlan, + recurring_interval: RecurringInterval, + ) -> FlowyResult<()> { + self + .cloud_services + .get_user_service()? + .update_workspace_subscription_payment_period(workspace_id, plan, recurring_interval) + .await?; + Ok(()) + } + #[instrument(level = "info", skip(self), err)] pub async fn get_workspace_usage( &self,