Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

fix(email): recover attachments whose upload never finished (#9599)

teo9cf153cba61f
diff --git a/.sqlx/query-1b81042c78e1ed0307bcea8735f699c3e4908ae7533f74144f27a5bcd0c4e5f6.json b/.sqlx/query-1b81042c78e1ed0307bcea8735f699c3e4908ae7533f74144f27a5bcd0c4e5f6.json
new file mode 100644
index 0000000000..b3e16dc3bb
--- /dev/null
+++ b/.sqlx/query-1b81042c78e1ed0307bcea8735f699c3e4908ae7533f74144f27a5bcd0c4e5f6.json
@@ -0,0 +1,28 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n SELECT\n document_id,\n (NOT d.uploaded AND d.\"fileType\" IS DISTINCT FROM 'docx') AS \"awaiting_upload!\"\n FROM document_email de\n INNER JOIN \"Document\" d on de.document_id = d.id\n INNER JOIN email_attachments ea on de.email_attachment_id = ea.id\n INNER JOIN email_messages em on ea.message_id = em.id\n WHERE email_attachment_id = $1 AND d.\"deletedAt\" IS NULL\n ",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "document_id",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 1,
+ "name": "awaiting_upload!",
+ "type_info": "Bool"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid"
+ ]
+ },
+ "nullable": [
+ false,
+ null
+ ]
+ },
+ "hash": "1b81042c78e1ed0307bcea8735f699c3e4908ae7533f74144f27a5bcd0c4e5f6"
+}
diff --git a/.sqlx/query-b3bb26f4b163d8bea13a5a3fb0ea3d89543ceaefae74f8962e4504196d0b76c2.json b/.sqlx/query-b3bb26f4b163d8bea13a5a3fb0ea3d89543ceaefae74f8962e4504196d0b76c2.json
deleted file mode 100644
index 3a0d508069..0000000000
--- a/.sqlx/query-b3bb26f4b163d8bea13a5a3fb0ea3d89543ceaefae74f8962e4504196d0b76c2.json
+++ /dev/null
@@ -1,22 +0,0 @@
-{
- "db_name": "PostgreSQL",
- "query": "\n SELECT document_id\n FROM document_email de\n INNER JOIN \"Document\" d on de.document_id = d.id\n INNER JOIN email_attachments ea on de.email_attachment_id = ea.id\n INNER JOIN email_messages em on ea.message_id = em.id\n WHERE email_attachment_id = $1 AND d.\"deletedAt\" IS NULL\n ",
- "describe": {
- "columns": [
- {
- "ordinal": 0,
- "name": "document_id",
- "type_info": "Text"
- }
- ],
- "parameters": {
- "Left": [
- "Uuid"
- ]
- },
- "nullable": [
- false
- ]
- },
- "hash": "b3bb26f4b163d8bea13a5a3fb0ea3d89543ceaefae74f8962e4504196d0b76c2"
-}
diff --git a/crates/documents/src/domain/service.rs b/crates/documents/src/domain/service.rs
index 97ae10fffa..2c6ae901aa 100644
--- a/crates/documents/src/domain/service.rs
+++ b/crates/documents/src/domain/service.rs
@@ -172,6 +172,24 @@ fn pending_content_for_file_type(file_type: Option<FileType>) -> DocumentContent
}
}
+/// Whether an import holding an attachment's bytes should upload them to the
+/// pending document it reused.
+///
+/// An import links the document before the bytes reach storage, so a failed
+/// upload leaves it pending with nothing else to retry it. DOCX stays pending
+/// through its conversion, and spreadsheets are not stored as objects. The
+/// checksum keeps different bytes from replacing the document's object.
+fn reused_email_document_needs_upload(
+ content: &DocumentContent,
+ file_type: Option<FileType>,
+ document_sha: Option<&str>,
+ sha: &str,
+) -> bool {
+ content.state == DocumentContentState::Pending
+ && !matches!(file_type, Some(FileType::Docx | FileType::Spreadsheet))
+ && document_sha == Some(sha)
+}
+
fn should_revoke_non_owner_user_access(
share_permission: Option<&UpdateSharePermissionRequestV2>,
) -> bool {
@@ -694,16 +712,47 @@ impl<
&self,
document_metadata: DocumentMetadata,
file_type: Option<FileType>,
+ sha: &str,
) -> 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.
+ // The reused document keeps its own type, whatever this attachment declared.
+ let file_type = document_metadata
+ .file_type
+ .as_deref()
+ .and_then(|file_type| FileType::from_str(file_type).ok())
+ .or(file_type);
+ // Reuse returns whatever content already exists, and an upload URL
+ // only when a previous import never finished storing the bytes.
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 mime_type = content_type.mime_type().to_string();
+ let presigned_url = if reused_email_document_needs_upload(
+ &content,
+ file_type,
+ document_metadata.sha.as_deref(),
+ sha,
+ ) {
+ tracing::warn!(%document_id, "reissuing the upload of a pending email document");
+ let key = build_cloud_storage_bucket_document_key(
+ &document_metadata.owner,
+ &document_id,
+ document_metadata.document_version_id,
+ );
+ let url = self
+ .upload_url_service
+ .put_document_storage_presigned_url(&key, sha, content_type)
+ .await
+ .map_err(|e| {
+ tracing::error!(error=?e, %document_id, "unable to generate presigned url");
+ DocumentError::Internal(anyhow!("unable to generate presigned url"))
+ })?;
+ Some(url)
+ } else {
+ None
+ };
let document_response_metadata =
DocumentResponseMetadata::from_document_metadata(&document_metadata).map_err(
|e| {
@@ -719,9 +768,9 @@ impl<
content,
)
.with_team_task_metadata(team_task_metadata),
- presigned_url: None,
+ presigned_url,
},
- content_type: content_type.mime_type().to_string(),
+ content_type: mime_type,
file_type: file_type.map(|f| f.to_string()),
})
}
@@ -1653,7 +1702,7 @@ impl<
.await
}
EmailImportRepoOutcome::Reused(document_metadata) => {
- self.reused_email_import_response(document_metadata, file_type)
+ self.reused_email_import_response(document_metadata, file_type, &sha)
.await
}
}
diff --git a/crates/documents/src/domain/service/tests.rs b/crates/documents/src/domain/service/tests.rs
index 7b6727758b..c6021cde94 100644
--- a/crates/documents/src/domain/service/tests.rs
+++ b/crates/documents/src/domain/service/tests.rs
@@ -2194,6 +2194,7 @@ async fn copy_document_best_effort_bumps_inherited_project_and_publishes_event()
struct RecordingUploadUrlPort {
copies: Arc<Mutex<Vec<(String, String)>>>,
deletions: Arc<Mutex<Vec<String>>>,
+ uploads: Arc<Mutex<Vec<(String, String)>>>,
fail_delete: bool,
}
@@ -2207,10 +2208,14 @@ impl PresignedUploadUrlPort for RecordingUploadUrlPort {
}
async fn put_document_storage_presigned_url(
&self,
- _key: &str,
- _sha: &str,
+ key: &str,
+ sha: &str,
_content_type: ContentType,
) -> anyhow::Result<String> {
+ self.uploads
+ .lock()
+ .unwrap()
+ .push((key.to_string(), sha.to_string()));
Ok(String::new())
}
@@ -2946,6 +2951,100 @@ async fn create_document_reuse_skips_content_url_and_created_event() {
assert!(event_broker.published().lock().unwrap().is_empty());
}
+/// Imports an attachment that reuses `metadata`, whose content is `content`,
+/// and returns the response with the uploads it signed.
+async fn reimport_email_document(
+ metadata: DocumentMetadata,
+ content: DocumentContent,
+ file_type: FileType,
+) -> (CreateDocumentResponseData, Vec<(String, String)>) {
+ let mut repo = make_mock_repo();
+ repo.expect_get_owner_team()
+ .returning(|_| Box::pin(std::future::ready(Ok(None))));
+ repo.expect_import_email_attachment_document()
+ .returning(move |_, _| {
+ Box::pin(std::future::ready(Ok(EmailImportRepoOutcome::Reused(
+ metadata.clone(),
+ ))))
+ });
+ repo.expect_set_document_content().times(0);
+ repo.expect_get_persisted_document_content()
+ .return_once(|_| Box::pin(std::future::ready(Ok(Some(content)))));
+ repo.expect_get_team_task_metadata()
+ .returning(|_| Box::pin(std::future::ready(Ok(None))));
+ let uploads = RecordingUploadUrlPort::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(),
+ ),
+ uploads.clone(),
+ TestTaskPropertiesPort,
+ TestConnectionService::default(),
+ TestEntityAccessManagementService::default(),
+ TestForeignEntityService::default(),
+ TestEventBroker::default(),
+ );
+ let args = ImportEmailAttachmentRepoArgs {
+ email_attachment_id: uuid::Uuid::from_u128(9),
+ owner: test_user(),
+ document: new_document(file_type),
+ };
+ let response = crate::domain::ports::DocumentService::import_email_attachment(&service, args)
+ .await
+ .unwrap();
+ let signed = uploads.uploads.lock().unwrap().clone();
+ (response, signed)
+}
+
+fn pending_email_document(file_type: &str, sha: &str) -> (DocumentMetadata, DocumentContent) {
+ let mut metadata = make_test_metadata();
+ metadata.file_type = Some(file_type.to_string());
+ metadata.sha = Some(sha.to_string());
+ (
+ metadata,
+ DocumentContent::pending_at(DocumentContentLocation::ObjectStorage),
+ )
+}
+
+#[tokio::test]
+async fn reusing_a_pending_email_document_signs_its_upload_again() {
+ let (metadata, content) = pending_email_document("pdf", "sha");
+ let key = build_cloud_storage_bucket_document_key(&metadata.owner, "doc-1", 1);
+ // The attachment's own declared type must not change the signed content type.
+ let (response, signed) = reimport_email_document(metadata, content, FileType::Txt).await;
+
+ assert!(response.document_response.presigned_url.is_some());
+ assert_eq!(response.content_type, "application/pdf");
+ assert_eq!(response.file_type.as_deref(), Some("pdf"));
+ assert_eq!(signed, [(key, "sha".to_string())]);
+}
+
+#[tokio::test]
+async fn different_bytes_cannot_replace_a_pending_email_document() {
+ let (metadata, content) = pending_email_document("pdf", "other-sha");
+ let (response, signed) = reimport_email_document(metadata, content, FileType::Pdf).await;
+
+ assert!(response.document_response.presigned_url.is_none());
+ assert!(signed.is_empty());
+}
+
+#[tokio::test]
+async fn reusing_a_converting_docx_does_not_sign_an_upload() {
+ let (metadata, _) = pending_email_document("docx", "sha");
+ let (response, signed) = reimport_email_document(
+ metadata,
+ DocumentContent::pending_at(DocumentContentLocation::ConvertedPdf),
+ FileType::Docx,
+ )
+ .await;
+
+ assert!(response.document_response.presigned_url.is_none());
+ assert!(signed.is_empty());
+}
+
#[tokio::test]
async fn edited_interaction_bumps_document_before_publishing() {
let mut repo = make_mock_repo();
diff --git a/crates/documents/src/outbound/pg_document_repo/tests.rs b/crates/documents/src/outbound/pg_document_repo/tests.rs
index fbdfa60655..c8e0fbd58a 100644
--- a/crates/documents/src/outbound/pg_document_repo/tests.rs
+++ b/crates/documents/src/outbound/pg_document_repo/tests.rs
@@ -2816,6 +2816,12 @@ async fn test_import_email_attachment_same_attachment_id_reuses_document(pool: P
assert!(matches!(first, EmailImportRepoOutcome::Created(_)));
assert!(matches!(second, EmailImportRepoOutcome::Reused(_)));
+ // A pending document's upload is signed again from this checksum and version.
+ assert_eq!(second.metadata().sha.as_deref(), Some(sha));
+ assert_eq!(
+ second.metadata().document_version_id,
+ first.metadata().document_version_id
+ );
assert_eq!(first.metadata().document_id, second.metadata().document_id);
assert_eq!(
document_email_rows(&pool, &first.metadata().document_id).await,
diff --git a/crates/email/AGENTS.md b/crates/email/AGENTS.md
index 4d111fd84a..4240d37aea 100644
--- a/crates/email/AGENTS.md
+++ b/crates/email/AGENTS.md
@@ -102,6 +102,11 @@ tools — hexagonal, heavily feature-gated.
- `.eml` downloads go through `domain/message_source.rs`, which authorizes like
attachment reads and fetches the provider's original RFC 822 bytes (Gmail
`format=raw`, Graph `$value`); drafts and unsynced messages have no source.
+- An attachment links to its document before the bytes reach storage, and a link
+ stops sync from offering it again. `AttachmentReadService::document` stores the
+ bytes again when a linked document is still awaiting its upload, and DSS signs a
+ new upload for that reuse; DOCX is left out because it stays pending while it
+ converts.
- Forwarded attachments register through `domain/draft_attachments.rs` and resolve
bytes through `domain/attachment_access.rs`; scheduled delivery also rechecks
source inbox access in `domain/forwarded_attachments.rs`, including legacy jobs.
diff --git a/crates/email/src/domain/attachment_access.rs b/crates/email/src/domain/attachment_access.rs
index b1cc42f853..7d6abae385 100644
--- a/crates/email/src/domain/attachment_access.rs
+++ b/crates/email/src/domain/attachment_access.rs
@@ -35,6 +35,14 @@ pub struct StoredAttachmentBlob {
pub size: i64,
}
+/// The Macro document already linked to an attachment.
+#[derive(Debug, Clone)]
+pub struct ExistingAttachmentDocument {
+ pub id: String,
+ /// The document's bytes have not finished reaching storage.
+ pub awaiting_upload: bool,
+}
+
pub trait AttachmentReadRepository: EmailUserRepo {
fn attachment_read_record(
&self,
@@ -43,7 +51,7 @@ pub trait AttachmentReadRepository: EmailUserRepo {
fn existing_attachment_document(
&self,
id: Uuid,
- ) -> impl Future<Output = Result<Option<String>, AttachmentError>> + Send;
+ ) -> impl Future<Output = Result<Option<ExistingAttachmentDocument>, AttachmentError>> + Send;
}
pub trait AttachmentBytes: Send + Sync + 'static {
fn bytes(
@@ -136,12 +144,35 @@ impl<R: AttachmentReadRepository, P: AttachmentBytes, S: AttachmentFiles, A: Ent
}
// Authorization precedes even the cache hit: a document ID is not proof
// that this caller can read the attachment's source thread.
- if let Some(id) = self.repository.existing_attachment_document(id).await? {
- return Ok(id);
+ if let Some(document) = self.repository.existing_attachment_document(id).await? {
+ if document.awaiting_upload {
+ self.store_unfinished_document(&record, &document.id).await;
+ }
+ return Ok(document.id);
}
let bytes = self.provider.bytes(&record).await?;
self.files.store_document(&record, bytes).await
}
+
+ /// Stores the bytes again for a document whose upload never finished. Its
+ /// link to the attachment is written first, so sync and later imports treat
+ /// the attachment as done and nothing else retries it. A failure still
+ /// opens the document: the first upload may simply be finishing.
+ #[tracing::instrument(
+ name = "email.attachment.store_unfinished_document",
+ skip_all,
+ fields(attachment_id = %record.attachment.db_id, document_id = %document_id)
+ )]
+ async fn store_unfinished_document(&self, record: &AttachmentReadRecord, document_id: &str) {
+ let stored = async {
+ let bytes = self.provider.bytes(record).await?;
+ self.files.store_document(record, bytes).await
+ }
+ .await;
+ if let Err(error) = stored {
+ tracing::warn!(?error, "could not store an unfinished attachment document");
+ }
+ }
}
#[cfg(test)]
diff --git a/crates/email/src/domain/attachment_access/test.rs b/crates/email/src/domain/attachment_access/test.rs
index 5222ab8169..5821da4de0 100644
--- a/crates/email/src/domain/attachment_access/test.rs
+++ b/crates/email/src/domain/attachment_access/test.rs
@@ -13,6 +13,9 @@ struct Probe {
document_reads: Arc<AtomicUsize>,
provider_reads: Arc<AtomicUsize>,
file_reads: Arc<AtomicUsize>,
+ document_stores: Arc<AtomicUsize>,
+ awaiting_upload: bool,
+ provider_unavailable: bool,
}
impl EmailUserRepo for Probe {
async fn user_sender_filters(&self, _: Uuid) -> Result<Vec<EmailFilter>, EmailErr> {
@@ -59,14 +62,20 @@ impl AttachmentReadRepository for Probe {
async fn existing_attachment_document(
&self,
_: Uuid,
- ) -> Result<Option<String>, AttachmentError> {
+ ) -> Result<Option<ExistingAttachmentDocument>, AttachmentError> {
self.document_reads.fetch_add(1, Ordering::Relaxed);
- Ok(Some("existing-document".into()))
+ Ok(Some(ExistingAttachmentDocument {
+ id: "existing-document".into(),
+ awaiting_upload: self.awaiting_upload,
+ }))
}
}
impl AttachmentBytes for Probe {
async fn bytes(&self, _: &AttachmentReadRecord) -> Result<Vec<u8>, AttachmentError> {
self.provider_reads.fetch_add(1, Ordering::Relaxed);
+ if self.provider_unavailable {
+ return Err(AttachmentError::Infrastructure);
+ }
Ok(vec![1, 2, 3])
}
}
@@ -90,7 +99,8 @@ impl AttachmentFiles for Probe {
_: &AttachmentReadRecord,
_: Vec<u8>,
) -> Result<String, AttachmentError> {
- panic!("existing document")
+ self.document_stores.fetch_add(1, Ordering::Relaxed);
+ Ok("existing-document".into())
}
}
fn probe(accessible: bool) -> Probe {
@@ -139,6 +149,9 @@ fn probe(accessible: bool) -> Probe {
document_reads: Arc::new(AtomicUsize::new(0)),
provider_reads: Arc::new(AtomicUsize::new(0)),
file_reads: Arc::new(AtomicUsize::new(0)),
+ document_stores: Arc::new(AtomicUsize::new(0)),
+ awaiting_upload: false,
+ provider_unavailable: false,
}
}
@@ -191,6 +204,69 @@ async fn authorized_delegate_uses_cache_without_provider_credentials() {
assert_eq!(probe.document_reads.load(Ordering::Relaxed), 1);
assert_eq!(probe.file_reads.load(Ordering::Relaxed), 1);
assert_eq!(probe.provider_reads.load(Ordering::Relaxed), 0);
+ assert_eq!(probe.document_stores.load(Ordering::Relaxed), 0);
+}
+
+#[tokio::test]
+async fn opening_a_document_whose_upload_never_finished_stores_it_again() {
+ let mut probe = probe(true);
+ probe.awaiting_upload = true;
+ let service = AttachmentReadService {
+ repository: probe.clone(),
+ provider: probe.clone(),
+ files: probe.clone(),
+ access: NoOpEntityAccessService,
+ };
+ let actor = MacroUserIdStr::try_from_email("[email protected]").unwrap();
+ let id = probe.record.attachment.db_id;
+ assert_eq!(
+ service.document(&actor, id).await.unwrap(),
+ "existing-document"
+ );
+ assert_eq!(probe.provider_reads.load(Ordering::Relaxed), 1);
+ assert_eq!(probe.document_stores.load(Ordering::Relaxed), 1);
+}
+
+#[tokio::test]
+async fn a_failed_repair_still_opens_the_existing_document() {
+ let mut probe = probe(true);
+ probe.awaiting_upload = true;
+ probe.provider_unavailable = true;
+ let service = AttachmentReadService {
+ repository: probe.clone(),
+ provider: probe.clone(),
+ files: probe.clone(),
+ access: NoOpEntityAccessService,
+ };
+ let actor = MacroUserIdStr::try_from_email("[email protected]").unwrap();
+ let id = probe.record.attachment.db_id;
+ assert_eq!(
+ service.document(&actor, id).await.unwrap(),
+ "existing-document"
+ );
+ assert_eq!(probe.provider_reads.load(Ordering::Relaxed), 1);
+ assert_eq!(probe.document_stores.load(Ordering::Relaxed), 0);
+}
+
+#[tokio::test]
+async fn unauthorized_reader_cannot_trigger_a_repair() {
+ let mut probe = probe(false);
+ probe.awaiting_upload = true;
+ let service = AttachmentReadService {
+ repository: probe.clone(),
+ provider: probe.clone(),
+ files: probe.clone(),
+ access: NoOpEntityAccessService,
+ };
+ let actor = MacroUserIdStr::try_from_email("[email protected]").unwrap();
+ assert!(matches!(
+ service
+ .document(&actor, probe.record.attachment.db_id)
+ .await,
+ Err(AttachmentError::Forbidden)
+ ));
+ assert_eq!(probe.provider_reads.load(Ordering::Relaxed), 0);
+ assert_eq!(probe.document_stores.load(Ordering::Relaxed), 0);
}
#[tokio::test]
diff --git a/crates/email/src/outbound/email_pg_repo/attachment_access.rs b/crates/email/src/outbound/email_pg_repo/attachment_access.rs
index 02c755c61c..1119531249 100644
--- a/crates/email/src/outbound/email_pg_repo/attachment_access.rs
+++ b/crates/email/src/outbound/email_pg_repo/attachment_access.rs
@@ -45,10 +45,15 @@ impl AttachmentReadRepository for EmailPgRepo {
async fn existing_attachment_document(
&self,
id: Uuid,
- ) -> Result<Option<String>, AttachmentError> {
- email_db_client::attachments::provider::get_document_id_by_att_id(&self.pool, id)
- .await
- .map_err(failed)
+ ) -> Result<Option<ExistingAttachmentDocument>, AttachmentError> {
+ let document =
+ email_db_client::attachments::provider::get_document_by_att_id(&self.pool, id)
+ .await
+ .map_err(failed)?;
+ Ok(document.map(|document| ExistingAttachmentDocument {
+ id: document.document_id,
+ awaiting_upload: document.awaiting_upload,
+ }))
}
}
diff --git a/crates/email_db_client/fixtures/get_document_by_att_id.sql b/crates/email_db_client/fixtures/get_document_by_att_id.sql
new file mode 100644
index 0000000000..a9fa3bff98
--- /dev/null
+++ b/crates/email_db_client/fixtures/get_document_by_att_id.sql
@@ -0,0 +1,59 @@
+-- SQL fixture for get_document_by_att_id tests
+-- One attachment per document state the attachment opener distinguishes.
+
+INSERT INTO email_links (id, macro_id, fusionauth_user_id, email_address, provider, is_sync_active, created_at, updated_at)
+VALUES ('00000000-0000-0000-0000-00000000b001', 'macro|[email protected]', '00000000-0000-0000-0000-00000000b001',
+ '[email protected]', 'GMAIL', true, NOW(), NOW());
+
+INSERT INTO email_contacts (id, link_id, email_address, created_at, updated_at)
+VALUES ('00000000-0000-0000-0000-00000000b002',
+ '00000000-0000-0000-0000-00000000b001',
+ NOW(), NOW());
+
+INSERT INTO email_threads (id, link_id, inbox_visible, is_read, created_at, updated_at)
+VALUES ('00000000-0000-0000-0000-00000000b003',
+ '00000000-0000-0000-0000-00000000b001',
+ true, false, NOW(), NOW());
+
+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, created_at, updated_at)
+VALUES ('00000000-0000-0000-0000-00000000b004',
+ '00000000-0000-0000-0000-00000000b003',
+ '00000000-0000-0000-0000-00000000b001',
+ 'provider-msg-b004',
+ FALSE,
+ '00000000-0000-0000-0000-00000000b002',
+ '2025-01-05 10:00:00 +00:00',
+ true, false, false, false, NOW(), NOW());
+
+INSERT INTO email_attachments (id, message_id, provider_attachment_id, filename, mime_type, created_at)
+VALUES ('00000000-0000-0000-0000-00000000a001', '00000000-0000-0000-0000-00000000b004', 'att-pending', 'pending.pdf', 'application/pdf', NOW()),
+ ('00000000-0000-0000-0000-00000000a002', '00000000-0000-0000-0000-00000000b004', 'att-uploaded', 'uploaded.pdf', 'application/pdf', NOW()),
+ ('00000000-0000-0000-0000-00000000a003', '00000000-0000-0000-0000-00000000b004', 'att-docx', 'converting.docx', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', NOW()),
+ ('00000000-0000-0000-0000-00000000a004', '00000000-0000-0000-0000-00000000b004', 'att-deleted', 'deleted.pdf', 'application/pdf', NOW()),
+ ('00000000-0000-0000-0000-00000000a005', '00000000-0000-0000-0000-00000000b004', 'att-unlinked', 'unlinked.pdf', 'application/pdf', NOW());
+
+INSERT INTO "macro_user" (id, username, email, stripe_customer_id)
+VALUES ('00000000-0000-0000-0000-00000000b001',
+ 'user',
+ 'cus_test123');
+
+INSERT INTO "User" (id, email, name, macro_user_id)
+VALUES ('00000000-0000-0000-0000-00000000b001',
+ 'User',
+ '00000000-0000-0000-0000-00000000b001');
+
+INSERT INTO "Document" (id, name, owner, "fileType", uploaded, "deletedAt", "createdAt", "updatedAt")
+VALUES ('doc-pending', 'pending', '00000000-0000-0000-0000-00000000b001', 'pdf', false, NULL, NOW(), NOW()),
+ ('doc-uploaded', 'uploaded', '00000000-0000-0000-0000-00000000b001', 'pdf', true, NULL, NOW(), NOW()),
+ ('doc-docx', 'converting', '00000000-0000-0000-0000-00000000b001', 'docx', false, NULL, NOW(), NOW()),
+ ('doc-deleted', 'deleted', '00000000-0000-0000-0000-00000000b001', 'pdf', false, NOW(), NOW(), NOW());
+
+INSERT INTO document_email (document_id, email_attachment_id)
+VALUES ('doc-pending', '00000000-0000-0000-0000-00000000a001'),
+ ('doc-uploaded', '00000000-0000-0000-0000-00000000a002'),
+ ('doc-docx', '00000000-0000-0000-0000-00000000a003'),
+ ('doc-deleted', '00000000-0000-0000-0000-00000000a004');
diff --git a/crates/email_db_client/src/attachments/provider.rs b/crates/email_db_client/src/attachments/provider.rs
index fe696c244f..016c4455d2 100644
--- a/crates/email_db_client/src/attachments/provider.rs
+++ b/crates/email_db_client/src/attachments/provider.rs
@@ -393,15 +393,27 @@ pub async fn get_document_id_by_att_id_and_link(
Ok(result.map(|record| record.document_id))
}
-/// Get document_id for email attachment if record exists
+/// The live document linked to an email attachment.
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub struct LinkedAttachmentDocument {
+ /// The document id.
+ pub document_id: String,
+ /// The document's bytes have not finished reaching storage. DOCX is left
+ /// out: it stays not uploaded until its conversion finishes.
+ pub awaiting_upload: bool,
+}
+
+/// Get the document for email attachment if record exists
#[tracing::instrument(skip(db), err)]
-pub async fn get_document_id_by_att_id(
+pub async fn get_document_by_att_id(
db: &Pool<Postgres>,
email_attachment_id: Uuid,
-) -> anyhow::Result<Option<String>> {
+) -> anyhow::Result<Option<LinkedAttachmentDocument>> {
let result = sqlx::query!(
r#"
- SELECT document_id
+ SELECT
+ document_id,
+ (NOT d.uploaded AND d."fileType" IS DISTINCT FROM 'docx') AS "awaiting_upload!"
FROM document_email de
INNER JOIN "Document" d on de.document_id = d.id
INNER JOIN email_attachments ea on de.email_attachment_id = ea.id
@@ -413,7 +425,10 @@ pub async fn get_document_id_by_att_id(
.fetch_optional(db)
.await?;
- Ok(result.map(|record| record.document_id))
+ Ok(result.map(|record| LinkedAttachmentDocument {
+ document_id: record.document_id,
+ awaiting_upload: record.awaiting_upload,
+ }))
}
/// Get thread_id and link_id for an email attachment
diff --git a/crates/email_db_client/src/attachments/provider/test.rs b/crates/email_db_client/src/attachments/provider/test.rs
index 17dfdcf7c1..4f48c194d0 100644
--- a/crates/email_db_client/src/attachments/provider/test.rs
+++ b/crates/email_db_client/src/attachments/provider/test.rs
@@ -1,4 +1,6 @@
-use crate::attachments::provider::fetch_db_attachments_in_bulk;
+use crate::attachments::provider::{
+ LinkedAttachmentDocument, fetch_db_attachments_in_bulk, get_document_by_att_id,
+};
use anyhow::Result;
use macro_db_migrator::MACRO_DB_MIGRATIONS;
use sqlx::types::Uuid;
@@ -260,3 +262,45 @@ async fn insert_attachments_sets_then_clears_calendar_flag(pool: Pool<Postgres>)
Ok(())
}
+
+#[sqlx::test(
+ migrator = "MACRO_DB_MIGRATIONS",
+ fixtures(path = "../../../fixtures", scripts("get_document_by_att_id"))
+)]
+async fn get_document_by_att_id_reports_documents_awaiting_their_upload(
+ pool: Pool<Postgres>,
+) -> Result<()> {
+ let attachment =
+ |last: &str| Uuid::parse_str(&format!("00000000-0000-0000-0000-00000000{last}"));
+ let linked = |document_id: &str, awaiting_upload| {
+ Some(LinkedAttachmentDocument {
+ document_id: document_id.to_string(),
+ awaiting_upload,
+ })
+ };
+
+ // A PDF that never finished uploading is the only state to repair.
+ assert_eq!(
+ get_document_by_att_id(&pool, attachment("a001")?).await?,
+ linked("doc-pending", true)
+ );
+ assert_eq!(
+ get_document_by_att_id(&pool, attachment("a002")?).await?,
+ linked("doc-uploaded", false)
+ );
+ // A DOCX is not uploaded until its conversion finishes.
+ assert_eq!(
+ get_document_by_att_id(&pool, attachment("a003")?).await?,
+ linked("doc-docx", false)
+ );
+ assert_eq!(
+ get_document_by_att_id(&pool, attachment("a004")?).await?,
+ None
+ );
+ assert_eq!(
+ get_document_by_att_id(&pool, attachment("a005")?).await?,
+ None
+ );
+
+ Ok(())
+}