feat: add api for workspace subscription info (#5717)

This commit is contained in:
Zack 2024-07-11 04:38:17 +08:00 committed by GitHub
parent 647934bf41
commit 4c18b2bc9d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 168 additions and 2 deletions

View File

@ -534,6 +534,18 @@ where
})
}
fn get_workspace_subscription_one(
&self,
workspace_id: String,
) -> FutureResult<Vec<WorkspaceSubscriptionStatus>, FlowyError> {
let try_get_client = self.server.try_get_client();
FutureResult::new(async move {
let client = try_get_client?;
let workspace_subscriptions = client.get_workspace_subscriptions(&workspace_id).await?;
Ok(workspace_subscriptions)
})
}
fn cancel_workspace_subscription(
&self,
workspace_id: String,

View File

@ -284,12 +284,21 @@ pub trait UserCloudService: Send + Sync + 'static {
FutureResult::new(async { Err(FlowyError::not_support()) })
}
/// Get all subscriptions for all workspaces for a user (email)
fn get_workspace_subscriptions(
&self,
) -> FutureResult<Vec<WorkspaceSubscriptionStatus>, FlowyError> {
FutureResult::new(async { Err(FlowyError::not_support()) })
}
/// Get the workspace subscriptions for a workspace
fn get_workspace_subscription_one(
&self,
workspace_id: String,
) -> FutureResult<Vec<WorkspaceSubscriptionStatus>, FlowyError> {
FutureResult::new(async { Err(FlowyError::not_support()) })
}
fn cancel_workspace_subscription(
&self,
workspace_id: String,

View File

@ -1,4 +1,6 @@
use client_api::entity::billing_dto::{RecurringInterval, SubscriptionPlan};
use client_api::entity::billing_dto::{
RecurringInterval, SubscriptionPlan, SubscriptionStatus, WorkspaceSubscriptionStatus,
};
use std::str::FromStr;
use validator::Validate;
@ -454,3 +456,117 @@ impl FromStr for AIModelPB {
}
}
}
#[derive(Debug, ProtoBuf, Default, Clone)]
pub struct WorkspaceSubscriptionInfoPB {
#[pb(index = 1)]
pub plan: WorkspacePlanPB,
#[pb(index = 2)]
pub plan_subscription: WorkspaceSubscriptionV2PB, // valid if plan is not WorkspacePlanFree
#[pb(index = 3)]
pub add_ons: Vec<WorkspaceAddOnPB>,
}
impl From<Vec<WorkspaceSubscriptionStatus>> for WorkspaceSubscriptionInfoPB {
fn from(subs: Vec<WorkspaceSubscriptionStatus>) -> Self {
let mut plan = WorkspacePlanPB::WorkspacePlanFree;
let mut plan_subscription = WorkspaceSubscriptionV2PB::default();
let mut add_ons = Vec::new();
for sub in subs {
match sub.workspace_plan {
SubscriptionPlan::Free => {
plan = WorkspacePlanPB::WorkspacePlanFree;
},
SubscriptionPlan::Pro => {
plan = WorkspacePlanPB::WorkspacePlanPro;
plan_subscription = sub.into();
},
SubscriptionPlan::Team => {
plan = WorkspacePlanPB::WorkspacePlanTeam;
},
SubscriptionPlan::AiMax => {
add_ons.push(WorkspaceAddOnPB {
type_: WorkspaceAddOnPBType::AddOnAiMax,
add_on_subscription: sub.into(),
});
},
SubscriptionPlan::AiLocal => {
add_ons.push(WorkspaceAddOnPB {
type_: WorkspaceAddOnPBType::AddOnAiLocal,
add_on_subscription: sub.into(),
});
},
}
}
WorkspaceSubscriptionInfoPB {
plan,
plan_subscription,
add_ons,
}
}
}
#[derive(ProtoBuf_Enum, Debug, Clone, Eq, PartialEq, Default)]
pub enum WorkspacePlanPB {
#[default]
WorkspacePlanFree = 0,
WorkspacePlanPro = 1,
WorkspacePlanTeam = 2,
}
#[derive(Debug, ProtoBuf, Default, Clone)]
pub struct WorkspaceAddOnPB {
#[pb(index = 1)]
type_: WorkspaceAddOnPBType,
#[pb(index = 2)]
add_on_subscription: WorkspaceSubscriptionV2PB,
}
#[derive(ProtoBuf_Enum, Debug, Clone, Eq, PartialEq, Default)]
pub enum WorkspaceAddOnPBType {
#[default]
AddOnAiLocal = 0,
AddOnAiMax = 1,
}
#[derive(Debug, ProtoBuf, Default, Clone)]
pub struct WorkspaceSubscriptionV2PB {
#[pb(index = 1)]
pub workspace_id: String,
#[pb(index = 2)]
pub subscription_plan: SubscriptionPlanPB,
#[pb(index = 3)]
pub status: WorkspaceSubscriptionStatusPB,
#[pb(index = 4)]
pub end_date: i64,
}
impl From<WorkspaceSubscriptionStatus> for WorkspaceSubscriptionV2PB {
fn from(sub: WorkspaceSubscriptionStatus) -> Self {
Self {
workspace_id: sub.workspace_id,
subscription_plan: sub.workspace_plan.clone().into(),
status: sub.subscription_status.into(),
end_date: sub.current_period_end,
}
}
}
#[derive(ProtoBuf_Enum, Debug, Clone, Eq, PartialEq, Default)]
pub enum WorkspaceSubscriptionStatusPB {
#[default]
Active = 0,
Canceled = 1,
}
impl From<SubscriptionStatus> for WorkspaceSubscriptionStatusPB {
fn from(status: SubscriptionStatus) -> Self {
match status {
SubscriptionStatus::Active => WorkspaceSubscriptionStatusPB::Active,
_ => WorkspaceSubscriptionStatusPB::Canceled,
}
}
}

