Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
TypeScript agentaiai-agentsall-in-onecrmcrm-systememaillinearmcpmessagingnotesnotionofficerustslackslack-alternativestartupstartup-toolstypescriptworkspace
fix: search surfacing deleted documents (#200)
gbirman5345fea4c48d
diff --git a/rust/cloud-storage/macro_db_client/.sqlx/query-740225c77125c24dfb4a9c5613798e085105ae948b9d17da636c72a084d89bd3.json b/rust/cloud-storage/macro_db_client/.sqlx/query-1d6e4800a6f297b505d4fa0cc84ac66002ff276389ca3ba736a417cca0b04d35.jsonsimilarity index 59%rename from rust/cloud-storage/macro_db_client/.sqlx/query-740225c77125c24dfb4a9c5613798e085105ae948b9d17da636c72a084d89bd3.jsonrename to rust/cloud-storage/macro_db_client/.sqlx/query-1d6e4800a6f297b505d4fa0cc84ac66002ff276389ca3ba736a417cca0b04d35.jsonindex adbdd3b0b8..9799f27621 100644--- a/rust/cloud-storage/macro_db_client/.sqlx/query-740225c77125c24dfb4a9c5613798e085105ae948b9d17da636c72a084d89bd3.json+++ b/rust/cloud-storage/macro_db_client/.sqlx/query-1d6e4800a6f297b505d4fa0cc84ac66002ff276389ca3ba736a417cca0b04d35.json@@ -1,6 +1,6 @@{"db_name": "PostgreSQL",- "query": "\n SELECT\n c.\"id\" as \"item_id!\",\n c.\"createdAt\" as \"created_at!\",\n c.\"updatedAt\" as \"updated_at!\",\n uh.\"updatedAt\" as \"viewed_at?\",\n c.\"projectId\" as \"project_id?\"\n FROM\n \"Document\" c\n LEFT JOIN\n \"UserHistory\" uh ON uh.\"itemId\" = c.\"id\"\n AND uh.\"userId\" = $1\n AND uh.\"itemType\" = 'document'\n WHERE\n c.\"id\" = ANY($2)\n AND c.\"deletedAt\" IS NULL\n ORDER BY\n c.\"updatedAt\" DESC\n ",+ "query": "\n SELECT\n c.\"id\" as \"item_id!\",\n c.\"createdAt\" as \"created_at!\",\n c.\"updatedAt\" as \"updated_at!\",\n c.\"deletedAt\" as \"deleted_at?\",\n uh.\"updatedAt\" as \"viewed_at?\",\n c.\"projectId\" as \"project_id?\"\n FROM\n \"Chat\" c\n LEFT JOIN\n \"UserHistory\" uh ON uh.\"itemId\" = c.\"id\"\n AND uh.\"userId\" = $1\n AND uh.\"itemType\" = 'chat'\n WHERE\n c.\"id\" = ANY($2)\n ORDER BY\n c.\"updatedAt\" DESC\n ","describe": {"columns": [{@@ -20,11 +20,16 @@},{"ordinal": 3,- "name": "viewed_at?",+ "name": "deleted_at?","type_info": "Timestamp"},{"ordinal": 4,+ "name": "viewed_at?",+ "type_info": "Timestamp"+ },+ {+ "ordinal": 5,"name": "project_id?","type_info": "Text"}@@ -39,9 +44,10 @@false,false,false,+ true,false,true]},- "hash": "740225c77125c24dfb4a9c5613798e085105ae948b9d17da636c72a084d89bd3"+ "hash": "1d6e4800a6f297b505d4fa0cc84ac66002ff276389ca3ba736a417cca0b04d35"}diff --git a/rust/cloud-storage/macro_db_client/.sqlx/query-44a3fab0bfebeee803bf537083c3ca4ec1638d130f85f6ab08696d876a33032b.json b/rust/cloud-storage/macro_db_client/.sqlx/query-7e8af5b9c65f85d340332199ee9f3ec43f5f0132b187cbff40a8086a0c548995.jsonsimilarity index 55%rename from rust/cloud-storage/macro_db_client/.sqlx/query-44a3fab0bfebeee803bf537083c3ca4ec1638d130f85f6ab08696d876a33032b.jsonrename to rust/cloud-storage/macro_db_client/.sqlx/query-7e8af5b9c65f85d340332199ee9f3ec43f5f0132b187cbff40a8086a0c548995.jsonindex e03a44b46d..4ef502a5aa 100644--- a/rust/cloud-storage/macro_db_client/.sqlx/query-44a3fab0bfebeee803bf537083c3ca4ec1638d130f85f6ab08696d876a33032b.json+++ b/rust/cloud-storage/macro_db_client/.sqlx/query-7e8af5b9c65f85d340332199ee9f3ec43f5f0132b187cbff40a8086a0c548995.json@@ -1,6 +1,6 @@{"db_name": "PostgreSQL",- "query": "\n SELECT\n c.\"id\" as \"item_id!\",\n c.\"createdAt\" as \"created_at!\",\n c.\"updatedAt\" as \"updated_at!\",\n uh.\"updatedAt\" as \"viewed_at?\",\n c.\"projectId\" as \"project_id?\"\n FROM\n \"Chat\" c\n LEFT JOIN\n \"UserHistory\" uh ON uh.\"itemId\" = c.\"id\"\n AND uh.\"userId\" = $1\n AND uh.\"itemType\" = 'chat'\n WHERE\n c.\"id\" = ANY($2)\n AND c.\"deletedAt\" IS NULL\n ORDER BY\n c.\"updatedAt\" DESC\n ",+ "query": "\n SELECT\n c.\"id\" as \"item_id!\",\n c.\"createdAt\" as \"created_at!\",\n c.\"updatedAt\" as \"updated_at!\",\n c.\"deletedAt\" as \"deleted_at?\",\n uh.\"updatedAt\" as \"viewed_at?\",\n c.\"projectId\" as \"project_id?\"\n FROM\n \"Document\" c\n LEFT JOIN\n \"UserHistory\" uh ON uh.\"itemId\" = c.\"id\"\n AND uh.\"userId\" = $1\n AND uh.\"itemType\" = 'document'\n WHERE\n c.\"id\" = ANY($2)\n ORDER BY\n c.\"updatedAt\" DESC\n ","describe": {"columns": [{@@ -20,11 +20,16 @@},{"ordinal": 3,- "name": "viewed_at?",+ "name": "deleted_at?","type_info": "Timestamp"},{"ordinal": 4,+ "name": "viewed_at?",+ "type_info": "Timestamp"+ },+ {+ "ordinal": 5,"name": "project_id?","type_info": "Text"}@@ -39,9 +44,10 @@false,false,false,+ true,false,true]},- "hash": "44a3fab0bfebeee803bf537083c3ca4ec1638d130f85f6ab08696d876a33032b"+ "hash": "7e8af5b9c65f85d340332199ee9f3ec43f5f0132b187cbff40a8086a0c548995"}diff --git a/rust/cloud-storage/macro_db_client/.sqlx/query-f677e666c02b88f826f21dfe0a635d4298e37069313df637ec90c63d0b61efc7.json b/rust/cloud-storage/macro_db_client/.sqlx/query-9b4a72a38a9c07c192f92d6cf883dfb65e04ec3553e7b85a0d6f356f4ef55aa7.jsonsimilarity index 55%rename from rust/cloud-storage/macro_db_client/.sqlx/query-f677e666c02b88f826f21dfe0a635d4298e37069313df637ec90c63d0b61efc7.jsonrename to rust/cloud-storage/macro_db_client/.sqlx/query-9b4a72a38a9c07c192f92d6cf883dfb65e04ec3553e7b85a0d6f356f4ef55aa7.jsonindex 1384d530b9..9461a43d4a 100644--- a/rust/cloud-storage/macro_db_client/.sqlx/query-f677e666c02b88f826f21dfe0a635d4298e37069313df637ec90c63d0b61efc7.json+++ b/rust/cloud-storage/macro_db_client/.sqlx/query-9b4a72a38a9c07c192f92d6cf883dfb65e04ec3553e7b85a0d6f356f4ef55aa7.json@@ -1,6 +1,6 @@{"db_name": "PostgreSQL",- "query": "\n SELECT\n p.\"id\" as \"item_id!\",\n p.\"createdAt\" as \"created_at!\",\n p.\"updatedAt\" as \"updated_at!\",\n p.\"parentId\" as \"parent_project_id?\",\n uh.\"updatedAt\" as \"viewed_at?\"\n FROM\n \"Project\" p\n LEFT JOIN\n \"UserHistory\" uh ON uh.\"itemId\" = p.\"id\"\n AND uh.\"userId\" = $1\n AND uh.\"itemType\" = 'project'\n WHERE\n p.\"id\" = ANY($2)\n AND p.\"deletedAt\" IS NULL\n ORDER BY\n p.\"updatedAt\" DESC\n ",+ "query": "\n SELECT\n p.\"id\" as \"item_id!\",\n p.\"createdAt\" as \"created_at!\",\n p.\"updatedAt\" as \"updated_at!\",\n p.\"deletedAt\" as \"deleted_at?\",\n p.\"parentId\" as \"parent_project_id?\",\n uh.\"updatedAt\" as \"viewed_at?\"\n FROM\n \"Project\" p\n LEFT JOIN\n \"UserHistory\" uh ON uh.\"itemId\" = p.\"id\"\n AND uh.\"userId\" = $1\n AND uh.\"itemType\" = 'project'\n WHERE\n p.\"id\" = ANY($2)\n ORDER BY\n p.\"updatedAt\" DESC\n ","describe": {"columns": [{@@ -20,11 +20,16 @@},{"ordinal": 3,+ "name": "deleted_at?",+ "type_info": "Timestamp"+ },+ {+ "ordinal": 4,"name": "parent_project_id?","type_info": "Text"},{- "ordinal": 4,+ "ordinal": 5,"name": "viewed_at?","type_info": "Timestamp"}@@ -40,8 +45,9 @@false,false,true,+ true,false]},- "hash": "f677e666c02b88f826f21dfe0a635d4298e37069313df637ec90c63d0b61efc7"+ "hash": "9b4a72a38a9c07c192f92d6cf883dfb65e04ec3553e7b85a0d6f356f4ef55aa7"}diff --git a/rust/cloud-storage/macro_db_client/src/chat/get.rs b/rust/cloud-storage/macro_db_client/src/chat/get.rsindex f10cc1fb10..a311dc5e98 100644--- a/rust/cloud-storage/macro_db_client/src/chat/get.rs+++ b/rust/cloud-storage/macro_db_client/src/chat/get.rs@@ -280,9 +280,11 @@ pub struct ChatHistoryInfo {pub updated_at: DateTime<Utc>,pub viewed_at: Option<DateTime<Utc>>,pub project_id: Option<String>,+ pub deleted_at: Option<DateTime<Utc>>,}/// Gets chat history information including when a user last viewed each chat+/// Returns only entries that exist in the database#[tracing::instrument(skip(db))]pub async fn get_chat_history_info(db: &sqlx::Pool<sqlx::Postgres>,@@ -299,6 +301,7 @@ pub async fn get_chat_history_info(c."id" as "item_id!",c."createdAt" as "created_at!",c."updatedAt" as "updated_at!",+ c."deletedAt" as "deleted_at?",uh."updatedAt" as "viewed_at?",c."projectId" as "project_id?"FROM@@ -309,7 +312,6 @@ pub async fn get_chat_history_info(AND uh."itemType" = 'chat'WHEREc."id" = ANY($2)- AND c."deletedAt" IS NULLORDER BYc."updatedAt" DESC"#,@@ -319,7 +321,7 @@ pub async fn get_chat_history_info(.fetch_all(db).await?;- let chat_history_map = results+ let chat_history_map: HashMap<String, ChatHistoryInfo> = results.into_iter().map(|row| {let info = ChatHistoryInfo {@@ -330,8 +332,11 @@ pub async fn get_chat_history_info(.viewed_at.map(|dt| DateTime::<Utc>::from_naive_utc_and_offset(dt, Utc)),project_id: row.project_id,+ deleted_at: row+ .deleted_at+ .map(|dt| DateTime::<Utc>::from_naive_utc_and_offset(dt, Utc)),};- (row.item_id.clone(), info)+ (row.item_id, info)}).collect();diff --git a/rust/cloud-storage/macro_db_client/src/document/get_document_history.rs b/rust/cloud-storage/macro_db_client/src/document/get_document_history.rsindex fd03c50826..e7f19f9a1a 100644--- a/rust/cloud-storage/macro_db_client/src/document/get_document_history.rs+++ b/rust/cloud-storage/macro_db_client/src/document/get_document_history.rs@@ -9,9 +9,11 @@ pub struct DocumentHistoryInfo {pub updated_at: DateTime<Utc>,pub viewed_at: Option<DateTime<Utc>>,pub project_id: Option<String>,+ pub deleted_at: Option<DateTime<Utc>>,}/// Gets document history information including when a user last viewed each document+/// Returns only entries that exist in the database#[tracing::instrument(skip(db))]pub async fn get_document_history_info(db: &sqlx::Pool<sqlx::Postgres>,@@ -28,6 +30,7 @@ pub async fn get_document_history_info(c."id" as "item_id!",c."createdAt" as "created_at!",c."updatedAt" as "updated_at!",+ c."deletedAt" as "deleted_at?",uh."updatedAt" as "viewed_at?",c."projectId" as "project_id?"FROM@@ -38,7 +41,6 @@ pub async fn get_document_history_info(AND uh."itemType" = 'document'WHEREc."id" = ANY($2)- AND c."deletedAt" IS NULLORDER BYc."updatedAt" DESC"#,@@ -48,7 +50,7 @@ pub async fn get_document_history_info(.fetch_all(db).await?;- let document_history_map = results+ let document_history_map: HashMap<String, DocumentHistoryInfo> = results.into_iter().map(|row| {let info = DocumentHistoryInfo {@@ -59,8 +61,11 @@ pub async fn get_document_history_info(.viewed_at.map(|dt| DateTime::<Utc>::from_naive_utc_and_offset(dt, Utc)),project_id: row.project_id,+ deleted_at: row+ .deleted_at+ .map(|dt| DateTime::<Utc>::from_naive_utc_and_offset(dt, Utc)),};- (row.item_id.clone(), info)+ (row.item_id, info)}).collect();diff --git a/rust/cloud-storage/macro_db_client/src/projects/get_project_history.rs b/rust/cloud-storage/macro_db_client/src/projects/get_project_history.rsindex c6eb3a533b..6e864e51d5 100644--- a/rust/cloud-storage/macro_db_client/src/projects/get_project_history.rs+++ b/rust/cloud-storage/macro_db_client/src/projects/get_project_history.rs@@ -9,9 +9,11 @@ pub struct ProjectHistoryInfo {pub updated_at: DateTime<Utc>,pub viewed_at: Option<DateTime<Utc>>,pub parent_project_id: Option<String>,+ pub deleted_at: Option<DateTime<Utc>>,}/// Gets project history information including when a user last viewed each project+/// Returns only entries that exist in the database#[tracing::instrument(skip(db))]pub async fn get_project_history_info(db: &sqlx::Pool<sqlx::Postgres>,@@ -28,6 +30,7 @@ pub async fn get_project_history_info(p."id" as "item_id!",p."createdAt" as "created_at!",p."updatedAt" as "updated_at!",+ p."deletedAt" as "deleted_at?",p."parentId" as "parent_project_id?",uh."updatedAt" as "viewed_at?"FROM@@ -38,7 +41,6 @@ pub async fn get_project_history_info(AND uh."itemType" = 'project'WHEREp."id" = ANY($2)- AND p."deletedAt" IS NULLORDER BYp."updatedAt" DESC"#,@@ -48,7 +50,7 @@ pub async fn get_project_history_info(.fetch_all(db).await?;- let project_history_map = results+ let project_history_map: HashMap<String, ProjectHistoryInfo> = results.into_iter().map(|row| {let info = ProjectHistoryInfo {@@ -59,8 +61,11 @@ pub async fn get_project_history_info(.viewed_at.map(|dt| DateTime::<Utc>::from_naive_utc_and_offset(dt, Utc)),parent_project_id: row.parent_project_id,+ deleted_at: row+ .deleted_at+ .map(|dt| DateTime::<Utc>::from_naive_utc_and_offset(dt, Utc)),};- (row.item_id.clone(), info)+ (row.item_id, info)}).collect();diff --git a/rust/cloud-storage/models_search/src/channel.rs b/rust/cloud-storage/models_search/src/channel.rsindex 92a3815632..183e89c469 100644--- a/rust/cloud-storage/models_search/src/channel.rs+++ b/rust/cloud-storage/models_search/src/channel.rs@@ -43,15 +43,22 @@ pub struct ChannelSearchResponseItem {pub channel_message_search_results: Vec<ChannelSearchResult>,}-/// ChannelSearchResponseItem object with channel metadata we fetch from macrodb. we don't store these-/// timestamps in opensearch as they would require us to update each chat message record for the chat-/// every time the chat updates (specifically for updated_at and viewed_at and interacted_at)+/// Metadata for a channel fetched from the database#[derive(Debug, Serialize, Deserialize, ToSchema)]-pub struct ChannelSearchResponseItemWithMetadata {+pub struct ChannelMetadata {pub created_at: i64,pub updated_at: i64,pub viewed_at: Option<i64>,pub interacted_at: Option<i64>,+}++/// ChannelSearchResponseItem object with channel metadata we fetch from macrodb. we don't store these+/// timestamps in opensearch as they would require us to update each chat message record for the chat+/// every time the chat updates (specifically for updated_at and viewed_at and interacted_at)+#[derive(Debug, Serialize, Deserialize, ToSchema)]+pub struct ChannelSearchResponseItemWithMetadata {+ /// Metadata from the database. None if the channel doesn't exist in the database.+ pub metadata: Option<ChannelMetadata>,#[serde(flatten)]pub extra: ChannelSearchResponseItem,}diff --git a/rust/cloud-storage/models_search/src/chat.rs b/rust/cloud-storage/models_search/src/chat.rsindex e8fa52cd0a..29a915ce55 100644--- a/rust/cloud-storage/models_search/src/chat.rs+++ b/rust/cloud-storage/models_search/src/chat.rs@@ -39,15 +39,23 @@ pub struct ChatSearchResponseItem {pub chat_search_results: Vec<ChatMessageSearchResult>,}-/// ChatSearchResponse object with channel metadata we fetch from macrodb. we don't store these-/// timestamps in opensearch as they would require us to update each chat message record for the chat-/// every time the chat updates (specifically for updated_at and viewed_at)+/// Metadata for a chat fetched from the database#[derive(Debug, Serialize, Deserialize, ToSchema)]-pub struct ChatSearchResponseItemWithMetadata {+pub struct ChatMetadata {pub created_at: i64,pub updated_at: i64,pub viewed_at: Option<i64>,pub project_id: Option<String>,+ pub deleted_at: Option<i64>,+}++/// ChatSearchResponse object with channel metadata we fetch from macrodb. we don't store these+/// timestamps in opensearch as they would require us to update each chat message record for the chat+/// every time the chat updates (specifically for updated_at and viewed_at)+#[derive(Debug, Serialize, Deserialize, ToSchema)]+pub struct ChatSearchResponseItemWithMetadata {+ /// Metadata from the database. None if the chat doesn't exist in the database.+ pub metadata: Option<ChatMetadata>,#[serde(flatten)]pub extra: ChatSearchResponseItem,}diff --git a/rust/cloud-storage/models_search/src/document.rs b/rust/cloud-storage/models_search/src/document.rsindex 4c364cc3b0..9bbddcf8ae 100644--- a/rust/cloud-storage/models_search/src/document.rs+++ b/rust/cloud-storage/models_search/src/document.rs@@ -41,15 +41,23 @@ pub struct DocumentSearchResponseItem {pub document_search_results: Vec<DocumentSearchResult>,}-/// DocumentSearchResponseItem object with document metadata we fetch from macrodb. we don't store these-/// timestamps in opensearch as they would require us to update document page record-/// every time the document updates (specifically for updated_at and viewed_at)+/// Metadata for a document fetched from the database#[derive(Debug, Serialize, Deserialize, ToSchema)]-pub struct DocumentSearchResponseItemWithMetadata {+pub struct DocumentMetadata {pub created_at: i64,pub updated_at: i64,pub viewed_at: Option<i64>,pub project_id: Option<String>,+ pub deleted_at: Option<i64>,+}++/// DocumentSearchResponseItem object with document metadata we fetch from macrodb. we don't store these+/// timestamps in opensearch as they would require us to update document page record+/// every time the document updates (specifically for updated_at and viewed_at)+#[derive(Debug, Serialize, Deserialize, ToSchema)]+pub struct DocumentSearchResponseItemWithMetadata {+ /// Metadata from the database. None if the document doesn't exist in the database.+ pub metadata: Option<DocumentMetadata>,#[serde(flatten)]pub extra: DocumentSearchResponseItem,}diff --git a/rust/cloud-storage/models_search/src/project.rs b/rust/cloud-storage/models_search/src/project.rsindex e4446ba6f7..6828db0e21 100644--- a/rust/cloud-storage/models_search/src/project.rs+++ b/rust/cloud-storage/models_search/src/project.rs@@ -34,15 +34,23 @@ pub struct ProjectSearchResponseItem {pub project_search_results: Vec<ProjectSearchResult>,}-/// ProjectSearchResponseItem object with project metadata we fetch from macrodb. we don't store these-/// timestamps in opensearch as they would require us to update the project record-/// every time the project updates (specifically for updated_at and viewed_at)+/// Metadata for a project fetched from the database#[derive(Debug, Serialize, Deserialize, ToSchema)]-pub struct ProjectSearchResponseItemWithMetadata {+pub struct ProjectMetadata {pub created_at: i64,pub updated_at: i64,pub viewed_at: Option<i64>,pub parent_project_id: Option<String>,+ pub deleted_at: Option<i64>,+}++/// ProjectSearchResponseItem object with project metadata we fetch from macrodb. we don't store these+/// timestamps in opensearch as they would require us to update the project record+/// every time the project updates (specifically for updated_at and viewed_at)+#[derive(Debug, Serialize, Deserialize, ToSchema)]+pub struct ProjectSearchResponseItemWithMetadata {+ /// Metadata from the database. None if the project doesn't exist in the database.+ pub metadata: Option<ProjectMetadata>,#[serde(flatten)]pub extra: ProjectSearchResponseItem,}diff --git a/rust/cloud-storage/search_service/src/api/search/channel.rs b/rust/cloud-storage/search_service/src/api/search/channel.rsindex 08b7ef73e0..1dc0d0072c 100644--- a/rust/cloud-storage/search_service/src/api/search/channel.rs+++ b/rust/cloud-storage/search_service/src/api/search/channel.rs@@ -122,16 +122,20 @@ pub fn construct_search_result(let result: Vec<ChannelSearchResponseItemWithMetadata> = result.into_iter().map(|item| {- let channel_uuid = Uuid::parse_str(&item.channel_id).unwrap_or_else(|_| Uuid::nil());- let channel_history_info = channel_histories- .get(&channel_uuid)- .cloned()- .unwrap_or_default();+ let metadata = Uuid::parse_str(&item.channel_id)+ .ok()+ .and_then(|channel_uuid| channel_histories.get(&channel_uuid))+ .map(+ |channel_history_info| models_search::channel::ChannelMetadata {+ created_at: channel_history_info.created_at.timestamp(),+ updated_at: channel_history_info.updated_at.timestamp(),+ viewed_at: channel_history_info.viewed_at.map(|a| a.timestamp()),+ interacted_at: channel_history_info.interacted_at.map(|a| a.timestamp()),+ },+ );+ChannelSearchResponseItemWithMetadata {- created_at: channel_history_info.created_at.timestamp(),- updated_at: channel_history_info.updated_at.timestamp(),- viewed_at: channel_history_info.viewed_at.map(|a| a.timestamp()),- interacted_at: channel_history_info.interacted_at.map(|a| a.timestamp()),+ metadata,extra: item,}})diff --git a/rust/cloud-storage/search_service/src/api/search/channel/test.rs b/rust/cloud-storage/search_service/src/api/search/channel/test.rsindex c80a2bb953..1dd0c3c446 100644--- a/rust/cloud-storage/search_service/src/api/search/channel/test.rs+++ b/rust/cloud-storage/search_service/src/api/search/channel/test.rs@@ -11,9 +11,10 @@ fn test_construct_search_result_empty_input() {#[test]fn test_construct_search_result_single_channel() {+ let channel_uuid = "550e8400-e29b-41d4-a716-446655440000";let search_results = vec![opensearch_client::search::channels::ChannelMessageSearchResponse {- channel_id: "channel1".to_string(),+ channel_id: channel_uuid.to_string(),channel_name: Some("Test Channel".to_string()),channel_type: "public".to_string(),org_id: Some(123),@@ -30,11 +31,17 @@ fn test_construct_search_result_single_channel() {},];- let result = construct_search_result(search_results, HashMap::new()).unwrap();+ let mut channel_histories = HashMap::new();+ channel_histories.insert(+ Uuid::parse_str(channel_uuid).unwrap(),+ create_channel_history(channel_uuid),+ );++ let result = construct_search_result(search_results, channel_histories).unwrap();assert_eq!(result.len(), 1);- assert_eq!(result[0].extra.channel_id, "channel1");- assert_eq!(result[0].extra.id, "channel1");+ assert_eq!(result[0].extra.channel_id, channel_uuid);+ assert_eq!(result[0].extra.id, channel_uuid);assert_eq!(result[0].extra.channel_name,Some("Test Channel".to_string())@@ -57,9 +64,10 @@ fn test_construct_search_result_single_channel() {#[test]fn test_construct_search_result_multiple_messages_same_channel() {+ let channel_uuid = "550e8400-e29b-41d4-a716-446655440001";let search_results = vec![opensearch_client::search::channels::ChannelMessageSearchResponse {- channel_id: "channel1".to_string(),+ channel_id: channel_uuid.to_string(),channel_name: Some("Test Channel".to_string()),channel_type: "public".to_string(),org_id: Some(123),@@ -75,7 +83,7 @@ fn test_construct_search_result_multiple_messages_same_channel() {},},opensearch_client::search::channels::ChannelMessageSearchResponse {- channel_id: "channel1".to_string(),+ channel_id: channel_uuid.to_string(),channel_name: Some("Test Channel".to_string()),channel_type: "public".to_string(),org_id: Some(123),@@ -92,11 +100,17 @@ fn test_construct_search_result_multiple_messages_same_channel() {},];- let result = construct_search_result(search_results, HashMap::new()).unwrap();+ let mut channel_histories = HashMap::new();+ channel_histories.insert(+ Uuid::parse_str(channel_uuid).unwrap(),+ create_channel_history(channel_uuid),+ );++ let result = construct_search_result(search_results, channel_histories).unwrap();assert_eq!(result.len(), 1);- assert_eq!(result[0].extra.channel_id, "channel1");- assert_eq!(result[0].extra.id, "channel1");+ assert_eq!(result[0].extra.channel_id, channel_uuid);+ assert_eq!(result[0].extra.id, channel_uuid);assert_eq!(result[0].extra.name, Some("Test Channel".to_string()));assert_eq!(result[0].extra.channel_message_search_results.len(), 2);@@ -121,9 +135,10 @@ fn test_construct_search_result_multiple_messages_same_channel() {#[test]fn test_construct_search_result_filters_messages_without_content() {+ let channel_uuid = "550e8400-e29b-41d4-a716-446655440002";let search_results = vec![opensearch_client::search::channels::ChannelMessageSearchResponse {- channel_id: "channel1".to_string(),+ channel_id: channel_uuid.to_string(),channel_name: Some("Test Channel".to_string()),channel_type: "public".to_string(),org_id: Some(123),@@ -139,7 +154,7 @@ fn test_construct_search_result_filters_messages_without_content() {},},opensearch_client::search::channels::ChannelMessageSearchResponse {- channel_id: "channel1".to_string(),+ channel_id: channel_uuid.to_string(),channel_name: Some("Test Channel".to_string()),channel_type: "public".to_string(),org_id: Some(123),@@ -153,7 +168,13 @@ fn test_construct_search_result_filters_messages_without_content() {},];- let result = construct_search_result(search_results, HashMap::new()).unwrap();+ let mut channel_histories = HashMap::new();+ channel_histories.insert(+ Uuid::parse_str(channel_uuid).unwrap(),+ create_channel_history(channel_uuid),+ );++ let result = construct_search_result(search_results, channel_histories).unwrap();assert_eq!(result.len(), 1);assert_eq!(result[0].extra.channel_message_search_results.len(), 2);@@ -187,6 +208,18 @@ fn create_test_channel_response(}}+fn create_channel_history(channel_id: &str) -> ChannelHistoryInfo {+ let now = chrono::Utc::now();+ let channel_uuid = Uuid::parse_str(channel_id).unwrap_or_else(|_| Uuid::new_v4());+ ChannelHistoryInfo {+ item_id: channel_uuid,+ created_at: now,+ updated_at: now,+ viewed_at: None,+ interacted_at: None,+ }+}+#[test]fn test_channel_history_timestamps() {// Create a mock channel history with known timestamps@@ -217,10 +250,12 @@ fn test_channel_history_timestamps() {// Verify that timestamps were copied from the channel historyassert_eq!(result.len(), 1);- assert_eq!(result[0].created_at, now.timestamp());- assert_eq!(result[0].updated_at, now.timestamp());- assert_eq!(result[0].viewed_at, Some(now.timestamp()));- assert_eq!(result[0].interacted_at, Some(now.timestamp()));+ assert!(result[0].metadata.is_some());+ let metadata = result[0].metadata.as_ref().unwrap();+ assert_eq!(metadata.created_at, now.timestamp());+ assert_eq!(metadata.updated_at, now.timestamp());+ assert_eq!(metadata.viewed_at, Some(now.timestamp()));+ assert_eq!(metadata.interacted_at, Some(now.timestamp()));}#[test]@@ -252,15 +287,9 @@ fn test_channel_history_missing_entry() {// Call the function under testlet result = construct_search_result(input, channel_histories).unwrap();- // Verify that default timestamps were used+ // Channels without history info should still be returned but with None metadataassert_eq!(result.len(), 1);-- // Default values from ChannelHistoryInfo::default()- let default_time = chrono::DateTime::<chrono::Utc>::default().timestamp();- assert_eq!(result[0].created_at, default_time);- assert_eq!(result[0].updated_at, default_time);- assert_eq!(result[0].viewed_at, None);- assert_eq!(result[0].interacted_at, None);+ assert!(result[0].metadata.is_none());}#[test]@@ -293,10 +322,12 @@ fn test_channel_history_null_viewed_at() {// Verify that timestamps were copied correctly and viewed_at is Noneassert_eq!(result.len(), 1);- assert_eq!(result[0].created_at, now.timestamp());- assert_eq!(result[0].updated_at, now.timestamp());- assert_eq!(result[0].viewed_at, None);- assert_eq!(result[0].interacted_at, None);+ assert!(result[0].metadata.is_some());+ let metadata = result[0].metadata.as_ref().unwrap();+ assert_eq!(metadata.created_at, now.timestamp());+ assert_eq!(metadata.updated_at, now.timestamp());+ assert!(metadata.viewed_at.is_none());+ assert!(metadata.interacted_at.is_none());}#[test]@@ -315,11 +346,7 @@ fn test_channel_history_invalid_uuid() {// Call the function under testlet result = construct_search_result(input, channel_histories).unwrap();- // Verify that default timestamps were used since UUID parsing failed+ // Channels with invalid UUIDs should still be returned but with None metadataassert_eq!(result.len(), 1);- let default_time = chrono::DateTime::<chrono::Utc>::default().timestamp();- assert_eq!(result[0].created_at, default_time);- assert_eq!(result[0].updated_at, default_time);- assert_eq!(result[0].viewed_at, None);- assert_eq!(result[0].interacted_at, None);+ assert!(result[0].metadata.is_none());}diff --git a/rust/cloud-storage/search_service/src/api/search/chat.rs b/rust/cloud-storage/search_service/src/api/search/chat.rsindex a5bf92897a..7c9485bf83 100644--- a/rust/cloud-storage/search_service/src/api/search/chat.rs+++ b/rust/cloud-storage/search_service/src/api/search/chat.rs@@ -7,7 +7,6 @@ use axum::{http::StatusCode,response::{IntoResponse, Json, Response},};-use macro_db_client::chat::get::ChatHistoryInfo;use model::{response::ErrorResponse, user::UserContext};use models_search::chat::{ChatMessageSearchResult, ChatSearchMetadata, ChatSearchRequest, ChatSearchResponse,@@ -81,7 +80,7 @@ pub async fn handler(pub fn construct_search_result(search_results: Vec<opensearch_client::search::chats::ChatSearchResponse>,- chat_histories: HashMap<String, ChatHistoryInfo>,+ chat_histories: HashMap<String, macro_db_client::chat::get::ChatHistoryInfo>,) -> anyhow::Result<Vec<ChatSearchResponseItemWithMetadata>> {let search_results = search_results.into_iter()@@ -99,15 +98,19 @@ pub fn construct_search_result(let result: Vec<ChatSearchResponseItemWithMetadata> = result.into_iter().map(|item| {- let chat_history_info = chat_histories- .get(&item.chat_id)- .cloned()- .unwrap_or_default();+ let metadata =+ chat_histories+ .get(&item.chat_id)+ .map(|info| models_search::chat::ChatMetadata {+ created_at: info.created_at.timestamp(),+ updated_at: info.updated_at.timestamp(),+ viewed_at: info.viewed_at.map(|a| a.timestamp()),+ project_id: info.project_id.clone(),+ deleted_at: info.deleted_at.map(|a| a.timestamp()),+ });+ChatSearchResponseItemWithMetadata {- created_at: chat_history_info.created_at.timestamp(),- updated_at: chat_history_info.updated_at.timestamp(),- viewed_at: chat_history_info.viewed_at.map(|a| a.timestamp()),- project_id: chat_history_info.project_id,+ metadata,extra: item,}})diff --git a/rust/cloud-storage/search_service/src/api/search/chat/test.rs b/rust/cloud-storage/search_service/src/api/search/chat/test.rsindex eddf6979af..88c1b2733d 100644--- a/rust/cloud-storage/search_service/src/api/search/chat/test.rs+++ b/rust/cloud-storage/search_service/src/api/search/chat/test.rs@@ -1,4 +1,5 @@use super::*;+use macro_db_client::chat::get::ChatHistoryInfo;use opensearch_client::search::model::Highlight;use sqlx::types::chrono;@@ -22,6 +23,18 @@ fn create_test_response(}}+fn create_chat_history(chat_id: &str) -> macro_db_client::chat::get::ChatHistoryInfo {+ let now = chrono::Utc::now();+ macro_db_client::chat::get::ChatHistoryInfo {+ item_id: chat_id.to_string(),+ created_at: now,+ updated_at: now,+ viewed_at: None,+ project_id: None,+ ..Default::default()+ }+}+#[test]fn test_empty_input() {let input = vec![];@@ -37,7 +50,22 @@ fn test_single_chat_with_content() {"user_1",Some(vec!["hello world".to_string()]),)];- let result = construct_search_result(input, HashMap::new()).unwrap();++ let mut chat_histories = HashMap::new();+ let now = chrono::Utc::now();+ chat_histories.insert(+ "chat_1".to_string(),+ macro_db_client::chat::get::ChatHistoryInfo {+ item_id: "chat_1".to_string(),+ created_at: now,+ updated_at: now,+ viewed_at: None,+ project_id: None,+ ..Default::default()+ },+ );++ let result = construct_search_result(input, chat_histories).unwrap();assert_eq!(result.len(), 1);assert_eq!(result[0].extra.chat_id, "chat_1");@@ -59,7 +87,11 @@ fn test_single_chat_with_content() {#[test]fn test_single_chat_without_content() {let input = vec![create_test_response("chat_1", "msg_1", "user_1", None)];- let result = construct_search_result(input, HashMap::new()).unwrap();++ let mut chat_histories = HashMap::new();+ chat_histories.insert("chat_1".to_string(), create_chat_history("chat_1"));++ let result = construct_search_result(input, chat_histories).unwrap();assert_eq!(result.len(), 1);assert_eq!(result[0].extra.chat_id, "chat_1");@@ -76,7 +108,11 @@ fn test_single_chat_multiple_messages() {create_test_response("chat_1", "msg_1", "user_1", Some(vec!["hello".to_string()])),create_test_response("chat_1", "msg_2", "user_1", Some(vec!["world".to_string()])),];- let result = construct_search_result(input, HashMap::new()).unwrap();++ let mut chat_histories = HashMap::new();+ chat_histories.insert("chat_1".to_string(), create_chat_history("chat_1"));++ let result = construct_search_result(input, chat_histories).unwrap();assert_eq!(result.len(), 1);assert_eq!(result[0].extra.chat_id, "chat_1");@@ -100,7 +136,12 @@ fn test_multiple_chats() {create_test_response("chat_1", "msg_1", "user_1", Some(vec!["hello".to_string()])),create_test_response("chat_2", "msg_2", "user_2", Some(vec!["world".to_string()])),];- let result = construct_search_result(input, HashMap::new()).unwrap();++ let mut chat_histories = HashMap::new();+ chat_histories.insert("chat_1".to_string(), create_chat_history("chat_1"));+ chat_histories.insert("chat_2".to_string(), create_chat_history("chat_2"));++ let result = construct_search_result(input, chat_histories).unwrap();assert_eq!(result.len(), 2);@@ -132,7 +173,11 @@ fn test_mixed_content_presence() {Some(vec!["also visible".to_string()]),),];- let result = construct_search_result(input, HashMap::new()).unwrap();++ let mut chat_histories = HashMap::new();+ chat_histories.insert("chat_1".to_string(), create_chat_history("chat_1"));++ let result = construct_search_result(input, chat_histories).unwrap();assert_eq!(result.len(), 1);assert_eq!(result[0].extra.chat_id, "chat_1");@@ -165,7 +210,11 @@ fn test_user_id_taken_from_first_result() {Some(vec!["content2".to_string()]),),];- let result = construct_search_result(input, HashMap::new()).unwrap();++ let mut chat_histories = HashMap::new();+ chat_histories.insert("chat_1".to_string(), create_chat_history("chat_1"));++ let result = construct_search_result(input, chat_histories).unwrap();assert_eq!(result.len(), 1);// user_id should come from the first result (base_search_result)@@ -193,6 +242,7 @@ fn test_chat_history_timestamps() {updated_at: now,viewed_at: Some(now),project_id: Some("project_1".to_string()),+ ..Default::default()};chat_histories.insert("chat_1".to_string(), history);@@ -202,9 +252,18 @@ fn test_chat_history_timestamps() {// Verify that timestamps were copied from the chat historyassert_eq!(result.len(), 1);- assert_eq!(result[0].created_at, now.timestamp());- assert_eq!(result[0].updated_at, now.timestamp());- assert_eq!(result[0].viewed_at, Some(now.timestamp()));+ assert_eq!(+ result[0].metadata.as_ref().unwrap().created_at,+ now.timestamp()+ );+ assert_eq!(+ result[0].metadata.as_ref().unwrap().updated_at,+ now.timestamp()+ );+ assert_eq!(+ result[0].metadata.as_ref().unwrap().viewed_at,+ Some(now.timestamp())+ );}#[test]@@ -227,6 +286,7 @@ fn test_chat_history_missing_entry() {updated_at: now,viewed_at: None,project_id: Some("project_1".to_string()),+ ..Default::default()};chat_histories.insert("different_chat".to_string(), history);@@ -234,14 +294,61 @@ fn test_chat_history_missing_entry() {// Call the function under testlet result = construct_search_result(input, chat_histories).unwrap();- // Verify that default timestamps were used+ // Chats without history info should have metadata=Noneassert_eq!(result.len(), 1);+ assert!(result[0].metadata.is_none());+}- // Default values from ChatHistoryInfo::default()- let default_time = chrono::DateTime::<chrono::Utc>::default().timestamp();- assert_eq!(result[0].created_at, default_time);- assert_eq!(result[0].updated_at, default_time);- assert_eq!(result[0].viewed_at, None);+#[test]+fn test_chat_history_deleted() {+ let now = chrono::Utc::now();++ // Test 1: Chat that exists but is soft-deleted+ let input_deleted = vec![create_test_response(+ "chat_deleted",+ "msg_1",+ "user_1",+ Some(vec!["hello world".to_string()]),+ )];++ let mut chat_histories = HashMap::new();+ chat_histories.insert(+ "chat_deleted".to_string(),+ macro_db_client::chat::get::ChatHistoryInfo {+ item_id: "chat_deleted".to_string(),+ created_at: now,+ updated_at: now,+ viewed_at: Some(now),+ project_id: Some("project_1".to_string()),+ deleted_at: Some(now), // Soft deleted+ },+ );++ let result = construct_search_result(input_deleted, chat_histories).unwrap();++ // Deleted chat should be returned with metadata including deleted_at+ assert_eq!(result.len(), 1);+ assert!(result[0].metadata.is_some());+ let metadata = result[0].metadata.as_ref().unwrap();+ assert_eq!(metadata.deleted_at, Some(now.timestamp()));+ assert_eq!(metadata.project_id, Some("project_1".to_string()));++ // Test 2: Chat that doesn't exist in DB (OpenSearch has stale data)+ let input_not_found = vec![create_test_response(+ "chat_not_found",+ "msg_2",+ "user_1",+ Some(vec!["stale data".to_string()]),+ )];++ let chat_histories_not_found = HashMap::new(); // No entry = not found++ let result_not_found =+ construct_search_result(input_not_found, chat_histories_not_found).unwrap();++ // Chat not in DB should be returned with metadata=None+ assert_eq!(result_not_found.len(), 1);+ assert!(result_not_found[0].metadata.is_none());}#[test]@@ -264,6 +371,7 @@ fn test_chat_history_null_viewed_at() {updated_at: now,viewed_at: None, // This user has never viewed this chatproject_id: Some("project_1".to_string()),+ ..Default::default()};chat_histories.insert("chat_1".to_string(), history);@@ -273,7 +381,13 @@ fn test_chat_history_null_viewed_at() {// Verify that timestamps were copied correctly and viewed_at is Noneassert_eq!(result.len(), 1);- assert_eq!(result[0].created_at, now.timestamp());- assert_eq!(result[0].updated_at, now.timestamp());- assert_eq!(result[0].viewed_at, None);+ assert_eq!(+ result[0].metadata.as_ref().unwrap().created_at,+ now.timestamp()+ );+ assert_eq!(+ result[0].metadata.as_ref().unwrap().updated_at,+ now.timestamp()+ );+ assert!(result[0].metadata.as_ref().unwrap().viewed_at.is_none());}diff --git a/rust/cloud-storage/search_service/src/api/search/document.rs b/rust/cloud-storage/search_service/src/api/search/document.rsindex 7c961dea8d..5b97fd7179 100644--- a/rust/cloud-storage/search_service/src/api/search/document.rs+++ b/rust/cloud-storage/search_service/src/api/search/document.rs@@ -8,7 +8,6 @@ use axum::{http::StatusCode,response::{IntoResponse, Json, Response},};-use macro_db_client::document::get_document_history::DocumentHistoryInfo;use model::{response::ErrorResponse, user::UserContext};use models_search::document::{DocumentSearchMetadata, DocumentSearchRequest, DocumentSearchResponse,@@ -87,7 +86,10 @@ pub async fn handler(pub fn construct_search_result(search_results: Vec<opensearch_client::search::documents::DocumentSearchResponse>,- document_histories: HashMap<String, DocumentHistoryInfo>,+ document_histories: HashMap<+ String,+ macro_db_client::document::get_document_history::DocumentHistoryInfo,+ >,) -> anyhow::Result<Vec<DocumentSearchResponseItemWithMetadata>> {let search_results = search_results.into_iter()@@ -105,15 +107,18 @@ pub fn construct_search_result(let result: Vec<DocumentSearchResponseItemWithMetadata> = result.into_iter().map(|item| {- let document_history_info = document_histories- .get(&item.document_id)- .cloned()- .unwrap_or_default();+ let metadata = document_histories.get(&item.document_id).map(|info| {+ models_search::document::DocumentMetadata {+ created_at: info.created_at.timestamp(),+ updated_at: info.updated_at.timestamp(),+ viewed_at: info.viewed_at.map(|a| a.timestamp()),+ project_id: info.project_id.clone(),+ deleted_at: info.deleted_at.map(|a| a.timestamp()),+ }+ });+DocumentSearchResponseItemWithMetadata {- created_at: document_history_info.created_at.timestamp(),- updated_at: document_history_info.updated_at.timestamp(),- viewed_at: document_history_info.viewed_at.map(|a| a.timestamp()),- project_id: document_history_info.project_id,+ metadata,extra: item,}})diff --git a/rust/cloud-storage/search_service/src/api/search/document/test.rs b/rust/cloud-storage/search_service/src/api/search/document/test.rsindex 893a00a35e..17ccc3f443 100644--- a/rust/cloud-storage/search_service/src/api/search/document/test.rs+++ b/rust/cloud-storage/search_service/src/api/search/document/test.rs@@ -1,3 +1,4 @@+use macro_db_client::document::get_document_history::DocumentHistoryInfo;use opensearch_client::search::model::Highlight;use super::*;@@ -27,7 +28,21 @@ fn test_construct_search_result_single_document() {},];- let result = construct_search_result(search_results, HashMap::new()).unwrap();+ let mut document_histories = HashMap::new();+ let now = chrono::Utc::now();+ document_histories.insert(+ "doc1".to_string(),+ DocumentHistoryInfo {+ item_id: "doc1".to_string(),+ created_at: now,+ updated_at: now,+ viewed_at: None,+ project_id: None,+ ..Default::default()+ },+ );++ let result = construct_search_result(search_results, document_histories).unwrap();assert_eq!(result.len(), 1);assert_eq!(result[0].extra.document_id, "doc1");@@ -75,7 +90,21 @@ fn test_construct_search_result_multiple_nodes_same_document() {},];- let result = construct_search_result(search_results, HashMap::new()).unwrap();+ let mut document_histories = HashMap::new();+ let now = chrono::Utc::now();+ document_histories.insert(+ "doc1".to_string(),+ DocumentHistoryInfo {+ item_id: "doc1".to_string(),+ created_at: now,+ updated_at: now,+ viewed_at: None,+ project_id: None,+ ..Default::default()+ },+ );++ let result = construct_search_result(search_results, document_histories).unwrap();assert_eq!(result.len(), 1);assert_eq!(result[0].extra.document_id, "doc1");@@ -135,6 +164,7 @@ fn test_document_history_timestamps() {updated_at: now,viewed_at: Some(now),project_id: Some("project_1".to_string()),+ ..Default::default()};document_histories.insert("doc_1".to_string(), history);@@ -144,9 +174,18 @@ fn test_document_history_timestamps() {// Verify that timestamps were copied from the document historyassert_eq!(result.len(), 1);- assert_eq!(result[0].created_at, now.timestamp());- assert_eq!(result[0].updated_at, now.timestamp());- assert_eq!(result[0].viewed_at, Some(now.timestamp()));+ assert_eq!(+ result[0].metadata.as_ref().unwrap().created_at,+ now.timestamp()+ );+ assert_eq!(+ result[0].metadata.as_ref().unwrap().updated_at,+ now.timestamp()+ );+ assert_eq!(+ result[0].metadata.as_ref().unwrap().viewed_at,+ Some(now.timestamp())+ );}#[test]@@ -169,6 +208,7 @@ fn test_document_history_missing_entry() {updated_at: now,viewed_at: None,project_id: Some("project_1".to_string()),+ ..Default::default()};document_histories.insert("different_doc".to_string(), history);@@ -176,14 +216,9 @@ fn test_document_history_missing_entry() {// Call the function under testlet result = construct_search_result(input, document_histories).unwrap();- // Verify that default timestamps were used+ // Documents without history info should have metadata=Noneassert_eq!(result.len(), 1);-- // Default values from DocumentHistoryInfo::default()- let default_time = chrono::DateTime::<chrono::Utc>::default().timestamp();- assert_eq!(result[0].created_at, default_time);- assert_eq!(result[0].updated_at, default_time);- assert_eq!(result[0].viewed_at, None);+ assert!(result[0].metadata.is_none());}#[test]@@ -206,6 +241,7 @@ fn test_document_history_null_viewed_at() {updated_at: now,viewed_at: None, // This user has never viewed this documentproject_id: Some("project_1".to_string()),+ ..Default::default()};document_histories.insert("doc_1".to_string(), history);@@ -215,9 +251,15 @@ fn test_document_history_null_viewed_at() {// Verify that timestamps were copied correctly and viewed_at is Noneassert_eq!(result.len(), 1);- assert_eq!(result[0].created_at, now.timestamp());- assert_eq!(result[0].updated_at, now.timestamp());- assert_eq!(result[0].viewed_at, None);+ assert_eq!(+ result[0].metadata.as_ref().unwrap().created_at,+ now.timestamp()+ );+ assert_eq!(+ result[0].metadata.as_ref().unwrap().updated_at,+ now.timestamp()+ );+ assert_eq!(result[0].metadata.as_ref().unwrap().viewed_at, None);}#[test]@@ -249,6 +291,7 @@ fn test_document_history_multiple_documents() {updated_at: now,viewed_at: Some(now),project_id: Some("project_1".to_string()),+ ..Default::default()};let history2 = DocumentHistoryInfo {@@ -257,6 +300,7 @@ fn test_document_history_multiple_documents() {updated_at: earlier,viewed_at: None,project_id: Some("project_2".to_string()),+ ..Default::default()};document_histories.insert("doc_1".to_string(), history1);@@ -278,13 +322,15 @@ fn test_document_history_multiple_documents() {.find(|r| r.extra.document_id == "doc_2").unwrap();- assert_eq!(doc1_result.created_at, now.timestamp());- assert_eq!(doc1_result.updated_at, now.timestamp());- assert_eq!(doc1_result.viewed_at, Some(now.timestamp()));+ let doc1_metadata = doc1_result.metadata.as_ref().unwrap();+ assert_eq!(doc1_metadata.created_at, now.timestamp());+ assert_eq!(doc1_metadata.updated_at, now.timestamp());+ assert_eq!(doc1_metadata.viewed_at, Some(now.timestamp()));- assert_eq!(doc2_result.created_at, earlier.timestamp());- assert_eq!(doc2_result.updated_at, earlier.timestamp());- assert_eq!(doc2_result.viewed_at, None);+ let doc2_metadata = doc2_result.metadata.as_ref().unwrap();+ assert_eq!(doc2_metadata.created_at, earlier.timestamp());+ assert_eq!(doc2_metadata.updated_at, earlier.timestamp());+ assert_eq!(doc2_metadata.viewed_at, None);}#[test]@@ -315,6 +361,7 @@ fn test_document_history_partial_missing_entries() {updated_at: now,viewed_at: Some(now),project_id: Some("project_1".to_string()),+ ..Default::default()};document_histories.insert("doc_exists".to_string(), history);@@ -322,27 +369,76 @@ fn test_document_history_partial_missing_entries() {// Call the function under testlet result = construct_search_result(input, document_histories).unwrap();- // Verify results+ // We should have 2 results - one with real data, one with defaultsassert_eq!(result.len(), 2);- // Find each document in results+ // The existing document should have real timestamps in metadatalet existing_doc = result.iter().find(|r| r.extra.document_id == "doc_exists").unwrap();+ assert!(existing_doc.metadata.is_some());+ let metadata = existing_doc.metadata.as_ref().unwrap();+ assert_eq!(metadata.created_at, now.timestamp());+ assert_eq!(metadata.updated_at, now.timestamp());+ assert_eq!(metadata.viewed_at, Some(now.timestamp()));++ // The missing document should have no metadatalet missing_doc = result.iter().find(|r| r.extra.document_id == "doc_missing").unwrap();+ assert!(missing_doc.metadata.is_none());+}++#[test]+fn test_document_history_deleted() {+ let now = chrono::Utc::now();++ // Test 1: Document that exists but is soft-deleted+ let input_deleted = vec![create_test_document_response(+ "doc_deleted",+ "node_1",+ "user_1",+ Some(vec!["hello world".to_string()]),+ )];++ let mut document_histories = HashMap::new();+ document_histories.insert(+ "doc_deleted".to_string(),+ macro_db_client::document::get_document_history::DocumentHistoryInfo {+ item_id: "doc_deleted".to_string(),+ created_at: now,+ updated_at: now,+ viewed_at: Some(now),+ project_id: Some("project_1".to_string()),+ deleted_at: Some(now), // Soft deleted+ },+ );++ let result = construct_search_result(input_deleted, document_histories).unwrap();++ // Deleted document should be returned with metadata including deleted_at+ assert_eq!(result.len(), 1);+ assert!(result[0].metadata.is_some());+ let metadata = result[0].metadata.as_ref().unwrap();+ assert_eq!(metadata.deleted_at, Some(now.timestamp()));+ assert_eq!(metadata.project_id, Some("project_1".to_string()));++ // Test 2: Document that doesn't exist in DB (OpenSearch has stale data)+ let input_not_found = vec![create_test_document_response(+ "doc_not_found",+ "node_2",+ "user_1",+ Some(vec!["stale data".to_string()]),+ )];++ let document_histories_not_found = HashMap::new(); // No entry = not found- // Existing document should have real timestamps- assert_eq!(existing_doc.created_at, now.timestamp());- assert_eq!(existing_doc.updated_at, now.timestamp());- assert_eq!(existing_doc.viewed_at, Some(now.timestamp()));+ let result_not_found =+ construct_search_result(input_not_found, document_histories_not_found).unwrap();- // Missing document should have default timestamps- let default_time = chrono::DateTime::<chrono::Utc>::default().timestamp();- assert_eq!(missing_doc.created_at, default_time);- assert_eq!(missing_doc.updated_at, default_time);- assert_eq!(missing_doc.viewed_at, None);+ // Document not in DB should be returned with metadata=None+ assert_eq!(result_not_found.len(), 1);+ assert!(result_not_found[0].metadata.is_none());}diff --git a/rust/cloud-storage/search_service/src/api/search/email/test.rs b/rust/cloud-storage/search_service/src/api/search/email/test.rsindex 3788a3cd55..914647e3b5 100644--- a/rust/cloud-storage/search_service/src/api/search/email/test.rs+++ b/rust/cloud-storage/search_service/src/api/search/email/test.rs@@ -2,6 +2,18 @@ use opensearch_client::search::model::Highlight;use super::*;+fn create_email_history(thread_id: &str) -> models_email::service::message::ThreadHistoryInfo {+ let now = chrono::Utc::now();+ let thread_uuid = Uuid::parse_str(thread_id).unwrap_or_else(|_| Uuid::new_v4());+ models_email::service::message::ThreadHistoryInfo {+ item_id: thread_uuid,+ created_at: now,+ updated_at: now,+ viewed_at: None,+ snippet: None,+ }+}+#[test]fn test_construct_search_result_empty_input() {let result = construct_search_result(vec![], HashMap::new());@@ -11,8 +23,9 @@ fn test_construct_search_result_empty_input() {#[test]fn test_construct_search_result_single_thread() {+ let thread_uuid = "550e8400-e29b-41d4-a716-446655440000";let search_results = vec![opensearch_client::search::emails::EmailSearchResponse {- thread_id: "thread1".to_string(),+ thread_id: thread_uuid.to_string(),message_id: "msg1".to_string(),sender: "[email protected]".to_string(),recipients: vec!["[email protected]".to_string()],@@ -30,10 +43,16 @@ fn test_construct_search_result_single_thread() {},}];- let result = construct_search_result(search_results, HashMap::new()).unwrap();+ let mut thread_histories = HashMap::new();+ thread_histories.insert(+ Uuid::parse_str(thread_uuid).unwrap(),+ create_email_history(thread_uuid),+ );++ let result = construct_search_result(search_results, thread_histories).unwrap();assert_eq!(result.len(), 1);- assert_eq!(result[0].extra.thread_id, "thread1");+ assert_eq!(result[0].extra.thread_id, thread_uuid);assert_eq!(result[0].extra.user_id, "user1");assert_eq!(result[0].extra.email_message_search_results.len(), 1);assert_eq!(diff --git a/rust/cloud-storage/search_service/src/api/search/project.rs b/rust/cloud-storage/search_service/src/api/search/project.rsindex a376c23fb1..8d6f312a61 100644--- a/rust/cloud-storage/search_service/src/api/search/project.rs+++ b/rust/cloud-storage/search_service/src/api/search/project.rs@@ -8,7 +8,6 @@ use axum::{http::StatusCode,response::{IntoResponse, Json, Response},};-use macro_db_client::projects::get_project_history::ProjectHistoryInfo;use model::{response::ErrorResponse, user::UserContext};use models_search::project::{ProjectSearchMetadata, ProjectSearchRequest, ProjectSearchResponse, ProjectSearchResponseItem,@@ -88,7 +87,10 @@ pub async fn handler(pub fn construct_search_result(search_results: Vec<opensearch_client::search::projects::ProjectSearchResponse>,- project_histories: HashMap<String, ProjectHistoryInfo>,+ project_histories: HashMap<+ String,+ macro_db_client::projects::get_project_history::ProjectHistoryInfo,+ >,) -> anyhow::Result<Vec<ProjectSearchResponseItemWithMetadata>> {let search_results = search_results.into_iter()@@ -104,12 +106,18 @@ pub fn construct_search_result(let result: Vec<ProjectSearchResponseItemWithMetadata> = result.into_iter().map(|item| {- let project_history_info = project_histories.get(&item.id).cloned().unwrap_or_default();+ let metadata = project_histories.get(&item.id).map(|info| {+ models_search::project::ProjectMetadata {+ created_at: info.created_at.timestamp(),+ updated_at: info.updated_at.timestamp(),+ viewed_at: info.viewed_at.map(|a| a.timestamp()),+ parent_project_id: info.parent_project_id.clone(),+ deleted_at: info.deleted_at.map(|a| a.timestamp()),+ }+ });+ProjectSearchResponseItemWithMetadata {- created_at: project_history_info.created_at.timestamp(),- updated_at: project_history_info.updated_at.timestamp(),- viewed_at: project_history_info.viewed_at.map(|a| a.timestamp()),- parent_project_id: project_history_info.parent_project_id,+ metadata,extra: item,}})