2727using Org . Eclipse . TractusX . Portal . Backend . PortalBackend . DBAccess ;
2828using Org . Eclipse . TractusX . Portal . Backend . PortalBackend . DBAccess . Models ;
2929using Org . Eclipse . TractusX . Portal . Backend . PortalBackend . DBAccess . Repositories ;
30+ using Org . Eclipse . TractusX . Portal . Backend . PortalBackend . PortalEntities . Entities ;
3031using Org . Eclipse . TractusX . Portal . Backend . PortalBackend . PortalEntities . Enums ;
32+ using Org . Eclipse . TractusX . Portal . Backend . PortalBackend . PortalEntities . Extensions ;
3133using Org . Eclipse . TractusX . Portal . Backend . PortalBackend . PortalEntities . Identities ;
34+ using Org . Eclipse . TractusX . Portal . Backend . Processes . Library ;
3235using Org . Eclipse . TractusX . Portal . Backend . Processes . Mailing . Library ;
3336using Org . Eclipse . TractusX . Portal . Backend . Provisioning . DBAccess ;
3437using Org . Eclipse . TractusX . Portal . Backend . Provisioning . Library ;
@@ -50,7 +53,7 @@ public class UserBusinessLogic(
5053 IIdentityService identityService ,
5154 IMailingProcessCreation mailingProcessCreation ,
5255 ILogger < UserBusinessLogic > logger ,
53- IBpnAccess bpnAccess ,
56+ IBpdmAccessService bpdmAccessService ,
5457 IOptions < UserSettings > options ) : IUserBusinessLogic
5558{
5659 private readonly UserSettings _settings = options . Value ;
@@ -267,74 +270,38 @@ await Task.WhenAll(details.IdpUserIds.Select(async x =>
267270 details . Email ) ;
268271 }
269272
270- public async Task < CompanyUsersBpnDetails > AddOwnCompanyUsersBusinessPartnerNumbersAsync ( Guid userId , string token , IEnumerable < string > businessPartnerNumbers , CancellationToken cancellationToken )
273+ public async Task AddOwnCompanyUsersBusinessPartnerNumbersAsync ( Guid userId , IEnumerable < string > businessPartnerNumbers , CancellationToken cancellationToken )
271274 {
272275 var companyId = _identityData . CompanyId ;
276+
277+ var invalidBpns = businessPartnerNumbers . Where ( bpn => bpn . Length > 20 ) ;
278+ if ( invalidBpns . Any ( ) )
279+ {
280+ throw new ControllerArgumentException ( $ "BusinessPartnerNumbers { string . Join ( "," , invalidBpns ) } must not exceed 20 characters") ;
281+ }
282+
273283 var ( assignedBusinessPartnerNumbers , isValidUser ) = await portalRepositories . GetInstance < IUserRepository > ( ) . GetOwnCompanyUserWithAssignedBusinessPartnerNumbersUntrackedAsync ( userId , companyId ) . ConfigureAwait ( ConfigureAwaitOptions . None ) ;
274284 if ( ! isValidUser )
275285 {
276286 throw new NotFoundException ( $ "user { userId } not found in company { companyId } ") ;
277287 }
278288
279- var iamUserId = await provisioningManager . GetUserByUserName ( userId . ToString ( ) ) . ConfigureAwait ( ConfigureAwaitOptions . None ) ??
280- throw new ConflictException ( $ "user { userId } not found in keycloak") ;
281-
282- var ( successfulBpns , unsuccessfulBpns ) = await businessPartnerNumbers . AggregateAwait (
283- ( SuccessfulBpns : ImmutableList . CreateBuilder < string > ( ) , UnsuccessfulBpns : ImmutableList . CreateBuilder < UnsuccessfulBpns > ( ) ) ,
284- async ( acc , bpn ) =>
285- {
286- var ( bpns , error ) = await CompanyUsersBpnCheck ( bpn , token , cancellationToken ) . ConfigureAwait ( false ) ;
287- if ( error == null )
288- {
289- acc . SuccessfulBpns . Add ( bpns ) ;
290- }
291- else
292- {
293- acc . UnsuccessfulBpns . Add ( new UnsuccessfulBpns ( bpns , error . Message ) ) ;
294- }
295- return acc ;
296- } ,
297- acc => ( acc . SuccessfulBpns . ToImmutable ( ) , acc . UnsuccessfulBpns . ToImmutable ( ) ) ,
298- cancellationToken
299- ) . ConfigureAwait ( ConfigureAwaitOptions . None ) ;
300-
301- if ( successfulBpns . Count != 0 )
289+ Action < CompanyUserAssignedBusinessPartner > CreateProcess ( )
302290 {
303- await provisioningManager . AddBpnAttributetoUserAsync ( iamUserId , successfulBpns ) . ConfigureAwait ( false ) ;
304- successfulBpns . Except ( assignedBusinessPartnerNumbers ) . IfAny ( businessPartnersToAdd =>
305- portalRepositories . GetInstance < IUserBusinessPartnerRepository > ( ) . CreateCompanyUserAssignedBusinessPartners ( businessPartnersToAdd . Select ( bpn => ( userId , bpn ) ) ) ) ;
291+ var processStepRepository = portalRepositories . GetInstance < IProcessStepRepository > ( ) ;
292+ var processId = processStepRepository . CreateProcess ( ProcessTypeId . USER_BPN ) . Id ;
293+ processStepRepository . CreateProcessStep ( ProcessStepTypeId . CHECK_LEGAL_ENTITY_DATA , ProcessStepStatusId . TODO , processId ) ;
294+ return x => x . ProcessId = processId ;
306295 }
307296
308- await portalRepositories . SaveAsync ( ) ;
309- return new CompanyUsersBpnDetails ( successfulBpns , unsuccessfulBpns ) ;
310- }
311-
312- private async ValueTask < ( string bpns , Exception ? error ) > CompanyUsersBpnCheck ( string bpn , string token , CancellationToken cancellationToken )
313- {
314- Exception ? error = null ;
315- try
316- {
317- if ( bpn . Length > 20 )
318- {
319- throw new ControllerArgumentException ( "BusinessPartnerNumbers must not exceed 20 characters" ) ;
320- }
321-
322- var legalEntity = await bpnAccess . FetchLegalEntityByBpn ( bpn , token , cancellationToken ) . ConfigureAwait ( false ) ;
323- if ( ! bpn . Equals ( legalEntity . Bpn , StringComparison . OrdinalIgnoreCase ) )
324- {
325- throw new ConflictException ( "Bpdm did return incorrect bpn legal-entity-data" ) ;
326- }
327- }
328- catch ( Exception ex )
329- {
330- error = ex ;
331- }
297+ portalRepositories . GetInstance < IUserBusinessPartnerRepository > ( )
298+ . CreateCompanyUserAssignedBusinessPartners ( businessPartnerNumbers . Except ( assignedBusinessPartnerNumbers ) . Select ( bpn => ( userId , bpn , CreateProcess ( ) ) ) ) ;
332299
333- return ( bpn , error ) ;
300+ await portalRepositories . SaveAsync ( ) . ConfigureAwait ( ConfigureAwaitOptions . None ) ;
334301 }
335302
336- public Task < CompanyUsersBpnDetails > AddOwnCompanyUsersBusinessPartnerNumberAsync ( Guid userId , string token , string businessPartnerNumber , CancellationToken cancellationToken ) =>
337- AddOwnCompanyUsersBusinessPartnerNumbersAsync ( userId , token , Enumerable . Repeat ( businessPartnerNumber , 1 ) , cancellationToken ) ;
303+ public Task AddOwnCompanyUsersBusinessPartnerNumberAsync ( Guid userId , string businessPartnerNumber , CancellationToken cancellationToken ) =>
304+ AddOwnCompanyUsersBusinessPartnerNumbersAsync ( userId , Enumerable . Repeat ( businessPartnerNumber , 1 ) , cancellationToken ) ;
338305
339306 public async Task < CompanyOwnUserDetails > GetOwnUserDetails ( )
340307 {
@@ -576,12 +543,10 @@ public async Task<bool> ExecuteOwnCompanyUserPasswordReset(Guid companyUserId)
576543 new [ ] { UserStatusId . ACTIVE , UserStatusId . INACTIVE } ,
577544 filter ) ) ;
578545
579- public async Task < int > DeleteOwnUserBusinessPartnerNumbersAsync ( Guid userId , string businessPartnerNumber )
546+ public async Task DeleteOwnUserBusinessPartnerNumbersAsync ( Guid userId , string businessPartnerNumber )
580547 {
581548 var userBusinessPartnerRepository = portalRepositories . GetInstance < IUserBusinessPartnerRepository > ( ) ;
582-
583549 var ( isValidUser , isAssignedBusinessPartner , isSameCompany ) = await userBusinessPartnerRepository . GetOwnCompanyUserWithAssignedBusinessPartnerNumbersAsync ( userId , _identityData . CompanyId , businessPartnerNumber . ToUpper ( ) ) . ConfigureAwait ( ConfigureAwaitOptions . None ) ;
584-
585550 if ( ! isValidUser )
586551 {
587552 throw new NotFoundException ( $ "user { userId } does not exist") ;
@@ -597,12 +562,21 @@ public async Task<int> DeleteOwnUserBusinessPartnerNumbersAsync(Guid userId, str
597562 throw new ForbiddenException ( $ "userId { userId } and adminUserId { _identityData . IdentityId } do not belong to same company") ;
598563 }
599564
600- var iamUserId = await provisioningManager . GetUserByUserName ( userId . ToString ( ) ) . ConfigureAwait ( ConfigureAwaitOptions . None ) ?? throw new ConflictException ( $ "user { userId } is not associated with a user in keycloak") ;
601-
602- userBusinessPartnerRepository . DeleteCompanyUserAssignedBusinessPartner ( userId , businessPartnerNumber . ToUpper ( ) ) ;
565+ var processStepRepository = portalRepositories . GetInstance < IProcessStepRepository > ( ) ;
566+ var processId = processStepRepository . CreateProcess ( ProcessTypeId . USER_BPN ) . Id ;
567+ processStepRepository . CreateProcessStep ( ProcessStepTypeId . DELETE_BPN_FROM_CENTRAL_USER , ProcessStepStatusId . TODO , processId ) ;
568+ userBusinessPartnerRepository . AttachAndModifyCompanyUserAssignedBusinessPartner ( userId , businessPartnerNumber , c => c . ProcessId = processId ) ;
569+ await portalRepositories . SaveAsync ( ) . ConfigureAwait ( ConfigureAwaitOptions . None ) ;
570+ }
603571
604- await provisioningManager . DeleteCentralUserBusinessPartnerNumberAsync ( iamUserId , businessPartnerNumber . ToUpper ( ) ) . ConfigureAwait ( ConfigureAwaitOptions . None ) ;
572+ public Task RetriggerUserBpnProcess ( Guid processId , ProcessStepTypeId processStepTypeId )
573+ {
574+ var ( processType , _) = processStepTypeId . GetProcessStepForRetrigger ( ) ;
575+ if ( processType != ProcessTypeId . USER_BPN )
576+ {
577+ throw new ConflictException ( $ "{ processStepTypeId } can't be retriggered for Process { processId } ") ;
578+ }
605579
606- return await portalRepositories . SaveAsync ( ) . ConfigureAwait ( ConfigureAwaitOptions . None ) ;
580+ return processStepTypeId . TriggerProcessStep ( processId , portalRepositories ) ;
607581 }
608582}
0 commit comments