Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(postcertificate): post company certificates #489

Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,41 @@ private async Task HandleSsiCreationAsync(

await _portalRepositories.SaveAsync().ConfigureAwait(false);
}
/// <inheritdoc />
public async Task CreateCompanyCertificate(Guid certificateId, CompanyCertificateCreationData data, CancellationToken cancellationToken)
Phil91 marked this conversation as resolved.
Show resolved Hide resolved
{

Phil91 marked this conversation as resolved.
Show resolved Hide resolved
var documentContentType = data.Document.ContentType.ParseMediaTypeId();
documentContentType.CheckDocumentContentType(_settings.CompanyCertificateMediaTypes);

var companyCertificateRepository = _portalRepositories.GetInstance<ICompanyCertificateRepository>();
if (await companyCertificateRepository.CheckCompanyCertificateId(certificateId).ConfigureAwait(false))
Phil91 marked this conversation as resolved.
Show resolved Hide resolved
{
throw new ControllerArgumentException($"{certificateId} already exists");
}

if (!await companyCertificateRepository.CheckCompanyCertificateType(data.CertificateType).ConfigureAwait(false))
Phil91 marked this conversation as resolved.
Show resolved Hide resolved
{
throw new ControllerArgumentException($"{data.CertificateType} is not assigned to a certificate");
}
await HandleCompanyCertificateCreationAsync(data.CertificateType, data.Document, documentContentType, companyCertificateRepository, data.ExpiryDate, cancellationToken).ConfigureAwait(false);
Phil91 marked this conversation as resolved.
Show resolved Hide resolved
}
/// <inheritdoc />
Phil91 marked this conversation as resolved.
Show resolved Hide resolved
private async Task HandleCompanyCertificateCreationAsync(CompanyCertificateTypeId companyCertificateTypeId, IFormFile document, MediaTypeId mediaTypeId,
ICompanyCertificateRepository companyCertificateRepository, DateTimeOffset? expiryDate, CancellationToken cancellationToken)
{
var (documentContent, hash) = await document.GetContentAndHash(cancellationToken).ConfigureAwait(false);
var doc = _portalRepositories.GetInstance<IDocumentRepository>().CreateDocument(document.FileName, documentContent,
hash, mediaTypeId, DocumentTypeId.COMPANY_CERTIFICATE, x =>
{
x.CompanyUserId = _identityData.IdentityId;
x.DocumentStatusId = DocumentStatusId.PENDING;
});

var companyCertificate = companyCertificateRepository.CreateCompanyCertificateData(_identityData.CompanyId, companyCertificateTypeId, doc.Id, (!expiryDate.HasValue) ? expiryDate : expiryDate.Value.UtcDateTime);
Phil91 marked this conversation as resolved.
Show resolved Hide resolved
Phil91 marked this conversation as resolved.
Show resolved Hide resolved

await _portalRepositories.SaveAsync().ConfigureAwait(false);
}
Phil91 marked this conversation as resolved.
Show resolved Hide resolved
/// <inheritdoc />
public Task<Pagination.Response<CredentialDetailData>> GetCredentials(int page, int size, CompanySsiDetailStatusId? companySsiDetailStatusId, VerifiedCredentialTypeId? credentialTypeId, string? companyName, CompanySsiDetailSorting? sorting)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public CompanyDataSettings()
{
UseCaseParticipationMediaTypes = null!;
SsiCertificateMediaTypes = null!;
CompanyCertificateMediaTypes = null!;
}

/// <summary>
Expand All @@ -48,6 +49,14 @@ public CompanyDataSettings()
[DistinctValues]
public IEnumerable<MediaTypeId> SsiCertificateMediaTypes { get; set; }

/// <summary>
/// The media types that are allowed for the uploaded document for company certificate
/// </summary>
[Required]
[EnumEnumeration]
[DistinctValues]
public IEnumerable<MediaTypeId> CompanyCertificateMediaTypes { get; set; }

/// <summary>
/// The maximum page size
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,5 @@ public interface ICompanyDataBusinessLogic
Task RejectCredential(Guid credentialId);

IAsyncEnumerable<VerifiedCredentialTypeId> GetCertificateTypes();
Task CreateCompanyCertificate(Guid certificateId, CompanyCertificateCreationData data, CancellationToken cancellationToken);
Phil91 marked this conversation as resolved.
Show resolved Hide resolved
}
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,30 @@ public async Task<NoContentResult> CreateSsiCertificate([FromForm] SsiCertificat
return NoContent();
}

/// <summary>
/// Creates the Company Certificate request
/// </summary>
/// <param name="data">The type and document</param>
/// <param name="cancellationToken">Cancellation Token</param>
/// <returns>The id of the created Company certificate request</returns>
/// <remarks>Example: POST: api/administration/companydata/companyCertificate</remarks>
/// <response code="204">Successfully created the Company certificate request.</response>
/// <response code="400">
/// </response>
[HttpPost]
[Consumes("multipart/form-data")]
[Authorize(Roles = "upload_certificates")]
[Authorize(Policy = PolicyTypes.ValidIdentity)]
[Authorize(Policy = PolicyTypes.ValidCompany)]
[Route("companyCertificate/{certificateId}/document")]
Phil91 marked this conversation as resolved.
Show resolved Hide resolved
[ProducesResponseType(StatusCodes.Status204NoContent)]
[ProducesResponseType(typeof(ErrorResponse), StatusCodes.Status400BadRequest)]
public async Task<NoContentResult> CreateCompanyCertificate([FromRoute] Guid certificateId, [FromForm] CompanyCertificateCreationData data, CancellationToken cancellationToken)
{
await _logic.CreateCompanyCertificate(certificateId, data, cancellationToken).ConfigureAwait(false);
return NoContent();
}

