Skip to content

Commit

Permalink
feat(invite): create application and company on invite (#960)
Browse files Browse the repository at this point in the history
* create application and company on invite
* return applicationId and companyId in response
Refs: #700
---------
Co-authored-by: Norbert Truchsess <[email protected]>
  • Loading branch information
Phil91 authored Sep 17, 2024
1 parent 0857d0b commit 8bb3bfd
Show file tree
Hide file tree
Showing 16 changed files with 10,091 additions and 140 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ namespace Org.Eclipse.TractusX.Portal.Backend.Administration.Service.BusinessLog
{
public interface IInvitationBusinessLogic
{
Task ExecuteInvitation(CompanyInvitationData invitationData);
Task<CompanyInvitationResponse> ExecuteInvitation(CompanyInvitationData invitationData);
Task RetriggerCreateCentralIdp(Guid processId);
Task RetriggerCreateSharedIdpServiceAccount(Guid processId);
Task RetriggerUpdateCentralIdpUrls(Guid processId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,35 +42,42 @@ public InvitationBusinessLogic(
_portalRepositories = portalRepositories;
}

public Task ExecuteInvitation(CompanyInvitationData invitationData)
public Task<CompanyInvitationResponse> ExecuteInvitation(CompanyInvitationData invitationData)
{
if (string.IsNullOrWhiteSpace(invitationData.Email))
{
throw new ControllerArgumentException("email must not be empty", "email");
}

if (invitationData.OrganisationName == null || !invitationData.OrganisationName.IsValidCompanyName())
if (!invitationData.OrganisationName.IsValidCompanyName())
{
throw ControllerArgumentException.Create(ValidationExpressionErrors.INCORRECT_COMPANY_NAME, [new ErrorParameter("name", nameof(invitationData.OrganisationName))]);
}

return ExecuteInvitationInternalAsync(invitationData);
}

private async Task ExecuteInvitationInternalAsync(CompanyInvitationData invitationData)
private async Task<CompanyInvitationResponse> ExecuteInvitationInternalAsync(CompanyInvitationData invitationData)
{
var (userName, firstName, lastName, email, organisationName) = invitationData;
var processStepRepository = _portalRepositories.GetInstance<IProcessStepRepository>();
var processId = processStepRepository.CreateProcess(ProcessTypeId.INVITATION).Id;
processStepRepository.CreateProcessStep(ProcessStepTypeId.INVITATION_CREATE_CENTRAL_IDP, ProcessStepStatusId.TODO, processId);
_portalRepositories.GetInstance<ICompanyInvitationRepository>().CreateCompanyInvitation(firstName, lastName, email, organisationName, processId, ci =>

var company = _portalRepositories.GetInstance<ICompanyRepository>().CreateCompany(organisationName);

var applicationRepository = _portalRepositories.GetInstance<IApplicationRepository>();
var applicationId = applicationRepository.CreateCompanyApplication(company.Id, CompanyApplicationStatusId.CREATED, CompanyApplicationTypeId.INTERNAL).Id;
_portalRepositories.GetInstance<ICompanyInvitationRepository>().CreateCompanyInvitation(applicationId, firstName, lastName, email, processId, ci =>
{
if (!string.IsNullOrWhiteSpace(userName))
{
if (!string.IsNullOrWhiteSpace(userName))
{
ci.UserName = userName;
}
});
ci.UserName = userName;
}
});

await _portalRepositories.SaveAsync().ConfigureAwait(ConfigureAwaitOptions.None);
return new CompanyInvitationResponse(applicationId, company.Id);
}

public Task RetriggerCreateCentralIdp(Guid processId) => ProcessStepTypeId.RETRIGGER_INVITATION_CREATE_CENTRAL_IDP.TriggerProcessStep(processId, _portalRepositories);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,12 @@ public InvitationController(IInvitationBusinessLogic logic)
[HttpPost]
[Authorize(Roles = "invite_new_partner")]
[Authorize(Policy = PolicyTypes.ValidCompany)]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(typeof(CompanyInvitationResponse), StatusCodes.Status200OK)]
[ProducesResponseType(typeof(ErrorResponse), StatusCodes.Status400BadRequest)]
[ProducesResponseType(typeof(ErrorResponse), StatusCodes.Status500InternalServerError)]
[ProducesResponseType(typeof(ErrorResponse), StatusCodes.Status502BadGateway)]
[ProducesResponseType(typeof(ErrorResponse), StatusCodes.Status409Conflict)]
public Task ExecuteInvitation([FromBody] CompanyInvitationData invitationData) =>
public Task<CompanyInvitationResponse> ExecuteInvitation([FromBody] CompanyInvitationData invitationData) =>
_logic.ExecuteInvitation(invitationData);

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/********************************************************************************
* Copyright (c) 2024 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
* SPDX-License-Identifier: Apache-2.0
********************************************************************************/

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

public record CompanyInvitationResponse(Guid ApplicationId, Guid CompanyId);
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
/********************************************************************************
* Copyright (c) 2024 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
* SPDX-License-Identifier: Apache-2.0
********************************************************************************/
* Copyright (c) 2024 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
* SPDX-License-Identifier: Apache-2.0
********************************************************************************/

using Microsoft.EntityFrameworkCore;
using Org.Eclipse.TractusX.Portal.Backend.PortalBackend.DBAccess.Models;
Expand All @@ -24,34 +24,31 @@

namespace Org.Eclipse.TractusX.Portal.Backend.PortalBackend.DBAccess.Repositories;

public class CompanyInvitationRepository : ICompanyInvitationRepository
public class CompanyInvitationRepository(PortalDbContext context)
: ICompanyInvitationRepository
{
private readonly PortalDbContext _context;

public CompanyInvitationRepository(PortalDbContext context) => _context = context;

public CompanyInvitation CreateCompanyInvitation(string firstName, string lastName, string email, string organisationName, Guid processId, Action<CompanyInvitation>? setOptionalFields)
CompanyInvitation ICompanyInvitationRepository.CreateCompanyInvitation(Guid applicationId, string firstName, string lastName, string email, Guid processId, Action<CompanyInvitation>? setOptionalFields)
{
var entity = new CompanyInvitation(Guid.NewGuid(), firstName, lastName, email, organisationName, processId);
var entity = new CompanyInvitation(Guid.NewGuid(), applicationId, firstName, lastName, email, processId);
setOptionalFields?.Invoke(entity);
return _context.Add(entity).Entity;
return context.Add(entity).Entity;
}

public Task<Guid> GetCompanyInvitationForProcessId(Guid processId) =>
_context.CompanyInvitations
context.CompanyInvitations
.AsNoTracking()
.Where(i => i.ProcessId == processId)
.Select(i => i.Id)
.SingleOrDefaultAsync();

public Task<string?> GetOrganisationNameForInvitation(Guid invitationId) =>
_context.CompanyInvitations
context.CompanyInvitations
.Where(x => x.Id == invitationId)
.Select(x => x.OrganisationName)
.Select(x => x.Application!.Company!.Name)
.SingleOrDefaultAsync();

public Task<(bool Exists, Guid? ApplicationId, Guid? CompanyId, string CompanyName, IEnumerable<(Guid IdpId, string IdpName)> IdpInformation, UserInvitationInformation UserInformation)> GetInvitationUserData(Guid companyInvitationId) =>
_context.CompanyInvitations
context.CompanyInvitations
.Where(x => x.Id == companyInvitationId)
.Select(x => new ValueTuple<bool, Guid?, Guid?, string, IEnumerable<ValueTuple<Guid, string>>, UserInvitationInformation>(
true,
Expand All @@ -64,34 +61,44 @@ public Task<Guid> GetCompanyInvitationForProcessId(Guid processId) =>
.SingleOrDefaultAsync();

public Task<(bool Exists, string OrgName, string? IdpName)> GetIdpAndOrgName(Guid invitationId) =>
_context.CompanyInvitations
context.CompanyInvitations
.Where(x => x.Id == invitationId)
.Select(x => new ValueTuple<bool, string, string?>(
true,
x.OrganisationName,
x.Application!.Company!.Name,
x.IdpName
))
.SingleOrDefaultAsync();

public Task<(bool Exists, Guid CompanyId, string? IdpName)> GetIdpAndCompanyId(Guid invitationId) =>
context.CompanyInvitations
.Where(x => x.Id == invitationId)
.Select(x => new ValueTuple<bool, Guid, string?>(
true,
x.Application!.CompanyId,
x.IdpName
))
.SingleOrDefaultAsync();

public void AttachAndModifyCompanyInvitation(Guid invitationId, Action<CompanyInvitation>? initialize, Action<CompanyInvitation> modify)
{
var entity = new CompanyInvitation(invitationId, null!, null!, null!, null!, Guid.Empty);
var entity = new CompanyInvitation(invitationId, Guid.Empty, null!, null!, null!, Guid.Empty);
initialize?.Invoke(entity);
_context.Attach(entity);
context.Attach(entity);
modify(entity);
}

public Task<string?> GetIdpNameForInvitationId(Guid invitationId) =>
_context.CompanyInvitations
context.CompanyInvitations
.Where(x => x.Id == invitationId)
.Select(x => x.IdpName)
.SingleOrDefaultAsync();

public Task<(string OrgName, string? IdpName, string? ClientId, byte[]? ClientSecret, byte[]? InitializationVector, int? EncryptionMode)> GetUpdateCentralIdpUrlData(Guid invitationId) =>
_context.CompanyInvitations
context.CompanyInvitations
.Where(x => x.Id == invitationId)
.Select(x => new ValueTuple<string, string?, string?, byte[]?, byte[]?, int?>(
x.OrganisationName,
x.Application!.Company!.Name,
x.IdpName,
x.ClientId,
x.ClientSecret,
Expand All @@ -100,7 +107,7 @@ public void AttachAndModifyCompanyInvitation(Guid invitationId, Action<CompanyIn
.SingleOrDefaultAsync();

public Task<string?> GetServiceAccountUserIdForInvitation(Guid invitationId) =>
_context.CompanyInvitations
context.CompanyInvitations
.Where(x => x.Id == invitationId)
.Select(x => x.ServiceAccountUserId)
.SingleOrDefaultAsync();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public void AttachAndModifyCompany(Guid companyId, Action<Company>? initialize,
modify(company);
}

public Address CreateAddress(string city, string streetname, string countryAlpha2Code, Action<Address>? setOptionalParameters = null)
Address ICompanyRepository.CreateAddress(string city, string streetname, string countryAlpha2Code, Action<Address>? setOptionalParameters)
{
var address = new Address(
Guid.NewGuid(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@ namespace Org.Eclipse.TractusX.Portal.Backend.PortalBackend.DBAccess.Repositorie

public interface ICompanyInvitationRepository
{
CompanyInvitation CreateCompanyInvitation(string firstName, string lastName, string email, string organisationName, Guid processId, Action<CompanyInvitation>? setOptionalFields);
CompanyInvitation CreateCompanyInvitation(Guid applicationId, string firstName, string lastName, string email, Guid processId, Action<CompanyInvitation>? setOptionalFields = null);
Task<Guid> GetCompanyInvitationForProcessId(Guid processId);
Task<string?> GetOrganisationNameForInvitation(Guid invitationId);
Task<(bool Exists, Guid? ApplicationId, Guid? CompanyId, string CompanyName, IEnumerable<(Guid IdpId, string IdpName)> IdpInformation, UserInvitationInformation UserInformation)> GetInvitationUserData(Guid companyInvitationId);
Task<(bool Exists, string OrgName, string? IdpName)> GetIdpAndOrgName(Guid invitationId);
Task<(bool Exists, Guid CompanyId, string? IdpName)> GetIdpAndCompanyId(Guid invitationId);
void AttachAndModifyCompanyInvitation(Guid invitationId, Action<CompanyInvitation>? initialize, Action<CompanyInvitation> modify);
Task<string?> GetIdpNameForInvitationId(Guid invitationId);
Task<(string OrgName, string? IdpName, string? ClientId, byte[]? ClientSecret, byte[]? InitializationVector, int? EncryptionMode)> GetUpdateCentralIdpUrlData(Guid invitationId);
Expand Down
Loading

0 comments on commit 8bb3bfd

Please sign in to comment.