feat: subscribe workspace default impl

This commit is contained in:
Zack Fu Zi Xiang 2024-05-07 14:38:00 +08:00
parent 4b00f9f298
commit 327474bb31
No known key found for this signature in database
6 changed files with 85 additions and 14 deletions

View File

@ -13,8 +13,9 @@ use tokio_stream::wrappers::WatchStream;
use uuid::Uuid;
use crate::entities::{
AuthResponse, Authenticator, Role, UpdateUserProfileParams, UserCredentials, UserProfile,
UserTokenState, UserWorkspace, WorkspaceInvitation, WorkspaceInvitationStatus, WorkspaceMember,
AuthResponse, Authenticator, RecurringInterval, Role, SubscriptionPlan, UpdateUserProfileParams,
UserCredentials, UserProfile, UserTokenState, UserWorkspace, WorkspaceInvitation,
WorkspaceInvitationStatus, WorkspaceMember,
};
#[derive(Debug, Clone, Serialize, Deserialize)]
@ -266,6 +267,16 @@ pub trait UserCloudService: Send + Sync + 'static {
fn leave_workspace(&self, workspace_id: &str) -> FutureResult<(), FlowyError> {
FutureResult::new(async { Ok(()) })
}
fn subscribe_workspace(
&self,
workspace_id: String,
recurring_interval: RecurringInterval,
workspace_subscription_plan: SubscriptionPlan,
success_url: String,
) -> FutureResult<String, FlowyError> {
FutureResult::new(async { Err(FlowyError::not_support()) })
}
}
pub type UserUpdateReceiver = tokio::sync::mpsc::Receiver<UserUpdate>;

View File

@ -421,3 +421,17 @@ pub struct WorkspaceInvitation {
pub status: WorkspaceInvitationStatus,
pub updated_at: DateTime<Utc>,
}
pub enum RecurringInterval {
UndefinedRecurringInterval,
Month,
Year,
}
pub enum SubscriptionPlan {
UndefinedSubscriptionPlan,
Pro,
Team,
}

View File

