mirror of
https://github.com/bitwarden/server.git
synced 2025-07-01 16:12:49 -05:00
[AC-432] Checking if any of the selected Organizations is already assigned to a Provider
This commit is contained in:
@ -370,7 +370,13 @@ public class ProviderService : IProviderService
|
||||
var provider = await _providerRepository.GetByIdAsync(providerId);
|
||||
if (provider.Type != ProviderType.Reseller)
|
||||
{
|
||||
throw new BadRequestException("Organization must be of type Reseller in order to assign Organizations to it.");
|
||||
throw new BadRequestException("Provider must be of type Reseller in order to assign Organizations to it.");
|
||||
}
|
||||
|
||||
var existingProviderOrganizations = await _providerOrganizationRepository.GetManyByOrganizationIdsAsync(organizationIds);
|
||||
if (existingProviderOrganizations.Any())
|
||||
{
|
||||
throw new BadRequestException("Organizations must not be assigned to any Provider.");
|
||||
}
|
||||
|
||||
var providerOrganizationsToInsert = organizationIds.Select(orgId => new ProviderOrganization { ProviderId = providerId, OrganizationId = orgId });
|
||||
|
@ -9,4 +9,5 @@ public interface IProviderOrganizationRepository : IRepository<ProviderOrganizat
|
||||
Task<ICollection<ProviderOrganizationOrganizationDetails>> GetManyDetailsByProviderAsync(Guid providerId);
|
||||
Task<ProviderOrganization> GetByOrganizationId(Guid organizationId);
|
||||
Task<IEnumerable<ProviderOrganizationProviderDetails>> GetManyByUserAsync(Guid userId);
|
||||
Task<IEnumerable<ProviderOrganization>> GetManyByOrganizationIdsAsync(IEnumerable<Guid> organizationIds);
|
||||
}
|
||||
|
@ -99,6 +99,20 @@ public class ProviderOrganizationRepository : Repository<ProviderOrganization, G
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<ProviderOrganization>> GetManyByOrganizationIdsAsync(
|
||||
IEnumerable<Guid> organizationIds)
|
||||
{
|
||||
using (var connection = new SqlConnection(ConnectionString))
|
||||
{
|
||||
var results = await connection.QueryAsync<ProviderOrganization>(
|
||||
$"[{Schema}].[ProviderOrganization_ReadByOrganizationIds]",
|
||||
new { Ids = organizationIds.ToGuidIdArrayTVP() },
|
||||
commandType: CommandType.StoredProcedure);
|
||||
|
||||
return results.ToList();
|
||||
}
|
||||
}
|
||||
|
||||
private DataTable BuildProviderOrganizationsTable(SqlBulkCopy bulkCopy, IEnumerable<ProviderOrganization> providerOrganizations)
|
||||
{
|
||||
var po = providerOrganizations.FirstOrDefault();
|
||||
|
@ -67,4 +67,16 @@ public class ProviderOrganizationRepository :
|
||||
return data;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<ProviderOrganization>> GetManyByOrganizationIdsAsync(IEnumerable<Guid> organizationIds)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var query = from po in dbContext.ProviderOrganizations
|
||||
where organizationIds.Contains(po.OrganizationId)
|
||||
select po;
|
||||
return await query.ToListAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -264,6 +264,7 @@
|
||||
<Build Include="dbo\Stored Procedures\Policy_Update.sql" />
|
||||
<Build Include="dbo\Stored Procedures\ProviderOrganizationOrganizationDetails_ReadByProviderId.sql" />
|
||||
<Build Include="dbo\Stored Procedures\ProviderOrganizationProviderDetails_ReadByUserId.sql" />
|
||||
<Build Include="dbo\Stored Procedures\ProviderOrganization_ReadByOrganizationIds.sql" />
|
||||
<Build Include="dbo\Stored Procedures\Provider_ReadByOrganizationId.sql" />
|
||||
<Build Include="dbo\Stored Procedures\Organization_UnassignedToProviderSearch.sql" />
|
||||
<Build Include="dbo\Stored Procedures\ProviderOrganization_Create.sql" />
|
||||
|
@ -0,0 +1,18 @@
|
||||
CREATE PROCEDURE [dbo].[ProviderOrganization_ReadByOrganizationIds]
|
||||
@Ids AS [dbo].[GuidIdArray] READONLY
|
||||
AS
|
||||
BEGIN
|
||||
SET NOCOUNT ON
|
||||
|
||||
IF (SELECT COUNT(1) FROM @Ids) < 1
|
||||
BEGIN
|
||||
RETURN(-1)
|
||||
END
|
||||
|
||||
SELECT
|
||||
*
|
||||
FROM
|
||||
[dbo].[ProviderOrganizationView]
|
||||
WHERE
|
||||
[OrganizationId] IN (SELECT [Id] FROM @Ids)
|
||||
END
|
@ -51,4 +51,24 @@ BEGIN
|
||||
FETCH NEXT @Take ROWS ONLY
|
||||
END
|
||||
END
|
||||
GO
|
||||
|
||||
CREATE OR ALTER PROCEDURE [dbo].[ProviderOrganization_ReadByOrganizationIds]
|
||||
@Ids AS [dbo].[GuidIdArray] READONLY
|
||||
AS
|
||||
BEGIN
|
||||
SET NOCOUNT ON
|
||||
|
||||
IF (SELECT COUNT(1) FROM @Ids) < 1
|
||||
BEGIN
|
||||
RETURN(-1)
|
||||
END
|
||||
|
||||
SELECT
|
||||
*
|
||||
FROM
|
||||
[dbo].[ProviderOrganizationView]
|
||||
WHERE
|
||||
[OrganizationId] IN (SELECT [Id] FROM @Ids)
|
||||
END
|
||||
GO
|
Reference in New Issue
Block a user