Skip to content

Commit

Permalink
feat(certificate): adjust get certificate data
Browse files Browse the repository at this point in the history
  • Loading branch information
Phil91 committed Feb 17, 2024
1 parent c101bba commit 3a5a3ee
Show file tree
Hide file tree
Showing 10 changed files with 76 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
-
- SPDX-License-Identifier: Apache-2.0
-->

<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<AssemblyName>Org.Eclipse.TractusX.Portal.Backend.Administration.Service</AssemblyName>
<RootNamespace>Org.Eclipse.TractusX.Portal.Backend.Administration.Service</RootNamespace>
Expand All @@ -30,6 +32,7 @@
<GenerateDocumentationFile>True</GenerateDocumentationFile>
<NoWarn>CS1591</NoWarn>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.10">
<PrivateAssets>all</PrivateAssets>
Expand All @@ -38,6 +41,7 @@
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.19.5" />
<PackageReference Include="PasswordGenerator" Version="2.1.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\externalsystems\Bpdm.Library\Bpdm.Library.csproj" />
<ProjectReference Include="..\..\externalsystems\OnboardingServiceProvider.Library\OnboardingServiceProvider.Library.csproj" />
Expand All @@ -64,6 +68,7 @@
<ProjectReference Include="..\..\web\Web.PublicInfos\Web.PublicInfos.csproj" />
<ProjectReference Include="..\..\framework\Framework.ErrorHandling.Controller\Framework.ErrorHandling.Controller.csproj" />
</ItemGroup>

<ItemGroup>
<Content Include="../../../LICENSE">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
Expand All @@ -75,9 +80,11 @@
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>

<ItemGroup>
<SonarQubeSetting Include="sonar.coverage.exclusions">
<Value>Program.cs</Value>
</SonarQubeSetting>
</ItemGroup>
</Project>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,7 @@ public IAsyncEnumerable<VerifiedCredentialTypeId> GetCertificateTypes() =>
_portalRepositories.GetInstance<ICompanySsiDetailsRepository>().GetCertificateTypes(_identityData.CompanyId);

