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
fix(document): edit document project correctly removes old project from entity access (#4558)
Will Hutchinson07665ae5502a
diff --git a/rust/cloud-storage/documents/src/domain/service.rs b/rust/cloud-storage/documents/src/domain/service.rsindex e07f7c3d4b..a40cdc746a 100644--- a/rust/cloud-storage/documents/src/domain/service.rs+++ b/rust/cloud-storage/documents/src/domain/service.rs@@ -1149,15 +1149,19 @@ impl<.await.map_err(|e| DocumentError::Internal(e.into()))?;- // Update project modified timestamps- if let Some(old_project_id) = &document_context.project_id+ // Update project modified timestamps. args.project_id of None means "no change",+ // so only move the document out of its old project when a different project (or+ // "" for no project) was explicitly requested.+ if let Some(new_project_id) = &args.project_id+ && let Some(old_project_id) = &document_context.project_id+ && new_project_id != old_project_id&& !old_project_id.is_empty(){let old_project_id = uuid::Uuid::parse_str(old_project_id).unwrap();let document_uuid = uuid::Uuid::parse_str(&document_context.document_id).unwrap();let _ = self.entity_access_management_service- .add_entity_to_project(&document_uuid, EntityType::Document, &old_project_id)+ .remove_entity_from_project(&document_uuid, EntityType::Document, &old_project_id).await.inspect_err(|e| tracing::error!(error=?e, project_id=?old_project_id, "unable to update entity access for project"));let _ = self.repo.update_project_modified(&old_project_id.to_string()).await.inspect_err(|e| tracing::error!(error=?e, project_id=?old_project_id, "unable to update project modified date"),diff --git a/rust/cloud-storage/documents/src/domain/service/tests.rs b/rust/cloud-storage/documents/src/domain/service/tests.rsindex d3e3c642c0..d2d216c03b 100644--- a/rust/cloud-storage/documents/src/domain/service/tests.rs+++ b/rust/cloud-storage/documents/src/domain/service/tests.rs@@ -167,16 +167,20 @@ impl ConnectionService for TestConnectionService {}}-#[derive(Clone)]-struct TestEntityAccessManagementService;+#[derive(Clone, Default)]+struct TestEntityAccessManagementService {+ added_to_projects: Arc<Mutex<Vec<uuid::Uuid>>>,+ removed_from_projects: Arc<Mutex<Vec<uuid::Uuid>>>,+}impl EntityAccessManagementService for TestEntityAccessManagementService {async fn add_entity_to_project(&self,_entity_id: &uuid::Uuid,_entity_type: EntityType,- _project_id: &uuid::Uuid,+ project_id: &uuid::Uuid,) -> Result<(), entity_access_management::domain::models::EntityAccessManagementError> {+ self.added_to_projects.lock().unwrap().push(*project_id);Ok(())}@@ -184,8 +188,12 @@ impl EntityAccessManagementService for TestEntityAccessManagementService {&self,_entity_id: &uuid::Uuid,_entity_type: EntityType,- _old_project_id: &uuid::Uuid,+ old_project_id: &uuid::Uuid,) -> Result<(), entity_access_management::domain::models::EntityAccessManagementError> {+ self.removed_from_projects+ .lock()+ .unwrap()+ .push(*old_project_id);Ok(())}@@ -362,12 +370,34 @@ fn make_test_service_with_foreign_entity_service(TestUploadUrlPort,TestTaskPropertiesPort,TestConnectionService,- TestEntityAccessManagementService,+ TestEntityAccessManagementService::default(),foreign_entity_service,TestEventBroker::default(),)}+/// Build a test service along with a handle to its recording entity access service.+fn make_test_service_with_entity_access(+ repo: MockDocumentRepo,+) -> (TestDocumentService, TestEntityAccessManagementService) {+ let entity_access = TestEntityAccessManagementService::default();+ let service = DocumentServiceImpl::new(+ repo,+ test_cloudfront_config(),+ sync_service_client::SyncServiceClient::new(+ "test-sync-key".to_string(),+ "http://sync-service.test".to_string(),+ ),+ TestUploadUrlPort,+ TestTaskPropertiesPort,+ TestConnectionService,+ entity_access.clone(),+ TestForeignEntityService::default(),+ TestEventBroker::default(),+ );+ (service, entity_access)+}+/// Build a test service along with a handle to its recording event broker.fn make_test_service_with_event_broker(repo: MockDocumentRepo,@@ -383,7 +413,7 @@ fn make_test_service_with_event_broker(TestUploadUrlPort,TestTaskPropertiesPort,TestConnectionService,- TestEntityAccessManagementService,+ TestEntityAccessManagementService::default(),TestForeignEntityService::default(),event_broker.clone(),);@@ -1199,6 +1229,86 @@ async fn test_edit_document_publishes_document_updated_event() {);}+#[tokio::test]+async fn test_edit_document_rename_only_keeps_project_access() {+ let document_id = uuid::Uuid::new_v4().to_string();+ let old_project_id = uuid::Uuid::new_v4();++ let mut repo = make_mock_repo();+ repo.expect_edit_document()+ .returning(|_| Box::pin(std::future::ready(Ok(()))));++ let (service, entity_access) = make_test_service_with_entity_access(repo);++ let mut context = task_document_context(&document_id);+ context.project_id = Some(old_project_id.to_string());++ service+ .edit_document(+ edit_receipt(&document_id),+ context,+ EditDocumentServiceArgs {+ document_name: Some("New name".to_string()),+ project_id: None,+ share_permission: None,+ file_type: None,+ },+ )+ .await+ .unwrap();++ assert!(+ entity_access+ .removed_from_projects+ .lock()+ .unwrap()+ .is_empty()+ );+ assert!(entity_access.added_to_projects.lock().unwrap().is_empty());+}++#[tokio::test]+async fn test_edit_document_project_change_moves_project_access() {+ let document_id = uuid::Uuid::new_v4().to_string();+ let old_project_id = uuid::Uuid::new_v4();+ let new_project_id = uuid::Uuid::new_v4();++ let mut repo = make_mock_repo();+ repo.expect_edit_document()+ .returning(|_| Box::pin(std::future::ready(Ok(()))));+ repo.expect_update_project_modified()+ .times(2)+ .returning(|_| Box::pin(std::future::ready(Ok(()))));++ let (service, entity_access) = make_test_service_with_entity_access(repo);++ let mut context = task_document_context(&document_id);+ context.project_id = Some(old_project_id.to_string());++ service+ .edit_document(+ edit_receipt(&document_id),+ context,+ EditDocumentServiceArgs {+ document_name: None,+ project_id: Some(new_project_id.to_string()),+ share_permission: None,+ file_type: None,+ },+ )+ .await+ .unwrap();++ assert_eq!(+ *entity_access.removed_from_projects.lock().unwrap(),+ vec![old_project_id]+ );+ assert_eq!(+ *entity_access.added_to_projects.lock().unwrap(),+ vec![new_project_id]+ );+}+#[tokio::test]async fn test_copy_document_publishes_document_copied_event() {let mut repo = make_mock_repo();