Skip to content

Commit

Permalink
fix(emailNotification): Add Multiple Users | Invite email didnt recei…
Browse files Browse the repository at this point in the history
…ve by User(s)

User(s) didnt receive invite emails when invited via Add Multiple Users.
Users has been created but invite email does not sent out to users.

Ref: #920
  • Loading branch information
tfjanjua committed Aug 12, 2024
1 parent 11b473d commit eb85556
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
using Org.Eclipse.TractusX.Portal.Backend.Framework.ErrorHandling.Service;
using Org.Eclipse.TractusX.Portal.Backend.Framework.IO;
using Org.Eclipse.TractusX.Portal.Backend.Framework.Linq;
using Org.Eclipse.TractusX.Portal.Backend.PortalBackend.DBAccess;
using Org.Eclipse.TractusX.Portal.Backend.PortalBackend.DBAccess.Models;
using Org.Eclipse.TractusX.Portal.Backend.PortalBackend.PortalEntities.Enums;
using Org.Eclipse.TractusX.Portal.Backend.PortalBackend.PortalEntities.Identities;
Expand All @@ -41,6 +42,7 @@ public class UserUploadBusinessLogic : IUserUploadBusinessLogic
private readonly UserSettings _settings;
private readonly IIdentityData _identityData;
private readonly IErrorMessageService _errorMessageService;
private readonly IPortalRepositories _portalRepositories;

/// <summary>
/// Constructor.
Expand All @@ -50,18 +52,21 @@ public class UserUploadBusinessLogic : IUserUploadBusinessLogic
/// <param name="identityService">Access to the identity Service</param>
/// <param name="errorMessageService">ErrorMessage Service</param>
/// <param name="settings">Settings</param>
/// <param name="portalRepositories">Portal Repositories</param>
public UserUploadBusinessLogic(
IUserProvisioningService userProvisioningService,
IMailingProcessCreation mailingProcessCreation,
IIdentityService identityService,
IErrorMessageService errorMessageService,
IOptions<UserSettings> settings)
IOptions<UserSettings> settings,
IPortalRepositories portalRepositories)
{
_userProvisioningService = userProvisioningService;
_mailingProcessCreation = mailingProcessCreation;
_identityData = identityService.IdentityData;
_errorMessageService = errorMessageService;
_settings = settings.Value;
_portalRepositories = portalRepositories;
}

public ValueTask<UserCreationStats> UploadOwnCompanyIdpUsersAsync(Guid identityProviderId, IFormFile document, CancellationToken cancellationToken)
Expand Down Expand Up @@ -167,6 +172,52 @@ await GetUserRoleDatas(parsed.Roles, validRoleData, _identityData.CompanyId).Con
}
}

private async IAsyncEnumerable<(Guid CompanyUserId, string UserName, string? Password, Exception? Error)> CreateOwnCompanySharedIdpUsersWithEmailAsync(string nameCreatedBy, CompanyNameIdpAliasData companyNameIdpAliasData, IAsyncEnumerable<UserCreationRoleDataIdpInfo> userCreationInfos, [EnumeratorCancellation] CancellationToken cancellationToken)
{
UserCreationRoleDataIdpInfo? userCreationInfo = null;

var displayName = await _userProvisioningService.GetIdentityProviderDisplayName(companyNameIdpAliasData.IdpAlias).ConfigureAwait(ConfigureAwaitOptions.None) ?? companyNameIdpAliasData.IdpAlias;

await foreach (var result in
_userProvisioningService
.CreateOwnCompanyIdpUsersAsync(
companyNameIdpAliasData,
userCreationInfos
.Select(info =>
{
userCreationInfo = info;
return info;
}),
cancellationToken)
.WithCancellation(cancellationToken)
.ConfigureAwait(false))
{
if (userCreationInfo == null)
{
throw new UnexpectedConditionException("userCreationInfo should never be null here");
}
if (result.Error != null || result.CompanyUserId == Guid.Empty || string.IsNullOrEmpty(userCreationInfo.Email))
{
yield return result;
continue;
}

var mailParameters = ImmutableDictionary.CreateRange(new[]
{
KeyValuePair.Create("password", result.Password ?? ""),
KeyValuePair.Create("companyName", displayName),
KeyValuePair.Create("nameCreatedBy", nameCreatedBy),
KeyValuePair.Create("url", _settings.Portal.BasePortalAddress),
KeyValuePair.Create("passwordResendUrl", _settings.Portal.PasswordResendAddress),
});
_mailingProcessCreation.CreateMailProcess(userCreationInfo.Email, "NewUserTemplate", mailParameters);
_mailingProcessCreation.CreateMailProcess(userCreationInfo.Email, "NewUserPasswordTemplate", mailParameters);
await _portalRepositories.SaveAsync().ConfigureAwait(ConfigureAwaitOptions.None);

yield return (result.CompanyUserId, result.UserName, result.Password, null);
}
}

