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(certificate): enhance company certificate db attributes and API details #823

Merged
Show file tree
Hide file tree
Changes from all 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 @@ -22,6 +22,7 @@
using Org.Eclipse.TractusX.Portal.Backend.Framework.Async;
using Org.Eclipse.TractusX.Portal.Backend.Framework.DateTimeProvider;
using Org.Eclipse.TractusX.Portal.Backend.Framework.ErrorHandling;
using Org.Eclipse.TractusX.Portal.Backend.Framework.Linq;
using Org.Eclipse.TractusX.Portal.Backend.Framework.Models;
using Org.Eclipse.TractusX.Portal.Backend.Framework.Web;
using Org.Eclipse.TractusX.Portal.Backend.IssuerComponent.Library.BusinessLogic;
Expand All @@ -31,6 +32,7 @@
using Org.Eclipse.TractusX.Portal.Backend.PortalBackend.DBAccess.Repositories;
using Org.Eclipse.TractusX.Portal.Backend.PortalBackend.PortalEntities.Enums;
using Org.Eclipse.TractusX.Portal.Backend.PortalBackend.PortalEntities.Identities;
using System.Text.RegularExpressions;

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

Expand All @@ -41,6 +43,9 @@ public class CompanyDataBusinessLogic(
IIssuerComponentBusinessLogic issuerComponentBusinessLogic,
IOptions<CompanyDataSettings> options) : ICompanyDataBusinessLogic
{
private static readonly Regex BpnsRegex = new(ValidationExpressions.Bpns, RegexOptions.Compiled, TimeSpan.FromSeconds(1));
private static readonly Regex EcmRegex = new(ValidationExpressions.ExternalCertificateNumber, RegexOptions.Compiled, TimeSpan.FromSeconds(1));
private static readonly Regex Company = new(ValidationExpressions.Company, RegexOptions.Compiled, TimeSpan.FromSeconds(1));
private readonly IIdentityData _identityData = identityService.IdentityData;
private readonly CompanyDataSettings _settings = options.Value;

Expand Down Expand Up @@ -240,6 +245,33 @@ public Task<Guid> CreateUseCaseParticipation(UseCaseParticipationCreationData da
/// <inheritdoc />
public async Task CreateCompanyCertificate(CompanyCertificateCreationData data, CancellationToken cancellationToken)
{
if (data.ExternalCertificateNumber != null && !EcmRegex.IsMatch(data.ExternalCertificateNumber))
{
throw new ControllerArgumentException("ExternalCertificateNumber must be alphanumeric and length should not be greater than 36");
}

if (data.Sites != null && data.Sites.Any(bpn => !BpnsRegex.IsMatch(bpn)))
{
throw new ControllerArgumentException("BPN must contain exactly 16 characters and must be prefixed with BPNS");
}

var now = dateTimeProvider.OffsetNow;

if (data.ValidFrom > now)
{
throw new ControllerArgumentException("ValidFrom date should not be greater than current date");
}

if (data.ValidTill < now)
{
throw new ControllerArgumentException("ValidTill date should be greater than current date");
}

if (data.Issuer != null && !Company.IsMatch(data.Issuer!))
{
throw new ControllerArgumentException("Issuer length must be 3-40 characters and *+=#%\\s not used as one of the first three characters in the company name");
}

var documentContentType = data.Document.ContentType.ParseMediaTypeId();
documentContentType.CheckDocumentContentType(_settings.CompanyCertificateMediaTypes);

Expand All @@ -249,14 +281,18 @@ public async Task CreateCompanyCertificate(CompanyCertificateCreationData data,
throw new ControllerArgumentException($"{data.CertificateType} is not assigned to a certificate");
}

await HandleCompanyCertificateCreationAsync(data.CertificateType, data.Document, documentContentType, companyCertificateRepository, data.ExpiryDate, cancellationToken).ConfigureAwait(ConfigureAwaitOptions.None);
await HandleCompanyCertificateCreationAsync(data.CertificateType, data.Document, documentContentType, companyCertificateRepository, data.ExternalCertificateNumber, data.Sites, data.ValidFrom, data.ValidTill, data.Issuer, cancellationToken).ConfigureAwait(ConfigureAwaitOptions.None);
}

private async Task HandleCompanyCertificateCreationAsync(CompanyCertificateTypeId companyCertificateTypeId,
IFormFile document,
MediaTypeId mediaTypeId,
ICompanyCertificateRepository companyCertificateRepository,
DateTimeOffset? expiryDate,
string? externalCertificateNumber,
IEnumerable<string>? sites,
DateTimeOffset? validFrom,
DateTimeOffset? validTill,
string? issuer,
CancellationToken cancellationToken)
{
var (documentContent, hash) = await document.GetContentAndHash(cancellationToken).ConfigureAwait(ConfigureAwaitOptions.None);
Expand All @@ -267,12 +303,17 @@ private async Task HandleCompanyCertificateCreationAsync(CompanyCertificateTypeI
x.DocumentStatusId = DocumentStatusId.LOCKED;
});

companyCertificateRepository.CreateCompanyCertificate(_identityData.CompanyId, companyCertificateTypeId, doc.Id,
var companyCertificate = companyCertificateRepository.CreateCompanyCertificate(_identityData.CompanyId, companyCertificateTypeId, CompanyCertificateStatusId.ACTIVE, doc.Id,
x =>
{
x.ValidTill = expiryDate?.ToUniversalTime();
x.ExternalCertificateNumber = externalCertificateNumber;
x.Issuer = issuer;
x.ValidFrom = validFrom;
x.ValidTill = validTill;
});

sites?.IfAny(x => companyCertificateRepository.CreateCompanyCertificateAssignedSites(companyCertificate.Id, x));

await portalRepositories.SaveAsync().ConfigureAwait(ConfigureAwaitOptions.None);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,9 @@ public record CompanyCertificateCreationData
(
CompanyCertificateTypeId CertificateType,
IFormFile Document,
DateTimeOffset? ExpiryDate
string? ExternalCertificateNumber,
IEnumerable<string>? Sites,
DateTimeOffset? ValidFrom,
DateTimeOffset? ValidTill,
string? Issuer
);
2 changes: 1 addition & 1 deletion src/framework/Framework.Async/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

<Project>
<PropertyGroup>
<VersionPrefix>2.3.0</VersionPrefix>
<VersionPrefix>2.4.0</VersionPrefix>
<VersionSuffix></VersionSuffix>
</PropertyGroup>
</Project>
2 changes: 1 addition & 1 deletion src/framework/Framework.Cors/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

<Project>
<PropertyGroup>
<VersionPrefix>2.3.0</VersionPrefix>
<VersionPrefix>2.4.0</VersionPrefix>
<VersionSuffix></VersionSuffix>
</PropertyGroup>
</Project>
2 changes: 1 addition & 1 deletion src/framework/Framework.DBAccess/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

<Project>
<PropertyGroup>
<VersionPrefix>2.3.0</VersionPrefix>
<VersionPrefix>2.4.0</VersionPrefix>
<VersionSuffix></VersionSuffix>
</PropertyGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

<Project>
<PropertyGroup>
<VersionPrefix>2.3.0</VersionPrefix>
<VersionPrefix>2.4.0</VersionPrefix>
<VersionSuffix></VersionSuffix>
</PropertyGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

<Project>
<PropertyGroup>
<VersionPrefix>2.3.0</VersionPrefix>
<VersionPrefix>2.4.0</VersionPrefix>
<VersionSuffix></VersionSuffix>
</PropertyGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

<Project>
<PropertyGroup>
<VersionPrefix>2.3.0</VersionPrefix>
<VersionPrefix>2.4.0</VersionPrefix>
<VersionSuffix></VersionSuffix>
</PropertyGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

<Project>
<PropertyGroup>
<VersionPrefix>2.3.0</VersionPrefix>
<VersionPrefix>2.4.0</VersionPrefix>
<VersionSuffix></VersionSuffix>
</PropertyGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

<Project>
<PropertyGroup>
<VersionPrefix>2.3.0</VersionPrefix>
<VersionPrefix>2.4.0</VersionPrefix>
<VersionSuffix></VersionSuffix>
</PropertyGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

<Project>
<PropertyGroup>
<VersionPrefix>2.3.0</VersionPrefix>
<VersionPrefix>2.4.0</VersionPrefix>
<VersionSuffix></VersionSuffix>
</PropertyGroup>
</Project>
2 changes: 1 addition & 1 deletion src/framework/Framework.IO/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

<Project>
<PropertyGroup>
<VersionPrefix>2.3.0</VersionPrefix>
<VersionPrefix>2.4.0</VersionPrefix>
<VersionSuffix></VersionSuffix>
</PropertyGroup>
</Project>
2 changes: 1 addition & 1 deletion src/framework/Framework.Linq/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

<Project>
<PropertyGroup>
<VersionPrefix>2.3.0</VersionPrefix>
<VersionPrefix>2.4.0</VersionPrefix>
<VersionSuffix></VersionSuffix>
</PropertyGroup>
</Project>
2 changes: 1 addition & 1 deletion src/framework/Framework.Logging/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

<Project>
<PropertyGroup>
<VersionPrefix>2.3.0</VersionPrefix>
<VersionPrefix>2.4.0</VersionPrefix>
<VersionSuffix></VersionSuffix>
</PropertyGroup>
</Project>
2 changes: 1 addition & 1 deletion src/framework/Framework.Models/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

<Project>
<PropertyGroup>
<VersionPrefix>2.3.0</VersionPrefix>
<VersionPrefix>2.4.0</VersionPrefix>
<VersionSuffix></VersionSuffix>
</PropertyGroup>
</Project>
2 changes: 2 additions & 0 deletions src/framework/Framework.Models/ValidationExpressions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,7 @@ public static class ValidationExpressions
{
public const string Name = @"^.+$";
public const string Bpn = @"^(BPNL|bpnl)[\w|\d]{12}$";
public const string Bpns = @"^(BPNS|bpns)[\w|\d]{12}$";
public const string Company = @"^\d*?[A-Za-zÀ-ÿ]\d?([A-Za-z0-9À-ÿ-_+=.,:;!?'\x22&#@()]\s?){2,40}$";
public const string ExternalCertificateNumber = @"^[a-zA-Z0-9]{0,36}$";
}
2 changes: 1 addition & 1 deletion src/framework/Framework.Seeding/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

<Project>
<PropertyGroup>
<VersionPrefix>2.3.0</VersionPrefix>
<VersionPrefix>2.4.0</VersionPrefix>
<VersionSuffix></VersionSuffix>
</PropertyGroup>
</Project>
2 changes: 1 addition & 1 deletion src/framework/Framework.Swagger/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

<Project>
<PropertyGroup>
<VersionPrefix>2.3.0</VersionPrefix>
<VersionPrefix>2.4.0</VersionPrefix>
<VersionSuffix></VersionSuffix>
</PropertyGroup>
</Project>
2 changes: 1 addition & 1 deletion src/framework/Framework.Token/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

<Project>
<PropertyGroup>
<VersionPrefix>2.3.0</VersionPrefix>
<VersionPrefix>2.4.0</VersionPrefix>
<VersionSuffix></VersionSuffix>
</PropertyGroup>
</Project>
2 changes: 1 addition & 1 deletion src/framework/Framework.Web/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

<Project>
<PropertyGroup>
<VersionPrefix>2.3.0</VersionPrefix>
<VersionPrefix>2.4.0</VersionPrefix>
<VersionSuffix></VersionSuffix>
</PropertyGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ public record CompanyCertificateBpnData(
CompanyCertificateTypeId CompanyCertificateType,
CompanyCertificateStatusId CompanyCertificateStatus,
Guid DocumentId,
DateTimeOffset ValidFrom,
DateTimeOffset? ValidTill
DateTimeOffset? ValidFrom,
DateTimeOffset? ValidTill,
string? ExternalCertificateNumber,
IEnumerable<string>? Sites,
string? Issuer,
string? Validator
);
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ public record CompanyCertificateData(
CompanyCertificateTypeId companyCertificateType,
CompanyCertificateStatusId companyCertificateStatus,
Guid documentId,
DateTimeOffset validFrom,
DateTimeOffset? validTill
DateTimeOffset? validFrom,
DateTimeOffset? validTill,
string? ExternalCertificateNumber,
IEnumerable<string>? Sites,
string? Issuer,
string? Validator
);
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ public Task<bool> CheckCompanyCertificateType(CompanyCertificateTypeId certifica
x.Id == certificateTypeId);

/// <inheritdoc />
public CompanyCertificate CreateCompanyCertificate(Guid companyId, CompanyCertificateTypeId companyCertificateTypeId, Guid docId, Action<CompanyCertificate>? setOptionalFields = null)
public CompanyCertificate CreateCompanyCertificate(Guid companyId, CompanyCertificateTypeId companyCertificateTypeId, CompanyCertificateStatusId companyCertificateStatusId, Guid docId, Action<CompanyCertificate>? setOptionalFields = null)
{
var companyCertificate = new CompanyCertificate(Guid.NewGuid(), DateTimeOffset.UtcNow, companyCertificateTypeId, CompanyCertificateStatusId.ACTIVE, companyId, docId);
var companyCertificate = new CompanyCertificate(Guid.NewGuid(), companyCertificateTypeId, companyCertificateStatusId, companyId, docId);
setOptionalFields?.Invoke(companyCertificate);
return _context.CompanyCertificates.Add(companyCertificate).Entity;
}
Expand All @@ -69,7 +69,11 @@ public IAsyncEnumerable<CompanyCertificateBpnData> GetCompanyCertificateData(Gui
ccb.CompanyCertificateStatusId,
ccb.DocumentId,
ccb.ValidFrom,
ccb.ValidTill))
ccb.ValidTill,
ccb.ExternalCertificateNumber,
ccb.CompanyCertificateAssignedSites.Select(x => x.Site),
ccb.Issuer,
ccb.Validator))
.ToAsyncEnumerable();

