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(email): reuse email-imported documents by content SHA (#5859)
teo15953bda2e12
diff --git a/.sqlx/query-39fbfe86d9b8ee884383c557bbd3f2030bef31de3a9f3430f8b440a098f86e8f.json b/.sqlx/query-39fbfe86d9b8ee884383c557bbd3f2030bef31de3a9f3430f8b440a098f86e8f.jsonnew file mode 100644index 0000000000..8e813855f1--- /dev/null+++ b/.sqlx/query-39fbfe86d9b8ee884383c557bbd3f2030bef31de3a9f3430f8b440a098f86e8f.json@@ -0,0 +1,22 @@+{+ "db_name": "PostgreSQL",+ "query": "\n SELECT de.document_id\n FROM document_email de\n JOIN \"Document\" d ON d.id = de.document_id\n WHERE de.email_attachment_id = $1\n ORDER BY (d.\"deletedAt\" IS NULL) DESC, de.document_id\n LIMIT 1\n ",+ "describe": {+ "columns": [+ {+ "ordinal": 0,+ "name": "document_id",+ "type_info": "Text"+ }+ ],+ "parameters": {+ "Left": [+ "Uuid"+ ]+ },+ "nullable": [+ false+ ]+ },+ "hash": "39fbfe86d9b8ee884383c557bbd3f2030bef31de3a9f3430f8b440a098f86e8f"+}diff --git a/.sqlx/query-5a9a8adfb252636418fcf651c15161fc20de39e58275ad8df06acee434b26cde.json b/.sqlx/query-5a9a8adfb252636418fcf651c15161fc20de39e58275ad8df06acee434b26cde.jsonnew file mode 100644index 0000000000..4e58630900--- /dev/null+++ b/.sqlx/query-5a9a8adfb252636418fcf651c15161fc20de39e58275ad8df06acee434b26cde.json@@ -0,0 +1,23 @@+{+ "db_name": "PostgreSQL",+ "query": "\n SELECT d.id\n FROM \"Document\" d\n INNER JOIN LATERAL (\n SELECT i.sha\n FROM \"DocumentInstance\" i\n WHERE i.\"documentId\" = d.id\n ORDER BY i.\"createdAt\" DESC\n LIMIT 1\n ) latest ON latest.sha = $2\n WHERE d.owner = $1\n AND d.\"deletedAt\" IS NULL\n AND EXISTS (\n SELECT 1\n FROM document_email de\n WHERE de.document_id = d.id\n )\n ORDER BY d.\"createdAt\" ASC, d.id ASC\n LIMIT 1\n ",+ "describe": {+ "columns": [+ {+ "ordinal": 0,+ "name": "id",+ "type_info": "Text"+ }+ ],+ "parameters": {+ "Left": [+ "Text",+ "Text"+ ]+ },+ "nullable": [+ false+ ]+ },+ "hash": "5a9a8adfb252636418fcf651c15161fc20de39e58275ad8df06acee434b26cde"+}diff --git a/.sqlx/query-7e076694e17fe7c86e7a948ab72e4a2a56f301223b62413247ac15bd5fef29c5.json b/.sqlx/query-7e076694e17fe7c86e7a948ab72e4a2a56f301223b62413247ac15bd5fef29c5.jsondeleted file mode 100644index 35ccaa385e..0000000000--- a/.sqlx/query-7e076694e17fe7c86e7a948ab72e4a2a56f301223b62413247ac15bd5fef29c5.json+++ /dev/null@@ -1,15 +0,0 @@-{- "db_name": "PostgreSQL",- "query": "\n INSERT INTO \"document_email\" (document_id, email_attachment_id)\n VALUES ($1, $2)\n ",- "describe": {- "columns": [],- "parameters": {- "Left": [- "Text",- "Uuid"- ]- },- "nullable": []- },- "hash": "7e076694e17fe7c86e7a948ab72e4a2a56f301223b62413247ac15bd5fef29c5"-}diff --git a/.sqlx/query-aadf64de7ecba24e0f59a6cb07d95c59e2e18eec73f43a76ebf234d13f68ed39.json b/.sqlx/query-aadf64de7ecba24e0f59a6cb07d95c59e2e18eec73f43a76ebf234d13f68ed39.jsonnew file mode 100644index 0000000000..15d06c1f30--- /dev/null+++ b/.sqlx/query-aadf64de7ecba24e0f59a6cb07d95c59e2e18eec73f43a76ebf234d13f68ed39.json@@ -0,0 +1,15 @@+{+ "db_name": "PostgreSQL",+ "query": "\n INSERT INTO \"document_email\" (document_id, email_attachment_id)\n VALUES ($1, $2)\n ",+ "describe": {+ "columns": [],+ "parameters": {+ "Left": [+ "Text",+ "Uuid"+ ]+ },+ "nullable": []+ },+ "hash": "aadf64de7ecba24e0f59a6cb07d95c59e2e18eec73f43a76ebf234d13f68ed39"+}diff --git a/.sqlx/query-b1b0e6bfc305c5a54ae7648f70cb57890fba3f268ffd081bef06879a9f177028.json b/.sqlx/query-b1b0e6bfc305c5a54ae7648f70cb57890fba3f268ffd081bef06879a9f177028.jsonnew file mode 100644index 0000000000..79d51c0de9--- /dev/null+++ b/.sqlx/query-b1b0e6bfc305c5a54ae7648f70cb57890fba3f268ffd081bef06879a9f177028.json@@ -0,0 +1,23 @@+{+ "db_name": "PostgreSQL",+ "query": "\n SELECT values\n FROM entity_properties\n WHERE entity_id = $1 AND property_definition_id = $2\n ",+ "describe": {+ "columns": [+ {+ "ordinal": 0,+ "name": "values",+ "type_info": "Jsonb"+ }+ ],+ "parameters": {+ "Left": [+ "Text",+ "Uuid"+ ]+ },+ "nullable": [+ true+ ]+ },+ "hash": "b1b0e6bfc305c5a54ae7648f70cb57890fba3f268ffd081bef06879a9f177028"+}diff --git a/.sqlx/query-b3200181380df2f645bfce270dc1cf520939fa4fa4b65371d11db7416ba0b14c.json b/.sqlx/query-b3200181380df2f645bfce270dc1cf520939fa4fa4b65371d11db7416ba0b14c.jsonnew file mode 100644index 0000000000..f3cc23c5b8--- /dev/null+++ b/.sqlx/query-b3200181380df2f645bfce270dc1cf520939fa4fa4b65371d11db7416ba0b14c.json@@ -0,0 +1,23 @@+{+ "db_name": "PostgreSQL",+ "query": "SELECT pg_advisory_xact_lock(hashtext($1), hashtext($2))",+ "describe": {+ "columns": [+ {+ "ordinal": 0,+ "name": "pg_advisory_xact_lock",+ "type_info": "Void"+ }+ ],+ "parameters": {+ "Left": [+ "Text",+ "Text"+ ]+ },+ "nullable": [+ null+ ]+ },+ "hash": "b3200181380df2f645bfce270dc1cf520939fa4fa4b65371d11db7416ba0b14c"+}diff --git a/.sqlx/query-c43a6c2bc9b44aca5837e186d411007edbce86a18218452bb8b967aba09275f2.json b/.sqlx/query-c43a6c2bc9b44aca5837e186d411007edbce86a18218452bb8b967aba09275f2.jsonnew file mode 100644index 0000000000..4daa3d438d--- /dev/null+++ b/.sqlx/query-c43a6c2bc9b44aca5837e186d411007edbce86a18218452bb8b967aba09275f2.json@@ -0,0 +1,18 @@+{+ "db_name": "PostgreSQL",+ "query": "\n INSERT INTO entity_properties (id, entity_id, entity_type, property_definition_id, values)\n SELECT\n u.id,\n u.entity_id,\n u.entity_type::property_entity_type,\n u.property_definition_id,\n u.values\n FROM UNNEST(\n $1::UUID[],\n $2::TEXT[],\n $3::TEXT[],\n $4::UUID[],\n $5::JSONB[]\n ) AS u(id, entity_id, entity_type, property_definition_id, values)\n ON CONFLICT (entity_id, entity_type, property_definition_id)\n DO NOTHING\n ",+ "describe": {+ "columns": [],+ "parameters": {+ "Left": [+ "UuidArray",+ "TextArray",+ "TextArray",+ "UuidArray",+ "JsonbArray"+ ]+ },+ "nullable": []+ },+ "hash": "c43a6c2bc9b44aca5837e186d411007edbce86a18218452bb8b967aba09275f2"+}diff --git a/crates/documents/src/domain/create.rs b/crates/documents/src/domain/create.rsindex 0d7ae93dde..29b5d55743 100644--- a/crates/documents/src/domain/create.rs+++ b/crates/documents/src/domain/create.rs@@ -28,7 +28,6 @@ pub struct NewDocumentMetadata {id: Option<uuid::Uuid>,document_name: String,project_id: Option<uuid::Uuid>,- email_attachment_id: Option<uuid::Uuid>,created_at: Option<chrono::DateTime<chrono::Utc>>,skip_history: bool,attribution: Option<Attribution>,@@ -47,7 +46,6 @@ impl NewDocumentMetadata {id: None,document_name: document_name.into(),project_id: None,- email_attachment_id: None,created_at: None,skip_history: false,attribution: None,@@ -68,7 +66,6 @@ impl NewDocumentMetadata {file_type: kind.file_type,project_id: self.project_id,team_id: kind.team_id,- email_attachment_id: self.email_attachment_id,created_at: self.created_at,sub_type: kind.subtype.sub_type(),skip_history: self.skip_history,@@ -96,12 +93,6 @@ impl NewDocumentMetadataBuilder {self}- /// Set an email attachment to link to this document.- pub fn email_attachment_id(mut self, email_attachment_id: uuid::Uuid) -> Self {- self.metadata.email_attachment_id = Some(email_attachment_id);- self- }-/// Set a custom creation timestamp.pub fn created_at(mut self, created_at: chrono::DateTime<chrono::Utc>) -> Self {self.metadata.created_at = Some(created_at);@@ -744,6 +735,7 @@ mod tests {MarkdownSubtype, NewDocumentMetadata, NewPlainTextDocument, RepoDocumentKind,RepoDocumentSubtype, file_shas,};+ use crate::domain::models::ImportEmailAttachmentRepoArgs;use activity::{Actor, Attribution};use macro_user_id::user_id::MacroUserIdStr;use model::document::FileType;@@ -776,13 +768,13 @@ mod tests {}#[test]- fn email_attachment_create_resolves_to_the_system_principal() {- let args = NewDocumentMetadata::builder("invoice.pdf")- .email_attachment_id(uuid::Uuid::from_u128(1))- .build()- .into_repo_args(owner(), repo_kind());+ fn email_import_resolves_to_the_system_principal() {+ let args = ImportEmailAttachmentRepoArgs {+ email_attachment_id: uuid::Uuid::from_u128(1),+ create: metadata().into_repo_args(owner(), repo_kind()),+ };- assert_eq!(args.user_id.as_ref(), "macro|[email protected]");+ assert_eq!(args.create.user_id.as_ref(), "macro|[email protected]");assert_eq!(args.resolved_attribution(),Attribution::direct(Actor::new_from_bot(bot_id::MACRO_SYSTEM_BOT_ID))diff --git a/crates/documents/src/domain/models.rs b/crates/documents/src/domain/models.rsindex d7ab74e119..c296c8c664 100644--- a/crates/documents/src/domain/models.rs+++ b/crates/documents/src/domain/models.rs@@ -322,8 +322,6 @@ pub struct CreateDocumentRepoArgs {pub project_id: Option<uuid::Uuid>,/// Team to use when assigning a per-team task number.pub team_id: Option<uuid::Uuid>,- /// Email attachment to link (internal only).- pub email_attachment_id: Option<uuid::Uuid>,/// Custom creation timestamp.pub created_at: Option<chrono::DateTime<chrono::Utc>>,/// Sub type of the document — task or snippet (MD files only).@@ -337,19 +335,53 @@ pub struct CreateDocumentRepoArgs {impl CreateDocumentRepoArgs {/// Resolves who created this document for activity recording.///- /// Ownership (`user_id`) is unchanged. Email auto-imports default to- /// the system principal so they do not appear on the owner's feed.+ /// Ownership (`user_id`) is unchanged.pub fn resolved_attribution(&self) -> Attribution {- self.attribution.clone().unwrap_or_else(|| {- if self.email_attachment_id.is_some() {- Attribution::direct(Actor::new_from_bot(bot_id::MACRO_SYSTEM_BOT_ID))- } else {- Attribution::direct(Actor::new_from_user(self.user_id.clone()))- }+ self.attribution+ .clone()+ .unwrap_or_else(|| Attribution::direct(Actor::new_from_user(self.user_id.clone())))+ }+}++/// Arguments for importing an email attachment as a document.+pub struct ImportEmailAttachmentRepoArgs {+ /// The email attachment being imported.+ pub email_attachment_id: uuid::Uuid,+ /// How to create the document when no reusable import exists.+ pub create: CreateDocumentRepoArgs,+}++impl ImportEmailAttachmentRepoArgs {+ /// Resolves who created this import for activity recording.+ ///+ /// Unset attribution is the system bot: email import is an internal+ /// pipeline, not a user-authored create. Ownership (`create.user_id`)+ /// is unchanged.+ pub fn resolved_attribution(&self) -> Attribution {+ self.create.attribution.clone().unwrap_or_else(|| {+ Attribution::direct(Actor::new_from_bot(bot_id::MACRO_SYSTEM_BOT_ID))})}}+/// Fact returned by the repository for an email-attachment import.+#[derive(Clone, Debug)]+pub enum EmailImportRepoOutcome {+ /// A new `Document` row was inserted and linked to the attachment.+ Created(DocumentMetadata),+ /// An existing live email-imported document was linked, or was already linked.+ Reused(DocumentMetadata),+}++impl EmailImportRepoOutcome {+ /// Metadata of the created or reused document.+ pub fn metadata(&self) -> &DocumentMetadata {+ match self {+ Self::Created(metadata) | Self::Reused(metadata) => metadata,+ }+ }+}+/// Configuration for CloudFront presigned URL generation.pub struct CloudFrontConfig {/// The CloudFront distribution URL.diff --git a/crates/documents/src/domain/ports.rs b/crates/documents/src/domain/ports.rsindex be72b81773..0f970c23c5 100644--- a/crates/documents/src/domain/ports.rs+++ b/crates/documents/src/domain/ports.rs@@ -29,8 +29,9 @@ use model_entity::Entity;use super::models::{BranchNameContext, CommentThread, CopyDocumentRepoArgs, CreateDocumentRepoArgs,CreateTaskRequest, DocumentError, DocumentTeamShare, DocumentTeamShareResponse,- EditDocumentRepoArgs, EditDocumentServiceArgs, GithubPullRequestsResponse, LocationQueryParams,- TaskBranchName, TeamTaskMetadata,+ EditDocumentRepoArgs, EditDocumentServiceArgs, EmailImportRepoOutcome,+ GithubPullRequestsResponse, ImportEmailAttachmentRepoArgs, LocationQueryParams, TaskBranchName,+ TeamTaskMetadata,};/// Repository for accessing document data from the database.@@ -129,9 +130,8 @@ pub trait DocumentRepo: Send + Sync + 'static {/// Create a new document with all associated records in a single transaction.///- /// Handles: Document row, version (DocumentInstance or DocumentBom),- /// document_sub_type, SharePermission, DocumentPermission, UserHistory,- /// ItemLastAccessed, UserItemAccess, and document_email.+ /// Always inserts a `Document` row. Email-attachment linking and SHA reuse+ /// belong on [`DocumentRepo::import_email_attachment_document`].////// `share_permission` is the pre-resolved initial share permission — the/// repository persists it verbatim and carries no share-policy of its own.@@ -141,6 +141,17 @@ pub trait DocumentRepo: Send + Sync + 'static {share_permission: SharePermissionV2,) -> impl Future<Output = Result<DocumentMetadata, Self::Err>> + Send;+ /// Import an email attachment: link it to a reusable live email document+ /// owned by the same user, or insert a new document and link it.+ ///+ /// Lookup, possible insert, and the `document_email` write happen in one+ /// transaction under an advisory lock on `(owner, sha)`.+ fn import_email_attachment_document(+ &self,+ args: ImportEmailAttachmentRepoArgs,+ share_permission: SharePermissionV2,+ ) -> impl Future<Output = Result<EmailImportRepoOutcome, Self::Err>> + Send;+/// Get the link-share preference of the user's team, or `None` when the/// user is not on a team.fn get_team_default_link_share(@@ -420,6 +431,16 @@ pub trait DocumentService: Send + Sync + 'static {job_id: Option<String>,) -> impl Future<Output = Result<CreateDocumentResponseData, DocumentError>> + Send;+ /// Import an email attachment as a document.+ ///+ /// Reuse returns existing content and no upload URL. A first import+ /// follows the same post-create lifecycle as [`DocumentService::create_document`].+ fn import_email_attachment(+ &self,+ user_id: MacroUserIdStr<'static>,+ args: ImportEmailAttachmentRepoArgs,+ ) -> impl Future<Output = Result<CreateDocumentResponseData, DocumentError>> + Send;+/// Get content lifecycle metadata for a document.fn get_document_content(&self,diff --git a/crates/documents/src/domain/service.rs b/crates/documents/src/domain/service.rsindex 6b404f2098..92e440a2db 100644--- a/crates/documents/src/domain/service.rs+++ b/crates/documents/src/domain/service.rs@@ -15,6 +15,7 @@ use std::str::FromStr;use std::time::{SystemTime, UNIX_EPOCH};use unicode_segmentation::UnicodeSegmentation;+use activity::Attribution;use anyhow::anyhow;use cloudfront_sign::{SignedOptions, get_signed_url};use connection::domain::models::{InvalidationEvent, InvalidationReason};@@ -29,7 +30,9 @@ use foreign_entity::domain::ports::ForeignEntityService;use macro_event_broker::MacroEventBroker;use macro_user_id::user_id::MacroUserIdStr;use model::document::response::{DocumentResponseMetadata, LocationResponseData};-use model::document::{ContentType, DocumentBasic, FileAssociation, FileType, FileTypeExt};+use model::document::{+ ContentType, DocumentBasic, DocumentMetadata, FileAssociation, FileType, FileTypeExt,+};use model::response::PresignedUrl;use s3_key::{build_cloud_storage_bucket_document_key, build_docx_staging_bucket_document_key,@@ -51,8 +54,9 @@ use super::events::{use super::models::{CloudFrontConfig, CommentThread, CopyDocumentRepoArgs, CreateDocumentRepoArgs,CreateTaskRequest, DocumentError, DocumentTeamShareResponse, EditDocumentRepoArgs,- EditDocumentServiceArgs, FileTypeUpdate, GithubPullRequest, GithubPullRequestsResponse,- LocationQueryParams, TaskBranchName, TeamTaskMetadata,+ EditDocumentServiceArgs, EmailImportRepoOutcome, FileTypeUpdate, GithubPullRequest,+ GithubPullRequestsResponse, ImportEmailAttachmentRepoArgs, LocationQueryParams, TaskBranchName,+ TeamTaskMetadata,};#[cfg(feature = "document_create")]use super::ports::create::DocumentCreationService;@@ -548,6 +552,177 @@ impl<tracing::error!(error=?e, "failed to publish document event");});}++ fn map_create_repo_error<E: Into<anyhow::Error>>(e: E) -> DocumentError {+ let err: anyhow::Error = e.into();+ if err.to_string().contains("document with ID already exists") {+ DocumentError::Conflict("document with ID already exists".to_string())+ } else {+ DocumentError::Internal(err)+ }+ }++ async fn reused_email_import_response(+ &self,+ document_metadata: DocumentMetadata,+ file_type: Option<FileType>,+ ) -> Result<CreateDocumentResponseData, DocumentError> {+ let document_id = document_metadata.document_id.clone();+ // Reuse must return whatever content already exists. A pending+ // placeholder here would tell the client to wait for an upload+ // that this path never issues.+ let content = self.content_for_document(&document_id, file_type).await?;+ let content_type = match file_type {+ Some(FileType::Docx) => ContentType::Docx,+ _ => file_type.into(),+ };+ let document_response_metadata =+ DocumentResponseMetadata::from_document_metadata(&document_metadata).map_err(+ |e| {+ tracing::error!(error=?e, document_id=?document_id, "unable to convert document metadata");+ DocumentError::Internal(anyhow!("unable to convert document metadata"))+ },+ )?;+ let team_task_metadata = self.team_task_metadata_for_document(&document_id).await?;+ Ok(CreateDocumentResponseData {+ document_response: DocumentResponse {+ document_metadata: DocumentResponseMetadataWithContent::new(+ document_response_metadata,+ content,+ )+ .with_team_task_metadata(team_task_metadata),+ presigned_url: None,+ },+ content_type: content_type.mime_type().to_string(),+ file_type: file_type.map(|f| f.to_string()),+ })+ }++ async fn finish_created_document(+ &self,+ document_metadata: DocumentMetadata,+ file_type: Option<FileType>,+ project_id: Option<uuid::Uuid>,+ sha: String,+ attribution: Attribution,+ job_id: Option<String>,+ ) -> Result<CreateDocumentResponseData, DocumentError> {+ let document_id = document_metadata.document_id.clone();++ let initial_content = pending_content_for_file_type(file_type);+ if let Err(e) = self+ .repo+ .set_document_content(&document_id, initial_content.clone())+ .await+ {+ tracing::error!(error=?e, document_id=?document_id, "failed to initialize document content metadata");+ self.cleanup_document(&document_id).await;+ return Err(DocumentError::Internal(e.into()));+ }++ if let Some(job_id) = &job_id+ && let Err(e) = self.repo.update_upload_job(&document_id, job_id).await+ {+ tracing::error!(error=?e, document_id=?document_id, "failed to update upload job");+ self.cleanup_document(&document_id).await;+ return Err(DocumentError::Internal(anyhow!(+ "unable to update upload job"+ )));+ }++ let content_type = match file_type {+ Some(FileType::Docx) => ContentType::Docx,+ _ => file_type.into(),+ };++ let mime_type = content_type.mime_type().to_string();++ let presigned_url = match file_type {+ Some(FileType::Docx) => {+ let docx_key = build_docx_staging_bucket_document_key(+ document_metadata.owner.as_ref(),+ &document_id,+ document_metadata.document_version_id,+ );+ self.upload_url_service+ .put_docx_upload_presigned_url(&docx_key, &sha, content_type)+ .await+ }+ _ => {+ let key = build_cloud_storage_bucket_document_key(+ document_metadata.owner.as_ref(),+ &document_id,+ document_metadata.document_version_id,+ );+ self.upload_url_service+ .put_document_storage_presigned_url(&key, &sha, content_type)+ .await+ }+ }+ .map_err(|e| {+ tracing::error!(error=?e, document_id=?document_id, "unable to generate presigned url");+ DocumentError::Internal(anyhow!("unable to generate presigned url"))+ })?;++ let document_response_metadata =+ DocumentResponseMetadata::from_document_metadata(&document_metadata).map_err(|e| {+ tracing::error!(error=?e, document_id=?document_id, "unable to convert document metadata");+ DocumentError::Internal(anyhow!("unable to convert document metadata"))+ })?;++ if let Some(project_id) = &project_id {+ let project_id_str = project_id.to_string();+ let document_uuid =+ uuid::Uuid::parse_str(&document_response_metadata.document_id).unwrap();+ let _ = self+ .entity_access_management_service+ .add_entity_to_project(&document_uuid, EntityType::Document, project_id)+ .await.inspect_err(|e| tracing::error!(error=?e, project_id=?project_id, "unable to update entity access for project"));+ let _ = self.repo.update_project_modified(&project_id_str).await.inspect_err(+ |e| tracing::error!(error=?e, project_id=?project_id, "unable to update project modified date"),+ );+ }++ if document_response_metadata.sub_type == Some(DocumentSubType::Task) {+ self.task_properties_service+ .attach_task_properties(vec![document_response_metadata.document_id.clone()])+ .await+ .map_err(|e| {+ tracing::error!(error=?e, document_id=?document_id, "failed to attach task properties");+ DocumentError::Internal(anyhow!("failed to attach task properties"))+ })?;+ }++ let team_task_metadata = self.team_task_metadata_for_document(&document_id).await?;++ self.publish_document_event(&DocumentMacroEvent::created(+ document_metadata.document_id.clone(),+ DocumentCreatedMetadata {+ document_id: document_metadata.document_id.clone(),+ owner: document_metadata.owner.clone(),+ actor: Some(attribution.actor()),+ on_behalf_of: attribution.on_behalf_of(),+ document_name: document_metadata.document_name.clone(),+ file_type,+ project_id: project_id.map(|p| p.to_string()),+ sub_type: document_metadata.sub_type,+ created_at: document_metadata.created_at,+ },+ ));++ Ok(CreateDocumentResponseData {+ document_response: DocumentResponse {+ document_metadata: DocumentResponseMetadataWithContent::new(+ document_response_metadata,+ initial_content,+ )+ .with_team_task_metadata(team_task_metadata),+ presigned_url: Some(presigned_url),+ },+ content_type: mime_type,+ file_type: file_type.map(|f| f.to_string()),+ })+ }}#[cfg(feature = "document_create")]@@ -1038,7 +1213,7 @@ impl<#[tracing::instrument(err, skip(self, args))]async fn create_document(&self,- user_id: MacroUserIdStr<'static>,+ _user_id: MacroUserIdStr<'static>,args: CreateDocumentRepoArgs,job_id: Option<String>,) -> Result<CreateDocumentResponseData, DocumentError> {@@ -1053,8 +1228,6 @@ impl<let sha = args.sha.clone();let attribution = args.resolved_attribution();- // The owner's team default link-share preference decides the initial- // share permission; without a team the per-file-type default applies.let team_default = self.repo.get_team_default_link_share(args.user_id.as_ref())@@ -1063,143 +1236,70 @@ impl<let share_permission =SharePermissionV2::new_document_share_permission(file_type, team_default);- // Create document metadata in the database (full transaction)let document_metadata = self.repo.create_document(args, share_permission).await- .map_err(|e| {- let err: anyhow::Error = e.into();- if err.to_string().contains("document with ID already exists") {- DocumentError::Conflict("document with ID already exists".to_string())- } else {- DocumentError::Internal(err)- }- })?;+ .map_err(Self::map_create_repo_error)?;++ self.finish_created_document(+ document_metadata,+ file_type,+ project_id,+ sha,+ attribution,+ job_id,+ )+ .await+ }- let document_id = document_metadata.document_id.clone();+ #[tracing::instrument(err, skip(self, args))]+ async fn import_email_attachment(+ &self,+ _user_id: MacroUserIdStr<'static>,+ args: ImportEmailAttachmentRepoArgs,+ ) -> Result<CreateDocumentResponseData, DocumentError> {+ if args.create.document_name.graphemes(true).count() > MAX_DOCUMENT_NAME_GRAPHEMES {+ return Err(DocumentError::NameTooLong {+ max: MAX_DOCUMENT_NAME_GRAPHEMES,+ });+ }- let initial_content = pending_content_for_file_type(file_type);- if let Err(e) = self+ let file_type = args.create.file_type;+ let project_id = args.create.project_id;+ let sha = args.create.sha.clone();+ let attribution = args.resolved_attribution();++ let team_default = self.repo- .set_document_content(&document_id, initial_content.clone())+ .get_team_default_link_share(args.create.user_id.as_ref()).await- {- tracing::error!(error=?e, document_id=?document_id, "failed to initialize document content metadata");- self.cleanup_document(&document_id).await;- return Err(DocumentError::Internal(e.into()));- }+ .map_err(|e| DocumentError::Internal(e.into()))?;+ let share_permission =+ SharePermissionV2::new_document_share_permission(file_type, team_default);- // Update upload job if job_id provided (outside the main transaction)- if let Some(job_id) = &job_id- && let Err(e) = self.repo.update_upload_job(&document_id, job_id).await+ match self+ .repo+ .import_email_attachment_document(args, share_permission)+ .await+ .map_err(Self::map_create_repo_error)?{- tracing::error!(error=?e, document_id=?document_id, "failed to update upload job");- self.cleanup_document(&document_id).await;- return Err(DocumentError::Internal(anyhow!(- "unable to update upload job"- )));- }-- let content_type = match file_type {- Some(FileType::Docx) => ContentType::Docx,- _ => file_type.into(),- };-- let mime_type = content_type.mime_type().to_string();-- // Generate presigned upload URL- // DOCX files go to the staging bucket with .docx extension (required by docx_unzip_handler)- // All other files use extensionless keys in the document storage bucket- let presigned_url = match file_type {- Some(FileType::Docx) => {- let docx_key = build_docx_staging_bucket_document_key(- document_metadata.owner.as_ref(),- &document_id,- document_metadata.document_version_id,- );- self.upload_url_service- .put_docx_upload_presigned_url(&docx_key, &sha, content_type)- .await+ EmailImportRepoOutcome::Created(document_metadata) => {+ self.finish_created_document(+ document_metadata,+ file_type,+ project_id,+ sha,+ attribution,+ None,+ )+ .await}- _ => {- let key = build_cloud_storage_bucket_document_key(- document_metadata.owner.as_ref(),- &document_id,- document_metadata.document_version_id,- );- self.upload_url_service- .put_document_storage_presigned_url(&key, &sha, content_type)+ EmailImportRepoOutcome::Reused(document_metadata) => {+ self.reused_email_import_response(document_metadata, file_type).await}}- .map_err(|e| {- tracing::error!(error=?e, document_id=?document_id, "unable to generate presigned url");- DocumentError::Internal(anyhow!("unable to generate presigned url"))- })?;-- // Convert metadata to response format- let document_response_metadata =- DocumentResponseMetadata::from_document_metadata(&document_metadata).map_err(|e| {- tracing::error!(error=?e, document_id=?document_id, "unable to convert document metadata");- DocumentError::Internal(anyhow!("unable to convert document metadata"))- })?;-- // Update project modified timestamp- if let Some(project_id) = &project_id {- let project_id_str = project_id.to_string();- let document_uuid =- uuid::Uuid::parse_str(&document_response_metadata.document_id).unwrap();- let _ = self- .entity_access_management_service- .add_entity_to_project(&document_uuid, EntityType::Document, project_id)- .await.inspect_err(|e| tracing::error!(error=?e, project_id=?project_id, "unable to update entity access for project"));- // Update project- let _ = self.repo.update_project_modified(&project_id_str).await.inspect_err(- |e| tracing::error!(error=?e, project_id=?project_id, "unable to update project modified date"),- );- }-- // Attach task properties if creating a task- if document_response_metadata.sub_type == Some(DocumentSubType::Task) {- self.task_properties_service- .attach_task_properties(vec![document_response_metadata.document_id.clone()])- .await- .map_err(|e| {- tracing::error!(error=?e, document_id=?document_id, "failed to attach task properties");- DocumentError::Internal(anyhow!("failed to attach task properties"))- })?;- }-- let team_task_metadata = self.team_task_metadata_for_document(&document_id).await?;-- self.publish_document_event(&DocumentMacroEvent::created(- document_metadata.document_id.clone(),- DocumentCreatedMetadata {- document_id: document_metadata.document_id.clone(),- owner: document_metadata.owner.clone(),- actor: Some(attribution.actor()),- on_behalf_of: attribution.on_behalf_of(),- document_name: document_metadata.document_name.clone(),- file_type,- project_id: project_id.map(|p| p.to_string()),- sub_type: document_metadata.sub_type,- created_at: document_metadata.created_at,- },- ));-- Ok(CreateDocumentResponseData {- document_response: DocumentResponse {- document_metadata: DocumentResponseMetadataWithContent::new(- document_response_metadata,- initial_content,- )- .with_team_task_metadata(team_task_metadata),- presigned_url: Some(presigned_url),- },- content_type: mime_type,- file_type: file_type.map(|f| f.to_string()),- })}#[tracing::instrument(err, skip(self, document_context, args))]diff --git a/crates/documents/src/domain/service/tests.rs b/crates/documents/src/domain/service/tests.rsindex e870b753df..f1cd328b9f 100644--- a/crates/documents/src/domain/service/tests.rs+++ b/crates/documents/src/domain/service/tests.rs@@ -8,7 +8,9 @@ use macro_user_id::cowlike::CowLike;use model::document::{DocumentMetadata, FileType};use std::sync::{Arc, Mutex};-use crate::domain::models::GithubPullRequest;+use crate::domain::models::{+ EmailImportRepoOutcome, GithubPullRequest, ImportEmailAttachmentRepoArgs,+};use crate::domain::ports::{DocumentContentEventService, MockDocumentRepo};use super::*;@@ -1900,7 +1902,6 @@ fn create_document_repo_args(file_type: FileType) -> CreateDocumentRepoArgs {file_type: Some(file_type),project_id: None,team_id: None,- email_attachment_id: None,created_at: None,sub_type: None,skip_history: false,@@ -1998,24 +1999,29 @@ async fn create_document_publishes_resolved_attribution() {repo.expect_get_team_default_link_share().returning(|_| Box::pin(std::future::ready(Ok(None))));let created_metadata = make_test_metadata();- repo.expect_create_document()- .returning(move |_, _| Box::pin(std::future::ready(Ok(created_metadata.clone()))));+ repo.expect_import_email_attachment_document()+ .returning(move |_, _| {+ Box::pin(std::future::ready(Ok(EmailImportRepoOutcome::Created(+ created_metadata.clone(),+ ))))+ });repo.expect_set_document_content().returning(|_, _| Box::pin(std::future::ready(Ok(()))));repo.expect_get_team_task_metadata().returning(|_| Box::pin(std::future::ready(Ok(None))));let (service, event_broker) = make_test_service_with_event_broker(repo);- let mut args = create_document_repo_args(FileType::Txt);- args.email_attachment_id = Some(uuid::Uuid::from_u128(7));+ let args = ImportEmailAttachmentRepoArgs {+ email_attachment_id: uuid::Uuid::from_u128(7),+ create: create_document_repo_args(FileType::Txt),+ };- crate::domain::ports::DocumentService::create_document(+ crate::domain::ports::DocumentService::import_email_attachment(&service,macro_user_id::user_id::MacroUserIdStr::parse_from_str("macro|[email protected]").unwrap().into_owned(),args,- None,).await.unwrap();@@ -2038,6 +2044,60 @@ async fn create_document_publishes_resolved_attribution() {);}+#[tokio::test]+async fn create_document_reuse_skips_content_url_and_created_event() {+ let mut repo = make_mock_repo();+ repo.expect_get_team_default_link_share()+ .returning(|_| Box::pin(std::future::ready(Ok(None))));+ let created_metadata = make_test_metadata();+ repo.expect_import_email_attachment_document()+ .returning(move |_, _| {+ Box::pin(std::future::ready(Ok(EmailImportRepoOutcome::Reused(+ created_metadata.clone(),+ ))))+ });+ repo.expect_set_document_content().times(0);+ repo.expect_get_persisted_document_content()+ .return_once(|_| {+ Box::pin(std::future::ready(Ok(Some(DocumentContent::ready(+ DocumentContentLocation::ObjectStorage,+ )))))+ });+ repo.expect_get_team_task_metadata()+ .returning(|_| Box::pin(std::future::ready(Ok(None))));++ let (service, event_broker) = make_test_service_with_event_broker(repo);+ let args = ImportEmailAttachmentRepoArgs {+ email_attachment_id: uuid::Uuid::from_u128(9),+ create: create_document_repo_args(FileType::Txt),+ };++ let response = crate::domain::ports::DocumentService::import_email_attachment(+ &service,+ macro_user_id::user_id::MacroUserIdStr::parse_from_str("macro|[email protected]")+ .unwrap()+ .into_owned(),+ args,+ )+ .await+ .unwrap();++ assert!(response.document_response.presigned_url.is_none());+ assert_eq!(+ response+ .document_response+ .document_metadata+ .metadata+ .document_id,+ "doc-1"+ );+ assert_eq!(+ response.document_response.document_metadata.content,+ DocumentContent::ready(DocumentContentLocation::ObjectStorage),+ );+ assert!(event_broker.published().lock().unwrap().is_empty());+}+#[tokio::test]async fn edited_interaction_bumps_document_before_publishing() {let mut repo = make_mock_repo();diff --git a/crates/documents/src/inbound/axum_router/create_document.rs b/crates/documents/src/inbound/axum_router/create_document.rsindex a6900c8a51..6c619f74f3 100644--- a/crates/documents/src/inbound/axum_router/create_document.rs+++ b/crates/documents/src/inbound/axum_router/create_document.rs@@ -13,7 +13,7 @@ use model::document::{FileType, FileTypeExt};use models_permissions::share_permission::access_level::EditAccessLevel;use super::DocumentRouterState;-use crate::domain::models::{CreateDocumentRepoArgs, DocumentError};+use crate::domain::models::{CreateDocumentRepoArgs, DocumentError, ImportEmailAttachmentRepoArgs};use crate::domain::ports::DocumentService;use crate::domain::response::CreateDocumentResponse;@@ -96,7 +96,6 @@ pub async fn create_document_handler<file_type,project_id: req.project_id,team_id,- email_attachment_id: req.email_attachment_id,created_at: req.created_at,sub_type: req.is_task@@ -105,14 +104,24 @@ pub async fn create_document_handler<attribution: None,};- let response_data = state- .service- .create_document(- user.authorization.user.macro_user_id.clone(),- args,- req.job_id,- )- .await?;+ let user_id = user.authorization.user.macro_user_id.clone();+ let response_data = if let Some(email_attachment_id) = req.email_attachment_id {+ state+ .service+ .import_email_attachment(+ user_id,+ ImportEmailAttachmentRepoArgs {+ email_attachment_id,+ create: args,+ },+ )+ .await?+ } else {+ state+ .service+ .create_document(user_id, args, req.job_id)+ .await?+ };Ok(Json(CreateDocumentResponse {error: false,diff --git a/crates/documents/src/inbound/axum_router/tests.rs b/crates/documents/src/inbound/axum_router/tests.rsindex 13068befe7..210328de9d 100644--- a/crates/documents/src/inbound/axum_router/tests.rs+++ b/crates/documents/src/inbound/axum_router/tests.rs@@ -51,7 +51,7 @@ use crate::{models::{CommentThread, CreateDocumentRepoArgs, CreateTaskRequest, DocumentError,DocumentTeamShareResponse, EditDocumentServiceArgs, GithubPullRequestsResponse,- LocationQueryParams, TaskBranchName,+ ImportEmailAttachmentRepoArgs, LocationQueryParams, TaskBranchName,},ports::{DocumentContentEventService, DocumentService, create::DocumentCreationService},response::{@@ -80,7 +80,12 @@ const RESOLVED_DOCUMENT_ID: &str = "resolved-document";#[derive(Clone, Debug, Eq, PartialEq)]struct CreateDocumentCall {user_id: String,- email_attachment_id: Option<Uuid>,+}++#[derive(Clone, Debug, Eq, PartialEq)]+struct ImportEmailAttachmentCall {+ user_id: String,+ email_attachment_id: Uuid,}#[derive(Clone, Debug, Eq, PartialEq)]@@ -113,6 +118,7 @@ enum TeamSlugResult {#[derive(Default)]struct FakeDocumentService {create_calls: Mutex<Vec<CreateDocumentCall>>,+ import_calls: Mutex<Vec<ImportEmailAttachmentCall>>,upload_snapshot_calls: Mutex<Vec<UploadSnapshotCall>>,content_uploaded_calls: Mutex<Vec<ContentUploadedCall>>,internal_get_calls: Mutex<Vec<String>>,@@ -129,6 +135,13 @@ impl FakeDocumentService {.clone()}+ fn import_calls(&self) -> Vec<ImportEmailAttachmentCall> {+ self.import_calls+ .lock()+ .expect("import calls lock poisoned")+ .clone()+ }+fn upload_snapshot_calls(&self) -> Vec<UploadSnapshotCall> {self.upload_snapshot_calls.lock()@@ -277,7 +290,7 @@ impl DocumentService for FakeDocumentService {async fn create_document(&self,user_id: MacroUserIdStr<'static>,- args: CreateDocumentRepoArgs,+ _args: CreateDocumentRepoArgs,_job_id: Option<String>,) -> Result<CreateDocumentResponseData, DocumentError> {self.create_calls@@ -285,6 +298,21 @@ impl DocumentService for FakeDocumentService {.expect("create calls lock poisoned").push(CreateDocumentCall {user_id: user_id.as_ref().to_string(),+ });++ Ok(create_document_response(user_id))+ }++ async fn import_email_attachment(+ &self,+ user_id: MacroUserIdStr<'static>,+ args: ImportEmailAttachmentRepoArgs,+ ) -> Result<CreateDocumentResponseData, DocumentError> {+ self.import_calls+ .lock()+ .expect("import calls lock poisoned")+ .push(ImportEmailAttachmentCall {+ user_id: user_id.as_ref().to_string(),email_attachment_id: args.email_attachment_id,});@@ -1121,11 +1149,12 @@ async fn legacy_internal_headers_reach_the_internal_only_creation_path() {let (status, _body) = send(&router, request).await;assert_eq!(status, StatusCode::OK);+ assert!(document_service.create_calls().is_empty());assert_eq!(- document_service.create_calls(),- [CreateDocumentCall {+ document_service.import_calls(),+ [ImportEmailAttachmentCall {user_id: LEGACY_INTERNAL_USER_ID.to_string(),- email_attachment_id: Some(email_attachment_id),+ email_attachment_id,}]);assert!(!authorization_service.calls().is_empty());@@ -1153,9 +1182,9 @@ async fn standard_internal_headers_reach_the_document_service() {document_service.create_calls(),[CreateDocumentCall {user_id: STANDARD_INTERNAL_USER_ID.to_string(),- email_attachment_id: None,}]);+ assert!(document_service.import_calls().is_empty());assert!(!authorization_service.calls().is_empty());assert!(authorization_service.calls().iter().all(|call| matches!(call,@@ -1183,9 +1212,9 @@ async fn standard_internal_headers_take_precedence_over_legacy_headers() {document_service.create_calls(),[CreateDocumentCall {user_id: STANDARD_INTERNAL_USER_ID.to_string(),- email_attachment_id: None,}]);+ assert!(document_service.import_calls().is_empty());assert!(!authorization_service.calls().is_empty());assert!(authorization_service.calls().iter().all(|call| matches!(call,@@ -1213,4 +1242,5 @@ async fn jwt_user_cannot_create_a_document_for_an_email_attachment() {assert_eq!(status, StatusCode::UNAUTHORIZED);assert_eq!(body, json!({ "message": "unauthorized" }));assert!(document_service.create_calls().is_empty());+ assert!(document_service.import_calls().is_empty());}diff --git a/crates/documents/src/inbound/toolset/edit_document/test.rs b/crates/documents/src/inbound/toolset/edit_document/test.rsindex 9700871989..7959363270 100644--- a/crates/documents/src/inbound/toolset/edit_document/test.rs+++ b/crates/documents/src/inbound/toolset/edit_document/test.rs@@ -7,7 +7,7 @@ use crate::domain::events::InteractionReason;use crate::domain::models::{CommentThread, CreateDocumentRepoArgs, CreateTaskRequest, DocumentError,DocumentTeamShareResponse, EditDocumentServiceArgs, GithubPullRequestsResponse,- LocationQueryParams, TaskBranchName,+ ImportEmailAttachmentRepoArgs, LocationQueryParams, TaskBranchName,};use crate::domain::ports::editing::{EditResult, EditingWorkerService};use crate::domain::response::{@@ -129,6 +129,14 @@ impl DocumentService for FakeDocumentService {panic!("unexpected create_document call")}+ async fn import_email_attachment(+ &self,+ _user_id: MacroUserIdStr<'static>,+ _args: ImportEmailAttachmentRepoArgs,+ ) -> Result<CreateDocumentResponseData, DocumentError> {+ panic!("unexpected import_email_attachment call")+ }+async fn get_short_id(&self,_entity_access_receipt: EntityAccessReceipt<ViewAccessLevel>,diff --git a/crates/documents/src/outbound/pg_document_repo.rs b/crates/documents/src/outbound/pg_document_repo.rsindex cb1a9ceb00..d961038632 100644--- a/crates/documents/src/outbound/pg_document_repo.rs+++ b/crates/documents/src/outbound/pg_document_repo.rs@@ -23,7 +23,8 @@ use sqlx::Row;use crate::domain::content::{DocumentContent, DocumentContentState};use crate::domain::models::{BranchNameContext, Comment, CommentThread, CopyDocumentRepoArgs, CreateDocumentRepoArgs,- DocumentTeamShare, EditDocumentRepoArgs, TeamTaskMetadata, Thread,+ DocumentTeamShare, EditDocumentRepoArgs, EmailImportRepoOutcome, ImportEmailAttachmentRepoArgs,+ TeamTaskMetadata, Thread,};use crate::domain::ports::DocumentRepo;@@ -418,121 +419,68 @@ impl DocumentRepo for PgDocumentRepo {args: CreateDocumentRepoArgs,share_permission: SharePermissionV2,) -> Result<DocumentMetadata, Self::Err> {- let CreateDocumentRepoArgs {- id,- sha,- document_name,- user_id,- file_type,- project_id,- team_id,+ let mut transaction = self.pool.begin().await?;+ let metadata =+ create::insert_new_document(&mut transaction, args, &share_permission).await?;+ transaction.commit().await?;+ Ok(metadata)+ }++ #[tracing::instrument(err, skip(self, args, share_permission))]+ async fn import_email_attachment_document(+ &self,+ args: ImportEmailAttachmentRepoArgs,+ share_permission: SharePermissionV2,+ ) -> Result<EmailImportRepoOutcome, Self::Err> {+ let ImportEmailAttachmentRepoArgs {email_attachment_id,- created_at: provided_created_at,- sub_type: requested_sub_type,- skip_history,- attribution: _,+ create,} = args;- let now = chrono::Utc::now();- let created_at = provided_created_at.as_ref().unwrap_or(&now);-let mut transaction = self.pool.begin().await?;- // Fetch project name if project_id provided- let project_name: Option<String> = if let Some(ref proj_id) = project_id {- sqlx::query_scalar!(- r#"SELECT name FROM "Project" WHERE id = $1"#,- &proj_id.to_string(),- )- .fetch_optional(&mut *transaction)- .await?- } else {- None- };-- let document_id = create::insert_document_row(+ if let Some(existing_id) = create::reuse_email_document(&mut transaction,- id.as_ref(),- &user_id,- &document_name,- file_type,- project_id.as_ref(),- created_at,+ create.user_id.as_ref(),+ &create.sha,+ email_attachment_id,)- .await?;-- // Insert document sub-type- let sub_type: Option<DocumentSubType> =- create::set_document_sub_type(&mut transaction, &document_id, requested_sub_type)- .await?;-- if sub_type == Some(DocumentSubType::Task)- && let Some(team_id) = team_id.as_ref()+ .await?{- create::allocate_team_task_number(&mut transaction, team_id, &document_id).await?;+ transaction.commit().await?;+ let metadata = self.get_document_metadata(&existing_id).await?;+ return Ok(EmailImportRepoOutcome::Reused(metadata));}- // Insert document version (DocumentBom for docx, DocumentInstance for others)- let document_version = create::set_document_version(- &mut transaction,- &document_id,- file_type,- sha,- created_at,- )- .await?;-- // Create share permission- create::set_share_permission(&mut transaction, &document_id, &share_permission).await?;-- // Add to user history (if not skipped)- if !skip_history {- create::insert_history(&mut transaction, &document_id, &user_id, created_at).await?;- }+ let metadata =+ create::insert_new_document(&mut transaction, create, &share_permission).await?;- // Insert user entity access (Owner level)- entity_access_db_utils::insert_entity_access_row(+ match create::link_document_email(&mut transaction,- &document_id,- entity_access_db_utils::EntityType::Document,- user_id.as_ref(),- entity_access_db_utils::EntityAccessSourceType::User,- entity_access_db_utils::AccessLevel::Owner,+ &metadata.document_id,+ email_attachment_id,)- .await?;-- // Link to email attachment if provided- if let Some(attachment_id) = email_attachment_id {- sqlx::query!(- r#"- INSERT INTO "document_email" (document_id, email_attachment_id)- VALUES ($1, $2)- "#,- &document_id.to_string(),- attachment_id,- )- .execute(&mut *transaction)- .await?;+ .await+ {+ Ok(()) => {}+ Err(sqlx::Error::Database(ref db_err)) if db_err.is_unique_violation() => {+ transaction.rollback().await?;+ let existing_id =+ create::find_document_id_for_email_attachment(&self.pool, email_attachment_id)+ .await?+ .ok_or_else(|| {+ sqlx::Error::Protocol(+ "email attachment already linked but document is missing".into(),+ )+ })?;+ let metadata = self.get_document_metadata(&existing_id).await?;+ return Ok(EmailImportRepoOutcome::Reused(metadata));+ }+ Err(e) => return Err(e),}transaction.commit().await?;-- Ok(DocumentMetadata::new_document(- &document_id.to_string(),- document_version.id,- user_id,- &document_name,- file_type,- &document_version.sha,- None,- None,- None,- project_id.map(|s| s.to_string()).as_deref(),- project_name.as_deref(),- document_version.created_at,- document_version.updated_at,- sub_type,- ))+ Ok(EmailImportRepoOutcome::Created(metadata))}#[tracing::instrument(err, skip(self, args))]diff --git a/crates/documents/src/outbound/pg_document_repo/create.rs b/crates/documents/src/outbound/pg_document_repo/create.rsindex a5c7aad731..7af0852795 100644--- a/crates/documents/src/outbound/pg_document_repo/create.rs+++ b/crates/documents/src/outbound/pg_document_repo/create.rs@@ -1,8 +1,10 @@use document_sub_type::DocumentSubType;use macro_user_id::user_id::MacroUserIdStr;-use model::document::{FileType, VersionIDWithTimeStamps};+use model::document::{DocumentMetadata, FileType, VersionIDWithTimeStamps};use models_permissions::share_permission::SharePermissionV2;+use crate::domain::models::CreateDocumentRepoArgs;+/// Inserts a record into the document table/// Returns the document id#[tracing::instrument(skip(transaction), err)]@@ -234,3 +236,205 @@ pub async fn insert_history<'a>(Ok(())}++pub async fn find_document_id_for_email_attachment<'e, E>(+ executor: E,+ email_attachment_id: uuid::Uuid,+) -> Result<Option<String>, sqlx::Error>+where+ E: sqlx::Executor<'e, Database = sqlx::Postgres>,+{+ sqlx::query_scalar!(+ r#"+ SELECT de.document_id+ FROM document_email de+ JOIN "Document" d ON d.id = de.document_id+ WHERE de.email_attachment_id = $1+ ORDER BY (d."deletedAt" IS NULL) DESC, de.document_id+ LIMIT 1+ "#,+ email_attachment_id,+ )+ .fetch_optional(executor)+ .await+}++pub async fn find_live_email_document_id_by_sha<'e, E>(+ executor: E,+ owner: &str,+ sha: &str,+) -> Result<Option<String>, sqlx::Error>+where+ E: sqlx::Executor<'e, Database = sqlx::Postgres>,+{+ sqlx::query_scalar!(+ r#"+ SELECT d.id+ FROM "Document" d+ INNER JOIN LATERAL (+ SELECT i.sha+ FROM "DocumentInstance" i+ WHERE i."documentId" = d.id+ ORDER BY i."createdAt" DESC+ LIMIT 1+ ) latest ON latest.sha = $2+ WHERE d.owner = $1+ AND d."deletedAt" IS NULL+ AND EXISTS (+ SELECT 1+ FROM document_email de+ WHERE de.document_id = d.id+ )+ ORDER BY d."createdAt" ASC, d.id ASC+ LIMIT 1+ "#,+ owner,+ sha,+ )+ .fetch_optional(executor)+ .await+}++pub async fn link_document_email(+ transaction: &mut sqlx::Transaction<'_, sqlx::Postgres>,+ document_id: &str,+ email_attachment_id: uuid::Uuid,+) -> Result<(), sqlx::Error> {+ sqlx::query!(+ r#"+ INSERT INTO "document_email" (document_id, email_attachment_id)+ VALUES ($1, $2)+ "#,+ document_id,+ email_attachment_id,+ )+ .execute(transaction.as_mut())+ .await?;++ Ok(())+}++pub async fn reuse_email_document(+ transaction: &mut sqlx::Transaction<'_, sqlx::Postgres>,+ owner: &str,+ sha: &str,+ email_attachment_id: uuid::Uuid,+) -> Result<Option<String>, sqlx::Error> {+ sqlx::query!(+ r#"SELECT pg_advisory_xact_lock(hashtext($1), hashtext($2))"#,+ owner,+ sha,+ )+ .execute(transaction.as_mut())+ .await?;++ if let Some(document_id) =+ find_document_id_for_email_attachment(&mut **transaction, email_attachment_id).await?+ {+ return Ok(Some(document_id));+ }++ if let Some(document_id) =+ find_live_email_document_id_by_sha(&mut **transaction, owner, sha).await?+ {+ link_document_email(transaction, &document_id, email_attachment_id).await?;+ return Ok(Some(document_id));+ }++ Ok(None)+}++/// Inserts a new `Document` row and its associated create records.+///+/// Does not link email attachments. Callers that need that belong on+/// [`crate::domain::ports::DocumentRepo::import_email_attachment_document`].+#[tracing::instrument(skip(transaction, args, share_permission), err)]+pub async fn insert_new_document(+ transaction: &mut sqlx::Transaction<'_, sqlx::Postgres>,+ args: CreateDocumentRepoArgs,+ share_permission: &SharePermissionV2,+) -> Result<DocumentMetadata, sqlx::Error> {+ let CreateDocumentRepoArgs {+ id,+ sha,+ document_name,+ user_id,+ file_type,+ project_id,+ team_id,+ created_at: provided_created_at,+ sub_type: requested_sub_type,+ skip_history,+ attribution: _,+ } = args;++ let now = chrono::Utc::now();+ let created_at = provided_created_at.as_ref().unwrap_or(&now);++ let project_name: Option<String> = if let Some(ref proj_id) = project_id {+ sqlx::query_scalar!(+ r#"SELECT name FROM "Project" WHERE id = $1"#,+ &proj_id.to_string(),+ )+ .fetch_optional(&mut **transaction)+ .await?+ } else {+ None+ };++ let document_id = insert_document_row(+ transaction,+ id.as_ref(),+ &user_id,+ &document_name,+ file_type,+ project_id.as_ref(),+ created_at,+ )+ .await?;++ let sub_type: Option<DocumentSubType> =+ set_document_sub_type(transaction, &document_id, requested_sub_type).await?;++ if sub_type == Some(DocumentSubType::Task)+ && let Some(team_id) = team_id.as_ref()+ {+ allocate_team_task_number(transaction, team_id, &document_id).await?;+ }++ let document_version =+ set_document_version(transaction, &document_id, file_type, sha, created_at).await?;++ set_share_permission(transaction, &document_id, share_permission).await?;++ if !skip_history {+ insert_history(transaction, &document_id, &user_id, created_at).await?;+ }++ entity_access_db_utils::insert_entity_access_row(+ transaction,+ &document_id,+ entity_access_db_utils::EntityType::Document,+ user_id.as_ref(),+ entity_access_db_utils::EntityAccessSourceType::User,+ entity_access_db_utils::AccessLevel::Owner,+ )+ .await?;++ Ok(DocumentMetadata::new_document(+ &document_id.to_string(),+ document_version.id,+ user_id,+ &document_name,+ file_type,+ &document_version.sha,+ None,+ None,+ None,+ project_id.map(|s| s.to_string()).as_deref(),+ project_name.as_deref(),+ document_version.created_at,+ document_version.updated_at,+ sub_type,+ ))+}diff --git a/crates/documents/src/outbound/pg_document_repo/tests.rs b/crates/documents/src/outbound/pg_document_repo/tests.rsindex aef50a34f4..2805b7eb19 100644--- a/crates/documents/src/outbound/pg_document_repo/tests.rs+++ b/crates/documents/src/outbound/pg_document_repo/tests.rs@@ -11,8 +11,8 @@ use models_permissions::share_permission::{use sqlx::{Pool, Postgres, Row};use crate::domain::models::{- CopyDocumentRepoArgs, CreateDocumentRepoArgs, EditDocumentRepoArgs, FileTypeUpdate,- GithubPullRequest, GithubPullRequestsResponse,+ CopyDocumentRepoArgs, CreateDocumentRepoArgs, EditDocumentRepoArgs, EmailImportRepoOutcome,+ FileTypeUpdate, GithubPullRequest, GithubPullRequestsResponse, ImportEmailAttachmentRepoArgs,};use crate::domain::ports::DocumentRepo;use crate::outbound::pg_document_repo::PgDocumentRepo;@@ -42,7 +42,6 @@ fn create_document_args(file_type: Some(model::document::FileType::Md),project_id: None,team_id,- email_attachment_id: None,created_at: None,sub_type: is_task.then_some(document_sub_type::DocumentSubType::Task),skip_history: false,@@ -1739,3 +1738,288 @@ async fn test_get_project_children_empty(pool: Pool<Postgres>) {assert!(children.is_empty());}++fn pdf_share_permission() -> SharePermissionV2 {+ SharePermissionV2::new_document_share_permission(Some(model::document::FileType::Pdf), None)+}++fn import_email_document_args(+ owner: &str,+ sha: &str,+ email_attachment_id: uuid::Uuid,+) -> ImportEmailAttachmentRepoArgs {+ ImportEmailAttachmentRepoArgs {+ email_attachment_id,+ create: CreateDocumentRepoArgs {+ id: None,+ sha: sha.to_string(),+ document_name: "contract".to_string(),+ user_id: user_id(owner),+ file_type: Some(model::document::FileType::Pdf),+ project_id: None,+ team_id: None,+ created_at: None,+ sub_type: None,+ skip_history: true,+ attribution: None,+ },+ }+}++async fn insert_email_attachments(pool: &Pool<Postgres>, count: usize) -> Vec<uuid::Uuid> {+ let link_id = uuid::Uuid::new_v4();+ let contact_id = uuid::Uuid::new_v4();+ let thread_id = uuid::Uuid::new_v4();+ let message_id = uuid::Uuid::new_v4();++ sqlx::query(+ r#"+ INSERT INTO email_links (id, macro_id, fusionauth_user_id, email_address, provider, is_sync_active)+ VALUES ($1, $2, $3, $4, 'GMAIL', true)+ "#,+ )+ .bind(link_id)+ .bind(TEST_DOCUMENT_OWNER_ID)+ .bind(TEST_DOCUMENT_OWNER_ID)+ .bind("[email protected]")+ .execute(pool)+ .await+ .unwrap();++ sqlx::query(+ r#"+ INSERT INTO email_contacts (id, link_id, email_address)+ VALUES ($1, $2, '[email protected]')+ "#,+ )+ .bind(contact_id)+ .bind(link_id)+ .execute(pool)+ .await+ .unwrap();++ sqlx::query(+ r#"+ INSERT INTO email_threads (id, link_id, inbox_visible, is_read)+ VALUES ($1, $2, true, false)+ "#,+ )+ .bind(thread_id)+ .bind(link_id)+ .execute(pool)+ .await+ .unwrap();++ sqlx::query(+ r#"+ INSERT INTO email_messages (+ id, thread_id, link_id, provider_id, is_sent, from_contact_id,+ internal_date_ts, has_attachments, is_read, is_starred, is_draft+ )+ VALUES ($1, $2, $3, $4, false, $5, NOW(), true, false, false, false)+ "#,+ )+ .bind(message_id)+ .bind(thread_id)+ .bind(link_id)+ .bind(format!("provider-msg-{message_id}"))+ .bind(contact_id)+ .execute(pool)+ .await+ .unwrap();++ let mut ids = Vec::with_capacity(count);+ for i in 0..count {+ let attachment_id = uuid::Uuid::new_v4();+ sqlx::query(+ r#"+ INSERT INTO email_attachments (+ id, message_id, provider_attachment_id, filename, mime_type, size_bytes+ )+ VALUES ($1, $2, $3, $4, 'application/pdf', 1024)+ "#,+ )+ .bind(attachment_id)+ .bind(message_id)+ .bind(format!("provider-att-{i}"))+ .bind(format!("contract-{i}.pdf"))+ .execute(pool)+ .await+ .unwrap();+ ids.push(attachment_id);+ }+ ids+}++async fn document_email_rows(pool: &Pool<Postgres>, document_id: &str) -> Vec<uuid::Uuid> {+ sqlx::query_scalar::<_, uuid::Uuid>(+ r#"SELECT email_attachment_id FROM document_email WHERE document_id = $1 ORDER BY email_attachment_id"#,+ )+ .bind(document_id)+ .fetch_all(pool)+ .await+ .unwrap()+}++#[sqlx::test(+ migrator = "MACRO_DB_MIGRATIONS",+ fixtures(path = "../../../fixtures", scripts("documents_test_data"))+)]+async fn test_import_email_attachment_reuses_document_by_sha(pool: Pool<Postgres>) {+ let repo = PgDocumentRepo::new(pool.clone());+ let attachments = insert_email_attachments(&pool, 2).await;+ let sha = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";++ let first = repo+ .import_email_attachment_document(+ import_email_document_args(TEST_DOCUMENT_OWNER_ID, sha, attachments[0]),+ pdf_share_permission(),+ )+ .await+ .unwrap();+ let second = repo+ .import_email_attachment_document(+ import_email_document_args(TEST_DOCUMENT_OWNER_ID, sha, attachments[1]),+ pdf_share_permission(),+ )+ .await+ .unwrap();++ assert!(matches!(first, EmailImportRepoOutcome::Created(_)));+ assert!(matches!(second, EmailImportRepoOutcome::Reused(_)));+ assert_eq!(first.metadata().document_id, second.metadata().document_id);+ let mut expected = attachments.clone();+ expected.sort();+ assert_eq!(+ document_email_rows(&pool, &first.metadata().document_id).await,+ expected+ );+}++#[sqlx::test(+ migrator = "MACRO_DB_MIGRATIONS",+ fixtures(path = "../../../fixtures", scripts("documents_test_data"))+)]+async fn test_import_email_attachment_same_attachment_id_reuses_document(pool: Pool<Postgres>) {+ let repo = PgDocumentRepo::new(pool.clone());+ let attachments = insert_email_attachments(&pool, 1).await;+ let sha = "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb";++ let first = repo+ .import_email_attachment_document(+ import_email_document_args(TEST_DOCUMENT_OWNER_ID, sha, attachments[0]),+ pdf_share_permission(),+ )+ .await+ .unwrap();+ let second = repo+ .import_email_attachment_document(+ import_email_document_args(TEST_DOCUMENT_OWNER_ID, sha, attachments[0]),+ pdf_share_permission(),+ )+ .await+ .unwrap();++ assert!(matches!(first, EmailImportRepoOutcome::Created(_)));+ assert!(matches!(second, EmailImportRepoOutcome::Reused(_)));+ assert_eq!(first.metadata().document_id, second.metadata().document_id);+ assert_eq!(+ document_email_rows(&pool, &first.metadata().document_id).await,+ attachments+ );+}++#[sqlx::test(+ migrator = "MACRO_DB_MIGRATIONS",+ fixtures(path = "../../../fixtures", scripts("documents_test_data"))+)]+async fn test_import_email_attachment_does_not_reuse_non_email_document_by_sha(+ pool: Pool<Postgres>,+) {+ let repo = PgDocumentRepo::new(pool.clone());+ let attachments = insert_email_attachments(&pool, 1).await;+ let sha = "cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc";++ let mut uploaded = create_document_args(TEST_DOCUMENT_OWNER_ID, false, None);+ uploaded.sha = sha.to_string();+ uploaded.file_type = Some(model::document::FileType::Pdf);+ uploaded.document_name = "manual-upload".to_string();++ let uploaded_doc = repo+ .create_document(uploaded, pdf_share_permission())+ .await+ .unwrap();+ let imported = repo+ .import_email_attachment_document(+ import_email_document_args(TEST_DOCUMENT_OWNER_ID, sha, attachments[0]),+ pdf_share_permission(),+ )+ .await+ .unwrap();++ assert!(matches!(imported, EmailImportRepoOutcome::Created(_)));+ assert_ne!(uploaded_doc.document_id, imported.metadata().document_id);+}++#[sqlx::test(+ migrator = "MACRO_DB_MIGRATIONS",+ fixtures(path = "../../../fixtures", scripts("documents_test_data"))+)]+async fn test_import_email_attachment_does_not_reuse_other_owner_sha(pool: Pool<Postgres>) {+ let repo = PgDocumentRepo::new(pool.clone());+ let attachments = insert_email_attachments(&pool, 2).await;+ let sha = "dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd";++ let owner_doc = repo+ .import_email_attachment_document(+ import_email_document_args(TEST_DOCUMENT_OWNER_ID, sha, attachments[0]),+ pdf_share_permission(),+ )+ .await+ .unwrap();+ let teammate_doc = repo+ .import_email_attachment_document(+ import_email_document_args(TEST_DOCUMENT_NON_OWNER_ID, sha, attachments[1]),+ pdf_share_permission(),+ )+ .await+ .unwrap();++ assert!(matches!(owner_doc, EmailImportRepoOutcome::Created(_)));+ assert!(matches!(teammate_doc, EmailImportRepoOutcome::Created(_)));+ assert_ne!(+ owner_doc.metadata().document_id,+ teammate_doc.metadata().document_id+ );+}++#[sqlx::test(+ migrator = "MACRO_DB_MIGRATIONS",+ fixtures(path = "../../../fixtures", scripts("documents_test_data"))+)]+async fn test_create_document_does_not_reuse_email_document_by_sha(pool: Pool<Postgres>) {+ let repo = PgDocumentRepo::new(pool.clone());+ let attachments = insert_email_attachments(&pool, 1).await;+ let sha = "eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee";++ let imported = repo+ .import_email_attachment_document(+ import_email_document_args(TEST_DOCUMENT_OWNER_ID, sha, attachments[0]),+ pdf_share_permission(),+ )+ .await+ .unwrap();++ let mut created = create_document_args(TEST_DOCUMENT_OWNER_ID, false, None);+ created.sha = sha.to_string();+ created.file_type = Some(model::document::FileType::Pdf);+ created.document_name = "same-sha-upload".to_string();++ let created_doc = repo+ .create_document(created, pdf_share_permission())+ .await+ .unwrap();++ assert!(matches!(imported, EmailImportRepoOutcome::Created(_)));+ assert_ne!(imported.metadata().document_id, created_doc.document_id);+}diff --git a/crates/github/src/domain/service/sync/test.rs b/crates/github/src/domain/service/sync/test.rsindex fe86a062e0..c33903bb1c 100644--- a/crates/github/src/domain/service/sync/test.rs+++ b/crates/github/src/domain/service/sync/test.rs@@ -15,7 +15,9 @@ use document_sub_type::DocumentSubType;use documents::domain::models::EditDocumentServiceArgs;use documents::domain::{content::{DocumentContent, DocumentContentLocation},- models::{CreateDocumentRepoArgs, DocumentError, LocationQueryParams},+ models::{+ CreateDocumentRepoArgs, DocumentError, ImportEmailAttachmentRepoArgs, LocationQueryParams,+ },ports::DocumentService,response::{CreateDocumentResponseData, DocumentMetadataWithContent, DocumentResponse,@@ -215,6 +217,14 @@ impl DocumentService for StubDocumentService {unimplemented!()}+ async fn import_email_attachment(+ &self,+ _user_id: MacroUserIdStr<'static>,+ _args: ImportEmailAttachmentRepoArgs,+ ) -> Result<CreateDocumentResponseData, DocumentError> {+ unimplemented!()+ }+async fn get_document_content(&self,_document_context: &DocumentBasic,diff --git a/crates/system_properties/src/domain/port.rs b/crates/system_properties/src/domain/port.rsindex 20d6b5581a..81fe522c57 100644--- a/crates/system_properties/src/domain/port.rs+++ b/crates/system_properties/src/domain/port.rs@@ -16,6 +16,12 @@ pub trait SystemPropertiesRepository: Clone + Send + Sync + 'static {rows: Vec<crate::domain::model::PropertyRow>,) -> impl Future<Output = Result<(), SystemPropertyError>> + Send;+ /// Insert property rows, leaving any existing values unchanged.+ fn bulk_insert_properties_if_absent(+ &self,+ rows: Vec<crate::domain::model::PropertyRow>,+ ) -> impl Future<Output = Result<(), SystemPropertyError>> + Send;+/// Copy all task properties from one entity to another.fn copy_task_properties(&self,diff --git a/crates/system_properties/src/domain/service.rs b/crates/system_properties/src/domain/service.rsindex b31727c6eb..29a5a4d7d7 100644--- a/crates/system_properties/src/domain/service.rs+++ b/crates/system_properties/src/domain/service.rs@@ -17,8 +17,9 @@ use crate::{pub trait SystemPropertiesService: Clone + Send + Sync + 'static {/// Set email attachment properties for multiple entities.///- /// Only properties that are `Some` will be updated.- /// All properties are upserted in a single query.+ /// Only properties that are `Some` will be written.+ /// Existing values are left unchanged so a later import does not clobber+ /// Source, Sender, Recipients, or Subject.fn set_email_attachment_properties(&self,items: Vec<EmailAttachmentInput>,@@ -84,7 +85,7 @@ where.flat_map(|item| collect_email_property_rows(&item.entity_id, item.properties)).collect();- self.repository.bulk_upsert_properties(rows).await+ self.repository.bulk_insert_properties_if_absent(rows).await}#[tracing::instrument(skip(self, entity_ids))]diff --git a/crates/system_properties/src/outbound/pgpool.rs b/crates/system_properties/src/outbound/pgpool.rsindex ec8ce66d41..1ff8abb875 100644--- a/crates/system_properties/src/outbound/pgpool.rs+++ b/crates/system_properties/src/outbound/pgpool.rs@@ -86,6 +86,60 @@ impl SystemPropertiesRepository for PgSystemPropertiesRepository {Ok(())}+ async fn bulk_insert_properties_if_absent(+ &self,+ rows: Vec<PropertyRow>,+ ) -> Result<(), SystemPropertyError> {+ if rows.is_empty() {+ return Ok(());+ }++ let ids: Vec<Uuid> = rows.iter().map(|_| generate_uuid_v7()).collect();+ let entity_ids: Vec<&str> = rows.iter().map(|r| r.entity_id()).collect();+ let entity_types: Vec<String> = rows+ .iter()+ .map(|r| {+ serde_json::to_value(r.entity_type())+ .expect("EntityType serializes to JSON")+ .as_str()+ .expect("EntityType serializes to string")+ .to_string()+ })+ .collect();+ let property_ids: Vec<Uuid> = rows.iter().map(|r| r.property_definition_id()).collect();+ let values: Vec<serde_json::Value> = rows.iter().map(|r| r.values().clone()).collect();++ sqlx::query!(+ r#"+ INSERT INTO entity_properties (id, entity_id, entity_type, property_definition_id, values)+ SELECT+ u.id,+ u.entity_id,+ u.entity_type::property_entity_type,+ u.property_definition_id,+ u.values+ FROM UNNEST(+ $1::UUID[],+ $2::TEXT[],+ $3::TEXT[],+ $4::UUID[],+ $5::JSONB[]+ ) AS u(id, entity_id, entity_type, property_definition_id, values)+ ON CONFLICT (entity_id, entity_type, property_definition_id)+ DO NOTHING+ "#,+ &ids,+ &entity_ids as &[&str],+ &entity_types,+ &property_ids,+ &values,+ )+ .execute(&self.pool)+ .await?;++ Ok(())+ }+async fn copy_task_properties(&self,from_task_id: &str,diff --git a/crates/system_properties/src/outbound/pgpool/test.rs b/crates/system_properties/src/outbound/pgpool/test.rsindex 75c2e15240..d4e7ef1a52 100644--- a/crates/system_properties/src/outbound/pgpool/test.rs+++ b/crates/system_properties/src/outbound/pgpool/test.rs@@ -128,6 +128,49 @@ async fn test_bulk_upsert_properties_multiple_entities(pool: Pool<Postgres>) ->Ok(())}+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]+async fn test_bulk_insert_properties_if_absent_keeps_first_write(+ pool: Pool<Postgres>,+) -> anyhow::Result<()> {+ let repo = PgSystemPropertiesRepository::new(pool.clone());+ let entity_id = "email-doc-first-write";+ let subject_id = SystemPropertyKey::Subject.uuid();++ repo.bulk_insert_properties_if_absent(vec![PropertyRow::string_value(+ entity_id,+ EntityType::Document,+ subject_id,+ "original subject",+ )])+ .await?;+ repo.bulk_insert_properties_if_absent(vec![PropertyRow::string_value(+ entity_id,+ EntityType::Document,+ subject_id,+ "forwarded subject",+ )])+ .await?;++ let stored = sqlx::query_scalar!(+ r#"+ SELECT values+ FROM entity_properties+ WHERE entity_id = $1 AND property_definition_id = $2+ "#,+ entity_id,+ subject_id,+ )+ .fetch_one(&pool)+ .await?;++ assert_eq!(+ stored.unwrap(),+ serde_json::json!({"type": "String", "value": "original subject"})+ );++ Ok(())+}+// ============================================================================// copy_task_properties tests// ============================================================================diff --git a/services/email_service/src/bin/backfill_gmail_attachments/upload.rs b/services/email_service/src/bin/backfill_gmail_attachments/upload.rsindex fe0cc77320..f4ae423131 100644--- a/services/email_service/src/bin/backfill_gmail_attachments/upload.rs+++ b/services/email_service/src/bin/backfill_gmail_attachments/upload.rs@@ -70,10 +70,12 @@ impl AttachmentProcessor {// 2. Calculate hashes required for validation.let (sha256_hex, sha256_base64) = Self::calculate_hashes(&data);- // 3. Get a presigned URL from the Document Storage Service.- let (presigned_url, content_type) = self.get_presigned_url(attachment, &sha256_hex).await?;+ let Some((presigned_url, content_type)) =+ self.get_presigned_url(attachment, &sha256_hex).await?+ else {+ return Ok(());+ };- // 4. Upload the data to the presigned URL (e.g., S3).self.upload_to_storage(&presigned_url, &content_type, &sha256_base64, data).await?;@@ -113,7 +115,7 @@ impl AttachmentProcessor {&self,attachment: &AttachmentUploadMetadata,sha256_hex: &str,- ) -> anyhow::Result<(String, String)> {+ ) -> anyhow::Result<Option<(String, String)>> {// we filter out null filenames in the db query, but set to default just in caselet file_name = attachment.filename@@ -153,13 +155,11 @@ impl AttachmentProcessor {.await.context("DSS create_document call failed")?;- let presigned_url = dss_response- .data- .document_response- .presigned_url- .context("DSS response did not include a presigned URL")?;+ let Some(presigned_url) = dss_response.data.document_response.presigned_url else {+ return Ok(None);+ };- Ok((presigned_url, dss_response.data.content_type))+ Ok(Some((presigned_url, dss_response.data.content_type)))}/// Performs the final PUT request to upload the file data to cloud storage.diff --git a/services/email_service/src/util/upload_attachment.rs b/services/email_service/src/util/upload_attachment.rsindex 677715a569..293533f375 100644--- a/services/email_service/src/util/upload_attachment.rs+++ b/services/email_service/src/util/upload_attachment.rs@@ -150,8 +150,9 @@ async fn upload_document_attachment().await?;- // 4. Upload the attachment data to the presigned URL.- upload_data_to_presigned_url(&dss_response, attachment_data, &base64_hash).await?;+ if dss_response.data.document_response.presigned_url.is_some() {+ upload_data_to_presigned_url(&dss_response, attachment_data, &base64_hash).await?;+ }// 5. Get document idlet document_id = dss_response