From ee6e095e883d933aa0d4c6beec0d4a93777ee2b9 Mon Sep 17 00:00:00 2001 From: Rui Tome Date: Mon, 10 Apr 2023 15:58:31 +0100 Subject: [PATCH] =?UTF-8?q?[AC-432]=C2=A0Replaced=20IProviderOrganizationR?= =?UTF-8?q?epository.GetCountByOrganizationIdsAsync=20with=20call=20to=20I?= =?UTF-8?q?ProviderOrganizationRepository.GetByOrganizationId?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Services/ProviderService.cs | 9 +++++--- .../IProviderOrganizationRepository.cs | 1 - .../ProviderOrganizationRepository.cs | 14 ------------- .../ProviderOrganizationRepository.cs | 6 ------ ...OrganizationCountByOrganizationIdsQuery.cs | 21 ------------------- src/Sql/Sql.sqlproj | 1 - ...rganization_ReadCountByOrganizationIds.sql | 18 ---------------- ...22_00_ProviderAddExistingOrganizations.sql | 20 ------------------ 8 files changed, 6 insertions(+), 84 deletions(-) delete mode 100644 src/Infrastructure.EntityFramework/Repositories/Queries/ProviderOrganizationCountByOrganizationIdsQuery.cs delete mode 100644 src/Sql/dbo/Stored Procedures/ProviderOrganization_ReadCountByOrganizationIds.sql diff --git a/bitwarden_license/src/Commercial.Core/Services/ProviderService.cs b/bitwarden_license/src/Commercial.Core/Services/ProviderService.cs index 6eef1deeb..d8490e6ef 100644 --- a/bitwarden_license/src/Commercial.Core/Services/ProviderService.cs +++ b/bitwarden_license/src/Commercial.Core/Services/ProviderService.cs @@ -373,10 +373,13 @@ public class ProviderService : IProviderService throw new BadRequestException("Provider must be of type Reseller in order to assign Organizations to it."); } - var existingProviderOrganizationsCount = await _providerOrganizationRepository.GetCountByOrganizationIdsAsync(organizationIds); - if (existingProviderOrganizationsCount > 0) + foreach (var orgId in organizationIds) { - throw new BadRequestException("Organizations must not be assigned to any Provider."); + var providerOrganization = await _providerOrganizationRepository.GetByOrganizationId(orgId); + if (providerOrganization != null) + { + throw new BadRequestException("Organizations must not be assigned to any Provider."); + } } var providerOrganizationsToInsert = organizationIds.Select(orgId => new ProviderOrganization { ProviderId = providerId, OrganizationId = orgId }); diff --git a/src/Core/Repositories/IProviderOrganizationRepository.cs b/src/Core/Repositories/IProviderOrganizationRepository.cs index 8550a3d7b..282bd1d32 100644 --- a/src/Core/Repositories/IProviderOrganizationRepository.cs +++ b/src/Core/Repositories/IProviderOrganizationRepository.cs @@ -9,5 +9,4 @@ public interface IProviderOrganizationRepository : IRepository> GetManyDetailsByProviderAsync(Guid providerId); Task GetByOrganizationId(Guid organizationId); Task> GetManyByUserAsync(Guid userId); - Task GetCountByOrganizationIdsAsync(IEnumerable organizationIds); } diff --git a/src/Infrastructure.Dapper/Repositories/ProviderOrganizationRepository.cs b/src/Infrastructure.Dapper/Repositories/ProviderOrganizationRepository.cs index 604d81a6e..2ea12e944 100644 --- a/src/Infrastructure.Dapper/Repositories/ProviderOrganizationRepository.cs +++ b/src/Infrastructure.Dapper/Repositories/ProviderOrganizationRepository.cs @@ -99,20 +99,6 @@ public class ProviderOrganizationRepository : Repository GetCountByOrganizationIdsAsync( - IEnumerable organizationIds) - { - using (var connection = new SqlConnection(ConnectionString)) - { - var results = await connection.ExecuteScalarAsync( - $"[{Schema}].[ProviderOrganization_ReadCountByOrganizationIds]", - new { Ids = organizationIds.ToGuidIdArrayTVP() }, - commandType: CommandType.StoredProcedure); - - return results; - } - } - private DataTable BuildProviderOrganizationsTable(SqlBulkCopy bulkCopy, IEnumerable providerOrganizations) { var po = providerOrganizations.FirstOrDefault(); diff --git a/src/Infrastructure.EntityFramework/Repositories/ProviderOrganizationRepository.cs b/src/Infrastructure.EntityFramework/Repositories/ProviderOrganizationRepository.cs index c5a6d34e2..7426aa43a 100644 --- a/src/Infrastructure.EntityFramework/Repositories/ProviderOrganizationRepository.cs +++ b/src/Infrastructure.EntityFramework/Repositories/ProviderOrganizationRepository.cs @@ -67,10 +67,4 @@ public class ProviderOrganizationRepository : return data; } } - - public async Task GetCountByOrganizationIdsAsync(IEnumerable organizationIds) - { - var query = new ProviderOrganizationCountByOrganizationIdsQuery(organizationIds); - return await GetCountFromQuery(query); - } } diff --git a/src/Infrastructure.EntityFramework/Repositories/Queries/ProviderOrganizationCountByOrganizationIdsQuery.cs b/src/Infrastructure.EntityFramework/Repositories/Queries/ProviderOrganizationCountByOrganizationIdsQuery.cs deleted file mode 100644 index 4d9357f2f..000000000 --- a/src/Infrastructure.EntityFramework/Repositories/Queries/ProviderOrganizationCountByOrganizationIdsQuery.cs +++ /dev/null @@ -1,21 +0,0 @@ -using Bit.Core.Entities.Provider; - -namespace Bit.Infrastructure.EntityFramework.Repositories.Queries; - -public class ProviderOrganizationCountByOrganizationIdsQuery : IQuery -{ - private readonly IEnumerable _organizationIds; - - public ProviderOrganizationCountByOrganizationIdsQuery(IEnumerable organizationIds) - { - _organizationIds = organizationIds; - } - - public IQueryable Run(DatabaseContext dbContext) - { - var query = from po in dbContext.ProviderOrganizations - where _organizationIds.Contains(po.OrganizationId) - select po; - return query; - } -} diff --git a/src/Sql/Sql.sqlproj b/src/Sql/Sql.sqlproj index e90d236a8..249c2a839 100644 --- a/src/Sql/Sql.sqlproj +++ b/src/Sql/Sql.sqlproj @@ -264,7 +264,6 @@ - diff --git a/src/Sql/dbo/Stored Procedures/ProviderOrganization_ReadCountByOrganizationIds.sql b/src/Sql/dbo/Stored Procedures/ProviderOrganization_ReadCountByOrganizationIds.sql deleted file mode 100644 index 43ee0dcce..000000000 --- a/src/Sql/dbo/Stored Procedures/ProviderOrganization_ReadCountByOrganizationIds.sql +++ /dev/null @@ -1,18 +0,0 @@ -CREATE PROCEDURE [dbo].[ProviderOrganization_ReadCountByOrganizationIds] - @Ids AS [dbo].[GuidIdArray] READONLY -AS -BEGIN - SET NOCOUNT ON - - IF (SELECT COUNT(1) FROM @Ids) < 1 - BEGIN - RETURN(-1) - END - - SELECT - COUNT(1) - FROM - [dbo].[ProviderOrganizationView] - WHERE - [OrganizationId] IN (SELECT [Id] FROM @Ids) -END \ No newline at end of file diff --git a/util/Migrator/DbScripts/2023-03-22_00_ProviderAddExistingOrganizations.sql b/util/Migrator/DbScripts/2023-03-22_00_ProviderAddExistingOrganizations.sql index 76035d101..ea74b7b74 100644 --- a/util/Migrator/DbScripts/2023-03-22_00_ProviderAddExistingOrganizations.sql +++ b/util/Migrator/DbScripts/2023-03-22_00_ProviderAddExistingOrganizations.sql @@ -52,23 +52,3 @@ BEGIN END END GO - -CREATE OR ALTER PROCEDURE [dbo].[ProviderOrganization_ReadCountByOrganizationIds] - @Ids AS [dbo].[GuidIdArray] READONLY -AS -BEGIN - SET NOCOUNT ON - - IF (SELECT COUNT(1) FROM @Ids) < 1 - BEGIN - RETURN(-1) - END - - SELECT - COUNT(1) - FROM - [dbo].[ProviderOrganizationView] - WHERE - [OrganizationId] IN (SELECT [Id] FROM @Ids) -END -GO \ No newline at end of file