View File

@ -787,6 +787,19 @@ pub async fn get_workspace_subscriptions_handler(
data_result_ok(RepeatedWorkspaceSubscriptionPB { items: subs })
}
#[tracing::instrument(level = "debug", skip_all, err)]
pub async fn get_workspace_subscription_info_handler(
params: AFPluginData<UserWorkspaceIdPB>,
manager: AFPluginState<Weak<UserManager>>,
) -> DataResult<WorkspaceSubscriptionInfoPB, FlowyError> {
let params = params.try_into_inner()?;
let manager = upgrade_manager(manager)?;
let subs = manager
.get_workspace_subscription_info(params.workspace_id)
.await?;
data_result_ok(WorkspaceSubscriptionInfoPB::from(subs))
}
#[tracing::instrument(level = "debug", skip_all, err)]
pub async fn cancel_workspace_subscription_handler(
param: AFPluginData<CancelWorkspaceSubscriptionPB>,

View File

@ -71,6 +71,7 @@ pub fn init(user_manager: Weak<UserManager>) -> AFPlugin {
// Billing
.event(UserEvent::SubscribeWorkspace, subscribe_workspace_handler)
.event(UserEvent::GetWorkspaceSubscriptions, get_workspace_subscriptions_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)
@ -262,6 +263,9 @@ pub enum UserEvent {
#[event(input = "UserWorkspaceIdPB", output = "UseAISettingPB")]
GetWorkspaceSetting = 58,
#[event(input = "UserWorkspaceIdPB", output = "WorkspaceSubscriptionInfoPB")]
GetWorkspaceSubscriptionInfo = 59,
}
pub trait UserStatusCallback: Send + Sync + 'static {

View File

@ -1,6 +1,6 @@
use chrono::{Duration, NaiveDateTime, Utc};
use client_api::entity::billing_dto::{
RecurringInterval, SubscriptionPlan, WorkspaceUsageAndLimit,
RecurringInterval, SubscriptionPlan, WorkspaceSubscriptionStatus, WorkspaceUsageAndLimit,
};
use std::convert::TryFrom;
use std::sync::Arc;
@ -478,6 +478,18 @@ impl UserManager {
Ok(subscriptions)
}
#[instrument(level = "info", skip(self), err)]
pub async fn get_workspace_subscription_info(
&self,
workspace_id: String,
) -> FlowyResult<Vec<WorkspaceSubscriptionStatus>> {
self
.cloud_services
.get_user_service()?
.get_workspace_subscriptions()
.await
}
async fn get_workspace_subscriptions_from_remote(
&self,
uid: i64,