Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
TypeScript agentaiai-agentsall-in-onecrmcrm-systememaillinearmcpmessagingnotesnotionofficerustslackslack-alternativestartupstartup-toolstypescriptworkspace
feat(crm): skip populate_contact when contact and user share a domain (#3510)
Evan Hutnik6fadcb9c3575
diff --git a/rust/cloud-storage/crm/src/domain/service.rs b/rust/cloud-storage/crm/src/domain/service.rsindex b87ceaf83e..816678166c 100644--- a/rust/cloud-storage/crm/src/domain/service.rs+++ b/rust/cloud-storage/crm/src/domain/service.rs@@ -33,6 +33,11 @@ pub trait CrmService: Clone + Send + Sync + 'static {/// `crm_contacts` row; later populates from other team members can't/// overwrite it. Pass `None` when no display name is available.///+ /// `user_email` is the email address registered against `link_id`+ /// (i.e. the user's own mailbox). When the contact's domain matches+ /// the user's, the call is a no-op: intra-company correspondence+ /// would just fill the team's CRM with the team itself.+ ////// Before the populate transaction, this method ensures/// `crm_domain_directory` has an entry for the email's domain — if/// not, it invokes the@@ -45,6 +50,7 @@ pub trait CrmService: Clone + Send + Sync + 'static {&self,team_id: &uuid::Uuid,link_id: &uuid::Uuid,+ user_email: &str,email: &str,name: Option<&str>,) -> impl Future<Output = Result<(), CrmError>> + Send;@@ -200,6 +206,7 @@ where&self,team_id: &uuid::Uuid,link_id: &uuid::Uuid,+ user_email: &str,email: &str,name: Option<&str>,) -> Result<(), CrmError> {@@ -220,6 +227,25 @@ where)));}+ // Skip when the contact lives on the user's own domain —+ // colleagues at the user's company shouldn't show up in their+ // team's CRM. Malformed user emails fall through to the regular+ // populate path; the link's email is treated as the source of+ // truth elsewhere, so we don't want a bad value here to error+ // out an otherwise valid contact populate.+ if let Some((user_local_part, user_domain)) = user_email.trim().split_once('@')+ && !user_local_part.is_empty()+ && !user_domain.is_empty()+ && !user_domain.contains('@')+ && user_domain.eq_ignore_ascii_case(domain)+ {+ tracing::debug!(+ domain,+ "Skipping CRM populate for contact on the user's own domain"+ );+ return Ok(());+ }+// Skip personal / free-mail-provider domains (gmail, yahoo,// hotmail, …). CRM rows are meant to represent companiesif is_generic_email_domain(domain) {diff --git a/rust/cloud-storage/crm/src/outbound/companies_repo/test.rs b/rust/cloud-storage/crm/src/outbound/companies_repo/test.rsindex 41a4f3ebc3..347fb1c4d4 100644--- a/rust/cloud-storage/crm/src/outbound/companies_repo/test.rs+++ b/rust/cloud-storage/crm/src/outbound/companies_repo/test.rs@@ -557,3 +557,120 @@ async fn set_contact_hidden_isolates_contacts_across_teams(pool: PgPool) -> anyhassert_eq!(fetch_contact_hidden(&pool, contact_a).await?, Some(false));Ok(())}++async fn enable_crm_for_team(pool: &PgPool, team_id: Uuid) -> sqlx::Result<()> {+ sqlx::query(+ r#"INSERT INTO team_crm_settings (team_id, crm_enabled) VALUES ($1, TRUE)+ ON CONFLICT (team_id) DO UPDATE SET crm_enabled = TRUE"#,+ )+ .bind(team_id)+ .execute(pool)+ .await?;+ Ok(())+}++async fn count_companies_for_domain(+ pool: &PgPool,+ team_id: Uuid,+ domain: &str,+) -> sqlx::Result<i64> {+ let (count,): (i64,) = sqlx::query_as(+ r#"SELECT COUNT(*) FROM crm_companies c+ JOIN crm_domains d ON d.company_id = c.id+ WHERE c.team_id = $1 AND LOWER(d.domain) = LOWER($2)"#,+ )+ .bind(team_id)+ .bind(domain)+ .fetch_one(pool)+ .await?;+ Ok(count)+}++#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]+async fn service_populate_contact_skips_when_domain_matches_user_domain(+ pool: PgPool,+) -> anyhow::Result<()> {+ let team_id = Uuid::now_v7();+ let owner_id = "macro|[email protected]";+ seed_team(&pool, team_id, owner_id).await?;+ enable_crm_for_team(&pool, team_id).await?;+ let link_id = insert_email_link(&pool, owner_id, "[email protected]").await?;++ let service = CrmServiceImpl::new(+ CompaniesRepositoryImpl::new(pool.clone()),+ NoOpCompanyMetadataResolver,+ );++ service+ .populate_contact(+ &team_id,+ &link_id,+ "[email protected]",+ "[email protected]",+ None,+ )+ .await?;++ assert_eq!(+ count_companies_for_domain(&pool, team_id, "macro.com").await?,+ 0,+ "contact on the user's own domain must not create a CRM row"+ );+ Ok(())+}++#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]+async fn service_populate_contact_same_domain_check_is_case_insensitive(+ pool: PgPool,+) -> anyhow::Result<()> {+ let team_id = Uuid::now_v7();+ let owner_id = "macro|[email protected]";+ seed_team(&pool, team_id, owner_id).await?;+ enable_crm_for_team(&pool, team_id).await?;+ let link_id = insert_email_link(&pool, owner_id, "[email protected]").await?;++ let service = CrmServiceImpl::new(+ CompaniesRepositoryImpl::new(pool.clone()),+ NoOpCompanyMetadataResolver,+ );++ service+ .populate_contact(+ &team_id,+ &link_id,+ "[email protected]",+ "[email protected]",+ None,+ )+ .await?;++ assert_eq!(+ count_companies_for_domain(&pool, team_id, "macro.com").await?,+ 0+ );+ Ok(())+}++#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]+async fn service_populate_contact_writes_when_domain_differs(pool: PgPool) -> anyhow::Result<()> {+ let team_id = Uuid::now_v7();+ let owner_id = "macro|[email protected]";+ seed_team(&pool, team_id, owner_id).await?;+ enable_crm_for_team(&pool, team_id).await?;+ let link_id = insert_email_link(&pool, owner_id, "[email protected]").await?;++ let service = CrmServiceImpl::new(+ CompaniesRepositoryImpl::new(pool.clone()),+ NoOpCompanyMetadataResolver,+ );++ service+ .populate_contact(&team_id, &link_id, "[email protected]", "[email protected]", None)+ .await?;++ assert_eq!(+ count_companies_for_domain(&pool, team_id, "acme.com").await?,+ 1+ );+ Ok(())+}diff --git a/rust/cloud-storage/email_service/src/pubsub/backfill/populate_crm_contact.rs b/rust/cloud-storage/email_service/src/pubsub/backfill/populate_crm_contact.rsindex c5e8623ea0..a312194c27 100644--- a/rust/cloud-storage/email_service/src/pubsub/backfill/populate_crm_contact.rs+++ b/rust/cloud-storage/email_service/src/pubsub/backfill/populate_crm_contact.rs@@ -47,6 +47,7 @@ pub async fn populate_crm_contact(.populate_contact(&team_id,&link.id,+ link.email_address.0.as_ref(),&p.contact_email,p.contact_name.as_deref(),)