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(referral): address referral edge cases (#2048)

Will Hutchinson58262a982083
diff --git a/rust/cloud-storage/macro_db_client/migrations/20260319143005_referral_tracking_constraints.sql b/rust/cloud-storage/macro_db_client/migrations/20260319143005_referral_tracking_constraints.sql
new file mode 100644
index 0000000000..500efc78e3
--- /dev/null
+++ b/rust/cloud-storage/macro_db_client/migrations/20260319143005_referral_tracking_constraints.sql
@@ -0,0 +1,2 @@
+ALTER TABLE public.referral_tracking
+ ADD CONSTRAINT referral_tracking_no_self_referral CHECK (referrer_id != referred_id);
diff --git a/rust/cloud-storage/referral/.sqlx/query-ae72806caeef15e9e4a57b0564601480bcbfd0f0195ef75f197d02f52b843778.json b/rust/cloud-storage/referral/.sqlx/query-a669145c6d8f00b9cdf41d2037c0bcfad130247e99f31c127c6dabcbe1b3790d.json
similarity index 56%
rename from rust/cloud-storage/referral/.sqlx/query-ae72806caeef15e9e4a57b0564601480bcbfd0f0195ef75f197d02f52b843778.json
rename to rust/cloud-storage/referral/.sqlx/query-a669145c6d8f00b9cdf41d2037c0bcfad130247e99f31c127c6dabcbe1b3790d.json
index 7f8c7057a9..b1d35c7959 100644
--- a/rust/cloud-storage/referral/.sqlx/query-ae72806caeef15e9e4a57b0564601480bcbfd0f0195ef75f197d02f52b843778.json
+++ b/rust/cloud-storage/referral/.sqlx/query-a669145c6d8f00b9cdf41d2037c0bcfad130247e99f31c127c6dabcbe1b3790d.json
@@ -1,6 +1,6 @@
{
"db_name": "PostgreSQL",
- "query": "\n UPDATE referral_tracking\n SET status = 'complete'\n WHERE referrer_id = $1 and referred_id = $2",
+ "query": "\n UPDATE referral_tracking\n SET status = 'complete'\n WHERE referrer_id = $1 AND referred_id = $2 AND status != 'complete'",
"describe": {
"columns": [],
"parameters": {
@@ -11,5 +11,5 @@
},
"nullable": []
},
- "hash": "ae72806caeef15e9e4a57b0564601480bcbfd0f0195ef75f197d02f52b843778"
+ "hash": "a669145c6d8f00b9cdf41d2037c0bcfad130247e99f31c127c6dabcbe1b3790d"
}
diff --git a/rust/cloud-storage/referral/src/outbound/pg_referral_repo.rs b/rust/cloud-storage/referral/src/outbound/pg_referral_repo.rs
index 3b4e7b04ef..315d9fe231 100644
--- a/rust/cloud-storage/referral/src/outbound/pg_referral_repo.rs
+++ b/rust/cloud-storage/referral/src/outbound/pg_referral_repo.rs
@@ -131,7 +131,7 @@ impl ReferralRepo for PgReferralRepo {
r#"
UPDATE referral_tracking
SET status = 'complete'
- WHERE referrer_id = $1 and referred_id = $2"#,
+ WHERE referrer_id = $1 AND referred_id = $2 AND status != 'complete'"#,
&referrer_id,
&referred_id
)
diff --git a/rust/cloud-storage/referral/src/outbound/pg_referral_repo/test.rs b/rust/cloud-storage/referral/src/outbound/pg_referral_repo/test.rs
index decffb4535..853d0a2fae 100644
--- a/rust/cloud-storage/referral/src/outbound/pg_referral_repo/test.rs
+++ b/rust/cloud-storage/referral/src/outbound/pg_referral_repo/test.rs
@@ -281,6 +281,32 @@ async fn test_complete_referral_not_tracked(pool: Pool<Postgres>) {
assert!(matches!(result, Err(sqlx::Error::RowNotFound)));
}
+#[sqlx::test(
+ migrator = "MACRO_DB_MIGRATIONS",
+ fixtures(path = "../../../fixtures", scripts("referral_users"))
+)]
+async fn test_complete_referral_already_complete(pool: Pool<Postgres>) {
+ let repo = PgReferralRepo::new(pool);
+
+ let referred_user_id = MacroUserIdStr::parse_from_str("macro|[email protected]")
+ .unwrap()
+ .into_owned();
+ let code = referrer_referral_code();
+
+ // Track and complete the referral
+ repo.track_referral(&referred_user_id.0, &code)
+ .await
+ .unwrap();
+ repo.complete_referral(&referred_user_id.0, &code)
+ .await
+ .unwrap();
+
+ // Second completion should fail — already complete
+ let result = repo.complete_referral(&referred_user_id.0, &code).await;
+
+ assert!(matches!(result, Err(sqlx::Error::RowNotFound)));
+}
+
#[sqlx::test(
migrator = "MACRO_DB_MIGRATIONS",
fixtures(path = "../../../fixtures", scripts("referral_users"))