@ -1,7 +1,9 @@
use validator::Validate;
use flowy_derive::{ProtoBuf, ProtoBuf_Enum};
use flowy_user_pub::entities::{Role, WorkspaceInvitation, WorkspaceMember};
use flowy_user_pub::entities::{
RecurringInterval, Role, SubscriptionPlan, WorkspaceInvitation, WorkspaceMember,
};
use lib_infra::validator_fn::required_not_empty_str;
#[derive(ProtoBuf, Default, Clone)]
@ -198,8 +200,8 @@ pub struct ChangeWorkspaceIconPB {
pub new_icon: String,
}
#[derive(ProtoBuf, Default, Clone, Validate)]
pub struct WorkspaceSubscriptionLinkPB {
#[derive(ProtoBuf, Default, Clone, Validate, Debug)]
pub struct SubscribeWorkspacePB {
#[pb(index = 1)]
#[validate(custom = "required_not_empty_str")]
pub workspace_id: String,
@ -214,7 +216,7 @@ pub struct WorkspaceSubscriptionLinkPB {
pub success_url: String,
}
#[derive(ProtoBuf_Enum, Clone, Default)]
#[derive(ProtoBuf_Enum, Clone, Default, Debug)]
pub enum RecurringIntervalPB {
#[default]
UndefinedRecurringInterval = 0,
@ -223,7 +225,19 @@ pub enum RecurringIntervalPB {
Year = 2,
}
#[derive(ProtoBuf_Enum, Clone, Default)]
impl From<RecurringIntervalPB> for RecurringInterval {
fn from(value: RecurringIntervalPB) -> Self {
match value {
RecurringIntervalPB::Month => RecurringInterval::Month,
RecurringIntervalPB::Year => RecurringInterval::Year,
RecurringIntervalPB::UndefinedRecurringInterval => {
RecurringInterval::UndefinedRecurringInterval
},
}
}
}
#[derive(ProtoBuf_Enum, Clone, Default, Debug)]
pub enum SubscriptionPlanPB {
#[default]
UndefinedSubscriptionPlan = 0,
@ -232,6 +246,16 @@ pub enum SubscriptionPlanPB {
Team = 2,
}
impl From<SubscriptionPlanPB> for SubscriptionPlan {
fn from(value: SubscriptionPlanPB) -> Self {
match value {
SubscriptionPlanPB::Pro => SubscriptionPlan::Pro,
SubscriptionPlanPB::Team => SubscriptionPlan::Team,
SubscriptionPlanPB::UndefinedSubscriptionPlan => SubscriptionPlan::UndefinedSubscriptionPlan,
}
}
}
#[derive(Debug, ProtoBuf, Default, Clone)]
pub struct PaymentLinkPB {
#[pb(index = 1)]

View File

@ -776,9 +776,12 @@ pub async fn leave_workspace_handler(
}
#[tracing::instrument(level = "debug", skip_all, err)]
pub async fn workspace_subscription_link_handler(
data: AFPluginData<WorkspaceSubscriptionLinkPB>,
pub async fn subscribe_workspace_handler(
params: AFPluginData<SubscribeWorkspacePB>,
manager: AFPluginState<Weak<UserManager>>,
) -> DataResult<PaymentLinkPB, FlowyError> {
todo!()
let params = params.try_into_inner()?;
let manager = upgrade_manager(manager)?;
let payment_link = manager.subscribe_workspace(params).await?;
data_result_ok(PaymentLinkPB { payment_link })
}

View File

@ -72,7 +72,7 @@ pub fn init(user_manager: Weak<UserManager>) -> AFPlugin {
.event(UserEvent::ListWorkspaceInvitations, list_workspace_invitations_handler)
.event(UserEvent::AcceptWorkspaceInvitation, accept_workspace_invitations_handler)
// Billing
.event(UserEvent::WorkspaceSubscriptionLink, workspace_subscription_link_handler)
.event(UserEvent::SubscribeWorkspace, subscribe_workspace_handler)
}
#[derive(Clone, Copy, PartialEq, Eq, Debug, Display, Hash, ProtoBuf_Enum, Flowy_Event)]
@ -233,8 +233,8 @@ pub enum UserEvent {
#[event(input = "MagicLinkSignInPB", output = "UserProfilePB")]
MagicLinkSignIn = 50,
#[event(input = "WorkspaceSubscriptionLinkPB", output = "PaymentLinkPB")]
WorkspaceSubscriptionLink = 51,
#[event(input = "SubscribeWorkspacePB", output = "PaymentLinkPB")]
SubscribeWorkspace = 51,
}
pub trait UserStatusCallback: Send + Sync + 'static {

View File

@ -14,7 +14,7 @@ use flowy_user_pub::entities::{
};
use lib_dispatch::prelude::af_spawn;
use crate::entities::{RepeatedUserWorkspacePB, ResetWorkspacePB};
use crate::entities::{RepeatedUserWorkspacePB, ResetWorkspacePB, SubscribeWorkspacePB};
use crate::migrations::AnonUser;
use crate::notification::{send_notification, UserNotification};
use crate::services::data_import::{
@ -401,6 +401,25 @@ impl UserManager {
.await?;
Ok(())
}
#[instrument(level = "info", skip(self), err)]
pub async fn subscribe_workspace(
&self,
workspace_subscription: SubscribeWorkspacePB,
) -> FlowyResult<String> {
let payment_link = self
.cloud_services
.get_user_service()?
.subscribe_workspace(
workspace_subscription.workspace_id,
workspace_subscription.recurring_interval.into(),
workspace_subscription.workspace_subscription_plan.into(),
workspace_subscription.success_url,
)
.await?;
Ok(payment_link)
}
}
pub fn save_user_workspaces(