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
refactor(cleanup): owner-aware user deletion (MACRO-3380) (#6831)
Will Hutchinson3b64cbef13b4
diff --git a/.sqlx/query-2d37c7d9327edc0989b655a735a6542f9b85c1fc99dc6f806344112e0a125a20.json b/.sqlx/query-2d37c7d9327edc0989b655a735a6542f9b85c1fc99dc6f806344112e0a125a20.jsonnew file mode 100644index 0000000000..d8dfd2c49f--- /dev/null+++ b/.sqlx/query-2d37c7d9327edc0989b655a735a6542f9b85c1fc99dc6f806344112e0a125a20.json@@ -0,0 +1,26 @@+{+ "db_name": "PostgreSQL",+ "query": "\n SELECT 'document' AS \"kind!\", id AS \"id!\" FROM \"Document\"\n UNION ALL SELECT 'chat', id FROM \"Chat\"\n UNION ALL SELECT 'project', id FROM \"Project\"\n UNION ALL SELECT 'entity', id::text FROM entity\n UNION ALL SELECT CASE WHEN granted_from_project_id IS NULL THEN 'access' ELSE 'inherited' END,\n entity_id::text FROM entity_access\n ORDER BY 1, 2\n ",+ "describe": {+ "columns": [+ {+ "ordinal": 0,+ "name": "kind!",+ "type_info": "Text"+ },+ {+ "ordinal": 1,+ "name": "id!",+ "type_info": "Text"+ }+ ],+ "parameters": {+ "Left": []+ },+ "nullable": [+ null,+ null+ ]+ },+ "hash": "2d37c7d9327edc0989b655a735a6542f9b85c1fc99dc6f806344112e0a125a20"+}diff --git a/crates/macro_db_client/fixtures/user_deletion_owners.sql b/crates/macro_db_client/fixtures/user_deletion_owners.sqlnew file mode 100644index 0000000000..deacff2e94--- /dev/null+++ b/crates/macro_db_client/fixtures/user_deletion_owners.sql@@ -0,0 +1,55 @@+INSERT INTO macro_user (id, username, email, stripe_customer_id)+VALUES ('01900000-0000-7000-8000-000000000001', 'cleanup', '[email protected]', 'cleanup-stripe-id');++-- Non-user principals still need compatibility rows for the legacy owner FKs.+INSERT INTO "User" (id, email, macro_user_id)+VALUES+ ('macro|[email protected]', '[email protected]', '01900000-0000-7000-8000-000000000001'),+ ('macro|[email protected]', '[email protected]', '01900000-0000-7000-8000-000000000001'),+ ('bot|01900000-0000-7000-8000-000000000002', '[email protected]', '01900000-0000-7000-8000-000000000001'),+ ('01900000-0000-7000-8000-000000000003', '[email protected]', '01900000-0000-7000-8000-000000000001');++INSERT INTO entity (id, entity_type, owner_type, owner_id)+VALUES+ ('01900000-0000-7000-8000-000000000101', 'document', 'user', 'macro|[email protected]'),+ ('01900000-0000-7000-8000-000000000102', 'chat', 'user', 'macro|[email protected]'),+ ('01900000-0000-7000-8000-000000000103', 'project', 'user', 'macro|[email protected]'),+ ('01900000-0000-7000-8000-000000000201', 'document', 'user', 'macro|[email protected]'),+ ('01900000-0000-7000-8000-000000000202', 'chat', 'user', 'macro|[email protected]'),+ ('01900000-0000-7000-8000-000000000203', 'project', 'user', 'macro|[email protected]'),+ ('01900000-0000-7000-8000-000000000301', 'document', 'bot', 'bot|01900000-0000-7000-8000-000000000002'),+ ('01900000-0000-7000-8000-000000000302', 'chat', 'bot', 'bot|01900000-0000-7000-8000-000000000002'),+ ('01900000-0000-7000-8000-000000000303', 'project', 'bot', 'bot|01900000-0000-7000-8000-000000000002'),+ ('01900000-0000-7000-8000-000000000401', 'document', 'team', '01900000-0000-7000-8000-000000000003'),+ ('01900000-0000-7000-8000-000000000402', 'chat', 'team', '01900000-0000-7000-8000-000000000003'),+ ('01900000-0000-7000-8000-000000000403', 'project', 'team', '01900000-0000-7000-8000-000000000003');++INSERT INTO "Document" (id, name, owner, "deletedAt")+SELECT id::text, 'cleanup document', owner_id,+ CASE WHEN owner_id = 'macro|[email protected]' THEN now() END+FROM entity WHERE entity_type = 'document';++INSERT INTO "Chat" (id, name, "userId")+SELECT id::text, 'cleanup chat', owner_id FROM entity WHERE entity_type = 'chat';++INSERT INTO "Project" (id, name, "userId")+SELECT id::text, 'cleanup project', owner_id FROM entity WHERE entity_type = 'project';++-- Include a UUID document whose registry record has not been backfilled yet.+INSERT INTO "Document" (id, name, owner)+VALUES ('01900000-0000-7000-8000-000000000104', 'unregistered', 'macro|[email protected]');++-- All resources are shared with the deleted user AND another user. Cleanup must+-- remove every grant on owned resources, but not grants on non-owned resources.+INSERT INTO entity_access (entity_id, entity_type, source_id, source_type, access_level)+SELECT e.id, e.entity_type, u.id, 'user', 'view'+FROM entity e+CROSS JOIN "User" u+WHERE u.id IN ('macro|[email protected]', 'macro|[email protected]');++INSERT INTO entity_access (entity_id, entity_type, source_id, source_type, access_level)+VALUES ('01900000-0000-7000-8000-000000000104', 'document', 'macro|[email protected]', 'user', 'view');++-- The unrelated document survives, but this inherited grant must not.+INSERT INTO entity_access (entity_id, entity_type, source_id, source_type, access_level, granted_from_project_id)+VALUES ('01900000-0000-7000-8000-000000000201', 'document', 'macro|[email protected]', 'user', 'view', '01900000-0000-7000-8000-000000000103');diff --git a/crates/macro_db_client/src/user/delete_user_dss_items/delete_chats.rs b/crates/macro_db_client/src/user/delete_user_dss_items/delete_chats.rsindex 12306ecb22..7739b5b221 100644--- a/crates/macro_db_client/src/user/delete_user_dss_items/delete_chats.rs+++ b/crates/macro_db_client/src/user/delete_user_dss_items/delete_chats.rs@@ -54,12 +54,13 @@ pub async fn delete_user_chats(.execute(transaction.as_mut()).await?;+ let chat_uuids = user_chats+ .iter()+ .filter_map(|id| macro_uuid::string_to_uuid(id).ok())+ .collect::<Vec<_>>();crate::item_access::delete::delete_user_entity_access_bulk(transaction,- &user_chats- .iter()- .filter_map(|p| macro_uuid::string_to_uuid(p).ok())- .collect::<Vec<uuid::Uuid>>(),+ &chat_uuids,EntityType::Chat,).await?;@@ -75,39 +76,9 @@ pub async fn delete_user_chats(.execute(transaction.as_mut()).await?;- Ok(())-}--#[cfg(test)]-mod tests {- use super::*;- use sqlx::{Pool, Postgres};-- #[sqlx::test(fixtures(- path = "../../../fixtures",- scripts("basic_user_with_lots_of_documents")- ))]- async fn test_delete_user_chats(pool: Pool<Postgres>) -> anyhow::Result<()> {- let mut transaction = pool.begin().await?;- delete_user_chats(&mut transaction, "macro|[email protected]").await?;- transaction.commit().await?;-- let chats = sqlx::query!(- r#"- SELECT- c.id- FROM- "Chat" c- WHERE- c."userId" = $1- "#,- "macro|[email protected]"- )- .fetch_all(&pool)- .await?;-- assert_eq!(chats.len(), 0);-- Ok(())+ for chat_uuid in chat_uuids {+ entity_registry_db_utils::delete_entity(transaction, chat_uuid).await?;}++ Ok(())}diff --git a/crates/macro_db_client/src/user/delete_user_dss_items/delete_documents.rs b/crates/macro_db_client/src/user/delete_user_dss_items/delete_documents.rsindex 61d056cfcf..3fed7d52f1 100644--- a/crates/macro_db_client/src/user/delete_user_dss_items/delete_documents.rs+++ b/crates/macro_db_client/src/user/delete_user_dss_items/delete_documents.rs@@ -1,3 +1,5 @@+use model_entity::EntityType;+/// Deletes all documents for a user and returns all document ids that were deleted/// Does not commit the transaction#[tracing::instrument(skip(transaction))]@@ -52,7 +54,18 @@ pub async fn delete_user_documents(.execute(transaction.as_mut()).await?;- // Delete chats+ let document_uuids = user_documents+ .iter()+ .filter_map(|id| macro_uuid::string_to_uuid(id).ok())+ .collect::<Vec<_>>();+ crate::item_access::delete::delete_user_entity_access_bulk(+ transaction,+ &document_uuids,+ EntityType::Document,+ )+ .await?;++ // Delete documentssqlx::query!(r#"DELETE FROM "Document"@@ -63,45 +76,9 @@ pub async fn delete_user_documents(.execute(transaction.as_mut()).await?;- for document_id in &user_documents {- let Ok(document_uuid) = macro_uuid::string_to_uuid(document_id) else {- continue;- };+ for document_uuid in document_uuids {entity_registry_db_utils::delete_entity(transaction, document_uuid).await?;}Ok(user_documents)}--#[cfg(test)]-mod tests {- use super::*;- use sqlx::{Pool, Postgres};-- #[sqlx::test(fixtures(- path = "../../../fixtures",- scripts("basic_user_with_lots_of_documents")- ))]- async fn test_delete_user_documents(pool: Pool<Postgres>) -> anyhow::Result<()> {- let mut transaction = pool.begin().await?;- let mut result = delete_user_documents(&mut transaction, "macro|[email protected]").await?;-- result.sort();-- assert_eq!(- result,- vec![- "document-deleted".to_string(),- "document-five".to_string(),- "document-four".to_string(),- "document-one".to_string(),- "document-seven".to_string(),- "document-six".to_string(),- "document-three".to_string(),- "document-two".to_string()- ]- );-- Ok(())- }-}diff --git a/crates/macro_db_client/src/user/delete_user_dss_items/delete_projects.rs b/crates/macro_db_client/src/user/delete_user_dss_items/delete_projects.rsindex ea62dcd963..08e1e7b687 100644--- a/crates/macro_db_client/src/user/delete_user_dss_items/delete_projects.rs+++ b/crates/macro_db_client/src/user/delete_user_dss_items/delete_projects.rs@@ -54,17 +54,18 @@ pub async fn delete_user_projects(.execute(transaction.as_mut()).await?;+ let project_uuids = user_projects+ .iter()+ .filter_map(|id| macro_uuid::string_to_uuid(id).ok())+ .collect::<Vec<_>>();crate::item_access::delete::delete_user_entity_access_bulk(transaction,- &user_projects- .iter()- .filter_map(|p| macro_uuid::string_to_uuid(p).ok())- .collect::<Vec<uuid::Uuid>>(),+ &project_uuids,EntityType::Project,).await?;- // Delete chats+ // Delete projectssqlx::query!(r#"DELETE FROM "Project"@@ -75,39 +76,9 @@ pub async fn delete_user_projects(.execute(transaction.as_mut()).await?;- Ok(user_projects)-}--#[cfg(test)]-mod tests {- use super::*;- use sqlx::{Pool, Postgres};-- #[sqlx::test(fixtures(- path = "../../../fixtures",- scripts("basic_user_with_lots_of_documents")- ))]- async fn test_delete_user_projects(pool: Pool<Postgres>) -> anyhow::Result<()> {- let mut transaction = pool.begin().await?;- delete_user_projects(&mut transaction, "macro|[email protected]").await?;- transaction.commit().await?;-- let project_ids = sqlx::query!(- r#"- SELECT- p.id- FROM- "Project" p- WHERE- p."userId" = $1- "#,- "macro|[email protected]"- )- .fetch_all(&pool)- .await?;-- assert_eq!(project_ids.len(), 0);-- Ok(())+ for project_uuid in project_uuids {+ entity_registry_db_utils::delete_entity(transaction, project_uuid).await?;}++ Ok(user_projects)}diff --git a/crates/macro_db_client/src/user/delete_user_dss_items/mod.rs b/crates/macro_db_client/src/user/delete_user_dss_items/mod.rsindex d5317d601b..031d60f516 100644--- a/crates/macro_db_client/src/user/delete_user_dss_items/mod.rs+++ b/crates/macro_db_client/src/user/delete_user_dss_items/mod.rs@@ -1,3 +1,6 @@pub mod delete_chats;pub mod delete_documents;pub mod delete_projects;++#[cfg(test)]+mod test;diff --git a/crates/macro_db_client/src/user/delete_user_dss_items/test.rs b/crates/macro_db_client/src/user/delete_user_dss_items/test.rsnew file mode 100644index 0000000000..095055804d--- /dev/null+++ b/crates/macro_db_client/src/user/delete_user_dss_items/test.rs@@ -0,0 +1,132 @@+use super::{+ delete_chats::delete_user_chats, delete_documents::delete_user_documents,+ delete_projects::delete_user_projects,+};+use sqlx::{PgConnection, PgPool};++const USER_ID: &str = "macro|[email protected]";+const DOCUMENT_ID: &str = "01900000-0000-7000-8000-000000000101";+const CHAT_ID: &str = "01900000-0000-7000-8000-000000000102";+const PROJECT_ID: &str = "01900000-0000-7000-8000-000000000103";+const UNREGISTERED_DOCUMENT_ID: &str = "01900000-0000-7000-8000-000000000104";++async fn snapshot(db: &mut PgConnection) -> anyhow::Result<Vec<(String, String)>> {+ Ok(sqlx::query!(+ r#"+ SELECT 'document' AS "kind!", id AS "id!" FROM "Document"+ UNION ALL SELECT 'chat', id FROM "Chat"+ UNION ALL SELECT 'project', id FROM "Project"+ UNION ALL SELECT 'entity', id::text FROM entity+ UNION ALL SELECT CASE WHEN granted_from_project_id IS NULL THEN 'access' ELSE 'inherited' END,+ entity_id::text FROM entity_access+ ORDER BY 1, 2+ "#,+ )+ .fetch_all(db)+ .await?+ .into_iter()+ .map(|row| (row.kind, row.id))+ .collect())+}++async fn delete_and_check(db: &mut sqlx::Transaction<'_, sqlx::Postgres>) -> anyhow::Result<()> {+ let before = snapshot(db.as_mut()).await?;+ let mut documents = delete_user_documents(db, USER_ID).await?;+ documents.sort();+ assert_eq!(documents, [DOCUMENT_ID, UNREGISTERED_DOCUMENT_ID]);+ let after_documents = snapshot(db.as_mut()).await?;+ assert_eq!(+ after_documents,+ before+ .iter()+ .filter(|(_, id)| id != DOCUMENT_ID && id != UNREGISTERED_DOCUMENT_ID)+ .cloned()+ .collect::<Vec<_>>()+ );++ delete_user_chats(db, USER_ID).await?;+ let after_chats = snapshot(db.as_mut()).await?;+ assert_eq!(+ after_chats,+ after_documents+ .into_iter()+ .filter(|(_, id)| id != CHAT_ID)+ .collect::<Vec<_>>()+ );++ assert_eq!(delete_user_projects(db, USER_ID).await?, [PROJECT_ID]);+ let after_projects = snapshot(db.as_mut()).await?;+ assert_eq!(+ after_projects,+ after_chats+ .into_iter()+ .filter(|(kind, id)| id != PROJECT_ID && kind != "inherited")+ .collect::<Vec<_>>()+ );++ // Retrying cleanup is safe even when registry rows were already absent.+ assert!(delete_user_documents(db, USER_ID).await?.is_empty());+ delete_user_chats(db, USER_ID).await?;+ assert!(delete_user_projects(db, USER_ID).await?.is_empty());+ assert_eq!(snapshot(db.as_mut()).await?, after_projects);+ Ok(())+}++#[sqlx::test(fixtures(path = "../../../fixtures", scripts("user_deletion_owners")))]+async fn owner_cleanup_is_scoped_and_transactional(pool: PgPool) -> anyhow::Result<()> {+ let before = snapshot(&mut *pool.acquire().await?).await?;+ assert_eq!(before.len(), 51);++ let mut transaction = pool.begin().await?;+ delete_and_check(&mut transaction).await?;+ transaction.rollback().await?;+ assert_eq!(snapshot(&mut *pool.acquire().await?).await?, before);++ let mut transaction = pool.begin().await?;+ delete_and_check(&mut transaction).await?;+ let after = snapshot(transaction.as_mut()).await?;+ transaction.commit().await?;+ assert_eq!(snapshot(&mut *pool.acquire().await?).await?, after);+ Ok(())+}++#[sqlx::test(fixtures(path = "../../../fixtures", scripts("user_deletion_owners")))]+async fn quota_counts_only_live_user_owned_documents(pool: PgPool) -> anyhow::Result<()> {+ let user_id = macro_user_id::user_id::MacroUserId::parse_from_str(USER_ID)?.lowercase();+ let quota = crate::user_quota::get_user_quota(&pool, &user_id).await?;+ // The registered user document is soft-deleted. Only the unregistered user+ // document counts, despite access to the other user's, bot's and team's docs.+ assert_eq!(quota.documents, 1);+ Ok(())+}++#[sqlx::test(fixtures(+ path = "../../../fixtures",+ scripts("basic_user_with_lots_of_documents")+))]+async fn legacy_non_uuid_items_are_still_deleted(pool: PgPool) -> anyhow::Result<()> {+ let mut transaction = pool.begin().await?;+ let mut documents = delete_user_documents(&mut transaction, "macro|[email protected]").await?;+ documents.sort();+ assert_eq!(+ documents,+ [+ "document-deleted",+ "document-five",+ "document-four",+ "document-one",+ "document-seven",+ "document-six",+ "document-three",+ "document-two",+ ]+ );+ delete_user_chats(&mut transaction, "macro|[email protected]").await?;+ assert_eq!(+ delete_user_projects(&mut transaction, "macro|[email protected]").await?,+ ["project-one"]+ );+ transaction.commit().await?;+ assert!(snapshot(&mut *pool.acquire().await?).await?.is_empty());+ Ok(())+}diff --git a/crates/macro_db_client/src/user_quota.rs b/crates/macro_db_client/src/user_quota.rsindex 3d2678514b..b518a2f972 100644--- a/crates/macro_db_client/src/user_quota.rs+++ b/crates/macro_db_client/src/user_quota.rs@@ -4,7 +4,10 @@ use user_quota::{CreateUserQuotaRequest, UserQuota};#[cfg(test)]mod test;-/// Retrieves the user quota for a user+/// Retrieves the user quota for a user.+///+/// Documents count user-owned rows only. Bot/team-owned document quota policy+/// remains open (ownership-v2 §11.5); access grants do not imply quota ownership.#[tracing::instrument(skip(db))]pub async fn get_user_quota(db: &sqlx::PgPool,diff --git a/services/document_storage_service/src/service/delete_document_worker/handle.rs b/services/document_storage_service/src/service/delete_document_worker/handle.rsindex 4fb11b06b7..a93dc13340 100644--- a/services/document_storage_service/src/service/delete_document_worker/handle.rs+++ b/services/document_storage_service/src/service/delete_document_worker/handle.rs@@ -14,26 +14,7 @@ pub async fn handle(tracing::debug!("processing delete document message");let (document_id, mut owner) = if let Some(attributes) = message.message_attributes.as_ref() {- let document_id = attributes- .get("document_id")- .map(|document_id| {- tracing::trace!(document_id=?document_id, "found document_id in message attributes");- document_id.string_value().unwrap_or_default()- })- .context("document_id should be a message attribute")?;-- // The `user_id` attribute carries the owner principal (`macro|<email>`,- // `bot|<uuid>`, or a team UUID); it keeps its historical name on the wire.- let owner = attributes- .get("user_id")- .map(|user_id| {- tracing::trace!(user_id=?user_id, "found user_id in message attributes");- Owner::from_principal_str(user_id.string_value().unwrap_or_default())- })- .transpose()- .context("user_id message attribute should be an owner principal")?;-- (document_id, owner)+ document_and_owner(attributes)?} else {ctx.worker.cleanup_message(message).await?;anyhow::bail!("message attributes not found")@@ -133,6 +114,25 @@ pub async fn handle(Ok(())}+pub(super) fn document_and_owner(+ attributes: &std::collections::HashMap<String, aws_sdk_sqs::types::MessageAttributeValue>,+) -> anyhow::Result<(&str, Option<Owner>)> {+ let document_id = attributes+ .get("document_id")+ .map(|document_id| document_id.string_value().unwrap_or_default())+ .context("document_id should be a message attribute")?;++ // The `user_id` attribute carries the owner principal (`macro|<email>`,+ // `bot|<uuid>`, or a team UUID); it keeps its historical name on the wire.+ let owner = attributes+ .get("user_id")+ .map(|owner| Owner::from_principal_str(owner.string_value().unwrap_or_default()))+ .transpose()+ .context("user_id message attribute should be an owner principal")?;++ Ok((document_id, owner))+}+pub(crate) fn document_cleanup_receipt(document_id: &str) -> EditReceipt {EditReceipt::dangerously_assert_internal_user(document_id, EntityType::Document)}diff --git a/services/document_storage_service/src/service/delete_document_worker/test.rs b/services/document_storage_service/src/service/delete_document_worker/test.rsindex 088a8a5f13..e57ad63a90 100644--- a/services/document_storage_service/src/service/delete_document_worker/test.rs+++ b/services/document_storage_service/src/service/delete_document_worker/test.rs@@ -1,6 +1,63 @@use entity_access::domain::models::{EntityAccessAuth, EntityType};-use super::handle::{count_occurrences, document_cleanup_receipt};+use super::handle::{count_occurrences, document_and_owner, document_cleanup_receipt};+use aws_sdk_sqs::types::{Message, MessageAttributeValue};+use model_owner::Owner;++fn deletion_message(owner: Option<&str>) -> Message {+ let mut message = Message::builder().message_attributes(+ "document_id",+ MessageAttributeValue::builder()+ .data_type("String")+ .string_value("01900000-0000-7000-8000-000000000001")+ .build()+ .unwrap(),+ );+ if let Some(owner) = owner {+ message = message.message_attributes(+ "user_id",+ MessageAttributeValue::builder()+ .data_type("String")+ .string_value(owner)+ .build()+ .unwrap(),+ );+ }+ message.build()+}++#[test]+fn deletion_message_preserves_owner_for_shared_s3_prefix() {+ for principal in [+ "macro|[email protected]",+ "bot|01900000-0000-7000-8000-000000000002",+ "01900000-0000-7000-8000-000000000003",+ ] {+ let message = deletion_message(Some(principal));+ let (document_id, owner) =+ document_and_owner(message.message_attributes().unwrap()).unwrap();+ let owner = owner.unwrap();++ assert_eq!(owner, Owner::from_principal_str(principal).unwrap());+ assert_eq!(+ s3_key::build_cloud_storage_bucket_document_prefix(&owner, document_id),+ format!("{principal}/{document_id}"),+ );+ }+}++#[test]+fn deletion_message_without_owner_requires_database_lookup() {+ let message = deletion_message(None);+ let (_, owner) = document_and_owner(message.message_attributes().unwrap()).unwrap();+ assert!(owner.is_none());+}++#[test]+fn deletion_message_rejects_invalid_owner() {+ let message = deletion_message(Some("bot|not-a-uuid"));+ assert!(document_and_owner(message.message_attributes().unwrap()).is_err());+}#[test]fn document_cleanup_receipt_is_internal_for_the_document() {