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(notif): filter invalid notifications instead of failing the whole query (#1780)
Sean Ayecc47f81b106a
diff --git a/rust/cloud-storage/notification/fixtures/user_notifications_with_invalid.sql b/rust/cloud-storage/notification/fixtures/user_notifications_with_invalid.sqlnew file mode 100644index 0000000000..cf5f364641--- /dev/null+++ b/rust/cloud-storage/notification/fixtures/user_notifications_with_invalid.sql@@ -0,0 +1,20 @@+-- Valid notification+INSERT INTO notification (id, notification_event_type, event_item_id, event_item_type, service_sender, metadata, sender_id)+VALUES ('0193b1ea-a542-7589-893b-2b4a509c1e76', 'test', 'a0000000-0000-0000-0000-000000000001', 'document', 'test_service', '{"message": "hello"}', NULL);++INSERT INTO user_notification (user_id, notification_id, created_at)+VALUES ('macro|[email protected]', '0193b1ea-a542-7589-893b-2b4a509c1e76', '2025-01-01 00:00:00');++-- Notification with invalid entity type (will fail EntityType::from_str)+INSERT INTO notification (id, notification_event_type, event_item_id, event_item_type, service_sender, metadata, sender_id)+VALUES ('0193b1ea-b642-7589-893b-2b4a509c1e76', 'test', 'a0000000-0000-0000-0000-000000000002', 'bogus_entity', 'test_service', '{"message": "bad entity"}', NULL);++INSERT INTO user_notification (user_id, notification_id, created_at)+VALUES ('macro|[email protected]', '0193b1ea-b642-7589-893b-2b4a509c1e76', '2025-01-02 00:00:00');++-- Notification with invalid metadata (will fail serde_json::from_value)+INSERT INTO notification (id, notification_event_type, event_item_id, event_item_type, service_sender, metadata, sender_id)+VALUES ('0193b1ea-c742-7589-893b-2b4a509c1e76', 'test', 'a0000000-0000-0000-0000-000000000003', 'document', 'test_service', '{"not_message": 123}', NULL);++INSERT INTO user_notification (user_id, notification_id, created_at)+VALUES ('macro|[email protected]', '0193b1ea-c742-7589-893b-2b4a509c1e76', '2025-01-03 00:00:00');diff --git a/rust/cloud-storage/notification/src/outbound/repository.rs b/rust/cloud-storage/notification/src/outbound/repository.rsindex 093c335534..5ef9d83c80 100644--- a/rust/cloud-storage/notification/src/outbound/repository.rs+++ b/rust/cloud-storage/notification/src/outbound/repository.rs@@ -13,6 +13,7 @@ use macro_user_id::user_id::MacroUserIdStr;use model_entity::EntityType;use models_pagination::{CreatedAt, Query};use rootcause::Report;+use rootcause::prelude::ResultExt;use serde::de::DeserializeOwned;use sqlx::PgPool;use std::collections::{HashMap, HashSet};@@ -478,41 +479,55 @@ impl NotificationDbOps for PgPool {.fetch_all(self).await?;- rows.into_iter()- .map(|row| {- let entity = EntityType::from_str(&row.event_item_type)- .map_err(|e| rootcause::report!(e))?- .with_entity_string(row.event_item_id);-- let sender_id = row- .sender_id- .map(|s| MacroUserIdStr::parse_from_str(&s).map(CowLike::into_owned))- .transpose()- .map_err(|e| rootcause::report!(e))?;-- let owner_id = MacroUserIdStr::parse_from_str(&row.owner_id)- .map(CowLike::into_owned)- .map_err(|e| rootcause::report!(e))?;-- let notification_metadata = serde_json::from_value::<T>(row.notification_metadata)- .map_err(|e| rootcause::report!(e))?;-- Ok(UserNotificationRow {- owner_id,- notification_id: row.notification_id,- notification_event_type: row.notification_event_type,- entity,- sent: row.sent,- done: row.done,- created_at: row.created_at,- viewed_at: row.viewed_at,- updated_at: row.updated_at,- deleted_at: row.deleted_at,- notification_metadata,- sender_id,- })+ Ok(rows+ .into_iter()+ .map(+ |row| -> Result<UserNotificationRow<T>, rootcause::Report<Uuid>> {+ let entity = EntityType::from_str(&row.event_item_type)+ .map_err(|e| rootcause::report!(e))+ .context(row.notification_id)?+ .with_entity_string(row.event_item_id);++ let sender_id = row+ .sender_id+ .map(|s| MacroUserIdStr::parse_from_str(&s).map(CowLike::into_owned))+ .transpose()+ .map_err(|e| rootcause::report!(e))+ .context(row.notification_id)?;++ let owner_id = MacroUserIdStr::parse_from_str(&row.owner_id)+ .map(CowLike::into_owned)+ .map_err(|e| rootcause::report!(e))+ .context(row.notification_id)?;++ let notification_metadata =+ serde_json::from_value::<T>(row.notification_metadata)+ .map_err(|e| rootcause::report!(e))+ .context(row.notification_id)?;++ Ok(UserNotificationRow {+ owner_id,+ notification_id: row.notification_id,+ notification_event_type: row.notification_event_type,+ entity,+ sent: row.sent,+ done: row.done,+ created_at: row.created_at,+ viewed_at: row.viewed_at,+ updated_at: row.updated_at,+ deleted_at: row.deleted_at,+ notification_metadata,+ sender_id,+ })+ },+ )+ .inspect(|r: &Result<UserNotificationRow<T>, _>| {+ if let Err(e) = r {+ tracing::warn!("skipping invalid notification: {e:?}");+ }})- .collect()+ .filter_map(Result::ok)+ .collect())}async fn get_user_notifications_by_event_item_ids<T: DeserializeOwned + Send>(@@ -562,24 +577,29 @@ impl NotificationDbOps for PgPool {.fetch_all(self).await?;- rows.into_iter()- .map(|row| {+ Ok(rows+ .into_iter()+ .map(|row| -> Result<UserNotificationRow<T>, Report<Uuid>> {let entity = EntityType::from_str(&row.event_item_type)- .map_err(|e| rootcause::report!(e))?+ .map_err(|e| rootcause::report!(e))+ .context(row.notification_id)?.with_entity_string(row.event_item_id);let sender_id = row.sender_id.map(|s| MacroUserIdStr::parse_from_str(&s).map(CowLike::into_owned)).transpose()- .map_err(|e| rootcause::report!(e))?;+ .map_err(|e| rootcause::report!(e))+ .context(row.notification_id)?;let owner_id = MacroUserIdStr::parse_from_str(&row.owner_id).map(CowLike::into_owned)- .map_err(|e| rootcause::report!(e))?;+ .map_err(|e| rootcause::report!(e))+ .context(row.notification_id)?;let notification_metadata = serde_json::from_value::<T>(row.notification_metadata)- .map_err(|e| rootcause::report!(e))?;+ .map_err(|e| rootcause::report!(e))+ .context(row.notification_id)?;Ok(UserNotificationRow {owner_id,@@ -596,7 +616,13 @@ impl NotificationDbOps for PgPool {sender_id,})})- .collect()+ .inspect(|r: &Result<UserNotificationRow<T>, _>| {+ if let Err(e) = r {+ tracing::warn!("skipping invalid notification: {e:?}");+ }+ })+ .filter_map(Result::ok)+ .collect())}async fn get_user_notification_by_id<T: DeserializeOwned + Send>(diff --git a/rust/cloud-storage/notification/src/outbound/repository/test.rs b/rust/cloud-storage/notification/src/outbound/repository/test.rsindex b424205a98..510287b36a 100644--- a/rust/cloud-storage/notification/src/outbound/repository/test.rs+++ b/rust/cloud-storage/notification/src/outbound/repository/test.rs@@ -302,6 +302,63 @@ async fn test_get_user_notifications(pool: Pool<Postgres>) {assert_eq!(row.notification_metadata.message, "hello");}+#[sqlx::test(+ migrator = "MACRO_DB_MIGRATIONS",+ fixtures(path = "../../../fixtures", scripts("user_notifications_with_invalid"))+)]+async fn test_get_user_notifications_skips_invalid_entity_type(pool: Pool<Postgres>) {+ let result: Vec<UserNotificationRow<TestNotification>> = pool+ .get_user_notifications(+ MacroUserIdStr::parse_from_str("macro|[email protected]").unwrap(),+ 10,+ Query::Sort(CreatedAt, ()),+ )+ .await+ .unwrap();++ // 3 notifications inserted, but only the valid one (with entity_type "document"+ // and correct metadata) should survive. The one with "bogus_entity" and the one+ // with non-matching metadata are silently filtered out.+ assert_eq!(result.len(), 1);+ assert_eq!(+ result[0].notification_id,+ uuid::Uuid::parse_str("0193b1ea-a542-7589-893b-2b4a509c1e76").unwrap()+ );+ assert_eq!(result[0].entity.entity_type, EntityType::Document);+ assert_eq!(result[0].notification_metadata.message, "hello");+}++#[sqlx::test(+ migrator = "MACRO_DB_MIGRATIONS",+ fixtures(path = "../../../fixtures", scripts("user_notifications_with_invalid"))+)]+async fn test_get_user_notifications_by_event_item_ids_skips_invalid(pool: Pool<Postgres>) {+ let valid_item = uuid::Uuid::parse_str("a0000000-0000-0000-0000-000000000001").unwrap();+ let invalid_entity_item =+ uuid::Uuid::parse_str("a0000000-0000-0000-0000-000000000002").unwrap();+ let invalid_metadata_item =+ uuid::Uuid::parse_str("a0000000-0000-0000-0000-000000000003").unwrap();++ let result: Vec<UserNotificationRow<TestNotification>> = pool+ .get_user_notifications_by_event_item_ids(+ MacroUserIdStr::parse_from_str("macro|[email protected]").unwrap(),+ &[valid_item, invalid_entity_item, invalid_metadata_item],+ 10,+ Query::Sort(CreatedAt, ()),+ )+ .await+ .unwrap();++ // All three are requested, but only the valid one survives filtering.+ assert_eq!(result.len(), 1);+ assert_eq!(+ result[0].notification_id,+ uuid::Uuid::parse_str("0193b1ea-a542-7589-893b-2b4a509c1e76").unwrap()+ );+ assert_eq!(result[0].entity.entity_type, EntityType::Document);+ assert_eq!(result[0].notification_metadata.message, "hello");+}+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]async fn test_get_user_notifications_empty(pool: Pool<Postgres>) {let result: Vec<UserNotificationRow<TestNotification>> = pool