private static void ValidateUserCreationRoles(IEnumerable<string> roles)
{
if (!roles.Any())
Expand Down Expand Up @@ -199,7 +250,7 @@ private async ValueTask<UserCreationStats> UploadOwnCompanySharedIdpUsersInterna
{
using var stream = document.OpenReadStream();

var (companyNameIdpAliasData, _) = await _userProvisioningService.GetCompanyNameSharedIdpAliasData(_identityData.IdentityId).ConfigureAwait(ConfigureAwaitOptions.None);
var (companyNameIdpAliasData, nameCreatedBy) = await _userProvisioningService.GetCompanyNameSharedIdpAliasData(_identityData.IdentityId).ConfigureAwait(ConfigureAwaitOptions.None);

var validRoleData = new List<UserRoleData>();

Expand All @@ -225,8 +276,8 @@ await GetUserRoleDatas(parsed.Roles, validRoleData, _identityData.CompanyId).Con
true);
},
lines =>
_userProvisioningService
.CreateOwnCompanyIdpUsersAsync(
CreateOwnCompanySharedIdpUsersWithEmailAsync(
nameCreatedBy,
companyNameIdpAliasData,
lines,
cancellationToken)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ namespace Org.Eclipse.TractusX.Portal.Backend.Administration.Service.BusinessLog
public class UserUploadBusinessLogicTests
{
private readonly IFixture _fixture;
private readonly IPortalRepositories _portalRepositories;
private readonly IUserProvisioningService _userProvisioningService;
private readonly IOptions<UserSettings> _options;
private readonly IFormFile _document;
Expand All @@ -60,6 +61,7 @@ public UserUploadBusinessLogicTests()
_fixture.Behaviors.OfType<ThrowingRecursionBehavior>().ToList()
.ForEach(b => _fixture.Behaviors.Remove(b));
_fixture.Behaviors.Add(new OmitOnRecursionBehavior());
_portalRepositories = A.Fake<IPortalRepositories>();

_random = new Random();

Expand Down Expand Up @@ -96,7 +98,7 @@ public async Task TestSetup()
{
SetupFakes(new[] { HeaderLine() });

var sut = new UserUploadBusinessLogic(_userProvisioningService, _mailingProcessCreation, _identityService, _errorMessageService, _options);
var sut = new UserUploadBusinessLogic(_userProvisioningService, _mailingProcessCreation, _identityService, _errorMessageService, _options, _portalRepositories);

var result = await sut.UploadOwnCompanyIdpUsersAsync(_identityProviderId, _document, CancellationToken.None);

Expand All @@ -122,7 +124,7 @@ public async Task TestUserCreationAllSuccess()
NextLine()
});

var sut = new UserUploadBusinessLogic(_userProvisioningService, _mailingProcessCreation, _identityService, _errorMessageService, _options);
var sut = new UserUploadBusinessLogic(_userProvisioningService, _mailingProcessCreation, _identityService, _errorMessageService, _options, _portalRepositories);

var result = await sut.UploadOwnCompanyIdpUsersAsync(_identityProviderId, _document, CancellationToken.None);

Expand All @@ -149,7 +151,7 @@ public async Task TestUserCreationHeaderParsingThrows()
NextLine()
});

