2121using Org . Eclipse . TractusX . Portal . Backend . Framework . ErrorHandling ;
2222using Org . Eclipse . TractusX . Portal . Backend . Framework . Identity ;
2323using Org . Eclipse . TractusX . Portal . Backend . Framework . IO ;
24+ using Org . Eclipse . TractusX . Portal . Backend . Framework . Processes . Library . Enums ;
25+ using Org . Eclipse . TractusX . Portal . Backend . Framework . Processes . Library . Extensions ;
2426using Org . Eclipse . TractusX . Portal . Backend . PortalBackend . DBAccess ;
2527using Org . Eclipse . TractusX . Portal . Backend . PortalBackend . DBAccess . Models ;
2628using Org . Eclipse . TractusX . Portal . Backend . PortalBackend . DBAccess . Repositories ;
3032
3133namespace Org . Eclipse . TractusX . Portal . Backend . Administration . Service . BusinessLogic ;
3234
33- public class SubscriptionConfigurationBusinessLogic : ISubscriptionConfigurationBusinessLogic
35+ public class SubscriptionConfigurationBusinessLogic (
36+ IOfferSubscriptionProcessService offerSubscriptionProcessService ,
37+ IPortalRepositories portalRepositories ,
38+ IIdentityService identityService )
39+ : ISubscriptionConfigurationBusinessLogic
3440{
35- private readonly IOfferSubscriptionProcessService _offerSubscriptionProcessService ;
36- private readonly IPortalRepositories _portalRepositories ;
37- private readonly IIdentityData _identityData ;
38-
39- public SubscriptionConfigurationBusinessLogic ( IOfferSubscriptionProcessService offerSubscriptionProcessService , IPortalRepositories portalRepositories , IIdentityService identityService )
40- {
41- _offerSubscriptionProcessService = offerSubscriptionProcessService ;
42- _portalRepositories = portalRepositories ;
43- _identityData = identityService . IdentityData ;
44- }
41+ private readonly IIdentityData _identityData = identityService . IdentityData ;
4542
4643 /// <inheritdoc />
4744 public async Task < ProviderDetailReturnData > GetProviderCompanyDetailsAsync ( )
4845 {
4946 var companyId = _identityData . CompanyId ;
50- var result = await _portalRepositories . GetInstance < ICompanyRepository > ( )
47+ var result = await portalRepositories . GetInstance < ICompanyRepository > ( )
5148 . GetProviderCompanyDetailAsync ( CompanyRoleId . SERVICE_PROVIDER , companyId )
5249 . ConfigureAwait ( ConfigureAwaitOptions . None ) ;
5350 if ( result == default )
5451 {
5552 throw ConflictException . Create ( AdministrationSubscriptionConfigurationErrors . SUBSCRIPTION_CONFLICT_COMPANY_NOT_FOUND , new ErrorParameter [ ] { new ( nameof ( companyId ) , companyId . ToString ( ) ) } ) ;
5653 }
54+
5755 if ( ! result . IsProviderCompany )
5856 {
5957 throw ForbiddenException . Create ( AdministrationSubscriptionConfigurationErrors . SUBSCRIPTION_FORBIDDEN_COMPANY_NOT_SERVICE_PROVIDER , new ErrorParameter [ ] { new ( nameof ( companyId ) , companyId . ToString ( ) ) } ) ;
@@ -78,7 +76,7 @@ public Task SetProviderCompanyDetailsAsync(ProviderDetailData data)
7876
7977 private async Task SetOfferProviderCompanyDetailsInternalAsync ( ProviderDetailData data , Guid companyId )
8078 {
81- var companyRepository = _portalRepositories . GetInstance < ICompanyRepository > ( ) ;
79+ var companyRepository = portalRepositories . GetInstance < ICompanyRepository > ( ) ;
8280 var providerDetailData = await companyRepository
8381 . GetProviderCompanyDetailsExistsForUser ( companyId )
8482 . ConfigureAwait ( ConfigureAwaitOptions . None ) ;
@@ -92,9 +90,14 @@ private async Task SetOfferProviderCompanyDetailsInternalAsync(ProviderDetailDat
9290 {
9391 companyRepository . AttachAndModifyProviderCompanyDetails (
9492 providerDetailData . ProviderCompanyDetailId ,
95- details => { details . AutoSetupUrl = providerDetailData . Url ; } ,
9693 details =>
9794 {
95+ details . AutoSetupUrl = providerDetailData . Url ;
96+ details . AutoSetupCallbackUrl = providerDetailData . CallbackUrl ;
97+ } ,
98+ details =>
99+ {
100+ details . AutoSetupCallbackUrl = data . CallbackUrl ;
98101 details . AutoSetupUrl = data . Url ;
99102 details . DateLastChanged = DateTimeOffset . UtcNow ;
100103 } ) ;
@@ -106,9 +109,35 @@ private async Task SetOfferProviderCompanyDetailsInternalAsync(ProviderDetailDat
106109 hasChanges = true ;
107110 }
108111
112+ if ( providerDetailData . CallbackUrl is not null && data . CallbackUrl is null )
113+ {
114+ await HandleOfferSetupProcesses ( companyId , companyRepository ) . ConfigureAwait ( ConfigureAwaitOptions . None ) ;
115+ hasChanges = true ;
116+ }
117+
109118 if ( hasChanges )
110119 {
111- await _portalRepositories . SaveAsync ( ) . ConfigureAwait ( ConfigureAwaitOptions . None ) ;
120+ await portalRepositories . SaveAsync ( ) . ConfigureAwait ( ConfigureAwaitOptions . None ) ;
121+ }
122+ }
123+
124+ private async Task HandleOfferSetupProcesses ( Guid companyId , ICompanyRepository companyRepository )
125+ {
126+ var processData = await companyRepository
127+ . GetOfferSubscriptionProcessesForCompanyId ( companyId )
128+ . ToListAsync ( )
129+ . ConfigureAwait ( false ) ;
130+
131+ foreach ( var context in processData
132+ . Where ( x => x . Process != null && x . ProcessSteps ? . Any ( ps => ps is
133+ {
134+ ProcessStepStatusId : ProcessStepStatusId . TODO ,
135+ ProcessStepTypeId : ProcessStepTypeId . RETRIGGER_PROVIDER
136+ } ) == true )
137+ . Select ( data => data . CreateManualProcessData ( ProcessStepTypeId . RETRIGGER_PROVIDER , portalRepositories , ( ) => $ "processId { data . Process ! . Id } ") ) )
138+ {
139+ context . FinalizeProcessStep ( ) ;
140+ context . ScheduleProcessSteps ( Enumerable . Repeat ( ProcessStepTypeId . AWAIT_START_AUTOSETUP , 1 ) ) ;
112141 }
113142 }
114143
@@ -129,11 +158,7 @@ private static async Task HandleCreateProviderCompanyDetails(ProviderDetailData
129158
130159 companyRepository . CreateProviderCompanyDetail ( companyId , data . Url ! , providerDetails =>
131160 {
132- if ( data . CallbackUrl != null )
133- {
134- providerDetails . AutoSetupCallbackUrl = data . CallbackUrl ;
135- }
136-
161+ providerDetails . AutoSetupCallbackUrl = data . CallbackUrl ;
137162 providerDetails . DateLastChanged = DateTimeOffset . UtcNow ;
138163 } ) ;
139164 }
@@ -158,18 +183,17 @@ public Task RetriggerProviderCallback(Guid offerSubscriptionId) =>
158183 public Task RetriggerCreateDimTechnicalUser ( Guid offerSubscriptionId ) =>
159184 TriggerProcessStep ( offerSubscriptionId , ProcessStepTypeId . RETRIGGER_OFFERSUBSCRIPTION_CREATE_DIM_TECHNICAL_USER , true ) ;
160185
161- /// <inheritdoc />
162186 private async Task TriggerProcessStep ( Guid offerSubscriptionId , ProcessStepTypeId stepToTrigger , bool mustBePending )
163187 {
164188 var nextStep = stepToTrigger . GetOfferSubscriptionStepToRetrigger ( ) ;
165- var context = await _offerSubscriptionProcessService . VerifySubscriptionAndProcessSteps ( offerSubscriptionId , stepToTrigger , null , mustBePending )
189+ var context = await offerSubscriptionProcessService . VerifySubscriptionAndProcessSteps ( offerSubscriptionId , stepToTrigger , null , mustBePending )
166190 . ConfigureAwait ( ConfigureAwaitOptions . None ) ;
167191
168- _offerSubscriptionProcessService . FinalizeProcessSteps ( context , Enumerable . Repeat ( nextStep , 1 ) ) ;
169- await _portalRepositories . SaveAsync ( ) . ConfigureAwait ( ConfigureAwaitOptions . None ) ;
192+ offerSubscriptionProcessService . FinalizeProcessSteps ( context , Enumerable . Repeat ( nextStep , 1 ) ) ;
193+ await portalRepositories . SaveAsync ( ) . ConfigureAwait ( ConfigureAwaitOptions . None ) ;
170194 }
171195
172196 /// <inheritdoc />
173197 public IAsyncEnumerable < ProcessStepData > GetProcessStepsForSubscription ( Guid offerSubscriptionId ) =>
174- _portalRepositories . GetInstance < IOfferSubscriptionsRepository > ( ) . GetProcessStepsForSubscription ( offerSubscriptionId ) ;
198+ portalRepositories . GetInstance < IOfferSubscriptionsRepository > ( ) . GetProcessStepsForSubscription ( offerSubscriptionId ) ;
175199}
0 commit comments