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(auth-service): Only fallback to contact name for empty Macro name (#674)

Evan Hutnika1b9b678c1ed
diff --git a/rust/cloud-storage/macro_db_client/.sqlx/query-18fb7105aab4316c3b3b13531513d7885f651652b20ad9b4234b8935bed6958a.json b/rust/cloud-storage/macro_db_client/.sqlx/query-18fb7105aab4316c3b3b13531513d7885f651652b20ad9b4234b8935bed6958a.json
deleted file mode 100644
index eea1d123d7..0000000000
--- a/rust/cloud-storage/macro_db_client/.sqlx/query-18fb7105aab4316c3b3b13531513d7885f651652b20ad9b4234b8935bed6958a.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 -- user's name in macro takes precedence over email contact name\n COALESCE(\n NULLIF(mui_first_name, 'N/A'),\n -- first word in email contact name is approximation of first name\n SPLIT_PART(contact_name, ' ', 1)\n ) as \"first_name\",\n -- user's name in macro takes precedence over email contact name\n COALESCE(\n NULLIF(mui_last_name, 'N/A'),\n CASE\n -- if there is a space in the email contact name, the rest is the last name\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 ) 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": "18fb7105aab4316c3b3b13531513d7885f651652b20ad9b4234b8935bed6958a"
-}
diff --git a/rust/cloud-storage/macro_db_client/.sqlx/query-cd2334cf312dee1e9b230cce0d7b26999ba3ac23f89c8882c144d4c9a657e235.json b/rust/cloud-storage/macro_db_client/.sqlx/query-cd2334cf312dee1e9b230cce0d7b26999ba3ac23f89c8882c144d4c9a657e235.json
new file mode 100644
index 0000000000..945fd5a6db
--- /dev/null
+++ b/rust/cloud-storage/macro_db_client/.sqlx/query-cd2334cf312dee1e9b230cce0d7b26999ba3ac23f89c8882c144d4c9a657e235.json
@@ -0,0 +1,35 @@
+{
+ "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/rust/cloud-storage/macro_db_client/fixtures/user_names_with_email.sql b/rust/cloud-storage/macro_db_client/fixtures/user_names_with_email.sql
index 15401b6918..ae16dcd82d 100644
--- a/rust/cloud-storage/macro_db_client/fixtures/user_names_with_email.sql
+++ b/rust/cloud-storage/macro_db_client/fixtures/user_names_with_email.sql
@@ -5,14 +5,18 @@ INSERT INTO macro_user (id, username, email, stripe_customer_id)
VALUES
('11111111-1111-1111-1111-111111111111', 'test_user_1', '[email protected]', 'cus_test1'),
('22222222-2222-2222-2222-222222222222', 'test_user_2', '[email protected]', 'cus_test2'),
- ('33333333-3333-3333-3333-333333333333', 'test_user_3', '[email protected]', 'cus_test3');
+ ('33333333-3333-3333-3333-333333333333', 'test_user_3', '[email protected]', 'cus_test3'),
+ ('44444444-4444-4444-4444-444444444444', 'test_user_4', '[email protected]', 'cus_test4'),
+ ('55555555-5555-5555-5555-555555555555', 'test_user_5', '[email protected]', 'cus_test5');
-- Create macro user info with names
INSERT INTO macro_user_info (macro_user_id, first_name, last_name)
VALUES
('11111111-1111-1111-1111-111111111111', 'JohnMacroContact', 'DoeMacroContact'),
('22222222-2222-2222-2222-222222222222', 'JaneMacroContact', 'SmithMacroContact'),
- ('33333333-3333-3333-3333-333333333333', 'N/A', 'N/A'); -- User with N/A name to test fallback
+ ('33333333-3333-3333-3333-333333333333', 'N/A', 'N/A'), -- User with N/A name to test fallback
+ ('44444444-4444-4444-4444-444444444444', 'OnlyFirstMacro', 'N/A'), -- Either populated => use Macro for BOTH; last should be NULL
+ ('55555555-5555-5555-5555-555555555555', 'N/A', 'OnlyLastMacro'); -- Either populated => use Macro for BOTH; first should be NULL
-- Create User (profile) entries
INSERT INTO "User" (id, email, macro_user_id, "organizationId", "hasChromeExt", "tutorialComplete", "hasOnboardingDocuments")
@@ -20,6 +24,8 @@ VALUES
('macro|[email protected]', '[email protected]', '11111111-1111-1111-1111-111111111111', NULL, false, false, false),
('macro|[email protected]', '[email protected]', '22222222-2222-2222-2222-222222222222', NULL, false, false, false),
('macro|[email protected]', '[email protected]', '33333333-3333-3333-3333-333333333333', NULL, false, false, false),
+ ('macro|[email protected]', '[email protected]', '44444444-4444-4444-4444-444444444444', NULL, false, false, false),
+ ('macro|[email protected]', '[email protected]', '55555555-5555-5555-5555-555555555555', NULL, false, false, false),
('macro|[email protected]', '[email protected]', NULL, NULL, false, false, false);
-- Create email link for macro user 1 (for email contacts lookup)
@@ -31,4 +37,6 @@ VALUES
INSERT INTO email_contacts (id, link_id, email_address, name)
VALUES
('cccccccc-cccc-cccc-cccc-cccccccccccc', 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa', '[email protected]', 'BobEmailContact JohnsonEmailContact'),
- ('dddddddd-dddd-dddd-dddd-dddddddddddd', 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa', '[email protected]', 'AliceEmailContact WilliamsEmailContact');
\ No newline at end of file
+ ('dddddddd-dddd-dddd-dddd-dddddddddddd', 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa', '[email protected]', 'AliceEmailContact WilliamsEmailContact'),
+ ('eeeeeeee-eeee-eeee-eeee-eeeeeeeeeeee', 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa', '[email protected]', 'FallbackFirst FallbackLast'),
+ ('ffffffff-ffff-ffff-ffff-ffffffffffff', 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa', '[email protected]', 'FallbackFirst2 FallbackLast2');
\ No newline at end of file
diff --git a/rust/cloud-storage/macro_db_client/src/user/get_user_name.rs b/rust/cloud-storage/macro_db_client/src/user/get_user_name.rs
index 537d97a719..e0acb90961 100644
--- a/rust/cloud-storage/macro_db_client/src/user/get_user_name.rs
+++ b/rust/cloud-storage/macro_db_client/src/user/get_user_name.rs
@@ -95,22 +95,26 @@ pub async fn get_user_names_with_email(
)
SELECT DISTINCT ON (user_profile_id)
user_profile_id as "user_profile_id!",
- -- user's name in macro takes precedence over email contact name
- COALESCE(
- NULLIF(mui_first_name, 'N/A'),
- -- first word in email contact name is approximation of first name
- SPLIT_PART(contact_name, ' ', 1)
- ) as "first_name",
- -- user's name in macro takes precedence over email contact name
- COALESCE(
- NULLIF(mui_last_name, 'N/A'),
CASE
- -- if there is a space in the email contact name, the rest is the last name
+ -- 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)), '')
ELSE NULL
END
- ) as "last_name"
+ END as "last_name"
FROM (
-- Users in User table with contact fallback (in case user hasn't set name in Macro)
SELECT
diff --git a/rust/cloud-storage/macro_db_client/src/user/get_user_name/tests.rs b/rust/cloud-storage/macro_db_client/src/user/get_user_name/tests.rs
index 0b68674701..8120adc7d4 100644
--- a/rust/cloud-storage/macro_db_client/src/user/get_user_name/tests.rs
+++ b/rust/cloud-storage/macro_db_client/src/user/get_user_name/tests.rs
@@ -123,3 +123,36 @@ async fn test_get_user_names_with_email_not_found(pool: Pool<Postgres>) -> anyho
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")))]
+async fn uses_macro_names_if_either_first_or_last_present(
+ pool: Pool<Postgres>,
+) -> anyhow::Result<()> {
+ let user_profile_ids = parse_user_ids(vec![
+ ])?;
+
+ let mut names =
+ get_user_names_with_email(&pool, "macro|[email protected]", user_profile_ids)
+ .await?;
+ names.sort_by(|a, b| a.id.cmp(&b.id));
+
+ let u4 = names
+ .iter()
+ .find(|n| n.id == "macro|[email protected]")
+ .expect("user_profile_4 should be returned");
+ assert_eq!(u4.first_name.as_deref(), Some("OnlyFirstMacro"));
+ assert_eq!(u4.last_name.as_deref(), None);
+
+ let u5 = names
+ .iter()
+ .find(|n| n.id == "macro|[email protected]")
+ .expect("user_profile_5 should be returned");
+ assert_eq!(u5.first_name.as_deref(), None);
+ assert_eq!(u5.last_name.as_deref(), Some("OnlyLastMacro"));
+
+ Ok(())
+}