diff --git a/src/Core/AdminConsole/OrganizationFeatures/Collections/CollectionUtils.cs b/src/Core/AdminConsole/OrganizationFeatures/Collections/CollectionUtils.cs
index e540c75555..25eb812837 100644
--- a/src/Core/AdminConsole/OrganizationFeatures/Collections/CollectionUtils.cs
+++ b/src/Core/AdminConsole/OrganizationFeatures/Collections/CollectionUtils.cs
@@ -7,16 +7,21 @@ namespace Bit.Core.AdminConsole.OrganizationFeatures.Collections;
public static class CollectionUtils
{
///
- /// Arranges Collection and CollectionUser objects to create default user collections.
+ /// Arranges semaphore, Collection and CollectionUser objects to create default user collections.
///
/// The organization ID.
/// The IDs for organization users who need default collections.
/// The encrypted string to use as the default collection name.
///
- public static (IEnumerable collection, IEnumerable collectionUsers)
+ public static (IEnumerable semaphores,
+ IEnumerable collection,
+ IEnumerable collectionUsers)
BuildDefaultUserCollections(Guid organizationId, IEnumerable organizationUserIds,
string defaultCollectionName)
{
+ var now = DateTime.UtcNow;
+
+ var semaphores = new List();
var collectionUsers = new List();
var collections = new List();
@@ -24,13 +29,19 @@ public static class CollectionUtils
{
var collectionId = CoreHelpers.GenerateComb();
+ semaphores.Add(new DefaultCollectionSemaphore
+ {
+ OrganizationUserId = orgUserId,
+ CreationDate = now
+ });
+
collections.Add(new Collection
{
Id = collectionId,
OrganizationId = organizationId,
Name = defaultCollectionName,
- CreationDate = DateTime.UtcNow,
- RevisionDate = DateTime.UtcNow,
+ CreationDate = now,
+ RevisionDate = now,
Type = CollectionType.DefaultUserCollection,
DefaultUserCollectionEmail = null
@@ -46,6 +57,6 @@ public static class CollectionUtils
});
}
- return (collections, collectionUsers);
+ return (semaphores, collections, collectionUsers);
}
}
diff --git a/src/Infrastructure.Dapper/Repositories/CollectionRepository.cs b/src/Infrastructure.Dapper/Repositories/CollectionRepository.cs
index 3340fec8f1..001efe07ba 100644
--- a/src/Infrastructure.Dapper/Repositories/CollectionRepository.cs
+++ b/src/Infrastructure.Dapper/Repositories/CollectionRepository.cs
@@ -398,7 +398,7 @@ public class CollectionRepository : Repository, ICollectionRep
return;
}
- var (collections, collectionUsers) =
+ var (semaphores, collections, collectionUsers) =
CollectionUtils.BuildDefaultUserCollections(organizationId, organizationUserIds, defaultCollectionName);
await using var connection = new SqlConnection(ConnectionString);
@@ -410,13 +410,6 @@ public class CollectionRepository : Repository, ICollectionRep
// CRITICAL: Insert semaphore entries BEFORE collections
// Database will throw on duplicate primary key (OrganizationUserId)
- var now = DateTime.UtcNow;
- var semaphores = collectionUsers.Select(c => new DefaultCollectionSemaphore
- {
- OrganizationUserId = c.OrganizationUserId,
- CreationDate = now
- }).ToList();
-
await BulkInsertDefaultCollectionSemaphoresAsync(connection, transaction, semaphores);
await BulkResourceCreationService.CreateCollectionsAsync(connection, transaction, collections);
await BulkResourceCreationService.CreateCollectionsUsersAsync(connection, transaction, collectionUsers);
@@ -447,8 +440,9 @@ public class CollectionRepository : Repository, ICollectionRep
return results.ToHashSet();
}
- private async Task BulkInsertDefaultCollectionSemaphoresAsync(SqlConnection connection, SqlTransaction transaction, List semaphores)
+ private async Task BulkInsertDefaultCollectionSemaphoresAsync(SqlConnection connection, SqlTransaction transaction, IEnumerable semaphores)
{
+ semaphores = semaphores.ToList();
if (!semaphores.Any())
{
return;
diff --git a/src/Infrastructure.EntityFramework/Repositories/CollectionRepository.cs b/src/Infrastructure.EntityFramework/Repositories/CollectionRepository.cs
index fb0539b3c3..fa8ffb258e 100644
--- a/src/Infrastructure.EntityFramework/Repositories/CollectionRepository.cs
+++ b/src/Infrastructure.EntityFramework/Repositories/CollectionRepository.cs
@@ -805,7 +805,7 @@ public class CollectionRepository : Repository new DefaultCollectionSemaphore
- {
- OrganizationUserId = c.OrganizationUserId,
- CreationDate = now
- }).ToList();
-
- await dbContext.BulkCopyAsync(semaphores);
+ await dbContext.BulkCopyAsync(Mapper.Map>(semaphores));
await dbContext.BulkCopyAsync(Mapper.Map>(collections));
await dbContext.BulkCopyAsync(Mapper.Map>(collectionUsers));