From ae9d18ac9bac8e9ac06fe73b4184e662d359a691 Mon Sep 17 00:00:00 2001 From: Thomas Rittson Date: Wed, 31 Dec 2025 13:28:45 +1000 Subject: [PATCH] Add read sproc for semaphores --- src/Core/Repositories/ICollectionRepository.cs | 7 +++++++ .../Repositories/CollectionRepository.cs | 13 +++++++++++++ .../Repositories/CollectionRepository.cs | 13 +++++++++++++ ...tCollectionSemaphore_ReadByOrganizationId.sql | 13 +++++++++++++ .../2025-12-30_00_DefaultCollectionSemaphore.sql | 16 ++++++++++++++++ 5 files changed, 62 insertions(+) create mode 100644 src/Sql/dbo/Stored Procedures/DefaultCollectionSemaphore_ReadByOrganizationId.sql diff --git a/src/Core/Repositories/ICollectionRepository.cs b/src/Core/Repositories/ICollectionRepository.cs index 6bf1839bc0..d64d4f3632 100644 --- a/src/Core/Repositories/ICollectionRepository.cs +++ b/src/Core/Repositories/ICollectionRepository.cs @@ -82,4 +82,11 @@ public interface ICollectionRepository : IRepository /// The encrypted string to use as the default collection name. /// Task UpsertDefaultCollectionsBulkAsync(Guid organizationId, IEnumerable organizationUserIds, string defaultCollectionName); + + /// + /// Gets organization user IDs that have default collection semaphore entries for the specified organization. + /// + /// The Organization ID. + /// Collection of organization user IDs that have default collection semaphores. + Task> GetDefaultCollectionSemaphoresAsync(Guid organizationId); } diff --git a/src/Infrastructure.Dapper/Repositories/CollectionRepository.cs b/src/Infrastructure.Dapper/Repositories/CollectionRepository.cs index 36551fb791..584ace3e73 100644 --- a/src/Infrastructure.Dapper/Repositories/CollectionRepository.cs +++ b/src/Infrastructure.Dapper/Repositories/CollectionRepository.cs @@ -430,6 +430,19 @@ public class CollectionRepository : Repository, ICollectionRep } } + public async Task> GetDefaultCollectionSemaphoresAsync(Guid organizationId) + { + using (var connection = new SqlConnection(ConnectionString)) + { + var results = await connection.QueryAsync( + "[dbo].[DefaultCollectionSemaphore_ReadByOrganizationId]", + new { OrganizationId = organizationId }, + commandType: CommandType.StoredProcedure); + + return results.ToList(); + } + } + private async Task> GetOrgUserIdsWithDefaultCollectionAsync(SqlConnection connection, SqlTransaction transaction, Guid organizationId) { const string sql = @" diff --git a/src/Infrastructure.EntityFramework/Repositories/CollectionRepository.cs b/src/Infrastructure.EntityFramework/Repositories/CollectionRepository.cs index 583b6abcdc..dc94619df3 100644 --- a/src/Infrastructure.EntityFramework/Repositories/CollectionRepository.cs +++ b/src/Infrastructure.EntityFramework/Repositories/CollectionRepository.cs @@ -839,6 +839,19 @@ public class CollectionRepository : Repository> GetDefaultCollectionSemaphoresAsync(Guid organizationId) + { + using var scope = ServiceScopeFactory.CreateScope(); + var dbContext = GetDatabaseContext(scope); + + var organizationUserIds = await dbContext.DefaultCollectionSemaphores + .Where(s => s.OrganizationId == organizationId) + .Select(s => s.OrganizationUserId) + .ToListAsync(); + + return organizationUserIds; + } + private async Task> GetOrgUserIdsWithDefaultCollectionAsync(DatabaseContext dbContext, Guid organizationId) { var results = await dbContext.DefaultCollectionSemaphores diff --git a/src/Sql/dbo/Stored Procedures/DefaultCollectionSemaphore_ReadByOrganizationId.sql b/src/Sql/dbo/Stored Procedures/DefaultCollectionSemaphore_ReadByOrganizationId.sql new file mode 100644 index 0000000000..f5a8b4d9c3 --- /dev/null +++ b/src/Sql/dbo/Stored Procedures/DefaultCollectionSemaphore_ReadByOrganizationId.sql @@ -0,0 +1,13 @@ +CREATE PROCEDURE [dbo].[DefaultCollectionSemaphore_ReadByOrganizationId] + @OrganizationId UNIQUEIDENTIFIER +AS +BEGIN + SET NOCOUNT ON + + SELECT + [OrganizationUserId] + FROM + [dbo].[DefaultCollectionSemaphore] + WHERE + [OrganizationId] = @OrganizationId +END diff --git a/util/Migrator/DbScripts/2025-12-30_00_DefaultCollectionSemaphore.sql b/util/Migrator/DbScripts/2025-12-30_00_DefaultCollectionSemaphore.sql index 421c6e2388..e60fe1072d 100644 --- a/util/Migrator/DbScripts/2025-12-30_00_DefaultCollectionSemaphore.sql +++ b/util/Migrator/DbScripts/2025-12-30_00_DefaultCollectionSemaphore.sql @@ -21,3 +21,19 @@ BEGIN ); END GO + +-- Create stored procedure to read semaphores by organization +CREATE OR ALTER PROCEDURE [dbo].[DefaultCollectionSemaphore_ReadByOrganizationId] + @OrganizationId UNIQUEIDENTIFIER +AS +BEGIN + SET NOCOUNT ON + + SELECT + [OrganizationUserId] + FROM + [dbo].[DefaultCollectionSemaphore] + WHERE + [OrganizationId] = @OrganizationId +END +GO