Skip to content

Commit

Permalink
feat(provider): adjust provider callbackurl handling
Browse files Browse the repository at this point in the history
when the provider callback url is removed the trigger provider callback steps are set to done and the await auto setup step is created

Refs: #1175
  • Loading branch information
Phil91 committed Jan 8, 2025
1 parent bd34841 commit 416bfc4
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,35 +25,32 @@
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 Org.Eclipse.TractusX.Portal.Backend.Processes.Library;
using Org.Eclipse.TractusX.Portal.Backend.Processes.OfferSubscription.Library;
using Org.Eclipse.TractusX.Portal.Backend.Processes.OfferSubscription.Library.Extensions;

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 +75,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 +89,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 +108,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 +157,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 +182,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 @@ -132,10 +132,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 @@ -458,4 +458,14 @@ public Task<bool> IsExistingCompany(Guid companyId) =>
.Where(x => x.BusinessPartnerNumber == bpn)
.Select(x => new ValueTuple<bool, Guid, IEnumerable<Guid>>(true, x.Id, x.CompanyApplications.Where(a => a.ApplicationStatusId == CompanyApplicationStatusId.SUBMITTED).Select(a => a.Id)))
.SingleOrDefaultAsync();

public IAsyncEnumerable<VerifyProcessData> GetOfferSubscriptionProcessesForCompanyId(Guid companyId) =>
context.Companies
.Where(c => c.Id == companyId)
.SelectMany(c => c.ProvidedOffers.SelectMany(po =>
po.OfferSubscriptions.Select(os =>
new VerifyProcessData(
os.Process,
os.Process!.ProcessSteps.Where(ps => ps.ProcessStepStatusId == ProcessStepStatusId.TODO)))))
.ToAsyncEnumerable();
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,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 @@ -183,4 +183,5 @@ public interface ICompanyRepository
Task<(Guid Id, IEnumerable<(UniqueIdentifierId Id, string Value)> UniqueIdentifiers, string? BusinessPartnerNumber, string CountryCode)> GetCompanyByProcessId(Guid processId);
Task<bool> IsExistingCompany(Guid companyId);
Task<(bool Exists, Guid CompanyId, IEnumerable<Guid> SubmittedCompanyApplicationId)> GetCompanyIdByBpn(string bpn);
IAsyncEnumerable<VerifyProcessData> GetOfferSubscriptionProcessesForCompanyId(Guid companyId);
}
Loading

0 comments on commit 416bfc4

Please sign in to comment.