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(provider): adjust provider callbackurl handling #1227

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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 @@ -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
Loading