var sut = new UserUploadBusinessLogic(_userProvisioningService, _mailingProcessCreation, _identityService, _errorMessageService, _options);
var sut = new UserUploadBusinessLogic(_userProvisioningService, _mailingProcessCreation, _identityService, _errorMessageService, _options, _portalRepositories);

async Task Act() => await sut.UploadOwnCompanyIdpUsersAsync(_identityProviderId, _document, CancellationToken.None);

Expand Down Expand Up @@ -180,7 +182,7 @@ public async Task TestUserCreationCreationError()
.With(x => x.Error, detailError)
.Create());

var sut = new UserUploadBusinessLogic(_userProvisioningService, _mailingProcessCreation, _identityService, _errorMessageService, _options);
var sut = new UserUploadBusinessLogic(_userProvisioningService, _mailingProcessCreation, _identityService, _errorMessageService, _options, _portalRepositories);

var result = await sut.UploadOwnCompanyIdpUsersAsync(_identityProviderId, _document, CancellationToken.None);

Expand Down Expand Up @@ -222,7 +224,7 @@ public async Task TestUserCreationCreationNoRolesError()
NextLine()
});

var sut = new UserUploadBusinessLogic(_userProvisioningService, _mailingProcessCreation, _identityService, _errorMessageService, _options);
var sut = new UserUploadBusinessLogic(_userProvisioningService, _mailingProcessCreation, _identityService, _errorMessageService, _options, _portalRepositories);

var result = await sut.UploadOwnCompanyIdpUsersAsync(_identityProviderId, _document, CancellationToken.None);

Expand Down Expand Up @@ -250,7 +252,7 @@ public async Task TestUserCreationParsingError()
NextLine()
});

var sut = new UserUploadBusinessLogic(_userProvisioningService, _mailingProcessCreation, _identityService, _errorMessageService, _options);
var sut = new UserUploadBusinessLogic(_userProvisioningService, _mailingProcessCreation, _identityService, _errorMessageService, _options, _portalRepositories);

var result = await sut.UploadOwnCompanyIdpUsersAsync(_identityProviderId, _document, CancellationToken.None);

Expand Down Expand Up @@ -280,7 +282,7 @@ public async Task TestUserCreationCreationThrows()
A.CallTo(() => _processLine(A<UserCreationRoleDataIdpInfo>.That.Matches(info => CreationInfoMatches(info, creationInfo))))
.Throws(_error);

var sut = new UserUploadBusinessLogic(_userProvisioningService, _mailingProcessCreation, _identityService, _errorMessageService, _options);
var sut = new UserUploadBusinessLogic(_userProvisioningService, _mailingProcessCreation, _identityService, _errorMessageService, _options, _portalRepositories);

var result = await sut.UploadOwnCompanyIdpUsersAsync(_identityProviderId, _document, CancellationToken.None);

Expand All @@ -304,7 +306,7 @@ public async Task TestSetupSharedIdp()
{
SetupFakes(new[] { HeaderLineSharedIdp() });

var sut = new UserUploadBusinessLogic(_userProvisioningService, _mailingProcessCreation, _identityService, _errorMessageService, _options);
var sut = new UserUploadBusinessLogic(_userProvisioningService, _mailingProcessCreation, _identityService, _errorMessageService, _options, _portalRepositories);

var result = await sut.UploadOwnCompanySharedIdpUsersAsync(_document, CancellationToken.None);

Expand All @@ -328,7 +330,7 @@ public async Task TestUserCreationSharedIdpAllSuccess()
NextLineSharedIdp()
});

