From 3a5a3ee381263fd8fe55d39e3f7dfc29ec25c5b4 Mon Sep 17 00:00:00 2001 From: Phil Schneider Date: Sun, 18 Feb 2024 00:48:28 +0100 Subject: [PATCH] feat(certificate): adjust get certificate data Refs: #464 --- .../Administration.Service.csproj | 9 +++- .../BusinessLogic/CompanyDataBusinessLogic.cs | 11 +++-- .../ICompanyDataBusinessLogic.cs | 2 +- .../Controllers/CompanyDataController.cs | 6 +-- .../Models/CompanyCertificateBpnData.cs | 10 ++--- .../PortalBackend.DBAccess.csproj | 42 ++++++++++--------- .../CompanyCertificateRepository.cs | 12 +++--- .../ICompanyCertificateRepository.cs | 4 +- .../CompanyDataBusinessLogicTests.cs | 28 ++++++------- .../CompanyCertificateRepositoryTests.cs | 17 ++++---- 10 files changed, 76 insertions(+), 65 deletions(-) diff --git a/src/administration/Administration.Service/Administration.Service.csproj b/src/administration/Administration.Service/Administration.Service.csproj index 6aa6780ef1..caf0d3dd04 100644 --- a/src/administration/Administration.Service/Administration.Service.csproj +++ b/src/administration/Administration.Service/Administration.Service.csproj @@ -17,7 +17,9 @@ - - SPDX-License-Identifier: Apache-2.0 --> + + Org.Eclipse.TractusX.Portal.Backend.Administration.Service Org.Eclipse.TractusX.Portal.Backend.Administration.Service @@ -30,6 +32,7 @@ True CS1591 + all @@ -38,6 +41,7 @@ + @@ -64,6 +68,7 @@ + PreserveNewest @@ -75,9 +80,11 @@ PreserveNewest + Program.cs - \ No newline at end of file + + diff --git a/src/administration/Administration.Service/BusinessLogic/CompanyDataBusinessLogic.cs b/src/administration/Administration.Service/BusinessLogic/CompanyDataBusinessLogic.cs index 0a4230854b..a30af520ee 100644 --- a/src/administration/Administration.Service/BusinessLogic/CompanyDataBusinessLogic.cs +++ b/src/administration/Administration.Service/BusinessLogic/CompanyDataBusinessLogic.cs @@ -579,7 +579,7 @@ public IAsyncEnumerable GetCertificateTypes() => _portalRepositories.GetInstance().GetCertificateTypes(_identityData.CompanyId); /// - public async Task> GetCompanyCertificatesBpnOthers(string businessPartnerNumber) + public async IAsyncEnumerable GetCompanyCertificatesByBpn(string businessPartnerNumber) { if (string.IsNullOrWhiteSpace(businessPartnerNumber)) { @@ -588,13 +588,16 @@ public async Task> GetCompanyCertificates var companyCertificateRepository = _portalRepositories.GetInstance(); - 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> GetAllCompanyCertificatesAsync(int page, int size, CertificateSorting? sorting, CompanyCertificateStatusId? certificateStatus, CompanyCertificateTypeId? certificateType) => diff --git a/src/administration/Administration.Service/BusinessLogic/ICompanyDataBusinessLogic.cs b/src/administration/Administration.Service/BusinessLogic/ICompanyDataBusinessLogic.cs index 9eac6b1186..f99c8629ad 100644 --- a/src/administration/Administration.Service/BusinessLogic/ICompanyDataBusinessLogic.cs +++ b/src/administration/Administration.Service/BusinessLogic/ICompanyDataBusinessLogic.cs @@ -53,7 +53,7 @@ public interface ICompanyDataBusinessLogic Task RejectCredential(Guid credentialId); IAsyncEnumerable GetCertificateTypes(); - Task> GetCompanyCertificatesBpnOthers(string businessPartnerNumber); + IAsyncEnumerable GetCompanyCertificatesByBpn(string businessPartnerNumber); Task CreateCompanyCertificate(CompanyCertificateCreationData data, CancellationToken cancellationToken); Task> GetAllCompanyCertificatesAsync(int page, int size, CertificateSorting? sorting, CompanyCertificateStatusId? certificateStatus, CompanyCertificateTypeId? certificateType); diff --git a/src/administration/Administration.Service/Controllers/CompanyDataController.cs b/src/administration/Administration.Service/Controllers/CompanyDataController.cs index c171ff5970..a29ae89001 100644 --- a/src/administration/Administration.Service/Controllers/CompanyDataController.cs +++ b/src/administration/Administration.Service/Controllers/CompanyDataController.cs @@ -303,12 +303,12 @@ public async Task CreateCompanyCertificate([FromForm] CompanyCe [Authorize(Roles = "view_certificates")] [Authorize(Policy = PolicyTypes.ValidCompany)] [Route("company/{businessPartnerNumber}/companyCertificates")] - [ProducesResponseType(typeof(CompanyRoleConsentViewData), StatusCodes.Status200OK)] + [ProducesResponseType(typeof(IEnumerable), StatusCodes.Status200OK)] [ProducesResponseType(typeof(ErrorResponse), StatusCodes.Status400BadRequest)] [ProducesResponseType(typeof(ErrorResponse), StatusCodes.Status404NotFound)] [ProducesResponseType(typeof(ErrorResponse), StatusCodes.Status409Conflict)] - public Task> GetCompanyCertificatesBpn(string businessPartnerNumber) => - _logic.GetCompanyCertificatesBpnOthers(businessPartnerNumber); + public IAsyncEnumerable GetCompanyCertificatesByBpn(string businessPartnerNumber) => + _logic.GetCompanyCertificatesByBpn(businessPartnerNumber); /// /// Retrieves all company certificates with respect userId. diff --git a/src/portalbackend/PortalBackend.DBAccess/Models/CompanyCertificateBpnData.cs b/src/portalbackend/PortalBackend.DBAccess/Models/CompanyCertificateBpnData.cs index 20810f1504..ba98c67a83 100644 --- a/src/portalbackend/PortalBackend.DBAccess/Models/CompanyCertificateBpnData.cs +++ b/src/portalbackend/PortalBackend.DBAccess/Models/CompanyCertificateBpnData.cs @@ -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 ); diff --git a/src/portalbackend/PortalBackend.DBAccess/PortalBackend.DBAccess.csproj b/src/portalbackend/PortalBackend.DBAccess/PortalBackend.DBAccess.csproj index bfc4619d45..9a86f3e25a 100644 --- a/src/portalbackend/PortalBackend.DBAccess/PortalBackend.DBAccess.csproj +++ b/src/portalbackend/PortalBackend.DBAccess/PortalBackend.DBAccess.csproj @@ -1,22 +1,23 @@ - + Org.Eclipse.TractusX.Portal.Backend.PortalBackend.DBAccess @@ -25,17 +26,20 @@ enable enable + + + - \ No newline at end of file + diff --git a/src/portalbackend/PortalBackend.DBAccess/Repositories/CompanyCertificateRepository.cs b/src/portalbackend/PortalBackend.DBAccess/Repositories/CompanyCertificateRepository.cs index f167610a04..a91fc8f23e 100644 --- a/src/portalbackend/PortalBackend.DBAccess/Repositories/CompanyCertificateRepository.cs +++ b/src/portalbackend/PortalBackend.DBAccess/Repositories/CompanyCertificateRepository.cs @@ -54,11 +54,14 @@ public CompanyCertificate CreateCompanyCertificate(Guid companyId, CompanyCertif } /// - public Task GetCompanyId(string businessPartnerNumber) => - _context.Companies.Where(x => x.BusinessPartnerNumber == businessPartnerNumber).SingleOrDefaultAsync(); + public Task GetCompanyIdByBpn(string businessPartnerNumber) => + _context.Companies + .Where(x => x.BusinessPartnerNumber == businessPartnerNumber) + .Select(x => x.Id) + .SingleOrDefaultAsync(); /// - public async Task> GetCompanyCertificateData(Guid companyId) => + public IAsyncEnumerable GetCompanyCertificateData(Guid companyId) => _context.CompanyCertificates .Where(x => x.CompanyId == companyId && x.CompanyCertificateStatusId == CompanyCertificateStatusId.ACTIVE) .Select(ccb => new CompanyCertificateBpnData( @@ -67,7 +70,7 @@ public async Task> GetCompanyCertificateD ccb.DocumentId, ccb.ValidFrom, ccb.ValidTill)) - .AsEnumerable(); + .ToAsyncEnumerable(); public Func?>> GetActiveCompanyCertificatePaginationSource(CertificateSorting? sorting, CompanyCertificateStatusId? certificateStatus, CompanyCertificateTypeId? certificateType, Guid companyId) => (skip, take) => Pagination.CreateSourceQueryAsync( @@ -77,7 +80,6 @@ public async Task> 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), diff --git a/src/portalbackend/PortalBackend.DBAccess/Repositories/ICompanyCertificateRepository.cs b/src/portalbackend/PortalBackend.DBAccess/Repositories/ICompanyCertificateRepository.cs index 3d12d3f5fc..400cd1b1f6 100644 --- a/src/portalbackend/PortalBackend.DBAccess/Repositories/ICompanyCertificateRepository.cs +++ b/src/portalbackend/PortalBackend.DBAccess/Repositories/ICompanyCertificateRepository.cs @@ -50,14 +50,14 @@ public interface ICompanyCertificateRepository /// /// bpn Id /// company entity - Task GetCompanyId(string businessPartnerNumber); + Task GetCompanyIdByBpn(string businessPartnerNumber); /// /// Gets company certificate details /// /// Id of the company /// Returns the CompanyCertificateBpnData Details - Task> GetCompanyCertificateData(Guid companyId); + IAsyncEnumerable GetCompanyCertificateData(Guid companyId); /// /// Gets all company certificate data from the persistence storage as pagination diff --git a/tests/administration/Administration.Service.Tests/BusinessLogic/CompanyDataBusinessLogicTests.cs b/tests/administration/Administration.Service.Tests/BusinessLogic/CompanyDataBusinessLogicTests.cs index 8514f8a35f..29449f92cf 100644 --- a/tests/administration/Administration.Service.Tests/BusinessLogic/CompanyDataBusinessLogicTests.cs +++ b/tests/administration/Administration.Service.Tests/BusinessLogic/CompanyDataBusinessLogicTests.cs @@ -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(Act).ConfigureAwait(false); @@ -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(Act).ConfigureAwait(false); @@ -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); @@ -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(); diff --git a/tests/portalbackend/PortalBackend.DBAccess.Tests/CompanyCertificateRepositoryTests.cs b/tests/portalbackend/PortalBackend.DBAccess.Tests/CompanyCertificateRepositoryTests.cs index 2d15e8d93b..846d866b8c 100644 --- a/tests/portalbackend/PortalBackend.DBAccess.Tests/CompanyCertificateRepositoryTests.cs +++ b/tests/portalbackend/PortalBackend.DBAccess.Tests/CompanyCertificateRepositoryTests.cs @@ -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(); @@ -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(); - 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] @@ -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] @@ -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();