/// <inheritdoc />
public async Task<IEnumerable<CompanyCertificateBpnData>> GetCompanyCertificatesBpnOthers(string businessPartnerNumber)
public async IAsyncEnumerable<CompanyCertificateBpnData> GetCompanyCertificatesByBpn(string businessPartnerNumber)
{
if (string.IsNullOrWhiteSpace(businessPartnerNumber))
{
Expand All @@ -588,13 +588,16 @@ public async Task<IEnumerable<CompanyCertificateBpnData>> GetCompanyCertificates

var companyCertificateRepository = _portalRepositories.GetInstance<ICompanyCertificateRepository>();

var companyId = await companyCertificateRepository.GetCompanyId(businessPartnerNumber).ConfigureAwait(false);
if (companyId == null)
var companyId = await companyCertificateRepository.GetCompanyIdByBpn(businessPartnerNumber).ConfigureAwait(false);
if (companyId == Guid.Empty)
{
throw new ControllerArgumentException($"company does not exist for {businessPartnerNumber}");
}

return await companyCertificateRepository.GetCompanyCertificateData(companyId.Id);
await foreach (var data in companyCertificateRepository.GetCompanyCertificateData(companyId))
{
yield return data;
}
}

public Task<Pagination.Response<CompanyCertificateData>> GetAllCompanyCertificatesAsync(int page, int size, CertificateSorting? sorting, CompanyCertificateStatusId? certificateStatus, CompanyCertificateTypeId? certificateType) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public interface ICompanyDataBusinessLogic
Task RejectCredential(Guid credentialId);

IAsyncEnumerable<VerifiedCredentialTypeId> GetCertificateTypes();
Task<IEnumerable<CompanyCertificateBpnData>> GetCompanyCertificatesBpnOthers(string businessPartnerNumber);
IAsyncEnumerable<CompanyCertificateBpnData> GetCompanyCertificatesByBpn(string businessPartnerNumber);
Task CreateCompanyCertificate(CompanyCertificateCreationData data, CancellationToken cancellationToken);

Task<Pagination.Response<CompanyCertificateData>> GetAllCompanyCertificatesAsync(int page, int size, CertificateSorting? sorting, CompanyCertificateStatusId? certificateStatus, CompanyCertificateTypeId? certificateType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -303,12 +303,12 @@ public async Task<NoContentResult> CreateCompanyCertificate([FromForm] CompanyCe
[Authorize(Roles = "view_certificates")]
[Authorize(Policy = PolicyTypes.ValidCompany)]
[Route("company/{businessPartnerNumber}/companyCertificates")]
[ProducesResponseType(typeof(CompanyRoleConsentViewData), StatusCodes.Status200OK)]
[ProducesResponseType(typeof(IEnumerable<CompanyCertificateBpnData>), StatusCodes.Status200OK)]
[ProducesResponseType(typeof(ErrorResponse), StatusCodes.Status400BadRequest)]
[ProducesResponseType(typeof(ErrorResponse), StatusCodes.Status404NotFound)]
[ProducesResponseType(typeof(ErrorResponse), StatusCodes.Status409Conflict)]
public Task<IEnumerable<CompanyCertificateBpnData>> GetCompanyCertificatesBpn(string businessPartnerNumber) =>
_logic.GetCompanyCertificatesBpnOthers(businessPartnerNumber);
public IAsyncEnumerable<CompanyCertificateBpnData> GetCompanyCertificatesByBpn(string businessPartnerNumber) =>
_logic.GetCompanyCertificatesByBpn(businessPartnerNumber);

/// <summary>
/// Retrieves all company certificates with respect userId.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
namespace Org.Eclipse.TractusX.Portal.Backend.PortalBackend.DBAccess.Models;

public record CompanyCertificateBpnData(
CompanyCertificateTypeId companyCertificateType,
CompanyCertificateStatusId companyCertificateStatus,
Guid documentId,
DateTimeOffset validFrom,
DateTimeOffset? validTill
CompanyCertificateTypeId CompanyCertificateType,
CompanyCertificateStatusId CompanyCertificateStatus,
Guid DocumentId,
DateTimeOffset ValidFrom,
DateTimeOffset? ValidTill
);
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
<!--
- Copyright (c) 2021, 2023 BMW Group AG
- Copyright (c) 2021, 2023 Contributors to the Eclipse Foundation
-
- See the NOTICE file(s) distributed with this work for additional
- information regarding copyright ownership.
-
- This program and the accompanying materials are made available under the
- terms of the Apache License, Version 2.0 which is available at
- https://www.apache.org/licenses/LICENSE-2.0.
-
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
- License for the specific language governing permissions and limitations
- under the License.
-
- SPDX-License-Identifier: Apache-2.0
<!--
- Copyright (c) 2021, 2023 BMW Group AG
- Copyright (c) 2021, 2023 Contributors to the Eclipse Foundation
-
- See the NOTICE file(s) distributed with this work for additional
- information regarding copyright ownership.
-
- This program and the accompanying materials are made available under the
- terms of the Apache License, Version 2.0 which is available at
- https://www.apache.org/licenses/LICENSE-2.0.
-
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
- License for the specific language governing permissions and limitations
- under the License.
-
- SPDX-License-Identifier: Apache-2.0
-->

<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<AssemblyName>Org.Eclipse.TractusX.Portal.Backend.PortalBackend.DBAccess</AssemblyName>
Expand All @@ -25,17 +26,20 @@
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\framework\Framework.Models\Framework.Models.csproj" />
<ProjectReference Include="..\..\framework\Framework.Seeding\Framework.Seeding.csproj" />
<ProjectReference Include="..\PortalBackend.PortalEntities\PortalBackend.PortalEntities.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Diagnostics.HealthChecks.EntityFrameworkCore" Version="7.0.10" />
<PackageReference Include="System.Linq.Async" Version="6.0.1" />
</ItemGroup>

<ItemGroup>
<Compile Remove="Models\SsiCertificateData.cs" />
<Compile Remove="Models\UseCaseParticipationData.cs" />
</ItemGroup>
</Project>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,14 @@ public CompanyCertificate CreateCompanyCertificate(Guid companyId, CompanyCertif
}

/// <inheritdoc />
public Task<Company?> GetCompanyId(string businessPartnerNumber) =>
_context.Companies.Where(x => x.BusinessPartnerNumber == businessPartnerNumber).SingleOrDefaultAsync();
public Task<Guid> GetCompanyIdByBpn(string businessPartnerNumber) =>
_context.Companies
.Where(x => x.BusinessPartnerNumber == businessPartnerNumber)
.Select(x => x.Id)
.SingleOrDefaultAsync();

/// <inheritdoc />
public async Task<IEnumerable<CompanyCertificateBpnData>> GetCompanyCertificateData(Guid companyId) =>
public IAsyncEnumerable<CompanyCertificateBpnData> GetCompanyCertificateData(Guid companyId) =>
_context.CompanyCertificates
.Where(x => x.CompanyId == companyId && x.CompanyCertificateStatusId == CompanyCertificateStatusId.ACTIVE)
.Select(ccb => new CompanyCertificateBpnData(
Expand All @@ -67,7 +70,7 @@ public async Task<IEnumerable<CompanyCertificateBpnData>> GetCompanyCertificateD
ccb.DocumentId,
ccb.ValidFrom,
ccb.ValidTill))
.AsEnumerable();
.ToAsyncEnumerable();

public Func<int, int, Task<Pagination.Source<CompanyCertificateData>?>> GetActiveCompanyCertificatePaginationSource(CertificateSorting? sorting, CompanyCertificateStatusId? certificateStatus, CompanyCertificateTypeId? certificateType, Guid companyId) =>
(skip, take) => Pagination.CreateSourceQueryAsync(
Expand All @@ -77,7 +80,6 @@ public async Task<IEnumerable<CompanyCertificateBpnData>> GetCompanyCertificateD
.AsNoTracking()
.Where(x =>
x.CompanyId == companyId &&
x.CompanyCertificateStatusId == CompanyCertificateStatusId.ACTIVE &&
(certificateStatus == null || x.CompanyCertificateStatusId == certificateStatus) &&
(certificateType == null || x.CompanyCertificateTypeId == certificateType))
.GroupBy(x => x.CompanyId),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,14 @@ public interface ICompanyCertificateRepository
/// </summary>
/// <param name="businessPartnerNumber">bpn Id</param>
/// <returns>company entity</returns>
Task<Company?> GetCompanyId(string businessPartnerNumber);
Task<Guid> GetCompanyIdByBpn(string businessPartnerNumber);

/// <summary>
/// Gets company certificate details
/// </summary>
/// <param name="companyId">Id of the company</param>
/// <returns>Returns the CompanyCertificateBpnData Details</returns>
Task<IEnumerable<CompanyCertificateBpnData>> GetCompanyCertificateData(Guid companyId);
IAsyncEnumerable<CompanyCertificateBpnData> GetCompanyCertificateData(Guid companyId);

/// <summary>
/// Gets all company certificate data from the persistence storage as pagination
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1076,11 +1076,8 @@ public async Task CheckCompanyCertificateType_WithInvalidCall_ThrowsControllerAr
[Fact]
public async Task GetCompanyCertificateWithNullOrEmptyBpn_ReturnsExpected()
{
// Arrange
var companyId = Guid.NewGuid();

// Act
async Task Act() => await _sut.GetCompanyCertificatesBpnOthers(string.Empty).ConfigureAwait(false);
async Task Act() => await _sut.GetCompanyCertificatesByBpn(string.Empty).ToListAsync().ConfigureAwait(false);

// Assert
var error = await Assert.ThrowsAsync<ControllerArgumentException>(Act).ConfigureAwait(false);
Expand All @@ -1091,15 +1088,14 @@ public async Task GetCompanyCertificateWithNullOrEmptyBpn_ReturnsExpected()
public async Task GetCompanyCertificateWithNoCompanyId_ReturnsExpected()
{
// Arrange
var companyId = Guid.NewGuid();
var companyId = Guid.Empty;
var businessPartnerNumber = "BPNL07800HZ01644";
Company company = null;

A.CallTo(() => _companyCertificateRepository.GetCompanyId(businessPartnerNumber))
.Returns(company);
A.CallTo(() => _companyCertificateRepository.GetCompanyIdByBpn(businessPartnerNumber))
.Returns(companyId);

// Act
async Task Act() => await _sut.GetCompanyCertificatesBpnOthers(businessPartnerNumber).ConfigureAwait(false);
async Task Act() => await _sut.GetCompanyCertificatesByBpn(businessPartnerNumber).ToListAsync().ConfigureAwait(false);

// Assert
var error = await Assert.ThrowsAsync<ControllerArgumentException>(Act).ConfigureAwait(false);
Expand All @@ -1117,14 +1113,14 @@ public async Task GetCompanyCertificateWithBpnNumber_WithValidRequest_ReturnsExp
.With(x => x.documentId, Guid.NewGuid())
.With(x => x.validFrom, DateTime.UtcNow)
.With(x => x.validTill, DateTime.UtcNow)
.CreateMany(5).AsEnumerable();
A.CallTo(() => _companyCertificateRepository.GetCompanyId("BPNL07800HZ01643"))
.Returns(new Company(companyId, "test", CompanyStatusId.ACTIVE, DateTime.UtcNow));
.CreateMany(5).ToAsyncEnumerable();
A.CallTo(() => _companyCertificateRepository.GetCompanyIdByBpn("BPNL07800HZ01643"))
.Returns(companyId);
A.CallTo(() => _companyCertificateRepository.GetCompanyCertificateData(companyId))
.Returns(data);

// Act
var result = await _sut.GetCompanyCertificatesBpnOthers("BPNL07800HZ01643").ConfigureAwait(false);
var result = await _sut.GetCompanyCertificatesByBpn("BPNL07800HZ01643").ToListAsync().ConfigureAwait(false);

// Assert
result.Should().HaveCount(5);
Expand All @@ -1135,11 +1131,11 @@ public async Task GetCompanyCertificateWithBpnNumber_WithEmptyResult_ReturnsExpe
{
// Arrange
var companyId = Guid.NewGuid();
A.CallTo(() => _companyCertificateRepository.GetCompanyId("BPNL07800HZ01643"))
.Returns(new Company(companyId, "test", CompanyStatusId.ACTIVE, DateTime.UtcNow));
A.CallTo(() => _companyCertificateRepository.GetCompanyIdByBpn("BPNL07800HZ01643"))
.Returns(companyId);

// Act
var result = await _sut.GetCompanyCertificatesBpnOthers("BPNL07800HZ01643").ConfigureAwait(false);
var result = await _sut.GetCompanyCertificatesByBpn("BPNL07800HZ01643").ToListAsync().ConfigureAwait(false);

// Assert
result.Should().BeEmpty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ public async Task GetAllCertificates_ReturnsExpectedResult(CertificateSorting so

// Assert
companyCertificateDetail.Should().NotBeNull();
companyCertificateDetail!.Count.Should().Be(6);
companyCertificateDetail.Data.Should().HaveCount(6);
companyCertificateDetail!.Count.Should().Be(8);
companyCertificateDetail.Data.Should().HaveCount(8);
if (sorting == CertificateSorting.CertificateTypeAsc)
{
companyCertificateDetail.Data.Select(data => data.companyCertificateType).Should().BeInAscendingOrder();
Expand Down Expand Up @@ -148,12 +148,11 @@ public async Task GetCompanyId_WithExistingData()
var sut = await CreateSut();

// Act
var result = await sut.GetCompanyId("BPNL07800HZ01643").ConfigureAwait(false);
var result = await sut.GetCompanyIdByBpn("BPNL07800HZ01643").ConfigureAwait(false);

// Assert
result.Should().NotBeNull();
result.Should().BeOfType<Company>();
result.Id.Should().Be("3390c2d7-75c1-4169-aa27-6ce00e1f3cdd");
result.Should().NotBe(Guid.Empty);
result.Should().Be(new Guid("3390c2d7-75c1-4169-aa27-6ce00e1f3cdd"));
}

[Fact]
Expand All @@ -163,10 +162,10 @@ public async Task GetCompanyId_WithNoExistingData()
var sut = await CreateSut();

// Act
var result = await sut.GetCompanyId("BPNL07800HZ01644").ConfigureAwait(false);
var result = await sut.GetCompanyIdByBpn("BPNL07800HZ01644").ConfigureAwait(false);

// Assert
result.Should().BeNull();
result.Should().Be(Guid.Empty);
}

[Fact]
Expand All @@ -176,7 +175,7 @@ public async Task GetCompanyCertificateData_NoResults_ReturnsExpected()
var sut = await CreateSut();

// Act
var result = await sut.GetCompanyCertificateData(Guid.NewGuid()).ConfigureAwait(false);
var result = await sut.GetCompanyCertificateData(Guid.NewGuid()).ToListAsync().ConfigureAwait(false);

// Assert
result.Should().BeEmpty();
Expand Down

0 comments on commit 3a5a3ee

Please sign in to comment.