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(registration): add current user to applicationDeclineData #390

Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -33,6 +33,7 @@ public record ApplicationChecklistData(ApplicationChecklistEntryTypeId TypeId, A
public record CompanyApplicationDeclineData(
Guid ApplicationId,
CompanyApplicationStatusId ApplicationStatus,
string User,
string CompanyName,
IEnumerable<string> Users
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,19 +165,32 @@ public IQueryable<CompanyApplication> GetCompanyApplicationsFilteredQuery(string
.Select(identifier => new ValueTuple<UniqueIdentifierId, string>(identifier.UniqueIdentifierId, identifier.Value))))
.SingleOrDefaultAsync();

public IAsyncEnumerable<CompanyApplicationDeclineData> GetCompanyApplicationsDeclineData(Guid userCompanyId, IEnumerable<CompanyApplicationStatusId> applicationStatusIds) =>
_dbContext.CompanyApplications
public Task<(string CompanyName, string? FirstName, string? LastName, string? Email, IEnumerable<(Guid ApplicationId, CompanyApplicationStatusId ApplicationStatusId, IEnumerable<(string? FirstName, string? LastName, string? Email)> InvitedUsers)> Applications)> GetCompanyApplicationsDeclineData(Guid companyUserId, IEnumerable<CompanyApplicationStatusId> applicationStatusIds) =>
_dbContext.CompanyUsers
.AsNoTracking()
.Where(application =>
application.Company!.Id == userCompanyId &&
applicationStatusIds.Contains(application.ApplicationStatusId))
.Select(application => new CompanyApplicationDeclineData(
application.Id,
application.ApplicationStatusId,
application.Company!.Name,
application.Company!.Identities.Where(identity => identity.CompanyUser!.Email != null).Select(
identity => identity.CompanyUser!.Email!)))
.AsAsyncEnumerable();
.AsSplitQuery()
.Where(user =>
user.Id == companyUserId &&
user.Identity!.UserStatusId == UserStatusId.ACTIVE
)
.Select(user => new ValueTuple<string, string?, string?, string?, IEnumerable<(Guid, CompanyApplicationStatusId, IEnumerable<(string?, string?, string?)>)>>(
user.Identity!.Company!.Name,
user.Firstname,
user.Lastname,
user.Email,
user.Identity.Company.CompanyApplications.Where(application => applicationStatusIds.Contains(application.ApplicationStatusId)).Select(application => new ValueTuple<Guid, CompanyApplicationStatusId, IEnumerable<(string?, string?, string?)>>(
application.Id,
application.ApplicationStatusId,
application.Invitations
.Where(invitation => invitation.CompanyUser!.Identity!.UserStatusId == UserStatusId.ACTIVE)
.Select(invitation => new ValueTuple<string?, string?, string?>(
invitation.CompanyUser!.Firstname,
invitation.CompanyUser.Lastname,
invitation.CompanyUser.Email
))
))
))
.SingleOrDefaultAsync();

public Task<(bool IsValidApplicationId, Guid CompanyId, bool IsSubmitted)> GetCompanyIdSubmissionStatusForApplication(Guid applicationId) =>
_dbContext.CompanyApplications
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public interface IApplicationRepository
Task<CompanyApplicationUserEmailData?> GetOwnCompanyApplicationUserEmailDataAsync(Guid applicationId, Guid companyUserId, IEnumerable<DocumentTypeId> submitDocumentTypeIds);
IQueryable<CompanyApplication> GetCompanyApplicationsFilteredQuery(string? companyName = null, IEnumerable<CompanyApplicationStatusId>? applicationStatusIds = null);
Task<CompanyApplicationDetailData?> GetCompanyApplicationDetailDataAsync(Guid applicationId, Guid userCompanyId, Guid? companyId = null);
IAsyncEnumerable<CompanyApplicationDeclineData> GetCompanyApplicationsDeclineData(Guid userCompanyId, IEnumerable<CompanyApplicationStatusId> applicationStatusIds);
Task<(string CompanyName, string? FirstName, string? LastName, string? Email, IEnumerable<(Guid ApplicationId, CompanyApplicationStatusId ApplicationStatusId, IEnumerable<(string? FirstName, string? LastName, string? Email)> InvitedUsers)> Applications)> GetCompanyApplicationsDeclineData(Guid companyUserId, IEnumerable<CompanyApplicationStatusId> applicationStatusIds);
Task<(bool IsValidApplicationId, Guid CompanyId, bool IsSubmitted)> GetCompanyIdSubmissionStatusForApplication(Guid applicationId);
Task<(Guid CompanyId, string CompanyName, string? BusinessPartnerNumber, IEnumerable<string> IamIdpAliasse, CompanyApplicationTypeId ApplicationTypeId, Guid? NetworkRegistrationProcessId)> GetCompanyAndApplicationDetailsForApprovalAsync(Guid applicationId);
Task<(Guid CompanyId, string CompanyName, string? BusinessPartnerNumber)> GetCompanyAndApplicationDetailsForCreateWalletAsync(Guid applicationId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public interface IRegistrationBusinessLogic
Task<(string FileName, byte[] Content, string MediaType)> GetDocumentContentAsync(Guid documentId);

IAsyncEnumerable<CompanyApplicationWithStatus> GetAllApplicationsForUserWithStatus();
IAsyncEnumerable<CompanyApplicationDeclineData> GetApplicationsDeclineData();
Task<IEnumerable<CompanyApplicationDeclineData>> GetApplicationsDeclineData();
Task<CompanyDetailData> GetCompanyDetailData(Guid applicationId);
Task SetCompanyDetailDataAsync(Guid applicationId, CompanyDetailData companyDetails);
Task<int> InviteNewUserAsync(Guid applicationId, UserCreationInfoWithMessage userCreationInfo);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@
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.ApplicationChecklist.Library;
using Org.Eclipse.TractusX.Portal.Backend.Provisioning.Library;
using Org.Eclipse.TractusX.Portal.Backend.Provisioning.Library.Models;
using Org.Eclipse.TractusX.Portal.Backend.Provisioning.Library.Service;
using Org.Eclipse.TractusX.Portal.Backend.Registration.Common;
using Org.Eclipse.TractusX.Portal.Backend.Registration.Service.Model;
using System.Text;
using System.Text.RegularExpressions;

namespace Org.Eclipse.TractusX.Portal.Backend.Registration.Service.BusinessLogic;
Expand Down Expand Up @@ -206,8 +206,41 @@ public async Task<int> UploadDocumentAsync(Guid applicationId, IFormFile documen
public IAsyncEnumerable<CompanyApplicationWithStatus> GetAllApplicationsForUserWithStatus() =>
_portalRepositories.GetInstance<IUserRepository>().GetApplicationsWithStatusUntrackedAsync(_identityData.CompanyId);

public IAsyncEnumerable<CompanyApplicationDeclineData> GetApplicationsDeclineData() =>
_portalRepositories.GetInstance<IApplicationRepository>().GetCompanyApplicationsDeclineData(_identityData.CompanyId, _settings.ApplicationDeclineStatusIds);
public async Task<IEnumerable<CompanyApplicationDeclineData>> GetApplicationsDeclineData()
{
string CreateNameString(string? firstName, string? lastName, string? email)
{
var sb = new StringBuilder();
if (firstName != null)
{
sb.Append(firstName);
}

if (lastName != null)
{
sb.AppendFormat(firstName == null ? "{0}" : ", {0}", lastName);
}

if (email != null)
{
sb.AppendFormat((firstName == null && lastName == null) ? "{0}" : " ({0})", email);
}

return firstName == null && lastName == null && email == null ? "unknown user" : sb.ToString();
}

var data = await _portalRepositories.GetInstance<IApplicationRepository>().GetCompanyApplicationsDeclineData(_identityData.IdentityId, _settings.ApplicationDeclineStatusIds).ConfigureAwait(false);
var user = CreateNameString(data.FirstName, data.LastName, data.Email) ?? "unknown user";
Phil91 marked this conversation as resolved.
Show resolved Hide resolved

return data.Applications.Select(application =>
new CompanyApplicationDeclineData(
application.ApplicationId,
application.ApplicationStatusId,
user,
data.CompanyName,
application.InvitedUsers.Select(user => CreateNameString(user.FirstName, user.LastName, user.Email))
));
}

public async Task<CompanyDetailData> GetCompanyDetailData(Guid applicationId)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,10 +175,10 @@ public IAsyncEnumerable<CompanyApplicationWithStatus> GetApplicationsWithStatusA
/// <response code="200">Returns a list of company applications</response>
[HttpGet]
[Authorize(Roles = "view_registration")]
[Authorize(Policy = PolicyTypes.ValidCompany)]
[Authorize(Policy = PolicyTypes.CompanyUser)]
[Route("applications/declinedata")]
[ProducesResponseType(typeof(IAsyncEnumerable<CompanyApplicationDeclineData>), StatusCodes.Status200OK)]
public IAsyncEnumerable<CompanyApplicationDeclineData> GetApplicationsDeclineData() =>
public Task<IEnumerable<CompanyApplicationDeclineData>> GetApplicationsDeclineData() =>
_registrationBusinessLogic.GetApplicationsDeclineData();

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -540,42 +540,59 @@ public async Task AttachAndModifyCompanyApplications()
public async Task GetCompanyApplicationsDeclineData_ReturnsExpected()
{
// Arrange
var sut = await CreateSut().ConfigureAwait(false);
var companyUserId = new Guid("ac1cf001-7fbc-1f2f-817f-bce058019994");
var statusIds = new[] {
CompanyApplicationStatusId.CREATED,
CompanyApplicationStatusId.ADD_COMPANY_DATA,
CompanyApplicationStatusId.INVITE_USER,
CompanyApplicationStatusId.SELECT_COMPANY_ROLE,
CompanyApplicationStatusId.UPLOAD_DOCUMENTS,
CompanyApplicationStatusId.VERIFY
CompanyApplicationStatusId.VERIFY,
CompanyApplicationStatusId.CONFIRMED,
CompanyApplicationStatusId.DECLINED
};
var sut = await CreateSut().ConfigureAwait(false);

// Act
var result = await sut.GetCompanyApplicationsDeclineData(CompanyId, statusIds).ToListAsync().ConfigureAwait(false);
var result = await sut.GetCompanyApplicationsDeclineData(companyUserId, statusIds).ConfigureAwait(false);

// Assert
result.Should().BeEmpty();
result.Should().NotBeNull().And.Match<(string CompanyName, string? FirstName, string? LastName, string? Email, IEnumerable<(Guid ApplicationId, CompanyApplicationStatusId ApplicationStatusId, IEnumerable<(string? FirstName, string? LastName, string? Email)> InvitedUsers)> Applications)>(x =>
x.CompanyName == "Security Company" &&
x.FirstName == "Test User" &&
x.LastName == "Company Admin 1" &&
x.Email == "[email protected]");

result.Applications.Should().BeEmpty();
}

[Fact]
public async Task GetCompanyApplicationsDeclineData_WithSubmittedApplication_ReturnsExpected()
{
// Arrange
var sut = await CreateSut().ConfigureAwait(false);
var companyUserId = new Guid("ac1cf001-7fbc-1f2f-817f-bce058019994");
var statusIds = new[] {
CompanyApplicationStatusId.SUBMITTED
};
var sut = await CreateSut().ConfigureAwait(false);

// Act
var result = await sut.GetCompanyApplicationsDeclineData(CompanyId, statusIds).ToListAsync().ConfigureAwait(false);
var result = await sut.GetCompanyApplicationsDeclineData(companyUserId, statusIds).ConfigureAwait(false);

// Assert
result.Should().ContainSingle().Which.Should().Match<CompanyApplicationDeclineData>(x =>
x.ApplicationId == new Guid("6b2d1263-c073-4a48-bfaf-704dc154ca9f") &&
x.ApplicationStatus == CompanyApplicationStatusId.SUBMITTED &&
x.CompanyName == "CX-Test-Access" &&
x.Users.Count() == 1 &&
x.Users.First() == "[email protected]");
result.Should().NotBeNull().And.Match<(string CompanyName, string? FirstName, string? LastName, string? Email, IEnumerable<(Guid ApplicationId, CompanyApplicationStatusId ApplicationStatusId, IEnumerable<(string? FirstName, string? LastName, string? Email)> InvitedUsers)> Applications)>(x =>
x.CompanyName == "Security Company" &&
x.FirstName == "Test User" &&
x.LastName == "Company Admin 1" &&
x.Email == "[email protected]");

result.Applications.Should().ContainSingle().Which.Should().Match<(Guid ApplicationId, CompanyApplicationStatusId ApplicationStatusId, IEnumerable<(string? FirstName, string? LastName, string? Email)> InvitedUsers)>(x =>
x.ApplicationId == new Guid("6b2d1263-c073-4a48-bfaf-704dc154ca9e") &&
x.ApplicationStatusId == CompanyApplicationStatusId.SUBMITTED);

result.Applications.First().InvitedUsers.Should().HaveCount(2).And.Satisfy(
x => x.FirstName == "Test User" && x.LastName == "Company Admin 1" && x.Email == "[email protected]",
x => x.FirstName == "Test" && x.LastName == "User" && x.Email == "[email protected]");
}

#endregion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3117,6 +3117,48 @@ private void SetupFakesForInvitation()

#endregion

#region GetApplicationsDeclineData

[Fact]
public async Task Foo()
{
// Arrange
var applicationId = Guid.NewGuid();
var data = ("Acme Corp", (string?)"Tony", (string?)"Stark", (string?)"[email protected]", new[] {
(applicationId, CompanyApplicationStatusId.CREATED, new (string?, string?, string?)[] {
("Test", "User", "[email protected]"),
(null, null, "[email protected]")
}.AsEnumerable())
});

A.CallTo(() => _applicationRepository.GetCompanyApplicationsDeclineData(A<Guid>._, A<IEnumerable<CompanyApplicationStatusId>>._))
.Returns(data);

var options = Options.Create(new RegistrationSettings
{
ApplicationDeclineStatusIds = new[] { CompanyApplicationStatusId.CREATED }
});

var sut = new RegistrationBusinessLogic(options, null!, null!, null!, null!, _portalRepositories, null!, _identityService, null!);

// Act
var result = await sut.GetApplicationsDeclineData().ConfigureAwait(false);

// Assert
A.CallTo(() => _applicationRepository.GetCompanyApplicationsDeclineData(_identity.IdentityId, A<IEnumerable<CompanyApplicationStatusId>>.That.IsSameSequenceAs(new[] { CompanyApplicationStatusId.CREATED })))
.MustHaveHappenedOnceExactly();

result.Should().ContainSingle().Which.Should().Match<CompanyApplicationDeclineData>(x =>
x.ApplicationId == applicationId &&
x.ApplicationStatus == CompanyApplicationStatusId.CREATED &&
x.CompanyName == "Acme Corp" &&
x.User == "Tony, Stark ([email protected])");

result.First().Users.Should().ContainInOrder(new[] { "Test, User ([email protected])", "[email protected]" });
}

#endregion

[Serializable]
public class TestException : Exception
{
Expand Down
Loading