Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

feat(teams): do not allow non-paying teams to invite or have members join (#3515)

Will Hutchinson1362a4816b93
diff --git a/rust/cloud-storage/.sqlx/query-0744566104aa67d5e88c96c3f1e52eb15603cfb48029d43dc507b9012088517b.json b/rust/cloud-storage/.sqlx/query-0744566104aa67d5e88c96c3f1e52eb15603cfb48029d43dc507b9012088517b.json
new file mode 100644
index 0000000000..acdd919ddd
--- /dev/null
+++ b/rust/cloud-storage/.sqlx/query-0744566104aa67d5e88c96c3f1e52eb15603cfb48029d43dc507b9012088517b.json
@@ -0,0 +1,22 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n SELECT paying\n FROM team\n WHERE id = $1\n ",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "paying",
+ "type_info": "Bool"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid"
+ ]
+ },
+ "nullable": [
+ false
+ ]
+ },
+ "hash": "0744566104aa67d5e88c96c3f1e52eb15603cfb48029d43dc507b9012088517b"
+}
diff --git a/rust/cloud-storage/.sqlx/query-5fe0b9eb83e39b8cb89be6aab89099b157418c1e52132b6940fee9bc8c2da4e2.json b/rust/cloud-storage/.sqlx/query-5fe0b9eb83e39b8cb89be6aab89099b157418c1e52132b6940fee9bc8c2da4e2.json
deleted file mode 100644
index 38f31f37b5..0000000000
--- a/rust/cloud-storage/.sqlx/query-5fe0b9eb83e39b8cb89be6aab89099b157418c1e52132b6940fee9bc8c2da4e2.json
+++ /dev/null
@@ -1,22 +0,0 @@
-{
- "db_name": "PostgreSQL",
- "query": "\n WITH prev AS (\n SELECT crm_enabled FROM team_crm_settings WHERE team_id = $1\n ),\n upsert AS (\n INSERT INTO team_crm_settings (team_id, crm_enabled)\n VALUES ($1, TRUE)\n ON CONFLICT (team_id) DO UPDATE\n SET crm_enabled = TRUE,\n updated_at = now()\n RETURNING crm_enabled\n )\n SELECT COALESCE((SELECT crm_enabled FROM prev), FALSE) AS \"prev_enabled!\"\n FROM upsert\n ",
- "describe": {
- "columns": [
- {
- "ordinal": 0,
- "name": "prev_enabled!",
- "type_info": "Bool"
- }
- ],
- "parameters": {
- "Left": [
- "Uuid"
- ]
- },
- "nullable": [
- null
- ]
- },
- "hash": "5fe0b9eb83e39b8cb89be6aab89099b157418c1e52132b6940fee9bc8c2da4e2"
-}
diff --git a/rust/cloud-storage/.sqlx/query-f04af5974db6b600e88a7c1fd3f39a09a5f4d3185af10d1a7a32e8035b2c5ec1.json b/rust/cloud-storage/.sqlx/query-f04af5974db6b600e88a7c1fd3f39a09a5f4d3185af10d1a7a32e8035b2c5ec1.json
new file mode 100644
index 0000000000..d5989a39ec
--- /dev/null
+++ b/rust/cloud-storage/.sqlx/query-f04af5974db6b600e88a7c1fd3f39a09a5f4d3185af10d1a7a32e8035b2c5ec1.json
@@ -0,0 +1,15 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n UPDATE team\n SET paying = $2\n WHERE id = $1\n ",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Bool"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "f04af5974db6b600e88a7c1fd3f39a09a5f4d3185af10d1a7a32e8035b2c5ec1"
+}
diff --git a/rust/cloud-storage/authentication_service/src/api/webhooks/user/stripe_webhook.rs b/rust/cloud-storage/authentication_service/src/api/webhooks/user/stripe_webhook.rs
index 6eaf38ae1d..4a54433ba5 100644
--- a/rust/cloud-storage/authentication_service/src/api/webhooks/user/stripe_webhook.rs
+++ b/rust/cloud-storage/authentication_service/src/api/webhooks/user/stripe_webhook.rs
@@ -1,4 +1,5 @@
use std::collections::HashMap;
+use std::str::FromStr;
use crate::api::context::ApiContext;
@@ -374,9 +375,10 @@ async fn handle_customer_subscription_event(
.await?;
// Track conversion events to GA and Meta (fire-and-forget)
+ let subscription_id = stripe::SubscriptionId::from_str(subscription_id).unwrap();
track_stripe_subscription(
ctx.analytics_client.clone(),
- subscription_id,
+ &subscription_id,
SubscriptionTrackingData {
ga_client_id,
fbp,
@@ -443,23 +445,39 @@ async fn handle_team_subscription_event<'a>(
macro_db_client::user::patch::update_macro_user_has_trialed(&ctx.db, email, true).await?;
}
+ let subscription_id = stripe::SubscriptionId::from_str(subscription_id).unwrap();
+ ctx.teams_service
+ .patch_team_subscription_id(team_id, &subscription_id)
+ .await?;
+
match subscription_status {
"active" => {
ctx.teams_service
.restore_permissions_for_team_members(team_id)
.await?;
- track_stripe_subscription(ctx.analytics_client.clone(), subscription_id, tracking_data);
+ ctx.teams_service
+ .patch_team_payment_status(team_id, true)
+ .await?;
+
+ track_stripe_subscription(
+ ctx.analytics_client.clone(),
+ &subscription_id,
+ tracking_data,
+ );
Ok(())
}
"canceled" | "past_due" | "paused" | "unpaid" => {
ctx.teams_service
.revoke_permissions_for_team_members(team_id)
.await?;
+ ctx.teams_service
+ .patch_team_payment_status(team_id, false)
+ .await?;
track_stripe_subscription(
ctx.analytics_client.clone(),
- subscription_id,
+ &subscription_id,
SubscriptionTrackingData {
is_new: false,
..tracking_data
@@ -496,7 +514,7 @@ struct SubscriptionTrackingData {
#[tracing::instrument(skip(client, data), fields(subscription_id, email = %data.email, status = %data.status, is_new = data.is_new))]
fn track_stripe_subscription(
client: std::sync::Arc<AnalyticsClient>,
- subscription_id: &str,
+ subscription_id: &stripe::SubscriptionId,
data: SubscriptionTrackingData,
) {
let Some(value_cents) = data.value_cents else {
diff --git a/rust/cloud-storage/macro_db_client/migrations/20260525122155_add_team_paying_boolean.sql b/rust/cloud-storage/macro_db_client/migrations/20260525122155_add_team_paying_boolean.sql
new file mode 100644
index 0000000000..44074e1edc
--- /dev/null
+++ b/rust/cloud-storage/macro_db_client/migrations/20260525122155_add_team_paying_boolean.sql
@@ -0,0 +1,8 @@
+-- Adds a new boolean to the teams table defaulting to false
+ALTER TABLE team
+ ADD COLUMN paying BOOLEAN NOT NULL DEFAULT false;
+
+-- For all existing teams we should set paying to true if they have a subscription id in `subscription_id` field
+UPDATE team
+SET paying = true
+WHERE subscription_id IS NOT NULL;
diff --git a/rust/cloud-storage/teams/src/domain/model.rs b/rust/cloud-storage/teams/src/domain/model.rs
index eae37bea9d..1bc7a34589 100644
--- a/rust/cloud-storage/teams/src/domain/model.rs
+++ b/rust/cloud-storage/teams/src/domain/model.rs
@@ -364,6 +364,9 @@ pub enum TeamError {
/// The team invite does not exist
#[error("The team invite does not exist")]
TeamInviteDoesNotExist,
+ /// The team is not paying
+ #[error("The team is not paying")]
+ TeamNotPaying,
/// Underlying entity access error
#[error("Access error")]
AccessError(#[from] entity_access::domain::models::AccessError),
diff --git a/rust/cloud-storage/teams/src/domain/team_repo.rs b/rust/cloud-storage/teams/src/domain/team_repo.rs
index 54e73fd9db..d38da61332 100644
--- a/rust/cloud-storage/teams/src/domain/team_repo.rs
+++ b/rust/cloud-storage/teams/src/domain/team_repo.rs
@@ -51,6 +51,12 @@ pub trait TeamRepository: Clone + Send + Sync + 'static {
team_id: &uuid::Uuid,
) -> impl Future<Output = Result<Option<stripe::SubscriptionId>, TeamError>> + Send;
+ /// Gets the payment status for a team
+ fn get_team_payment_status(
+ &self,
+ team_id: &uuid::Uuid,
+ ) -> impl Future<Output = Result<bool, TeamError>> + Send;
+
/// Creates a new team
fn create_team(
&self,
@@ -109,6 +115,13 @@ pub trait TeamRepository: Clone + Send + Sync + 'static {
subscription_id: &stripe::SubscriptionId,
) -> impl Future<Output = Result<(), TeamError>> + Send;
+ /// Updates the teams payment status
+ fn update_team_payment_status(
+ &self,
+ team_id: &uuid::Uuid,
+ paying: bool,
+ ) -> impl Future<Output = Result<(), TeamError>> + Send;
+
/// Deletes a team
fn delete_team(
&self,
@@ -321,6 +334,22 @@ pub trait TeamService: Clone + Send + Sync + 'static {
team_id: &uuid::Uuid,
) -> impl Future<Output = Result<(), RestorePermissionsForTeamMembersError>> + Send;
+ /// Patches the team subscription id
+ /// NOTE: this is not exposed via axum and is meant for internal usage within stripe webhook only.
+ fn patch_team_subscription_id(
+ &self,
+ team_id: &uuid::Uuid,
+ subscription_id: &stripe::SubscriptionId,
+ ) -> impl Future<Output = Result<(), TeamError>> + Send;
+
+ /// Patches the teams payment status
+ /// NOTE: this is not exposed via axum and is meant for internal usage within stripe webhook only.
+ fn patch_team_payment_status(
+ &self,
+ team_id: &uuid::Uuid,
+ paying: bool,
+ ) -> impl Future<Output = Result<(), TeamError>> + Send;
+
/// Gets a team by id with all its members
fn get_team(
&self,
diff --git a/rust/cloud-storage/teams/src/domain/team_service.rs b/rust/cloud-storage/teams/src/domain/team_service.rs
index 76e2fcaeb6..dd7bfa67ea 100644
--- a/rust/cloud-storage/teams/src/domain/team_service.rs
+++ b/rust/cloud-storage/teams/src/domain/team_service.rs
@@ -302,6 +302,14 @@ where
let team_id =
macro_uuid::string_to_uuid(&entity_access_receipt.entity().entity_id).unwrap();
+ if !self
+ .team_repository
+ .get_team_payment_status(&team_id)
+ .await?
+ {
+ return Err(InviteUsersToTeamError::TeamError(TeamError::TeamNotPaying));
+ }
+
let invited_by = entity_access_receipt
.get_authenticated_user()
.map_err(|e| InviteUsersToTeamError::TeamError(TeamError::AccessError(e)))?;
@@ -614,6 +622,24 @@ where
let team_member = accepted_invite.member.clone();
+ if !self
+ .team_repository
+ .get_team_payment_status(&team_member.team_id)
+ .await?
+ {
+ self.team_repository
+ .rollback_accept_team_invite(&accepted_invite)
+ .await
+ .inspect_err(|rollback_err| {
+ tracing::error!(
+ error=?rollback_err,
+ "unable to rollback accepted team invite after getting team subscription failed"
+ );
+ })
+ .ok();
+ return Err(JoinTeamError::TeamError(TeamError::TeamNotPaying));
+ }
+
let subscription_id = match self.get_team_subscription(&team_member.team_id).await {
Ok(subscription_id) => subscription_id,
Err(e) => {
@@ -791,6 +817,32 @@ where
Ok(())
}
+ #[tracing::instrument(skip(self), err)]
+ async fn patch_team_subscription_id(
+ &self,
+ team_id: &uuid::Uuid,
+ subscription_id: &stripe::SubscriptionId,
+ ) -> Result<(), TeamError> {
+ self.team_repository
+ .update_team_subscription(team_id, subscription_id)
+ .await?;
+
+ Ok(())
+ }
+
+ #[tracing::instrument(skip(self), err)]
+ async fn patch_team_payment_status(
+ &self,
+ team_id: &uuid::Uuid,
+ paying: bool,
+ ) -> Result<(), TeamError> {
+ self.team_repository
+ .update_team_payment_status(team_id, paying)
+ .await?;
+
+ Ok(())
+ }
+
#[tracing::instrument(skip(self), err)]
async fn get_team(
&self,
diff --git a/rust/cloud-storage/teams/src/domain/team_service/test.rs b/rust/cloud-storage/teams/src/domain/team_service/test.rs
index 06713a3240..d68ef5fe1a 100644
--- a/rust/cloud-storage/teams/src/domain/team_service/test.rs
+++ b/rust/cloud-storage/teams/src/domain/team_service/test.rs
@@ -56,6 +56,7 @@ struct MockTeamRepository {
team_for_get_by_id: Option<Team>,
team_subscription_id: Option<stripe::SubscriptionId>,
stripe_customer_id: Option<stripe::CustomerId>,
+ team_payment_status: bool,
accepted_invite: Option<AcceptedTeamInvite<'static>>,
removed_member: Option<TeamMember<'static>>,
rollback_accept_calls: Arc<Mutex<usize>>,
@@ -79,6 +80,7 @@ impl MockTeamRepository {
team_for_get_by_id: None,
team_subscription_id: None,
stripe_customer_id: None,
+ team_payment_status: true,
accepted_invite: None,
removed_member: None,
rollback_accept_calls: Arc::new(Mutex::new(0)),
@@ -113,6 +115,14 @@ impl TeamRepository for MockTeamRepository {
async move { Ok(subscription_id) }
}
+ fn get_team_payment_status(
+ &self,
+ _: &uuid::Uuid,
+ ) -> impl Future<Output = Result<bool, TeamError>> + Send {
+ let team_payment_status = self.team_payment_status;
+ async move { Ok(team_payment_status) }
+ }
+
fn has_user_trialed(
&self,
_: &MacroUserIdStr<'_>,
@@ -199,6 +209,14 @@ impl TeamRepository for MockTeamRepository {
async { Ok(()) }
}
+ fn update_team_payment_status(
+ &self,
+ _: &uuid::Uuid,
+ _: bool,
+ ) -> impl Future<Output = Result<(), TeamError>> + Send {
+ async { Ok(()) }
+ }
+
fn delete_team(&self, _: &uuid::Uuid) -> impl Future<Output = Result<(), TeamError>> + Send {
async { unimplemented!() }
}
diff --git a/rust/cloud-storage/teams/src/outbound/team_repo.rs b/rust/cloud-storage/teams/src/outbound/team_repo.rs
index df461e2096..023bc63104 100644
--- a/rust/cloud-storage/teams/src/outbound/team_repo.rs
+++ b/rust/cloud-storage/teams/src/outbound/team_repo.rs
@@ -322,6 +322,22 @@ impl TeamRepository for TeamRepositoryImpl {
Ok(team_subscription_id)
}
+ #[tracing::instrument(skip(self), err)]
+ async fn get_team_payment_status(&self, team_id: &uuid::Uuid) -> Result<bool, TeamError> {
+ let paying = sqlx::query_scalar!(
+ r#"
+ SELECT paying
+ FROM team
+ WHERE id = $1
+ "#,
+ team_id,
+ )
+ .fetch_one(&self.pool)
+ .await?;
+
+ Ok(paying)
+ }
+
#[tracing::instrument(skip(self), err)]
async fn create_team(
&self,
@@ -627,6 +643,31 @@ impl TeamRepository for TeamRepositoryImpl {
Ok(())
}
+ #[tracing::instrument(skip(self), err)]
+ async fn update_team_payment_status(
+ &self,
+ team_id: &uuid::Uuid,
+ paying: bool,
+ ) -> Result<(), TeamError> {
+ let result = sqlx::query!(
+ r#"
+ UPDATE team
+ SET paying = $2
+ WHERE id = $1
+ "#,
+ team_id,
+ paying,
+ )
+ .execute(&self.pool)
+ .await?;
+
+ if result.rows_affected() == 0 {
+ return Err(TeamError::TeamDoesNotExist);
+ }
+
+ Ok(())
+ }
+
#[tracing::instrument(skip(self), err)]
async fn delete_team(&self, team_id: &uuid::Uuid) -> Result<(), TeamError> {
sqlx::query!(
diff --git a/rust/cloud-storage/teams/src/outbound/team_repo/test.rs b/rust/cloud-storage/teams/src/outbound/team_repo/test.rs
index 0f731fd742..e9bda3013f 100644
--- a/rust/cloud-storage/teams/src/outbound/team_repo/test.rs
+++ b/rust/cloud-storage/teams/src/outbound/team_repo/test.rs
@@ -94,6 +94,44 @@ async fn test_get_team_subscription_id(pool: Pool<Postgres>) -> anyhow::Result<(
Ok(())
}
+#[sqlx::test(
+ migrator = "MACRO_DB_MIGRATIONS",
+ fixtures(path = "../../../fixtures", scripts("teams"))
+)]
+async fn test_get_team_payment_status(pool: Pool<Postgres>) -> anyhow::Result<()> {
+ let team_repo = TeamRepositoryImpl::new(pool.clone());
+
+ let team_id = macro_uuid::string_to_uuid("11111111-1111-1111-1111-111111111111")?;
+
+ let paying = team_repo.get_team_payment_status(&team_id).await?;
+ assert!(!paying);
+
+ sqlx::query!(
+ r#"
+ UPDATE team
+ SET paying = TRUE
+ WHERE id = $1
+ "#,
+ &team_id,
+ )
+ .execute(&pool)
+ .await?;
+
+ let paying = team_repo.get_team_payment_status(&team_id).await?;
+ assert!(paying);
+
+ let missing_team_id = macro_uuid::string_to_uuid("63333333-3333-3333-3333-333333333333")?;
+ let err = team_repo
+ .get_team_payment_status(&missing_team_id)
+ .await
+ .err()
+ .unwrap();
+
+ assert!(err.to_string().contains("does not exist"));
+
+ Ok(())
+}
+
#[sqlx::test(
migrator = "MACRO_DB_MIGRATIONS",
fixtures(path = "../../../fixtures", scripts("teams"))
@@ -588,6 +626,59 @@ async fn test_update_team_subscription(pool: Pool<Postgres>) -> anyhow::Result<(
Ok(())
}
+#[sqlx::test(
+ migrator = "MACRO_DB_MIGRATIONS",
+ fixtures(path = "../../../fixtures", scripts("teams"))
+)]
+async fn test_update_team_payment_status(pool: Pool<Postgres>) -> anyhow::Result<()> {
+ let team_repo = TeamRepositoryImpl::new(pool.clone());
+
+ let team_id = macro_uuid::string_to_uuid("11111111-1111-1111-1111-111111111111")?;
+
+ team_repo.update_team_payment_status(&team_id, true).await?;
+
+ let row = sqlx::query!(
+ r#"
+ SELECT paying
+ FROM team
+ WHERE id = $1
+ "#,
+ &team_id,
+ )
+ .fetch_one(&pool)
+ .await?;
+
+ assert!(row.paying);
+
+ team_repo
+ .update_team_payment_status(&team_id, false)
+ .await?;
+
+ let row = sqlx::query!(
+ r#"
+ SELECT paying
+ FROM team
+ WHERE id = $1
+ "#,
+ &team_id,
+ )
+ .fetch_one(&pool)
+ .await?;
+
+ assert!(!row.paying);
+
+ let missing_team_id = macro_uuid::string_to_uuid("63333333-3333-3333-3333-333333333333")?;
+ let err = team_repo
+ .update_team_payment_status(&missing_team_id, true)
+ .await
+ .err()
+ .unwrap();
+
+ assert!(err.to_string().contains("does not exist"));
+
+ Ok(())
+}
+
#[sqlx::test(
migrator = "MACRO_DB_MIGRATIONS",
fixtures(path = "../../../fixtures", scripts("teams"))