Skip to content

Commit

Permalink
feat(sd): add process steps for sd creation (#1189)
Browse files Browse the repository at this point in the history
* feat(sd): add process steps for sd creation + retrigger
* update framework minor version
* add and adjust unit tests
Refs: #1182
---------
Co-authored-by: Norbert Truchsess <[email protected]>
  • Loading branch information
Phil91 committed Jan 31, 2025
1 parent ce4aa6a commit f5c0f2b
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
using Org.Eclipse.TractusX.Portal.Backend.Framework.ErrorHandling;
using Org.Eclipse.TractusX.Portal.Backend.Framework.Identity;
using Org.Eclipse.TractusX.Portal.Backend.Framework.IO;
using Org.Eclipse.TractusX.Portal.Backend.Framework.Processes.Library.Enums;
using Org.Eclipse.TractusX.Portal.Backend.Framework.Processes.Library.Extensions;
using Org.Eclipse.TractusX.Portal.Backend.PortalBackend.DBAccess;
using Org.Eclipse.TractusX.Portal.Backend.PortalBackend.DBAccess.Models;
using Org.Eclipse.TractusX.Portal.Backend.PortalBackend.DBAccess.Repositories;
Expand All @@ -30,30 +32,26 @@

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

public class SubscriptionConfigurationBusinessLogic : ISubscriptionConfigurationBusinessLogic
public class SubscriptionConfigurationBusinessLogic(
IOfferSubscriptionProcessService offerSubscriptionProcessService,
IPortalRepositories portalRepositories,
IIdentityService identityService)
: ISubscriptionConfigurationBusinessLogic
{
private readonly IOfferSubscriptionProcessService _offerSubscriptionProcessService;
private readonly IPortalRepositories _portalRepositories;
private readonly IIdentityData _identityData;

public SubscriptionConfigurationBusinessLogic(IOfferSubscriptionProcessService offerSubscriptionProcessService, IPortalRepositories portalRepositories, IIdentityService identityService)
{
_offerSubscriptionProcessService = offerSubscriptionProcessService;
_portalRepositories = portalRepositories;
_identityData = identityService.IdentityData;
}
private readonly IIdentityData _identityData = identityService.IdentityData;

/// <inheritdoc />
public async Task<ProviderDetailReturnData> GetProviderCompanyDetailsAsync()
{
var companyId = _identityData.CompanyId;
var result = await _portalRepositories.GetInstance<ICompanyRepository>()
var result = await portalRepositories.GetInstance<ICompanyRepository>()
.GetProviderCompanyDetailAsync(CompanyRoleId.SERVICE_PROVIDER, companyId)
.ConfigureAwait(ConfigureAwaitOptions.None);
if (result == default)
{
throw ConflictException.Create(AdministrationSubscriptionConfigurationErrors.SUBSCRIPTION_CONFLICT_COMPANY_NOT_FOUND, new ErrorParameter[] { new(nameof(companyId), companyId.ToString()) });
}

if (!result.IsProviderCompany)
{
throw ForbiddenException.Create(AdministrationSubscriptionConfigurationErrors.SUBSCRIPTION_FORBIDDEN_COMPANY_NOT_SERVICE_PROVIDER, new ErrorParameter[] { new(nameof(companyId), companyId.ToString()) });
Expand All @@ -78,7 +76,7 @@ public Task SetProviderCompanyDetailsAsync(ProviderDetailData data)

private async Task SetOfferProviderCompanyDetailsInternalAsync(ProviderDetailData data, Guid companyId)
{
var companyRepository = _portalRepositories.GetInstance<ICompanyRepository>();
var companyRepository = portalRepositories.GetInstance<ICompanyRepository>();
var providerDetailData = await companyRepository
.GetProviderCompanyDetailsExistsForUser(companyId)
.ConfigureAwait(ConfigureAwaitOptions.None);
Expand All @@ -92,9 +90,14 @@ private async Task SetOfferProviderCompanyDetailsInternalAsync(ProviderDetailDat
{
companyRepository.AttachAndModifyProviderCompanyDetails(
providerDetailData.ProviderCompanyDetailId,
details => { details.AutoSetupUrl = providerDetailData.Url; },
details =>
{
details.AutoSetupUrl = providerDetailData.Url;
details.AutoSetupCallbackUrl = providerDetailData.CallbackUrl;
},
details =>
{
details.AutoSetupCallbackUrl = data.CallbackUrl;
details.AutoSetupUrl = data.Url;
details.DateLastChanged = DateTimeOffset.UtcNow;
});
Expand All @@ -106,9 +109,35 @@ private async Task SetOfferProviderCompanyDetailsInternalAsync(ProviderDetailDat
hasChanges = true;
}

if (providerDetailData.CallbackUrl is not null && data.CallbackUrl is null)
{
await HandleOfferSetupProcesses(companyId, companyRepository).ConfigureAwait(ConfigureAwaitOptions.None);
hasChanges = true;
}

if (hasChanges)
{
await _portalRepositories.SaveAsync().ConfigureAwait(ConfigureAwaitOptions.None);
await portalRepositories.SaveAsync().ConfigureAwait(ConfigureAwaitOptions.None);
}
}

private async Task HandleOfferSetupProcesses(Guid companyId, ICompanyRepository companyRepository)
{
var processData = await companyRepository
.GetOfferSubscriptionProcessesForCompanyId(companyId)
.ToListAsync()
.ConfigureAwait(false);

foreach (var context in processData
.Where(x => x.Process != null && x.ProcessSteps?.Any(ps => ps is
{
ProcessStepStatusId: ProcessStepStatusId.TODO,
ProcessStepTypeId: ProcessStepTypeId.RETRIGGER_PROVIDER
}) == true)
.Select(data => data.CreateManualProcessData(ProcessStepTypeId.RETRIGGER_PROVIDER, portalRepositories, () => $"processId {data.Process!.Id}")))
{
context.FinalizeProcessStep();
context.ScheduleProcessSteps(Enumerable.Repeat(ProcessStepTypeId.AWAIT_START_AUTOSETUP, 1));
}
}

Expand All @@ -129,11 +158,7 @@ private static async Task HandleCreateProviderCompanyDetails(ProviderDetailData

companyRepository.CreateProviderCompanyDetail(companyId, data.Url!, providerDetails =>
{
if (data.CallbackUrl != null)
{
providerDetails.AutoSetupCallbackUrl = data.CallbackUrl;
}

providerDetails.AutoSetupCallbackUrl = data.CallbackUrl;
providerDetails.DateLastChanged = DateTimeOffset.UtcNow;
});
}
Expand All @@ -158,18 +183,17 @@ public Task RetriggerProviderCallback(Guid offerSubscriptionId) =>
public Task RetriggerCreateDimTechnicalUser(Guid offerSubscriptionId) =>
TriggerProcessStep(offerSubscriptionId, ProcessStepTypeId.RETRIGGER_OFFERSUBSCRIPTION_CREATE_DIM_TECHNICAL_USER, true);

/// <inheritdoc />
private async Task TriggerProcessStep(Guid offerSubscriptionId, ProcessStepTypeId stepToTrigger, bool mustBePending)
{
var nextStep = stepToTrigger.GetOfferSubscriptionStepToRetrigger();
var context = await _offerSubscriptionProcessService.VerifySubscriptionAndProcessSteps(offerSubscriptionId, stepToTrigger, null, mustBePending)
var context = await offerSubscriptionProcessService.VerifySubscriptionAndProcessSteps(offerSubscriptionId, stepToTrigger, null, mustBePending)
.ConfigureAwait(ConfigureAwaitOptions.None);

_offerSubscriptionProcessService.FinalizeProcessSteps(context, Enumerable.Repeat(nextStep, 1));
await _portalRepositories.SaveAsync().ConfigureAwait(ConfigureAwaitOptions.None);
offerSubscriptionProcessService.FinalizeProcessSteps(context, Enumerable.Repeat(nextStep, 1));
await portalRepositories.SaveAsync().ConfigureAwait(ConfigureAwaitOptions.None);
}

/// <inheritdoc />
public IAsyncEnumerable<ProcessStepData> GetProcessStepsForSubscription(Guid offerSubscriptionId) =>
_portalRepositories.GetInstance<IOfferSubscriptionsRepository>().GetProcessStepsForSubscription(offerSubscriptionId);
portalRepositories.GetInstance<IOfferSubscriptionsRepository>().GetProcessStepsForSubscription(offerSubscriptionId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,10 @@ public IAsyncEnumerable<string> GetAllMemberCompaniesBPNAsync(IEnumerable<string
.SingleOrDefaultAsync();

/// <inheritdoc />
public Task<(Guid ProviderCompanyDetailId, string Url)> GetProviderCompanyDetailsExistsForUser(Guid companyId) =>
public Task<(Guid ProviderCompanyDetailId, string Url, string? CallbackUrl)> GetProviderCompanyDetailsExistsForUser(Guid companyId) =>
context.ProviderCompanyDetails.AsNoTracking()
.Where(details => details.CompanyId == companyId)
.Select(details => new ValueTuple<Guid, string>(details.Id, details.AutoSetupUrl))
.Select(details => new ValueTuple<Guid, string, string?>(details.Id, details.AutoSetupUrl, details.AutoSetupCallbackUrl))
.SingleOrDefaultAsync();

/// <inheritdoc />
Expand Down Expand Up @@ -482,4 +482,14 @@ public Task<bool> IsExistingCompany(Guid companyId) =>
c.SdCreationProcess,
c.SdCreationProcess!.ProcessSteps.Where(step => step.ProcessStepStatusId == ProcessStepStatusId.TODO)))
.SingleOrDefaultAsync();

public IAsyncEnumerable<VerifyProcessData<ProcessTypeId, ProcessStepTypeId>> GetOfferSubscriptionProcessesForCompanyId(Guid companyId) =>
context.Companies
.Where(c => c.Id == companyId)
.SelectMany(c => c.ProvidedOffers.SelectMany(po =>
po.OfferSubscriptions.Select(os =>
new VerifyProcessData<ProcessTypeId, ProcessStepTypeId>(
os.Process,
os.Process!.ProcessSteps.Where(ps => ps.ProcessStepStatusId == ProcessStepStatusId.TODO)))))
.ToAsyncEnumerable();
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public interface ICompanyRepository
/// <returns><c>true</c> if the company exists for the given user, otherwise <c>false</c></returns>
Task<(bool IsValidCompanyId, bool IsCompanyRoleOwner)> IsValidCompanyRoleOwner(Guid companyId, IEnumerable<CompanyRoleId> companyRoleIds);

Task<(Guid ProviderCompanyDetailId, string Url)> GetProviderCompanyDetailsExistsForUser(Guid companyId);
Task<(Guid ProviderCompanyDetailId, string Url, string? CallbackUrl)> GetProviderCompanyDetailsExistsForUser(Guid companyId);

/// <summary>
/// Creates service provider company details
Expand Down Expand Up @@ -185,4 +185,5 @@ public interface ICompanyRepository
Task<bool> IsExistingCompany(Guid companyId);
Task<(bool Exists, Guid CompanyId, IEnumerable<Guid> SubmittedCompanyApplicationId)> GetCompanyIdByBpn(string bpn);
Task<VerifyProcessData<ProcessTypeId, ProcessStepTypeId>?> GetProcessDataForCompanyIdId(Guid companyId);
IAsyncEnumerable<VerifyProcessData<ProcessTypeId, ProcessStepTypeId>> GetOfferSubscriptionProcessesForCompanyId(Guid companyId);
}
Loading

0 comments on commit f5c0f2b

Please sign in to comment.