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(email-service): Upsert thread history when upserting draft (#958)
Evan Hutnikdeee52b6a4ce
diff --git a/rust/cloud-storage/email_db_client/src/messages/insert.rs b/rust/cloud-storage/email_db_client/src/messages/insert.rsindex 9adb79d8d4..05d2b317bb 100644--- a/rust/cloud-storage/email_db_client/src/messages/insert.rs+++ b/rust/cloud-storage/email_db_client/src/messages/insert.rs@@ -217,7 +217,7 @@ pub async fn insert_message(}/// insert message that user created via macro frontend-#[tracing::instrument(skip(tx, service_message))]+#[tracing::instrument(skip(tx, service_message), err)]pub async fn insert_message_to_send(tx: &mut sqlx::PgConnection,service_message: &mut message::MessageToSend,@@ -241,8 +241,7 @@ pub async fn insert_message_to_send(db_message_to_send.link_id,addresses,)- .await- .context("Failed to insert address ids")?;+ .await?;// Insert the message into the databasesqlx::query!(@@ -300,24 +299,17 @@ pub async fn insert_message_to_send(Utc::now()).execute(&mut *tx)- .await- .with_context(|| format!("Failed to insert message with thread_id {}", thread_id))?;+ .await?;service_message.db_id = Some(message_db_id);- process_scheduled_message(tx, service_message, message_db_id, is_draft)- .await- .context("Failed to process scheduled message")?;+ process_scheduled_message(tx, service_message, message_db_id, is_draft).await?;if let Some(mut attachments) = service_message.attachments_macro.clone() {- marco::insert_macro_attachments(tx, message_db_id, &mut attachments)- .await- .context("Failed to insert macro attachments")?;+ marco::insert_macro_attachments(tx, message_db_id, &mut attachments).await?;}- contacts::upsert_message::upsert_message_recipients(tx, message_db_id, &recipients)- .await- .context("Failed to insert recipients")?;+ contacts::upsert_message::upsert_message_recipients(tx, message_db_id, &recipients).await?;Ok(())}diff --git a/rust/cloud-storage/email_db_client/src/user_history/mod.rs b/rust/cloud-storage/email_db_client/src/user_history/mod.rsindex b9a8f168ce..1224702506 100644--- a/rust/cloud-storage/email_db_client/src/user_history/mod.rs+++ b/rust/cloud-storage/email_db_client/src/user_history/mod.rs@@ -3,17 +3,20 @@ mod test;use anyhow::anyhow;use models_email::service::message::ThreadHistoryInfo;-use sqlx::PgPool;use sqlx::types::Uuid;+use sqlx::{Executor, PgPool, Postgres};use std::collections::HashMap;// upsert user_history row for user and thread-#[tracing::instrument(skip(pool), level = "info")]-pub async fn upsert_user_history(- pool: &PgPool,+#[tracing::instrument(skip(executor), err)]+pub async fn upsert_user_history<'e, E>(+ executor: E,link_id: Uuid,thread_id: Uuid,-) -> anyhow::Result<()> {+) -> anyhow::Result<()>+where+ E: Executor<'e, Database = Postgres>,+{sqlx::query!(r#"INSERT INTO email_user_history (link_id, thread_id, created_at, updated_at)@@ -25,7 +28,7 @@ pub async fn upsert_user_history(link_id,thread_id)- .execute(pool)+ .execute(executor).await.map_err(|_| anyhow!("Failed to upsert user history"))?;diff --git a/rust/cloud-storage/email_service/src/api/email/drafts/create.rs b/rust/cloud-storage/email_service/src/api/email/drafts/create.rsindex fdfeb75e84..9e8e5881de 100644--- a/rust/cloud-storage/email_service/src/api/email/drafts/create.rs+++ b/rust/cloud-storage/email_service/src/api/email/drafts/create.rs@@ -1,11 +1,11 @@use crate::api::context::ApiContext;use crate::api::email::validation::{self, ValidationError};-use anyhow::Context;use axum::extract::State;use axum::http::StatusCode;use axum::response::{IntoResponse, Response};use axum::{Extension, Json};use email_db_client::messages::insert::insert_message_to_send;+use email_db_client::user_history::upsert_user_history;use model::response::ErrorResponse;use model::user::UserContext;use models_email::service::link::Link;@@ -114,18 +114,18 @@ pub async fn handler(}}+#[tracing::instrument(skip(tx), err)]async fn insert_draft(tx: &mut sqlx::PgConnection,draft: &mut message::MessageToSend,from_email: &str,) -> anyhow::Result<()> {- let mut thread_db_id = draft.thread_db_id;let link_id = draft.link_id;let now: DateTime<Utc> = Utc::now();- // if there isn't already a thread associated with this message, create one- if thread_db_id.is_none() {- let link_id = draft.link_id;+ let thread_db_id = if let Some(id) = draft.thread_db_id {+ id+ } else {let thread = thread::Thread {db_id: None,provider_id: None,@@ -141,22 +141,19 @@ async fn insert_draft(messages: Vec::new(),};- thread_db_id = Option::from(- email_db_client::threads::insert::insert_thread(&mut *tx, &thread, link_id)- .await- .context("unable to insert thread")?,- );- draft.thread_db_id = thread_db_id;- }+ let new_id =+ email_db_client::threads::insert::insert_thread(&mut *tx, &thread, link_id).await?;++ draft.thread_db_id = Some(new_id);+ new_id+ };let from_email_id =- // safe because we always populate the from field before calling this function- email_db_client::contacts::get::fetch_id_by_email(tx, link_id, from_email)- .await.context("unable to fetch from email id")?;+ email_db_client::contacts::get::fetch_id_by_email(tx, link_id, from_email).await?;++ insert_message_to_send(tx, draft, thread_db_id, from_email_id, true).await?;- insert_message_to_send(tx, draft, thread_db_id.unwrap(), from_email_id, true)- .await- .context("unable to insert message to send")?;+ upsert_user_history(tx, link_id, thread_db_id).await?;Ok(())}