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
opts(auth): name and email retrieval (#4897)
teo4830419d41f1
diff --git a/.sqlx/query-beeb3f92cd18664a6d748be12f52f8051602e6d2aaf7d5194af235e77902265b.json b/.sqlx/query-beeb3f92cd18664a6d748be12f52f8051602e6d2aaf7d5194af235e77902265b.jsonnew file mode 100644index 0000000000..2f9e8ba545--- /dev/null+++ b/.sqlx/query-beeb3f92cd18664a6d748be12f52f8051602e6d2aaf7d5194af235e77902265b.json@@ -0,0 +1,35 @@+{+ "db_name": "PostgreSQL",+ "query": "\n WITH requested_ids AS (\n SELECT DISTINCT id\n FROM UNNEST($2::text[]) AS requested(id)\n )\n SELECT\n req.id as \"user_profile_id!\",\n CASE\n WHEN NULLIF(mui.first_name, 'N/A') IS NOT NULL\n OR NULLIF(mui.last_name, 'N/A') IS NOT NULL\n THEN NULLIF(mui.first_name, 'N/A')\n ELSE SPLIT_PART(contact.name, ' ', 1)\n END as \"first_name\",\n CASE\n WHEN NULLIF(mui.first_name, 'N/A') IS NOT NULL\n OR NULLIF(mui.last_name, 'N/A') IS NOT NULL\n THEN NULLIF(mui.last_name, 'N/A')\n ELSE CASE\n WHEN POSITION(' ' IN contact.name) > 0\n THEN NULLIF(TRIM(SUBSTRING(contact.name FROM POSITION(' ' IN contact.name) + 1)), '')\n ELSE NULL\n END\n END as \"last_name\"\n FROM requested_ids req\n LEFT JOIN \"User\" u ON u.id = req.id\n LEFT JOIN macro_user_info mui ON mui.macro_user_id = u.macro_user_id\n LEFT JOIN LATERAL (\n SELECT ec.name\n FROM email_links li\n JOIN email_contacts ec\n ON ec.link_id = li.id\n AND ec.email_address = REPLACE(req.id, 'macro|', '')\n AND ec.name IS NOT NULL\n WHERE li.macro_id = $1\n AND NULLIF(mui.first_name, 'N/A') IS NULL\n AND NULLIF(mui.last_name, 'N/A') IS NULL\n LIMIT 1\n ) contact ON TRUE\n WHERE u.id IS NOT NULL OR contact.name IS NOT NULL\n ",+ "describe": {+ "columns": [+ {+ "ordinal": 0,+ "name": "user_profile_id!",+ "type_info": "Text"+ },+ {+ "ordinal": 1,+ "name": "first_name",+ "type_info": "Text"+ },+ {+ "ordinal": 2,+ "name": "last_name",+ "type_info": "Text"+ }+ ],+ "parameters": {+ "Left": [+ "Text",+ "TextArray"+ ]+ },+ "nullable": [+ true,+ null,+ null+ ]+ },+ "hash": "beeb3f92cd18664a6d748be12f52f8051602e6d2aaf7d5194af235e77902265b"+}diff --git a/.sqlx/query-cd2334cf312dee1e9b230cce0d7b26999ba3ac23f89c8882c144d4c9a657e235.json b/.sqlx/query-cd2334cf312dee1e9b230cce0d7b26999ba3ac23f89c8882c144d4c9a657e235.jsondeleted file mode 100644index 945fd5a6db..0000000000--- a/.sqlx/query-cd2334cf312dee1e9b230cce0d7b26999ba3ac23f89c8882c144d4c9a657e235.json+++ /dev/null@@ -1,35 +0,0 @@-{- "db_name": "PostgreSQL",- "query": "\n WITH requested_ids AS (\n SELECT UNNEST($2::text[]) as id\n ),\n user_links AS (\n SELECT id as link_id\n FROM email_links\n WHERE macro_id = $1\n ),\n users_found AS (\n SELECT\n u.id as user_profile_id,\n mui.first_name as mui_first_name,\n mui.last_name as mui_last_name\n FROM requested_ids req\n JOIN \"User\" u ON u.id = req.id\n LEFT JOIN macro_user_info mui ON u.macro_user_id = mui.macro_user_id\n ),\n contacts_requested AS (\n SELECT\n req.id as user_profile_id,\n ec.name as contact_name\n FROM requested_ids req\n CROSS JOIN user_links li\n JOIN email_contacts ec\n ON ec.link_id = li.link_id\n AND ec.email_address = REPLACE(req.id, 'macro|', '')\n AND ec.name IS NOT NULL\n )\n SELECT DISTINCT ON (user_profile_id)\n user_profile_id as \"user_profile_id!\",\n CASE\n -- If Macro has either first or last name (non-\"N/A\"), use Macro for BOTH fields.\n WHEN NULLIF(mui_first_name, 'N/A') IS NOT NULL\n OR NULLIF(mui_last_name, 'N/A') IS NOT NULL\n THEN NULLIF(mui_first_name, 'N/A')\n -- Otherwise fall back to email contact name parsing.\n ELSE SPLIT_PART(contact_name, ' ', 1)\n END as \"first_name\",\n CASE\n -- If Macro has either first or last name (non-\"N/A\"), use Macro for BOTH fields.\n WHEN NULLIF(mui_first_name, 'N/A') IS NOT NULL\n OR NULLIF(mui_last_name, 'N/A') IS NOT NULL\n THEN NULLIF(mui_last_name, 'N/A')\n -- Otherwise fall back to email contact name parsing.\n ELSE CASE\n WHEN POSITION(' ' IN contact_name) > 0\n THEN NULLIF(TRIM(SUBSTRING(contact_name FROM POSITION(' ' IN contact_name) + 1)), '')\n ELSE NULL\n END\n END as \"last_name\"\n FROM (\n -- Users in User table with contact fallback (in case user hasn't set name in Macro)\n SELECT\n uf.user_profile_id,\n uf.mui_first_name,\n uf.mui_last_name,\n cr.contact_name\n FROM users_found uf\n LEFT JOIN contacts_requested cr ON cr.user_profile_id = uf.user_profile_id\n\n UNION ALL\n\n -- People only in email_contacts (haven't joined Macro yet)\n SELECT\n cr.user_profile_id,\n NULL::text as mui_first_name,\n NULL::text as mui_last_name,\n cr.contact_name\n FROM contacts_requested cr\n WHERE NOT EXISTS (\n SELECT 1\n FROM users_found uf\n WHERE uf.user_profile_id = cr.user_profile_id\n )\n ) combined\n ORDER BY user_profile_id\n ",- "describe": {- "columns": [- {- "ordinal": 0,- "name": "user_profile_id!",- "type_info": "Text"- },- {- "ordinal": 1,- "name": "first_name",- "type_info": "Text"- },- {- "ordinal": 2,- "name": "last_name",- "type_info": "Text"- }- ],- "parameters": {- "Left": [- "Text",- "TextArray"- ]- },- "nullable": [- null,- null,- null- ]- },- "hash": "cd2334cf312dee1e9b230cce0d7b26999ba3ac23f89c8882c144d4c9a657e235"-}diff --git a/crates/macro_db_client/src/user/get_user_name.rs b/crates/macro_db_client/src/user/get_user_name.rsindex e0acb90961..ce9d7bd0aa 100644--- a/crates/macro_db_client/src/user/get_user_name.rs+++ b/crates/macro_db_client/src/user/get_user_name.rs@@ -66,81 +66,43 @@ pub async fn get_user_names_with_email(let user_names = sqlx::query!(r#"WITH requested_ids AS (- SELECT UNNEST($2::text[]) as id- ),- user_links AS (- SELECT id as link_id- FROM email_links- WHERE macro_id = $1- ),- users_found AS (- SELECT- u.id as user_profile_id,- mui.first_name as mui_first_name,- mui.last_name as mui_last_name- FROM requested_ids req- JOIN "User" u ON u.id = req.id- LEFT JOIN macro_user_info mui ON u.macro_user_id = mui.macro_user_id- ),- contacts_requested AS (- SELECT- req.id as user_profile_id,- ec.name as contact_name- FROM requested_ids req- CROSS JOIN user_links li- JOIN email_contacts ec- ON ec.link_id = li.link_id- AND ec.email_address = REPLACE(req.id, 'macro|', '')- AND ec.name IS NOT NULL+ SELECT DISTINCT id+ FROM UNNEST($2::text[]) AS requested(id))- SELECT DISTINCT ON (user_profile_id)- user_profile_id as "user_profile_id!",- CASE- -- If Macro has either first or last name (non-"N/A"), use Macro for BOTH fields.- WHEN NULLIF(mui_first_name, 'N/A') IS NOT NULL- OR NULLIF(mui_last_name, 'N/A') IS NOT NULL- THEN NULLIF(mui_first_name, 'N/A')- -- Otherwise fall back to email contact name parsing.- ELSE SPLIT_PART(contact_name, ' ', 1)- END as "first_name",- CASE- -- If Macro has either first or last name (non-"N/A"), use Macro for BOTH fields.- WHEN NULLIF(mui_first_name, 'N/A') IS NOT NULL- OR NULLIF(mui_last_name, 'N/A') IS NOT NULL- THEN NULLIF(mui_last_name, 'N/A')- -- Otherwise fall back to email contact name parsing.- ELSE CASE- WHEN POSITION(' ' IN contact_name) > 0- THEN NULLIF(TRIM(SUBSTRING(contact_name FROM POSITION(' ' IN contact_name) + 1)), '')+ SELECT+ req.id as "user_profile_id!",+ CASE+ WHEN NULLIF(mui.first_name, 'N/A') IS NOT NULL+ OR NULLIF(mui.last_name, 'N/A') IS NOT NULL+ THEN NULLIF(mui.first_name, 'N/A')+ ELSE SPLIT_PART(contact.name, ' ', 1)+ END as "first_name",+ CASE+ WHEN NULLIF(mui.first_name, 'N/A') IS NOT NULL+ OR NULLIF(mui.last_name, 'N/A') IS NOT NULL+ THEN NULLIF(mui.last_name, 'N/A')+ ELSE CASE+ WHEN POSITION(' ' IN contact.name) > 0+ THEN NULLIF(TRIM(SUBSTRING(contact.name FROM POSITION(' ' IN contact.name) + 1)), '')ELSE NULLEND- END as "last_name"- FROM (- -- Users in User table with contact fallback (in case user hasn't set name in Macro)- SELECT- uf.user_profile_id,- uf.mui_first_name,- uf.mui_last_name,- cr.contact_name- FROM users_found uf- LEFT JOIN contacts_requested cr ON cr.user_profile_id = uf.user_profile_id-- UNION ALL-- -- People only in email_contacts (haven't joined Macro yet)- SELECT- cr.user_profile_id,- NULL::text as mui_first_name,- NULL::text as mui_last_name,- cr.contact_name- FROM contacts_requested cr- WHERE NOT EXISTS (- SELECT 1- FROM users_found uf- WHERE uf.user_profile_id = cr.user_profile_id- )- ) combined- ORDER BY user_profile_id+ END as "last_name"+ FROM requested_ids req+ LEFT JOIN "User" u ON u.id = req.id+ LEFT JOIN macro_user_info mui ON mui.macro_user_id = u.macro_user_id+ LEFT JOIN LATERAL (+ SELECT ec.name+ FROM email_links li+ JOIN email_contacts ec+ ON ec.link_id = li.id+ AND ec.email_address = REPLACE(req.id, 'macro|', '')+ AND ec.name IS NOT NULL+ WHERE li.macro_id = $1+ AND NULLIF(mui.first_name, 'N/A') IS NULL+ AND NULLIF(mui.last_name, 'N/A') IS NULL+ LIMIT 1+ ) contact ON TRUE+ WHERE u.id IS NOT NULL OR contact.name IS NOT NULL"#,macro_user_id,&user_profile_ids_str as &[&str]diff --git a/crates/macro_db_client/src/user/get_user_name/tests.rs b/crates/macro_db_client/src/user/get_user_name/tests.rsindex bef1f34816..bfae941ac1 100644--- a/crates/macro_db_client/src/user/get_user_name/tests.rs+++ b/crates/macro_db_client/src/user/get_user_name/tests.rs@@ -125,6 +125,23 @@ async fn test_get_user_names_with_email_not_found(pool: Pool<Postgres>) -> anyhoOk(())}+#[sqlx::test(fixtures(path = "../../../fixtures", scripts("user_names_with_email")))]+async fn deduplicates_requested_user_ids(pool: Pool<Postgres>) -> anyhow::Result<()> {+ let user_profile_ids = parse_user_ids(vec![+ "macro|[email protected]",+ "macro|[email protected]",+ ])?;++ let names =+ get_user_names_with_email(&pool, "macro|[email protected]", user_profile_ids)+ .await?;++ assert_eq!(names.len(), 1);+ assert_eq!(names[0].id, "macro|[email protected]");++ Ok(())+}+/// only fallback to using email contact names if the user has neither first nor last name set/// in macro. don't use the email contact last name with the macro first name if exists.#[sqlx::test(fixtures(path = "../../../fixtures", scripts("user_names_with_email")))]diff --git a/services/authentication_service/src/api/mod.rs b/services/authentication_service/src/api/mod.rsindex 97a7f5f3c5..8b299c64f1 100644--- a/services/authentication_service/src/api/mod.rs+++ b/services/authentication_service/src/api/mod.rs@@ -8,6 +8,7 @@ use native_app_service::inbound::RouterState;use std::net::SocketAddr;use std::time::Duration;use tower::ServiceBuilder;+use tower_http::compression::CompressionLayer;use utoipa::OpenApi;use utoipa_swagger_ui::SwaggerUi;@@ -53,7 +54,8 @@ pub async fn setup_and_serve(state: ApiContext, port: usize) -> anyhow::Result<(// The health router is attached here so we don't attach the logging middleware to it.merge(health::router()).layer(cors)- .merge(SwaggerUi::new("/docs").url("/api-doc/openapi.json", swagger::ApiDoc::openapi()));+ .merge(SwaggerUi::new("/docs").url("/api-doc/openapi.json", swagger::ApiDoc::openapi()))+ .layer(CompressionLayer::new());let listener = tokio::net::TcpListener::bind(format!("0.0.0.0:{}", port)).await