Skip to content

Commit

Permalink
feat(getcertificate): get company certificates with bpn (#509)
Browse files Browse the repository at this point in the history
Refs: #464
Co-authored-by: Phil Schneider <[email protected]>
Reviewed-by: Phil Schneider <[email protected]>
  • Loading branch information
AnuragNagpure and Phil91 authored Feb 18, 2024
1 parent 90acec2 commit bf15299
Show file tree
Hide file tree
Showing 9 changed files with 274 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,28 @@ await _mailingService.SendMails(requesterEmail, mailParameters, Enumerable.Repea
public IAsyncEnumerable<VerifiedCredentialTypeId> GetCertificateTypes() =>
_portalRepositories.GetInstance<ICompanySsiDetailsRepository>().GetCertificateTypes(_identityData.CompanyId);

/// <inheritdoc />
public async IAsyncEnumerable<CompanyCertificateBpnData> GetCompanyCertificatesByBpn(string businessPartnerNumber)
{
if (string.IsNullOrWhiteSpace(businessPartnerNumber))
{
throw new ControllerArgumentException("businessPartnerNumber must not be empty");
}

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

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

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) =>
Pagination.CreateResponseAsync(
page,
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();

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 @@ -293,6 +293,23 @@ public async Task<NoContentResult> CreateCompanyCertificate([FromForm] CompanyCe
return NoContent();
}

/// <summary>
/// Gets the companyCertificates Details
/// </summary>
/// <returns>the companyCertificates details</returns>
/// <remarks>Example: GET: api/administration/companydata/businessPartnerNumber}/companyCertificates</remarks>
/// <response code="200">Returns the companyCertificates details.</response>
[HttpGet]
[Authorize(Roles = "view_certificates")]
[Authorize(Policy = PolicyTypes.ValidCompany)]
[Route("company/{businessPartnerNumber}/companyCertificates")]
[ProducesResponseType(typeof(IEnumerable<CompanyCertificateBpnData>), StatusCodes.Status200OK)]
[ProducesResponseType(typeof(ErrorResponse), StatusCodes.Status400BadRequest)]
[ProducesResponseType(typeof(ErrorResponse), StatusCodes.Status404NotFound)]
[ProducesResponseType(typeof(ErrorResponse), StatusCodes.Status409Conflict)]
public IAsyncEnumerable<CompanyCertificateBpnData> GetCompanyCertificatesByBpn(string businessPartnerNumber) =>
_logic.GetCompanyCertificatesByBpn(businessPartnerNumber);

/// <summary>
/// Retrieves all company certificates with respect userId.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/********************************************************************************
* Copyright (c) 2021, 2024 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
********************************************************************************/

using Org.Eclipse.TractusX.Portal.Backend.PortalBackend.PortalEntities.Enums;
using System.Reflection.Metadata;

namespace Org.Eclipse.TractusX.Portal.Backend.PortalBackend.DBAccess.Models;

public record CompanyCertificateBpnData(
CompanyCertificateTypeId CompanyCertificateType,
CompanyCertificateStatusId CompanyCertificateStatus,
Guid DocumentId,
DateTimeOffset ValidFrom,
DateTimeOffset? ValidTill
);
Original file line number Diff line number Diff line change
@@ -1,45 +1,45 @@
<!--
- 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>
<RootNamespace>Org.Eclipse.TractusX.Portal.Backend.PortalBackend.DBAccess</RootNamespace>
<TargetFramework>net7.0</TargetFramework>
<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>
<!--
- 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>
<RootNamespace>Org.Eclipse.TractusX.Portal.Backend.PortalBackend.DBAccess</RootNamespace>
<TargetFramework>net7.0</TargetFramework>
<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>
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
using Org.Eclipse.TractusX.Portal.Backend.Framework.Models;
using Org.Eclipse.TractusX.Portal.Backend.PortalBackend.DBAccess.Models;
using Org.Eclipse.TractusX.Portal.Backend.PortalBackend.PortalEntities;
using Org.Eclipse.TractusX.Portal.Backend.PortalBackend.PortalEntities.Entities;
using Org.Eclipse.TractusX.Portal.Backend.PortalBackend.PortalEntities.Enums;

namespace Org.Eclipse.TractusX.Portal.Backend.PortalBackend.DBAccess.Repositories;
Expand Down Expand Up @@ -52,6 +53,25 @@ public CompanyCertificate CreateCompanyCertificate(Guid companyId, CompanyCertif
return _context.CompanyCertificates.Add(companyCertificate).Entity;
}

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

/// <inheritdoc />
public IAsyncEnumerable<CompanyCertificateBpnData> GetCompanyCertificateData(Guid companyId) =>
_context.CompanyCertificates
.Where(x => x.CompanyId == companyId && x.CompanyCertificateStatusId == CompanyCertificateStatusId.ACTIVE)
.Select(ccb => new CompanyCertificateBpnData(
ccb.CompanyCertificateTypeId,
ccb.CompanyCertificateStatusId,
ccb.DocumentId,
ccb.ValidFrom,
ccb.ValidTill))
.ToAsyncEnumerable();

public Func<int, int, Task<Pagination.Source<CompanyCertificateData>?>> GetActiveCompanyCertificatePaginationSource(CertificateSorting? sorting, CompanyCertificateStatusId? certificateStatus, CompanyCertificateTypeId? certificateType, Guid companyId) =>
(skip, take) => Pagination.CreateSourceQueryAsync(
skip,
Expand All @@ -60,7 +80,6 @@ public CompanyCertificate CreateCompanyCertificate(Guid companyId, CompanyCertif
.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 @@ -19,7 +19,9 @@

using Org.Eclipse.TractusX.Portal.Backend.Framework.Models;
using Org.Eclipse.TractusX.Portal.Backend.PortalBackend.DBAccess.Models;
using Org.Eclipse.TractusX.Portal.Backend.PortalBackend.DBAccess.Models;
using Org.Eclipse.TractusX.Portal.Backend.PortalBackend.PortalEntities;
using Org.Eclipse.TractusX.Portal.Backend.PortalBackend.PortalEntities.Entities;
using Org.Eclipse.TractusX.Portal.Backend.PortalBackend.PortalEntities.Enums;

namespace Org.Eclipse.TractusX.Portal.Backend.PortalBackend.DBAccess.Repositories;
Expand All @@ -43,6 +45,20 @@ public interface ICompanyCertificateRepository
/// <returns>The created entity</returns>
CompanyCertificate CreateCompanyCertificate(Guid companyId, CompanyCertificateTypeId companyCertificateTypeId, Guid docId, Action<CompanyCertificate>? setOptionalFields = null);

/// <summary>
/// Get companyId against businessPartnerNumber
/// </summary>
/// <param name="businessPartnerNumber">bpn Id</param>
/// <returns>company entity</returns>
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>
IAsyncEnumerable<CompanyCertificateBpnData> GetCompanyCertificateData(Guid companyId);

/// <summary>
/// Gets all company certificate data from the persistence storage as pagination
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1071,6 +1071,78 @@ public async Task CheckCompanyCertificateType_WithInvalidCall_ThrowsControllerAr
}
#endregion

#region GetCompanyCertificateWithBpnNumber

[Fact]
public async Task GetCompanyCertificateWithNullOrEmptyBpn_ReturnsExpected()
{
// Act
async Task Act() => await _sut.GetCompanyCertificatesByBpn(string.Empty).ToListAsync().ConfigureAwait(false);

// Assert
var error = await Assert.ThrowsAsync<ControllerArgumentException>(Act).ConfigureAwait(false);
error.Message.Should().StartWith("businessPartnerNumber must not be empty");
}

[Fact]
public async Task GetCompanyCertificateWithNoCompanyId_ReturnsExpected()
{
// Arrange
var companyId = Guid.Empty;
var businessPartnerNumber = "BPNL07800HZ01644";

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

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

// Assert
var error = await Assert.ThrowsAsync<ControllerArgumentException>(Act).ConfigureAwait(false);
error.Message.Should().StartWith($"company does not exist for {businessPartnerNumber}");
}

[Fact]
public async Task GetCompanyCertificateWithBpnNumber_WithValidRequest_ReturnsExpected()
{
// Arrange
var companyId = Guid.NewGuid();
var data = _fixture.Build<CompanyCertificateBpnData>()
.With(x => x.CompanyCertificateStatus, CompanyCertificateStatusId.ACTIVE)
.With(x => x.CompanyCertificateType, CompanyCertificateTypeId.ISO_9001)
.With(x => x.DocumentId, Guid.NewGuid())
.With(x => x.ValidFrom, DateTime.UtcNow)
.With(x => x.ValidTill, DateTime.UtcNow)
.CreateMany(5).ToAsyncEnumerable();
A.CallTo(() => _companyCertificateRepository.GetCompanyIdByBpn("BPNL07800HZ01643"))
.Returns(companyId);
A.CallTo(() => _companyCertificateRepository.GetCompanyCertificateData(companyId))
.Returns(data);

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

// Assert
result.Should().HaveCount(5);
}

[Fact]
public async Task GetCompanyCertificateWithBpnNumber_WithEmptyResult_ReturnsExpected()
{
// Arrange
var companyId = Guid.NewGuid();
A.CallTo(() => _companyCertificateRepository.GetCompanyIdByBpn("BPNL07800HZ01643"))
.Returns(companyId);

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

// Assert
result.Should().BeEmpty();
}

#endregion

#region GetCredentials

[Fact]
Expand Down
Loading

0 comments on commit bf15299

Please sign in to comment.