/// <summary>
/// Gets all outstanding, existing and inactive credentials
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/********************************************************************************
* Copyright (c) 2021, 2023 BMW Group AG
Phil91 marked this conversation as resolved.
Show resolved Hide resolved
* Copyright (c) 2021, 2023 Contributors to the Eclipse Foundation
Phil91 marked this conversation as resolved.
Show resolved Hide resolved
*
* 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;
using Org.Eclipse.TractusX.Portal.Backend.PortalBackend.PortalEntities.Enums;

namespace Org.Eclipse.TractusX.Portal.Backend.Administration.Service.Models;

public record CompanyCertificateCreationData
(
CompanyCertificateTypeId CertificateType,
IFormFile Document,
DateTimeOffset? ExpiryDate
);
3 changes: 2 additions & 1 deletion src/administration/Administration.Service/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,8 @@
"CompanyData": {
"MaxPageSize": 20,
"UseCaseParticipationMediaTypes": [],
"SsiCertificateMediaTypes": []
"SsiCertificateMediaTypes": [],
"CompanyCertificateMediaTypes":[]
},
"Network2Network": {
"InitialRoles": [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public class PortalRepositories : IPortalRepositories
{ typeof(IUserBusinessPartnerRepository), context => new UserBusinessPartnerRepository(context) },
{ typeof(IUserRepository), context => new UserRepository(context) },
{ typeof(IUserRolesRepository), context => new UserRolesRepository(context) },
{ typeof(ICompanyCertificateRepository), context => new CompanyCertificateRepository(context) },
}.ToImmutableDictionary();

public PortalRepositories(PortalDbContext portalDbContext)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/********************************************************************************
* Copyright (c) 2021, 2023 BMW Group AG
* Copyright (c) 2021, 2023 Contributors to the Eclipse Foundation
Phil91 marked this conversation as resolved.
Show resolved Hide resolved
*
* 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 Microsoft.EntityFrameworkCore;
using Org.Eclipse.TractusX.Portal.Backend.Framework.DBAccess;
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;

public class CompanyCertificateRepository : ICompanyCertificateRepository
{
private readonly PortalDbContext _context;

/// <summary>
/// Constructor.
/// </summary>
/// <param name="portalDbContext">Portal DB context.</param>
public CompanyCertificateRepository(PortalDbContext portalDbContext)
{
_context = portalDbContext;
}

public Task<bool> CheckCompanyCertificateType(CompanyCertificateTypeId certificateTypeId) =>
Phil91 marked this conversation as resolved.
Show resolved Hide resolved
_context.CompanyCertificateTypes
.AnyAsync(x =>
x.Id == certificateTypeId);

public Task<bool> CheckCompanyCertificateId(Guid id) =>
_context.CompanyCertificates
.AnyAsync(x =>
x.Id == id);

/// <inheritdoc />
public CompanyCertificate CreateCompanyCertificateData(Guid companyId, CompanyCertificateTypeId companyCertificateTypeId, Guid docId, DateTimeOffset? expiryDate, Action<CompanyCertificate>? setOptionalFields)
Phil91 marked this conversation as resolved.
Show resolved Hide resolved
Phil91 marked this conversation as resolved.
Show resolved Hide resolved
{
var companyCertificate = new CompanyCertificate(Guid.NewGuid(), DateTimeOffset.UtcNow, companyCertificateTypeId, CompanyCertificateStatusId.ACTIVE, companyId, docId, expiryDate);
setOptionalFields?.Invoke(companyCertificate);
return _context.CompanyCertificates.Add(companyCertificate).Entity;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/********************************************************************************
* Copyright (c) 2021, 2023 BMW Group AG
* Copyright (c) 2021, 2023 Contributors to the Eclipse Foundation
Phil91 marked this conversation as resolved.
Show resolved Hide resolved
*
* 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.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;

public interface ICompanyCertificateRepository
{
/// <summary>
/// Checks whether the given CertificateType is a <see cref="CompanyCertificateTypeId"/> Certificate
/// </summary>
/// <param name="CertificateTypeId">Id of the credentialTypeId</param>
/// <returns><c>true</c> if the tpye is a certificate, otherwise <c>false</c></returns>
Task<bool> CheckCompanyCertificateType(CompanyCertificateTypeId CertificateTypeId);

/// <summary>
/// Checks whether the given CertificateId exist or not<see cref="CompanyCertificate"/> Certificate
/// </summary>
/// <param name="Id">Id of the credentialTypeId</param>
/// <returns><c>true</c> if the tpye is a certificate, otherwise <c>false</c></returns>
Task<bool> CheckCompanyCertificateId(Guid Id);

/// <summary>
/// Creates the company certificate data
/// </summary>
/// <param name="companyId">Id of the company</param>
/// <param name="companyCertificateTypeId">Id of the company certificate types</param>
/// <param name="docId">id of the document</param>
/// <param name="expiryDate">expiry date</param>
/// <param name="companyCertificateStatusId">company certificate status id</param>
/// <returns>The created entity</returns>
CompanyCertificate CreateCompanyCertificateData(Guid companyId, CompanyCertificateTypeId companyCertificateTypeId, Guid docId, DateTimeOffset? expiryDate, Action<CompanyCertificate>? setOptionalFields = null);
}
Phil91 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/********************************************************************************
/********************************************************************************
* Copyright (c) 2021, 2024 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
Expand Down Expand Up @@ -9203,4 +9203,4 @@ protected override void BuildModel(ModelBuilder modelBuilder)
#pragma warning restore 612, 618
}
}
}
}
Loading
Loading