public Func<int, int, Task<Pagination.Source<CompanyCertificateData>?>> GetActiveCompanyCertificatePaginationSource(CertificateSorting? sorting, CompanyCertificateStatusId? certificateStatus, CompanyCertificateTypeId? certificateType, Guid companyId) =>
Expand All @@ -96,7 +100,11 @@ public IAsyncEnumerable<CompanyCertificateBpnData> GetCompanyCertificateData(Gui
companyCertificate.CompanyCertificateStatusId,
companyCertificate.DocumentId,
companyCertificate.ValidFrom,
companyCertificate.ValidTill
companyCertificate.ValidTill,
companyCertificate.ExternalCertificateNumber,
companyCertificate.CompanyCertificateAssignedSites.Select(x => x.Site),
companyCertificate.Issuer,
companyCertificate.Validator
))
.SingleOrDefaultAsync();

Expand All @@ -120,7 +128,7 @@ public IAsyncEnumerable<CompanyCertificateBpnData> GetCompanyCertificateData(Gui

public void AttachAndModifyCompanyCertificateDetails(Guid id, Action<CompanyCertificate>? initialize, Action<CompanyCertificate> updateFields)
{
var entity = new CompanyCertificate(id, default, default, default, Guid.Empty, Guid.Empty);
var entity = new CompanyCertificate(id, default, default, Guid.Empty, Guid.Empty);
initialize?.Invoke(entity);
_context.Attach(entity);
updateFields.Invoke(entity);
Expand All @@ -141,4 +149,7 @@ public void AttachAndModifyCompanyCertificateDocumentDetails(Guid id, Action<Doc
x.CompanyUser!.Identity!.CompanyId == companyId)
.Select(x => new ValueTuple<byte[], string, MediaTypeId, bool>(x.DocumentContent, x.DocumentName, x.MediaTypeId, true))
.SingleOrDefaultAsync();

public void CreateCompanyCertificateAssignedSites(Guid companyCertificateId, IEnumerable<string> sites) =>
_context.AddRange(sites.Select(companyCertificateAssignedSite => new CompanyCertificateAssignedSite(companyCertificateId, companyCertificateAssignedSite)));
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ public interface ICompanyCertificateRepository
/// <param name="docId">id of the document</param>
/// <param name="setOptionalFields">Action to set optional fields</param>
/// <returns>The created entity</returns>
CompanyCertificate CreateCompanyCertificate(Guid companyId, CompanyCertificateTypeId companyCertificateTypeId, Guid docId, Action<CompanyCertificate>? setOptionalFields = null);
CompanyCertificate CreateCompanyCertificate(Guid companyId, CompanyCertificateTypeId companyCertificateTypeId, CompanyCertificateStatusId companyCertificateStatusId, Guid docId, Action<CompanyCertificate>? setOptionalFields = null);

void CreateCompanyCertificateAssignedSites(Guid companyCertificateId, IEnumerable<string> sites);

/// <summary>
/// Get companyId against businessPartnerNumber
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
********************************************************************************/

using Microsoft.EntityFrameworkCore.Migrations;
using System;

#nullable disable

Expand Down
Loading
Loading