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: update UserItemAccess in edit_document (#2017)

Will Hutchinsonbd59f3d0b83e
diff --git a/rust/cloud-storage/documents/.sqlx/query-2db739bbaa907ce20364049887168f9a47b0e7d1c06d619df37fe7644418c104.json b/rust/cloud-storage/documents/.sqlx/query-2db739bbaa907ce20364049887168f9a47b0e7d1c06d619df37fe7644418c104.json
new file mode 100644
index 0000000000..2eb400f754
--- /dev/null
+++ b/rust/cloud-storage/documents/.sqlx/query-2db739bbaa907ce20364049887168f9a47b0e7d1c06d619df37fe7644418c104.json
@@ -0,0 +1,15 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n INSERT INTO \"UserItemAccess\" (\n \"id\",\n \"user_id\",\n \"item_id\",\n \"item_type\",\n \"access_level\",\n \"granted_from_channel_id\",\n \"created_at\",\n \"updated_at\"\n )\n SELECT\n gen_random_uuid(),\n cp.user_id,\n $1,\n 'document',\n 'view'::\"AccessLevel\",\n cp.channel_id,\n NOW(),\n NOW()\n FROM comms_channel_participants cp\n WHERE cp.channel_id = ANY($2::uuid[])\n AND cp.left_at IS NULL\n ON CONFLICT (\"user_id\", \"item_id\", \"item_type\", \"granted_from_channel_id\")\n DO UPDATE SET\n \"access_level\" = EXCLUDED.\"access_level\",\n \"updated_at\" = NOW()\n ",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Text",
+ "UuidArray"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "2db739bbaa907ce20364049887168f9a47b0e7d1c06d619df37fe7644418c104"
+}
diff --git a/rust/cloud-storage/documents/.sqlx/query-37b5168f739c490a3373c65eab35f247a34af0a8e659cbad61cb99e3094e5a60.json b/rust/cloud-storage/documents/.sqlx/query-37b5168f739c490a3373c65eab35f247a34af0a8e659cbad61cb99e3094e5a60.json
deleted file mode 100644
index 44d3a67e72..0000000000
--- a/rust/cloud-storage/documents/.sqlx/query-37b5168f739c490a3373c65eab35f247a34af0a8e659cbad61cb99e3094e5a60.json
+++ /dev/null
@@ -1,23 +0,0 @@
-{
- "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
deleted file mode 100644
index 60bfee0647..0000000000
--- a/rust/cloud-storage/documents/.sqlx/query-3a7be99469ed150e0506efa786b672198a6d3419c6bd5548418520cd3e6a8e95.json
+++ /dev/null
@@ -1,23 +0,0 @@
-{
- "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
deleted file mode 100644
index 58618b2697..0000000000
--- a/rust/cloud-storage/documents/.sqlx/query-4afa79ca95bdbcebe9d3de7ebb204636419e6cf89e650a8716c40ab2e588aa5b.json
+++ /dev/null
@@ -1,23 +0,0 @@
-{
- "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
deleted file mode 100644
index 524cc25ec7..0000000000
--- a/rust/cloud-storage/documents/.sqlx/query-4c695b011d49ba7d5fce4b309875ed952dc386ac760444d7883c5a76718f15be.json
+++ /dev/null
@@ -1,23 +0,0 @@
-{
- "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
deleted file mode 100644
index 03a8dbf6bb..0000000000
--- a/rust/cloud-storage/documents/.sqlx/query-54e3755d63a676a7fa70da77321e618032f0130c73eb6b0ae985f07c5c6c448b.json
+++ /dev/null
@@ -1,22 +0,0 @@
-{
- "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
deleted file mode 100644
index 655495fb29..0000000000
--- a/rust/cloud-storage/documents/.sqlx/query-7617f808df94ffe77c8195a911ee6b230a21fe9935e099d1310d244248ca253a.json
+++ /dev/null
@@ -1,23 +0,0 @@
-{
- "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-79d3bc72e6f424765706f7deea426379ae49cbd70dd122daedd95343618b9ac6.json b/rust/cloud-storage/documents/.sqlx/query-79d3bc72e6f424765706f7deea426379ae49cbd70dd122daedd95343618b9ac6.json
deleted file mode 100644
index 2bde15938f..0000000000
--- a/rust/cloud-storage/documents/.sqlx/query-79d3bc72e6f424765706f7deea426379ae49cbd70dd122daedd95343618b9ac6.json
+++ /dev/null
@@ -1,22 +0,0 @@
-{
- "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-96d853b887f2e6d7aac6b15ddd2b71a8d8900d2ef30047da7ca1a5940503b6d0.json b/rust/cloud-storage/documents/.sqlx/query-96d853b887f2e6d7aac6b15ddd2b71a8d8900d2ef30047da7ca1a5940503b6d0.json
deleted file mode 100644
index e42c9f2009..0000000000
--- a/rust/cloud-storage/documents/.sqlx/query-96d853b887f2e6d7aac6b15ddd2b71a8d8900d2ef30047da7ca1a5940503b6d0.json
+++ /dev/null
@@ -1,22 +0,0 @@
-{
- "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-b3aeddd507e0efee0e0cecef6eae7503820f0f3302e95a459790c5cb78e5dfb0.json b/rust/cloud-storage/documents/.sqlx/query-b3aeddd507e0efee0e0cecef6eae7503820f0f3302e95a459790c5cb78e5dfb0.json
deleted file mode 100644
index 2b67c5606d..0000000000
--- a/rust/cloud-storage/documents/.sqlx/query-b3aeddd507e0efee0e0cecef6eae7503820f0f3302e95a459790c5cb78e5dfb0.json
+++ /dev/null
@@ -1,23 +0,0 @@
-{
- "db_name": "PostgreSQL",
- "query": "\n -- CTE to recursively find all parent projects for the given email thread.\n WITH RECURSIVE project_hierarchy AS (\n -- Base case: Start with the project directly associated with the thread.\n SELECT\n p.id as project_id\n FROM\n \"EmailThreadPermission\" etp\n JOIN \"Project\" p ON etp.\"projectId\" = p.id AND p.\"deletedAt\" IS NULL\n WHERE\n etp.\"threadId\" = $1\n UNION ALL\n -- Recursive case: Find the parent of the project from the previous step.\n SELECT\n parent.id as project_id\n FROM\n 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 SELECT $1 -- The thread ID 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 \"EmailThreadPermission\" WHERE \"threadId\" = $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": "b3aeddd507e0efee0e0cecef6eae7503820f0f3302e95a459790c5cb78e5dfb0"
-}
diff --git a/rust/cloud-storage/documents/.sqlx/query-e7235d6ad2033a2ff7be0bf57ab042cd71e3b3d0e1f8a9750e55cdda92e70c6f.json b/rust/cloud-storage/documents/.sqlx/query-e7235d6ad2033a2ff7be0bf57ab042cd71e3b3d0e1f8a9750e55cdda92e70c6f.json
deleted file mode 100644
index 4c96ff0fd5..0000000000
--- a/rust/cloud-storage/documents/.sqlx/query-e7235d6ad2033a2ff7be0bf57ab042cd71e3b3d0e1f8a9750e55cdda92e70c6f.json
+++ /dev/null
@@ -1,22 +0,0 @@
-{
- "db_name": "PostgreSQL",
- "query": "\n WITH RECURSIVE project_hierarchy AS (\n SELECT p.id as project_id\n FROM \"EmailThreadPermission\" etp\n JOIN \"Project\" p ON etp.\"projectId\" = p.id AND p.\"deletedAt\" IS NULL\n WHERE etp.\"threadId\" = $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 $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": "e7235d6ad2033a2ff7be0bf57ab042cd71e3b3d0e1f8a9750e55cdda92e70c6f"
-}
diff --git a/rust/cloud-storage/documents/fixtures/documents_test_data.sql b/rust/cloud-storage/documents/fixtures/documents_test_data.sql
index cb57840220..edf687f4d9 100644
--- a/rust/cloud-storage/documents/fixtures/documents_test_data.sql
+++ b/rust/cloud-storage/documents/fixtures/documents_test_data.sql
@@ -105,6 +105,15 @@ INSERT INTO public."SharePermission" ("id", "isPublic", "publicAccessLevel", "cr
INSERT INTO public."DocumentPermission" ("documentId", "sharePermissionId")
(SELECT 'document-one', 'sp-doc-one');
+-- Channel and participants for channel share permission tests
+INSERT INTO comms_channels (id, name, channel_type, owner_id) VALUES
+ ('c0000000-0000-0000-0000-000000000001', 'test-channel', 'private', 'macro|[email protected]');
+
+INSERT INTO comms_channel_participants (user_id, channel_id, role) VALUES
+ ('macro|[email protected]', 'c0000000-0000-0000-0000-000000000001', 'owner'),
+ ('macro|[email protected]', 'c0000000-0000-0000-0000-000000000001', 'member'),
+ ('macro|[email protected]', 'c0000000-0000-0000-0000-000000000001', 'member');
+
-- Share permissions for document-two
INSERT INTO public."SharePermission" ("id", "isPublic", "createdAt", "updatedAt")
(SELECT 'sp-doc-two', false, NOW(), NOW());
diff --git a/rust/cloud-storage/documents/src/outbound/pg_document_repo/edit.rs b/rust/cloud-storage/documents/src/outbound/pg_document_repo/edit.rs
index 1d5d80e78f..a79279a924 100644
--- a/rust/cloud-storage/documents/src/outbound/pg_document_repo/edit.rs
+++ b/rust/cloud-storage/documents/src/outbound/pg_document_repo/edit.rs
@@ -74,7 +74,13 @@ pub(super) async fn update_share_permission(
// Handle channel share permission changes
if let Some(channel_perms) = &share_permission.channel_share_permissions {
- update_channel_share_permissions(transaction, &share_permission_id, channel_perms).await?;
+ update_channel_share_permissions(
+ transaction,
+ document_id,
+ &share_permission_id,
+ channel_perms,
+ )
+ .await?;
}
Ok(())
@@ -160,9 +166,11 @@ async fn update_share_permission_row(
Ok(())
}
-/// Update channel share permissions (add/replace/remove).
+/// Update channel share permissions (add/replace/remove) and ensure UserItemAccess rows
+/// are created for all active channel participants.
async fn update_channel_share_permissions(
transaction: &mut Transaction<'_, Postgres>,
+ document_id: &str,
share_permission_id: &str,
channel_perms: &[models_permissions::share_permission::channel_share_permission::UpdateChannelSharePermission],
) -> Result<(), sqlx::Error> {
@@ -220,6 +228,51 @@ async fn update_channel_share_permissions(
)
.execute(transaction.as_mut())
.await?;
+
+ // Upsert UserItemAccess rows for all active participants of the upserted channels.
+ // This ensures that even if the comms_service's update_channel_share_permission sees
+ // the CSP already exists and returns early, the UserItemAccess rows still get created.
+ let channel_uuids: Vec<uuid::Uuid> = upsert_channel_ids
+ .iter()
+ .filter_map(|id| uuid::Uuid::parse_str(id).ok())
+ .collect();
+
+ if !channel_uuids.is_empty() {
+ sqlx::query!(
+ r#"
+ INSERT INTO "UserItemAccess" (
+ "id",
+ "user_id",
+ "item_id",
+ "item_type",
+ "access_level",
+ "granted_from_channel_id",
+ "created_at",
+ "updated_at"
+ )
+ SELECT
+ gen_random_uuid(),
+ cp.user_id,
+ $1,
+ 'document',
+ 'view'::"AccessLevel",
+ cp.channel_id,
+ NOW(),
+ NOW()
+ FROM comms_channel_participants cp
+ WHERE cp.channel_id = ANY($2::uuid[])
+ AND cp.left_at IS NULL
+ ON CONFLICT ("user_id", "item_id", "item_type", "granted_from_channel_id")
+ DO UPDATE SET
+ "access_level" = EXCLUDED."access_level",
+ "updated_at" = NOW()
+ "#,
+ document_id,
+ &channel_uuids,
+ )
+ .execute(transaction.as_mut())
+ .await?;
+ }
}
Ok(())
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 a47d32cfda..6b40a93dee 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
@@ -1,6 +1,9 @@
use macro_db_migrator::MACRO_DB_MIGRATIONS;
use models_permissions::share_permission::UpdateSharePermissionRequestV2;
use models_permissions::share_permission::access_level::AccessLevel;
+use models_permissions::share_permission::channel_share_permission::{
+ UpdateChannelSharePermission, UpdateOperation,
+};
use sqlx::{Pool, Postgres};
use crate::domain::models::EditDocumentRepoArgs;
@@ -536,3 +539,128 @@ async fn test_share_with_team_called_by_teammate(pool: Pool<Postgres>) {
.unwrap();
assert_eq!(t2.access_level, Some("comment".to_string()));
}
+
+#[sqlx::test(
+ migrator = "MACRO_DB_MIGRATIONS",
+ fixtures(path = "../../../fixtures", scripts("documents_test_data"))
+)]
+async fn test_edit_document_channel_share_creates_user_item_access(pool: Pool<Postgres>) {
+ let repo = PgDocumentRepo::new(pool.clone());
+ let channel_id = "c0000000-0000-0000-0000-000000000001";
+
+ repo.edit_document(EditDocumentRepoArgs {
+ document_id: "document-one".to_string(),
+ document_name: None,
+ project_id: None,
+ share_permission: Some(UpdateSharePermissionRequestV2 {
+ is_public: None,
+ public_access_level: None,
+ channel_share_permissions: Some(vec![UpdateChannelSharePermission {
+ operation: UpdateOperation::Add,
+ channel_id: channel_id.to_string(),
+ access_level: Some(AccessLevel::View),
+ }]),
+ }),
+ })
+ .await
+ .unwrap();
+
+ // Verify ChannelSharePermission was created
+ let csp_count = sqlx::query_scalar!(
+ r#"
+ SELECT COUNT(*) as "count!"
+ FROM "ChannelSharePermission"
+ WHERE "channel_id" = $1::text
+ "#,
+ channel_id,
+ )
+ .fetch_one(&pool)
+ .await
+ .unwrap();
+ assert_eq!(
+ csp_count, 1,
+ "Should have created one ChannelSharePermission"
+ );
+
+ // Verify UserItemAccess rows were created for all 3 active channel participants
+ let channel_uuid = uuid::Uuid::parse_str(channel_id).unwrap();
+
+ let access_rows = sqlx::query!(
+ r#"
+ SELECT "user_id", "access_level"::text as "access_level", "granted_from_channel_id"
+ FROM "UserItemAccess"
+ WHERE "item_id" = 'document-one'
+ AND "item_type" = 'document'
+ AND "granted_from_channel_id" = $1
+ ORDER BY "user_id"
+ "#,
+ channel_uuid,
+ )
+ .fetch_all(&pool)
+ .await
+ .unwrap();
+
+ assert_eq!(
+ access_rows.len(),
+ 3,
+ "All 3 channel participants should have UserItemAccess rows"
+ );
+
+ for row in &access_rows {
+ assert_eq!(row.access_level, Some("view".to_string()));
+ assert_eq!(row.granted_from_channel_id, Some(channel_uuid));
+ }
+
+ let user_ids: Vec<&str> = access_rows.iter().map(|r| r.user_id.as_str()).collect();
+ assert!(user_ids.contains(&"macro|[email protected]"));
+ assert!(user_ids.contains(&"macro|[email protected]"));
+ assert!(user_ids.contains(&"macro|[email protected]"));
+}
+
+#[sqlx::test(
+ migrator = "MACRO_DB_MIGRATIONS",
+ fixtures(path = "../../../fixtures", scripts("documents_test_data"))
+)]
+async fn test_edit_document_channel_share_idempotent(pool: Pool<Postgres>) {
+ let repo = PgDocumentRepo::new(pool.clone());
+ let channel_id = "c0000000-0000-0000-0000-000000000001";
+ let channel_uuid = uuid::Uuid::parse_str(channel_id).unwrap();
+
+ let make_args = || EditDocumentRepoArgs {
+ document_id: "document-one".to_string(),
+ document_name: None,
+ project_id: None,
+ share_permission: Some(UpdateSharePermissionRequestV2 {
+ is_public: None,
+ public_access_level: None,
+ channel_share_permissions: Some(vec![UpdateChannelSharePermission {
+ operation: UpdateOperation::Add,
+ channel_id: channel_id.to_string(),
+ access_level: Some(AccessLevel::View),
+ }]),
+ }),
+ };
+
+ // Call twice — second call should upsert without duplicates
+ repo.edit_document(make_args()).await.unwrap();
+ repo.edit_document(make_args()).await.unwrap();
+
+ let count = sqlx::query_scalar!(
+ r#"
+ SELECT COUNT(*) as "count!"
+ FROM "UserItemAccess"
+ WHERE "item_id" = 'document-one'
+ AND "item_type" = 'document'
+ AND "granted_from_channel_id" = $1
+ "#,
+ channel_uuid,
+ )
+ .fetch_one(&pool)
+ .await
+ .unwrap();
+
+ assert_eq!(
+ count, 3,
+ "Should still have exactly 3 rows after idempotent upsert"
+ );
+}