var sut = new UserUploadBusinessLogic(_userProvisioningService, _mailingProcessCreation, _identityService, _errorMessageService, _options);
var sut = new UserUploadBusinessLogic(_userProvisioningService, _mailingProcessCreation, _identityService, _errorMessageService, _options, _portalRepositories);

var result = await sut.UploadOwnCompanySharedIdpUsersAsync(_document, CancellationToken.None);

Expand All @@ -338,7 +340,7 @@ public async Task TestUserCreationSharedIdpAllSuccess()
result.Total.Should().Be(5);
result.Errors.Should().BeEmpty();
A.CallTo(() => _mailingProcessCreation.CreateMailProcess(A<string>._, A<string>._, A<IReadOnlyDictionary<string, string>>._))
.MustNotHaveHappened();
.MustHaveHappened();
}

[Fact]
Expand All @@ -355,7 +357,7 @@ public async Task TestUserCreationSharedIdpHeaderParsingThrows()
NextLineSharedIdp()
});

var sut = new UserUploadBusinessLogic(_userProvisioningService, _mailingProcessCreation, _identityService, _errorMessageService, _options);
var sut = new UserUploadBusinessLogic(_userProvisioningService, _mailingProcessCreation, _identityService, _errorMessageService, _options, _portalRepositories);

async Task Act() => await sut.UploadOwnCompanySharedIdpUsersAsync(_document, CancellationToken.None);

Expand All @@ -379,7 +381,7 @@ public async Task TestUserCreationSharedIdpNoRolesError()
NextLineSharedIdp(),
});

var sut = new UserUploadBusinessLogic(_userProvisioningService, _mailingProcessCreation, _identityService, _errorMessageService, _options);
var sut = new UserUploadBusinessLogic(_userProvisioningService, _mailingProcessCreation, _identityService, _errorMessageService, _options, _portalRepositories);

var result = await sut.UploadOwnCompanySharedIdpUsersAsync(_document, CancellationToken.None);

Expand Down Expand Up @@ -417,7 +419,7 @@ public async Task TestUserCreationSharedIdpCreationError()
.With(x => x.Error, detailError)
.Create());

var sut = new UserUploadBusinessLogic(_userProvisioningService, _mailingProcessCreation, _identityService, _errorMessageService, _options);
var sut = new UserUploadBusinessLogic(_userProvisioningService, _mailingProcessCreation, _identityService, _errorMessageService, _options, _portalRepositories);

var result = await sut.UploadOwnCompanySharedIdpUsersAsync(_document, CancellationToken.None);

Expand Down Expand Up @@ -453,7 +455,7 @@ public async Task TestUserCreationSharedIdpParsingError()
NextLineSharedIdp()
});

var sut = new UserUploadBusinessLogic(_userProvisioningService, _mailingProcessCreation, _identityService, _errorMessageService, _options);
var sut = new UserUploadBusinessLogic(_userProvisioningService, _mailingProcessCreation, _identityService, _errorMessageService, _options, _portalRepositories);

var result = await sut.UploadOwnCompanySharedIdpUsersAsync(_document, CancellationToken.None);

Expand Down Expand Up @@ -482,7 +484,7 @@ public async Task TestUserCreationSharedIdpCreationThrows()
A.CallTo(() => _processLine(A<UserCreationRoleDataIdpInfo>.That.Matches(info => CreationInfoMatchesSharedIdp(info, creationInfo))))
.Throws(_error);

var sut = new UserUploadBusinessLogic(_userProvisioningService, _mailingProcessCreation, _identityService, _errorMessageService, _options);
var sut = new UserUploadBusinessLogic(_userProvisioningService, _mailingProcessCreation, _identityService, _errorMessageService, _options, _portalRepositories);

var result = await sut.UploadOwnCompanySharedIdpUsersAsync(_document, CancellationToken.None);

Expand Down

0 comments on commit eb85556

Please sign in to comment.