From 013653bc6696f61046004654c513aa4bf9c6ead7 Mon Sep 17 00:00:00 2001 From: Zack Fu Zi Xiang Date: Wed, 10 Jul 2024 16:12:26 +0800 Subject: [PATCH 1/3] chore: improve billing api --- libs/client-api/src/http_billing.rs | 21 +++++++++++++++++++++ libs/shared-entity/src/dto/billing_dto.rs | 7 ++++--- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/libs/client-api/src/http_billing.rs b/libs/client-api/src/http_billing.rs index 25b76513..3b36fad0 100644 --- a/libs/client-api/src/http_billing.rs +++ b/libs/client-api/src/http_billing.rs @@ -161,4 +161,25 @@ impl Client { .await? .into_data() } + + /// Query all active subscription, minimal information but faster + pub async fn get_active_workspace_subscriptions( + &self, + workspace_id: &str, + ) -> Result, AppResponseError> { + let url = format!( + "{}/billing/api/v1/active-subscription/{}", + self.base_billing_url(), + workspace_id + ); + let resp = self + .http_client_with_auth(Method::GET, &url) + .await? + .send() + .await?; + + AppResponse::>::from_response(resp) + .await? + .into_data() + } } diff --git a/libs/shared-entity/src/dto/billing_dto.rs b/libs/shared-entity/src/dto/billing_dto.rs index bcd7c544..ef059c22 100644 --- a/libs/shared-entity/src/dto/billing_dto.rs +++ b/libs/shared-entity/src/dto/billing_dto.rs @@ -80,17 +80,18 @@ pub enum SubscriptionStatus { Unpaid, } -#[derive(Deserialize, Debug)] +#[derive(Serialize, Deserialize, Debug)] pub struct WorkspaceSubscriptionStatus { pub workspace_id: String, pub workspace_plan: SubscriptionPlan, pub recurring_interval: RecurringInterval, pub subscription_status: SubscriptionStatus, pub subscription_quantity: u64, - pub canceled_at: Option, + pub cancel_at: Option, + pub current_end_date: i64, } -#[derive(Deserialize)] +#[derive(Serialize, Deserialize)] pub struct WorkspaceUsageAndLimit { pub member_count: i64, pub member_count_limit: i64, From 549e0ac9d748f49272059ea2464ebeaac188c979 Mon Sep 17 00:00:00 2001 From: Zack Fu Zi Xiang Date: Wed, 10 Jul 2024 18:41:18 +0800 Subject: [PATCH 2/3] chore: use more consistent names from stripe --- libs/shared-entity/src/dto/billing_dto.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libs/shared-entity/src/dto/billing_dto.rs b/libs/shared-entity/src/dto/billing_dto.rs index ef059c22..b5942c14 100644 --- a/libs/shared-entity/src/dto/billing_dto.rs +++ b/libs/shared-entity/src/dto/billing_dto.rs @@ -88,7 +88,7 @@ pub struct WorkspaceSubscriptionStatus { pub subscription_status: SubscriptionStatus, pub subscription_quantity: u64, pub cancel_at: Option, - pub current_end_date: i64, + pub current_period_end: i64, } #[derive(Serialize, Deserialize)] From e0e7950ae1a3129928120505337c37988bf2235e Mon Sep 17 00:00:00 2001 From: Zack Fu Zi Xiang Date: Wed, 10 Jul 2024 18:54:39 +0800 Subject: [PATCH 3/3] chore: add try from for subscription plan --- libs/shared-entity/src/dto/billing_dto.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/libs/shared-entity/src/dto/billing_dto.rs b/libs/shared-entity/src/dto/billing_dto.rs index b5942c14..1ce62c49 100644 --- a/libs/shared-entity/src/dto/billing_dto.rs +++ b/libs/shared-entity/src/dto/billing_dto.rs @@ -67,6 +67,21 @@ impl AsRef for SubscriptionPlan { } } +impl TryFrom<&str> for SubscriptionPlan { + type Error = String; + + fn try_from(value: &str) -> Result { + match value { + "free" => Ok(SubscriptionPlan::Free), + "pro" => Ok(SubscriptionPlan::Pro), + "team" => Ok(SubscriptionPlan::Team), + "ai_max" => Ok(SubscriptionPlan::AiMax), + "ai_local" => Ok(SubscriptionPlan::AiLocal), + _ => Err(format!("Invalid SubscriptionPlan value: {}", value)), + } + } +} + #[derive(Copy, Clone, Debug, Deserialize, Serialize, Eq, PartialEq)] #[serde(rename_all = "snake_case")] pub enum SubscriptionStatus {