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.

feat(access): update useritemaccess to track the team id association for sharing (#2078)

Will Hutchinsond38aa259355a
diff --git a/rust/cloud-storage/documents/.sqlx/query-03a323aefdba22727c6789b28d90d7200e795a4591812dab4c4e7718c5512055.json b/rust/cloud-storage/documents/.sqlx/query-03a323aefdba22727c6789b28d90d7200e795a4591812dab4c4e7718c5512055.json
new file mode 100644
index 0000000000..83caec4173
--- /dev/null
+++ b/rust/cloud-storage/documents/.sqlx/query-03a323aefdba22727c6789b28d90d7200e795a4591812dab4c4e7718c5512055.json
@@ -0,0 +1,23 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n WITH RECURSIVE project_hierarchy AS (\n SELECT p.id as project_id\n FROM email_threads t\n JOIN \"Project\" p ON t.project_id = p.id AND p.\"deletedAt\" IS NULL\n WHERE t.id = $1\n UNION ALL\n SELECT parent.id as project_id\n FROM project_hierarchy ph\n JOIN \"Project\" parent ON parent.id = (\n SELECT \"parentId\" FROM \"Project\" WHERE id = ph.project_id AND \"parentId\" IS NOT NULL AND \"deletedAt\" IS NULL\n )\n )\n SELECT DISTINCT user_id FROM \"UserItemAccess\"\n WHERE item_id IN (\n SELECT $2\n UNION\n SELECT project_id FROM project_hierarchy\n )\n ",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "user_id",
+ "type_info": "Text"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Text"
+ ]
+ },
+ "nullable": [
+ false
+ ]
+ },
+ "hash": "03a323aefdba22727c6789b28d90d7200e795a4591812dab4c4e7718c5512055"
+}
diff --git a/rust/cloud-storage/documents/.sqlx/query-12bbf364a130b8f3af7e7bc7f8c64eb026124d2a66d2735b95ef387433cecd03.json b/rust/cloud-storage/documents/.sqlx/query-12bbf364a130b8f3af7e7bc7f8c64eb026124d2a66d2735b95ef387433cecd03.json
new file mode 100644
index 0000000000..8bcde4e677
--- /dev/null
+++ b/rust/cloud-storage/documents/.sqlx/query-12bbf364a130b8f3af7e7bc7f8c64eb026124d2a66d2735b95ef387433cecd03.json
@@ -0,0 +1,22 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n SELECT team_id\n FROM team_user\n WHERE user_id = $1\n LIMIT 1\n ",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "team_id",
+ "type_info": "Uuid"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Text"
+ ]
+ },
+ "nullable": [
+ false
+ ]
+ },
+ "hash": "12bbf364a130b8f3af7e7bc7f8c64eb026124d2a66d2735b95ef387433cecd03"
+}
diff --git a/rust/cloud-storage/documents/.sqlx/query-37b5168f739c490a3373c65eab35f247a34af0a8e659cbad61cb99e3094e5a60.json b/rust/cloud-storage/documents/.sqlx/query-37b5168f739c490a3373c65eab35f247a34af0a8e659cbad61cb99e3094e5a60.json
new file mode 100644
index 0000000000..44d3a67e72
--- /dev/null
+++ b/rust/cloud-storage/documents/.sqlx/query-37b5168f739c490a3373c65eab35f247a34af0a8e659cbad61cb99e3094e5a60.json
@@ -0,0 +1,23 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n SELECT EXISTS (\n SELECT 1\n FROM public.email_threads t\n JOIN public.email_links l ON l.id = t.link_id\n WHERE t.id = $1::uuid\n AND l.macro_id = $2\n ) AS \"exists!\"\n ",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "exists!",
+ "type_info": "Bool"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Text"
+ ]
+ },
+ "nullable": [
+ null
+ ]
+ },
+ "hash": "37b5168f739c490a3373c65eab35f247a34af0a8e659cbad61cb99e3094e5a60"
+}
diff --git a/rust/cloud-storage/documents/.sqlx/query-3a7be99469ed150e0506efa786b672198a6d3419c6bd5548418520cd3e6a8e95.json b/rust/cloud-storage/documents/.sqlx/query-3a7be99469ed150e0506efa786b672198a6d3419c6bd5548418520cd3e6a8e95.json
new file mode 100644
index 0000000000..60bfee0647
--- /dev/null
+++ b/rust/cloud-storage/documents/.sqlx/query-3a7be99469ed150e0506efa786b672198a6d3419c6bd5548418520cd3e6a8e95.json
@@ -0,0 +1,23 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n WITH RECURSIVE project_hierarchy AS (\n SELECT p.id as project_id\n FROM \"Document\" d\n JOIN \"Project\" p ON d.\"projectId\" = p.id AND p.\"deletedAt\" IS NULL\n WHERE d.id = $1 AND d.\"deletedAt\" IS NULL\n UNION ALL\n SELECT parent.id as project_id\n FROM project_hierarchy ph\n JOIN \"Project\" parent ON parent.id = (\n SELECT \"parentId\" FROM \"Project\" WHERE id = ph.project_id AND \"parentId\" IS NOT NULL AND \"deletedAt\" IS NULL\n )\n )\n SELECT access_level FROM (\n -- Source 1: Cast the AccessLevel enum to text.\n SELECT access_level::text FROM \"UserItemAccess\"\n WHERE user_id = $2 AND item_id IN (\n SELECT $1\n UNION\n SELECT project_id FROM project_hierarchy\n )\n UNION ALL\n -- Source 2: Select the publicAccessLevel (which is already text).\n SELECT \"publicAccessLevel\" as access_level\n FROM \"SharePermission\"\n WHERE \"isPublic\" = true AND \"publicAccessLevel\" IS NOT NULL AND id IN (\n SELECT \"sharePermissionId\" FROM \"DocumentPermission\" WHERE \"documentId\" = $1\n UNION\n SELECT \"sharePermissionId\" FROM \"ProjectPermission\" WHERE \"projectId\" IN (SELECT project_id FROM project_hierarchy)\n )\n ) as all_levels\n ",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "access_level",
+ "type_info": "Text"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Text",
+ "Text"
+ ]
+ },
+ "nullable": [
+ null
+ ]
+ },
+ "hash": "3a7be99469ed150e0506efa786b672198a6d3419c6bd5548418520cd3e6a8e95"
+}
diff --git a/rust/cloud-storage/documents/.sqlx/query-4afa79ca95bdbcebe9d3de7ebb204636419e6cf89e650a8716c40ab2e588aa5b.json b/rust/cloud-storage/documents/.sqlx/query-4afa79ca95bdbcebe9d3de7ebb204636419e6cf89e650a8716c40ab2e588aa5b.json
new file mode 100644
index 0000000000..58618b2697
--- /dev/null
+++ b/rust/cloud-storage/documents/.sqlx/query-4afa79ca95bdbcebe9d3de7ebb204636419e6cf89e650a8716c40ab2e588aa5b.json
@@ -0,0 +1,23 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n -- CTE to recursively find all parent projects, starting from the given project.\n WITH RECURSIVE project_hierarchy AS (\n -- Base case: Start with the project ID provided.\n SELECT id as project_id\n FROM \"Project\"\n WHERE id = $1 AND \"deletedAt\" IS NULL\n UNION ALL\n -- Recursive case: Find the parent of the project from the previous step.\n SELECT parent.id as project_id\n FROM project_hierarchy ph\n JOIN \"Project\" parent ON parent.id = (\n SELECT \"parentId\" FROM \"Project\" WHERE id = ph.project_id AND \"parentId\" IS NOT NULL AND \"deletedAt\" IS NULL\n )\n )\n -- The subquery now gathers all levels as plain text.\n SELECT access_level FROM (\n -- Source 1: Cast the AccessLevel enum to text.\n SELECT access_level::text FROM \"UserItemAccess\"\n WHERE user_id = $2 AND item_id IN (\n -- The hierarchy CTE includes the starting project, so this is all we need.\n SELECT project_id FROM project_hierarchy\n )\n UNION ALL\n -- Source 2: Select the publicAccessLevel (which is already text).\n SELECT \"publicAccessLevel\" as access_level\n FROM \"SharePermission\"\n WHERE \"isPublic\" = true AND \"publicAccessLevel\" IS NOT NULL AND id IN (\n -- We only need to check ProjectPermission for the items in the hierarchy.\n SELECT \"sharePermissionId\" FROM \"ProjectPermission\"\n WHERE \"projectId\" IN (SELECT project_id FROM project_hierarchy)\n )\n ) as all_levels\n ",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "access_level",
+ "type_info": "Text"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Text",
+ "Text"
+ ]
+ },
+ "nullable": [
+ null
+ ]
+ },
+ "hash": "4afa79ca95bdbcebe9d3de7ebb204636419e6cf89e650a8716c40ab2e588aa5b"
+}
diff --git a/rust/cloud-storage/documents/.sqlx/query-4c695b011d49ba7d5fce4b309875ed952dc386ac760444d7883c5a76718f15be.json b/rust/cloud-storage/documents/.sqlx/query-4c695b011d49ba7d5fce4b309875ed952dc386ac760444d7883c5a76718f15be.json
new file mode 100644
index 0000000000..524cc25ec7
--- /dev/null
+++ b/rust/cloud-storage/documents/.sqlx/query-4c695b011d49ba7d5fce4b309875ed952dc386ac760444d7883c5a76718f15be.json
@@ -0,0 +1,23 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n WITH RECURSIVE project_hierarchy AS (\n SELECT p.id as project_id\n FROM \"Chat\" c\n JOIN \"Project\" p ON c.\"projectId\" = p.id AND p.\"deletedAt\" IS NULL\n WHERE c.id = $1 AND c.\"deletedAt\" IS NULL\n UNION ALL\n SELECT parent.id as project_id\n FROM project_hierarchy ph\n JOIN \"Project\" parent ON parent.id = (\n SELECT \"parentId\" FROM \"Project\" WHERE id = ph.project_id AND \"parentId\" IS NOT NULL AND \"deletedAt\" IS NULL\n )\n )\n SELECT access_level FROM (\n -- Source 1: Cast the AccessLevel enum to text.\n SELECT access_level::text FROM \"UserItemAccess\"\n WHERE user_id = $2 AND item_id IN (\n SELECT $1 -- The chat itself\n UNION\n SELECT project_id FROM project_hierarchy -- All parent projects\n )\n UNION ALL\n -- Source 2: Select the publicAccessLevel (which is already text).\n SELECT \"publicAccessLevel\" as access_level\n FROM \"SharePermission\"\n WHERE \"isPublic\" = true AND \"publicAccessLevel\" IS NOT NULL AND id IN (\n SELECT \"sharePermissionId\" FROM \"ChatPermission\" WHERE \"chatId\" = $1\n UNION\n SELECT \"sharePermissionId\" FROM \"ProjectPermission\" WHERE \"projectId\" IN (SELECT project_id FROM project_hierarchy)\n )\n ) as all_levels\n ",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "access_level",
+ "type_info": "Text"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Text",
+ "Text"
+ ]
+ },
+ "nullable": [
+ null
+ ]
+ },
+ "hash": "4c695b011d49ba7d5fce4b309875ed952dc386ac760444d7883c5a76718f15be"
+}
diff --git a/rust/cloud-storage/documents/.sqlx/query-54e3755d63a676a7fa70da77321e618032f0130c73eb6b0ae985f07c5c6c448b.json b/rust/cloud-storage/documents/.sqlx/query-54e3755d63a676a7fa70da77321e618032f0130c73eb6b0ae985f07c5c6c448b.json
new file mode 100644
index 0000000000..03a8dbf6bb
--- /dev/null
+++ b/rust/cloud-storage/documents/.sqlx/query-54e3755d63a676a7fa70da77321e618032f0130c73eb6b0ae985f07c5c6c448b.json
@@ -0,0 +1,22 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n WITH RECURSIVE project_hierarchy AS (\n SELECT p.id as project_id\n FROM \"Chat\" c\n JOIN \"Project\" p ON c.\"projectId\" = p.id AND p.\"deletedAt\" IS NULL\n WHERE c.id = $1 AND c.\"deletedAt\" IS NULL\n UNION ALL\n SELECT parent.id as project_id\n FROM project_hierarchy ph\n JOIN \"Project\" parent ON parent.id = (\n SELECT \"parentId\" FROM \"Project\" WHERE id = ph.project_id AND \"parentId\" IS NOT NULL AND \"deletedAt\" IS NULL\n )\n )\n SELECT DISTINCT user_id FROM \"UserItemAccess\"\n WHERE item_id IN (\n SELECT $1\n UNION\n SELECT project_id FROM project_hierarchy\n )\n ",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "user_id",
+ "type_info": "Text"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Text"
+ ]
+ },
+ "nullable": [
+ false
+ ]
+ },
+ "hash": "54e3755d63a676a7fa70da77321e618032f0130c73eb6b0ae985f07c5c6c448b"
+}
diff --git a/rust/cloud-storage/documents/.sqlx/query-7617f808df94ffe77c8195a911ee6b230a21fe9935e099d1310d244248ca253a.json b/rust/cloud-storage/documents/.sqlx/query-7617f808df94ffe77c8195a911ee6b230a21fe9935e099d1310d244248ca253a.json
new file mode 100644
index 0000000000..655495fb29
--- /dev/null
+++ b/rust/cloud-storage/documents/.sqlx/query-7617f808df94ffe77c8195a911ee6b230a21fe9935e099d1310d244248ca253a.json
@@ -0,0 +1,23 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n SELECT c.id\n FROM comms_channels c\n INNER JOIN comms_channel_participants cp ON cp.channel_id = c.id\n WHERE cp.user_id = $1 AND cp.left_at IS NULL\n AND c.id = ANY($2::uuid[])\n ",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "id",
+ "type_info": "Uuid"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Text",
+ "UuidArray"
+ ]
+ },
+ "nullable": [
+ false
+ ]
+ },
+ "hash": "7617f808df94ffe77c8195a911ee6b230a21fe9935e099d1310d244248ca253a"
+}
diff --git a/rust/cloud-storage/documents/.sqlx/query-77bffdfa5963168a6eb6c908df6d5ad5f1733890733ae55d187f2bd24cc41449.json b/rust/cloud-storage/documents/.sqlx/query-77bffdfa5963168a6eb6c908df6d5ad5f1733890733ae55d187f2bd24cc41449.json
deleted file mode 100644
index eb7ed37fe4..0000000000
--- a/rust/cloud-storage/documents/.sqlx/query-77bffdfa5963168a6eb6c908df6d5ad5f1733890733ae55d187f2bd24cc41449.json
+++ /dev/null
@@ -1,22 +0,0 @@
-{
- "db_name": "PostgreSQL",
- "query": "\n SELECT tu2.user_id\n FROM team_user tu1\n JOIN team_user tu2 ON tu1.team_id = tu2.team_id\n WHERE tu1.user_id = $1\n ",
- "describe": {
- "columns": [
- {
- "ordinal": 0,
- "name": "user_id",
- "type_info": "Text"
- }
- ],
- "parameters": {
- "Left": [
- "Text"
- ]
- },
- "nullable": [
- false
- ]
- },
- "hash": "77bffdfa5963168a6eb6c908df6d5ad5f1733890733ae55d187f2bd24cc41449"
-}
diff --git a/rust/cloud-storage/documents/.sqlx/query-79d3bc72e6f424765706f7deea426379ae49cbd70dd122daedd95343618b9ac6.json b/rust/cloud-storage/documents/.sqlx/query-79d3bc72e6f424765706f7deea426379ae49cbd70dd122daedd95343618b9ac6.json
new file mode 100644
index 0000000000..2bde15938f
--- /dev/null
+++ b/rust/cloud-storage/documents/.sqlx/query-79d3bc72e6f424765706f7deea426379ae49cbd70dd122daedd95343618b9ac6.json
@@ -0,0 +1,22 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n WITH RECURSIVE project_hierarchy AS (\n SELECT id as project_id\n FROM \"Project\"\n WHERE id = $1 AND \"deletedAt\" IS NULL\n UNION ALL\n SELECT parent.id as project_id\n FROM project_hierarchy ph\n JOIN \"Project\" parent ON parent.id = (\n SELECT \"parentId\" FROM \"Project\" WHERE id = ph.project_id AND \"parentId\" IS NOT NULL AND \"deletedAt\" IS NULL\n )\n )\n SELECT DISTINCT user_id FROM \"UserItemAccess\"\n WHERE item_id IN (\n SELECT project_id FROM project_hierarchy\n )\n ",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "user_id",
+ "type_info": "Text"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Text"
+ ]
+ },
+ "nullable": [
+ false
+ ]
+ },
+ "hash": "79d3bc72e6f424765706f7deea426379ae49cbd70dd122daedd95343618b9ac6"
+}
diff --git a/rust/cloud-storage/documents/.sqlx/query-86844f724bc063be712fbf68d1a21e252b27aba90790bc14e979f07bf6dedf92.json b/rust/cloud-storage/documents/.sqlx/query-86844f724bc063be712fbf68d1a21e252b27aba90790bc14e979f07bf6dedf92.json
new file mode 100644
index 0000000000..3023106dc8
--- /dev/null
+++ b/rust/cloud-storage/documents/.sqlx/query-86844f724bc063be712fbf68d1a21e252b27aba90790bc14e979f07bf6dedf92.json
@@ -0,0 +1,22 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n SELECT user_id\n FROM team_user\n WHERE team_id = $1\n ",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "user_id",
+ "type_info": "Text"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid"
+ ]
+ },
+ "nullable": [
+ false
+ ]
+ },
+ "hash": "86844f724bc063be712fbf68d1a21e252b27aba90790bc14e979f07bf6dedf92"
+}
diff --git a/rust/cloud-storage/documents/.sqlx/query-96d853b887f2e6d7aac6b15ddd2b71a8d8900d2ef30047da7ca1a5940503b6d0.json b/rust/cloud-storage/documents/.sqlx/query-96d853b887f2e6d7aac6b15ddd2b71a8d8900d2ef30047da7ca1a5940503b6d0.json
new file mode 100644
index 0000000000..e42c9f2009
--- /dev/null
+++ b/rust/cloud-storage/documents/.sqlx/query-96d853b887f2e6d7aac6b15ddd2b71a8d8900d2ef30047da7ca1a5940503b6d0.json
@@ -0,0 +1,22 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n WITH RECURSIVE project_hierarchy AS (\n SELECT p.id as project_id\n FROM \"Document\" d\n JOIN \"Project\" p ON d.\"projectId\" = p.id AND p.\"deletedAt\" IS NULL\n WHERE d.id = $1 AND d.\"deletedAt\" IS NULL\n UNION ALL\n SELECT parent.id as project_id\n FROM project_hierarchy ph\n JOIN \"Project\" parent ON parent.id = (\n SELECT \"parentId\" FROM \"Project\" WHERE id = ph.project_id AND \"parentId\" IS NOT NULL AND \"deletedAt\" IS NULL\n )\n )\n SELECT DISTINCT user_id FROM \"UserItemAccess\"\n WHERE item_id IN (\n SELECT $1\n UNION\n SELECT project_id FROM project_hierarchy\n )\n ",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "user_id",
+ "type_info": "Text"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Text"
+ ]
+ },
+ "nullable": [
+ false
+ ]
+ },
+ "hash": "96d853b887f2e6d7aac6b15ddd2b71a8d8900d2ef30047da7ca1a5940503b6d0"
+}
diff --git a/rust/cloud-storage/documents/.sqlx/query-9f96b785a258c4286db939e1849991361c31618d132a49bf3b7d04ae729ac0dc.json b/rust/cloud-storage/documents/.sqlx/query-9f96b785a258c4286db939e1849991361c31618d132a49bf3b7d04ae729ac0dc.json
deleted file mode 100644
index 224106f514..0000000000
--- a/rust/cloud-storage/documents/.sqlx/query-9f96b785a258c4286db939e1849991361c31618d132a49bf3b7d04ae729ac0dc.json
+++ /dev/null
@@ -1,16 +0,0 @@
-{
- "db_name": "PostgreSQL",
- "query": "\n INSERT INTO \"UserItemAccess\" (\n \"id\", \"user_id\", \"item_id\", \"item_type\", \"access_level\",\n \"granted_from_channel_id\", \"created_at\", \"updated_at\"\n )\n SELECT\n u.id,\n u.user_id,\n $1 AS item_id,\n 'document' AS item_type,\n 'comment' AS access_level,\n NULL AS granted_from_channel_id,\n NOW() AS created_at,\n NOW() AS updated_at\n FROM UNNEST($2::uuid[], $3::text[]) AS u(id, user_id)\n WHERE NOT EXISTS (\n SELECT 1 FROM \"UserItemAccess\" uia\n WHERE uia.\"user_id\" = u.user_id\n AND uia.\"item_id\" = $1\n AND uia.\"item_type\" = 'document'\n AND uia.\"granted_from_channel_id\" IS NULL\n )\n ",
- "describe": {
- "columns": [],
- "parameters": {
- "Left": [
- "Text",
- "UuidArray",
- "TextArray"
- ]
- },
- "nullable": []
- },
- "hash": "9f96b785a258c4286db939e1849991361c31618d132a49bf3b7d04ae729ac0dc"
-}
diff --git a/rust/cloud-storage/documents/.sqlx/query-d536db3ccfb32aee9a48227e2b15d2404023ea24b62c159466c95899d76e29ed.json b/rust/cloud-storage/documents/.sqlx/query-d536db3ccfb32aee9a48227e2b15d2404023ea24b62c159466c95899d76e29ed.json
new file mode 100644
index 0000000000..6ef8856744
--- /dev/null
+++ b/rust/cloud-storage/documents/.sqlx/query-d536db3ccfb32aee9a48227e2b15d2404023ea24b62c159466c95899d76e29ed.json
@@ -0,0 +1,24 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n WITH RECURSIVE project_hierarchy AS (\n SELECT p.id as project_id\n FROM email_threads t\n JOIN \"Project\" p ON t.project_id = p.id AND p.\"deletedAt\" IS NULL\n WHERE t.id = $1\n UNION ALL\n SELECT parent.id as project_id\n FROM project_hierarchy ph\n JOIN \"Project\" parent ON parent.id = (\n SELECT \"parentId\" FROM \"Project\" WHERE id = ph.project_id AND \"parentId\" IS NOT NULL AND \"deletedAt\" IS NULL\n )\n )\n SELECT access_level FROM (\n -- Source 1: Cast the AccessLevel enum to text.\n SELECT access_level::text FROM \"UserItemAccess\"\n WHERE user_id = $3 AND item_id IN (\n SELECT $2 -- The thread ID as text\n UNION\n SELECT project_id FROM project_hierarchy -- All parent projects\n )\n UNION ALL\n -- Source 2: Select the publicAccessLevel (which is already text).\n SELECT \"publicAccessLevel\" as access_level\n FROM \"SharePermission\"\n WHERE \"isPublic\" = true AND \"publicAccessLevel\" IS NOT NULL AND id IN (\n SELECT \"sharePermissionId\" FROM \"EmailThreadPermission\" WHERE \"threadId\" = $2\n UNION\n SELECT \"sharePermissionId\" FROM \"ProjectPermission\" WHERE \"projectId\" IN (SELECT project_id FROM project_hierarchy)\n )\n ) as all_levels\n ",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "access_level",
+ "type_info": "Text"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Text",
+ "Text"
+ ]
+ },
+ "nullable": [
+ null
+ ]
+ },
+ "hash": "d536db3ccfb32aee9a48227e2b15d2404023ea24b62c159466c95899d76e29ed"
+}
diff --git a/rust/cloud-storage/documents/.sqlx/query-db69e5ddf8fbee090a57d3cfd0b2910bdf3bcaa2de68e51d43425b56b68f1e8b.json b/rust/cloud-storage/documents/.sqlx/query-db69e5ddf8fbee090a57d3cfd0b2910bdf3bcaa2de68e51d43425b56b68f1e8b.json
new file mode 100644
index 0000000000..fdd501e89e
--- /dev/null
+++ b/rust/cloud-storage/documents/.sqlx/query-db69e5ddf8fbee090a57d3cfd0b2910bdf3bcaa2de68e51d43425b56b68f1e8b.json
@@ -0,0 +1,17 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n INSERT INTO \"UserItemAccess\" (\n \"id\", \"user_id\", \"item_id\", \"item_type\", \"access_level\",\n \"granted_from_channel_id\", \"granted_from_team_id\",\n \"created_at\", \"updated_at\"\n )\n SELECT\n u.id,\n u.user_id,\n $1 AS item_id,\n 'document' AS item_type,\n 'comment' AS access_level,\n NULL AS granted_from_channel_id,\n $4 AS granted_from_team_id,\n NOW() AS created_at,\n NOW() AS updated_at\n FROM UNNEST($2::uuid[], $3::text[]) AS u(id, user_id)\n WHERE NOT EXISTS (\n SELECT 1 FROM \"UserItemAccess\" uia\n WHERE uia.\"user_id\" = u.user_id\n AND uia.\"item_id\" = $1\n AND uia.\"item_type\" = 'document'\n AND uia.\"granted_from_channel_id\" IS NULL\n )\n ",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Text",
+ "UuidArray",
+ "TextArray",
+ "Uuid"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "db69e5ddf8fbee090a57d3cfd0b2910bdf3bcaa2de68e51d43425b56b68f1e8b"
+}
diff --git a/rust/cloud-storage/documents/src/outbound/pg_document_repo/share.rs b/rust/cloud-storage/documents/src/outbound/pg_document_repo/share.rs
index ee2049a5d9..f60c7251eb 100644
--- a/rust/cloud-storage/documents/src/outbound/pg_document_repo/share.rs
+++ b/rust/cloud-storage/documents/src/outbound/pg_document_repo/share.rs
@@ -4,8 +4,9 @@ use uuid::Uuid;
/// Share a document with all members of the given user's team.
///
/// Finds team members via the `team_user` join table, then bulk-inserts
-/// `UserItemAccess` rows with `comment` access and a NULL
-/// `granted_from_channel_id`. Skips users who already have a direct
+/// `UserItemAccess` rows with `comment` access, a NULL
+/// `granted_from_channel_id`, and the originating `granted_from_team_id`.
+/// Skips users who already have a direct
/// (non-channel) access row so existing permissions (e.g. the owner's
/// `owner` row) are never downgraded. Channel-granted rows are left
/// untouched and may coexist.
@@ -15,16 +16,32 @@ pub async fn share_with_team(
user_id: &str,
document_id: &str,
) -> Result<(), sqlx::Error> {
- // Find all users on the same team(s) as the given user.
- let team_members: Vec<String> = sqlx::query_scalar!(
+ // Find the team_id for the given user.
+ let team_id: Option<Uuid> = sqlx::query_scalar!(
r#"
- SELECT tu2.user_id
- FROM team_user tu1
- JOIN team_user tu2 ON tu1.team_id = tu2.team_id
- WHERE tu1.user_id = $1
+ SELECT team_id
+ FROM team_user
+ WHERE user_id = $1
+ LIMIT 1
"#,
user_id,
)
+ .fetch_optional(pool)
+ .await?;
+
+ let Some(team_id) = team_id else {
+ return Ok(());
+ };
+
+ // Find all users on the same team.
+ let team_members: Vec<String> = sqlx::query_scalar!(
+ r#"
+ SELECT user_id
+ FROM team_user
+ WHERE team_id = $1
+ "#,
+ team_id,
+ )
.fetch_all(pool)
.await?;
@@ -45,7 +62,8 @@ pub async fn share_with_team(
r#"
INSERT INTO "UserItemAccess" (
"id", "user_id", "item_id", "item_type", "access_level",
- "granted_from_channel_id", "created_at", "updated_at"
+ "granted_from_channel_id", "granted_from_team_id",
+ "created_at", "updated_at"
)
SELECT
u.id,
@@ -54,6 +72,7 @@ pub async fn share_with_team(
'document' AS item_type,
'comment' AS access_level,
NULL AS granted_from_channel_id,
+ $4 AS granted_from_team_id,
NOW() AS created_at,
NOW() AS updated_at
FROM UNNEST($2::uuid[], $3::text[]) AS u(id, user_id)
@@ -68,6 +87,7 @@ pub async fn share_with_team(
document_id,
&ids,
&team_members,
+ team_id,
)
.execute(pool)
.await?;
diff --git a/rust/cloud-storage/documents/src/outbound/pg_document_repo/tests.rs b/rust/cloud-storage/documents/src/outbound/pg_document_repo/tests.rs
index 6b40a93dee..8d6732df6b 100644
--- a/rust/cloud-storage/documents/src/outbound/pg_document_repo/tests.rs
+++ b/rust/cloud-storage/documents/src/outbound/pg_document_repo/tests.rs
@@ -308,10 +308,12 @@ async fn test_share_with_team_creates_access_for_team_members(pool: Pool<Postgre
.await
.unwrap();
+ let team_id = uuid::Uuid::parse_str("a0000000-0000-0000-0000-000000000001").unwrap();
+
// All 3 team members should have access rows
let rows = sqlx::query!(
r#"
- SELECT "user_id", "access_level"::text as "access_level"
+ SELECT "user_id", "access_level"::text as "access_level", "granted_from_team_id"
FROM "UserItemAccess"
WHERE "item_id" = 'document-one' AND "item_type" = 'document'
ORDER BY "user_id"
@@ -323,25 +325,28 @@ async fn test_share_with_team_creates_access_for_team_members(pool: Pool<Postgre
assert_eq!(rows.len(), 3);
- // Owner row should still be 'owner' (not downgraded)
+ // Owner row should still be 'owner' (not downgraded) and have no team grant
let owner_row = rows
.iter()
.find(|r| r.user_id == "macro|[email protected]")
.unwrap();
assert_eq!(owner_row.access_level, Some("owner".to_string()));
+ assert_eq!(owner_row.granted_from_team_id, None);
- // Teammates should have 'comment' access
+ // Teammates should have 'comment' access with granted_from_team_id set
let t1 = rows
.iter()
.find(|r| r.user_id == "macro|[email protected]")
.unwrap();
assert_eq!(t1.access_level, Some("comment".to_string()));
+ assert_eq!(t1.granted_from_team_id, Some(team_id));
let t2 = rows
.iter()
.find(|r| r.user_id == "macro|[email protected]")
.unwrap();
assert_eq!(t2.access_level, Some("comment".to_string()));
+ assert_eq!(t2.granted_from_team_id, Some(team_id));
}
#[sqlx::test(
@@ -500,6 +505,8 @@ async fn test_share_with_team_skips_user_with_existing_direct_access(pool: Pool<
async fn test_share_with_team_called_by_teammate(pool: Pool<Postgres>) {
let repo = PgDocumentRepo::new(pool.clone());
+ let team_id = uuid::Uuid::parse_str("a0000000-0000-0000-0000-000000000001").unwrap();
+
// A teammate (not the owner) triggers the share — should still find the
// same team and share with all members including the owner.
repo.share_with_team("macro|[email protected]", "document-one")
@@ -508,7 +515,7 @@ async fn test_share_with_team_called_by_teammate(pool: Pool<Postgres>) {
let rows = sqlx::query!(
r#"
- SELECT "user_id", "access_level"::text as "access_level"
+ SELECT "user_id", "access_level"::text as "access_level", "granted_from_team_id"
FROM "UserItemAccess"
WHERE "item_id" = 'document-one' AND "item_type" = 'document'
ORDER BY "user_id"
@@ -532,12 +539,14 @@ async fn test_share_with_team_called_by_teammate(pool: Pool<Postgres>) {
.find(|r| r.user_id == "macro|[email protected]")
.unwrap();
assert_eq!(t1.access_level, Some("comment".to_string()));
+ assert_eq!(t1.granted_from_team_id, Some(team_id));
let t2 = rows
.iter()
.find(|r| r.user_id == "macro|[email protected]")
.unwrap();
assert_eq!(t2.access_level, Some("comment".to_string()));
+ assert_eq!(t2.granted_from_team_id, Some(team_id));
}
#[sqlx::test(
diff --git a/rust/cloud-storage/macro_db_client/.sqlx/query-1435acbba365796ddcb6ddbef86f4ebf664cffc44082996f6b60a60373c28f75.json b/rust/cloud-storage/macro_db_client/.sqlx/query-1435acbba365796ddcb6ddbef86f4ebf664cffc44082996f6b60a60373c28f75.json
deleted file mode 100644
index d2b084cf10..0000000000
--- a/rust/cloud-storage/macro_db_client/.sqlx/query-1435acbba365796ddcb6ddbef86f4ebf664cffc44082996f6b60a60373c28f75.json
+++ /dev/null
@@ -1,17 +0,0 @@
-{
- "db_name": "PostgreSQL",
- "query": "\n INSERT INTO entity_properties (id, entity_id, entity_type, property_definition_id, values)\n SELECT \n u.id,\n u.entity_id,\n 'TASK'::property_entity_type,\n u.property_definition_id,\n u.values\n FROM UNNEST(\n $1::UUID[],\n $2::TEXT[],\n $3::UUID[],\n $4::JSONB[]\n ) AS u(id, entity_id, property_definition_id, values)\n ON CONFLICT (entity_id, entity_type, property_definition_id)\n DO UPDATE SET \n values = EXCLUDED.values,\n updated_at = NOW()\n ",
- "describe": {
- "columns": [],
- "parameters": {
- "Left": [
- "UuidArray",
- "TextArray",
- "UuidArray",
- "JsonbArray"
- ]
- },
- "nullable": []
- },
- "hash": "1435acbba365796ddcb6ddbef86f4ebf664cffc44082996f6b60a60373c28f75"
-}
diff --git a/rust/cloud-storage/macro_db_client/.sqlx/query-63492b018803ce26db5891964a0f52f37d2f54bd8243265c6561f4423af8617c.json b/rust/cloud-storage/macro_db_client/.sqlx/query-63492b018803ce26db5891964a0f52f37d2f54bd8243265c6561f4423af8617c.json
deleted file mode 100644
index 6d97183829..0000000000
--- a/rust/cloud-storage/macro_db_client/.sqlx/query-63492b018803ce26db5891964a0f52f37d2f54bd8243265c6561f4423af8617c.json
+++ /dev/null
@@ -1,16 +0,0 @@
-{
- "db_name": "PostgreSQL",
- "query": "\n INSERT INTO entity_properties (id, entity_id, entity_type, property_definition_id, values)\n SELECT \n u.id,\n u.entity_id,\n 'TASK'::property_entity_type,\n u.property_definition_id,\n NULL\n FROM UNNEST(\n $1::UUID[],\n $2::TEXT[],\n $3::UUID[]\n ) AS u(id, entity_id, property_definition_id)\n ON CONFLICT (entity_id, entity_type, property_definition_id)\n DO NOTHING\n ",
- "describe": {
- "columns": [],
- "parameters": {
- "Left": [
- "UuidArray",
- "TextArray",
- "UuidArray"
- ]
- },
- "nullable": []
- },
- "hash": "63492b018803ce26db5891964a0f52f37d2f54bd8243265c6561f4423af8617c"
-}
diff --git a/rust/cloud-storage/macro_db_client/.sqlx/query-9cd40fbb1ade51e9e0d49792839d1a363cada017252a6f653c2272608189bcc4.json b/rust/cloud-storage/macro_db_client/.sqlx/query-9cd40fbb1ade51e9e0d49792839d1a363cada017252a6f653c2272608189bcc4.json
deleted file mode 100644
index 9d717fa5f9..0000000000
--- a/rust/cloud-storage/macro_db_client/.sqlx/query-9cd40fbb1ade51e9e0d49792839d1a363cada017252a6f653c2272608189bcc4.json
+++ /dev/null
@@ -1,28 +0,0 @@
-{
- "db_name": "PostgreSQL",
- "query": "\n SELECT property_definition_id, values\n FROM entity_properties\n WHERE entity_id = $1\n AND entity_type = 'TASK'\n ",
- "describe": {
- "columns": [
- {
- "ordinal": 0,
- "name": "property_definition_id",
- "type_info": "Uuid"
- },
- {
- "ordinal": 1,
- "name": "values",
- "type_info": "Jsonb"
- }
- ],
- "parameters": {
- "Left": [
- "Text"
- ]
- },
- "nullable": [
- false,
- true
- ]
- },
- "hash": "9cd40fbb1ade51e9e0d49792839d1a363cada017252a6f653c2272608189bcc4"
-}
diff --git a/rust/cloud-storage/macro_db_client/.sqlx/query-cf71680da3444f573f4cddf66b6269bfe31aeee4bbec34b7e6067a855e28f2b7.json b/rust/cloud-storage/macro_db_client/.sqlx/query-cf71680da3444f573f4cddf66b6269bfe31aeee4bbec34b7e6067a855e28f2b7.json
deleted file mode 100644
index 9dc9838dc0..0000000000
--- a/rust/cloud-storage/macro_db_client/.sqlx/query-cf71680da3444f573f4cddf66b6269bfe31aeee4bbec34b7e6067a855e28f2b7.json
+++ /dev/null
@@ -1,15 +0,0 @@
-{
- "db_name": "PostgreSQL",
- "query": "\n UPDATE entity_properties\n SET values = $2\n WHERE entity_id = $1;\n ",
- "describe": {
- "columns": [],
- "parameters": {
- "Left": [
- "Text",
- "Jsonb"
- ]
- },
- "nullable": []
- },
- "hash": "cf71680da3444f573f4cddf66b6269bfe31aeee4bbec34b7e6067a855e28f2b7"
-}
diff --git a/rust/cloud-storage/macro_db_client/.sqlx/query-f2d5d7aea9918978cd69c14f8afe24156c2fa387e001ee8eaf57e4a70a106069.json b/rust/cloud-storage/macro_db_client/.sqlx/query-f2d5d7aea9918978cd69c14f8afe24156c2fa387e001ee8eaf57e4a70a106069.json
deleted file mode 100644
index 182e0858ba..0000000000
--- a/rust/cloud-storage/macro_db_client/.sqlx/query-f2d5d7aea9918978cd69c14f8afe24156c2fa387e001ee8eaf57e4a70a106069.json
+++ /dev/null
@@ -1,18 +0,0 @@
-{
- "db_name": "PostgreSQL",
- "query": "\n INSERT INTO entity_properties (id, entity_id, entity_type, property_definition_id, values)\n SELECT \n u.id,\n u.entity_id,\n u.entity_type::property_entity_type,\n u.property_definition_id,\n u.values\n FROM UNNEST(\n $1::UUID[],\n $2::TEXT[],\n $3::TEXT[],\n $4::UUID[],\n $5::JSONB[]\n ) AS u(id, entity_id, entity_type, property_definition_id, values)\n ON CONFLICT (entity_id, entity_type, property_definition_id)\n DO UPDATE SET \n values = EXCLUDED.values,\n updated_at = NOW()\n ",
- "describe": {
- "columns": [],
- "parameters": {
- "Left": [
- "UuidArray",
- "TextArray",
- "TextArray",
- "UuidArray",
- "JsonbArray"
- ]
- },
- "nullable": []
- },
- "hash": "f2d5d7aea9918978cd69c14f8afe24156c2fa387e001ee8eaf57e4a70a106069"
-}
diff --git a/rust/cloud-storage/macro_db_client/migrations/20260320153627_user_item_access_team_tracking.sql b/rust/cloud-storage/macro_db_client/migrations/20260320153627_user_item_access_team_tracking.sql
new file mode 100644
index 0000000000..6299f1c279
--- /dev/null
+++ b/rust/cloud-storage/macro_db_client/migrations/20260320153627_user_item_access_team_tracking.sql
@@ -0,0 +1,8 @@
+ALTER TABLE "UserItemAccess" ADD COLUMN "granted_from_team_id" UUID
+ REFERENCES "team" ("id") ON DELETE CASCADE;
+
+-- Fast lookups for all items shared to your team
+CREATE INDEX "UserItemAccess_granted_from_team_id_idx" ON "UserItemAccess" ("granted_from_team_id");
+
+-- Cannot share an item more than once through your team
+CREATE UNIQUE INDEX "UserItemAccess_user_id_item_id_item_type_granted_from_team_key" ON "UserItemAccess" ("user_id", "item_id", "item_type", "granted_from_team_id");