Skip to content

Commit

Permalink
feat(mail): adjust mailing endpoint
Browse files Browse the repository at this point in the history
Refs: #564
  • Loading branch information
Phil91 committed Apr 9, 2024
1 parent 09132e7 commit 7dc6ca2
Show file tree
Hide file tree
Showing 17 changed files with 128 additions and 166 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,28 +26,20 @@

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

public class MailBusinessLogic : IMailBusinessLogic
public class MailBusinessLogic(IPortalRepositories portalRepositories, IMailingProcessCreation mailingProcessCreation)
: IMailBusinessLogic
{
private readonly IPortalRepositories _portalRepositories;
private readonly IMailingProcessCreation _mailingProcessCreation;

public MailBusinessLogic(IPortalRepositories portalRepositories, IMailingProcessCreation mailingProcessCreation)
{
_portalRepositories = portalRepositories;
_mailingProcessCreation = mailingProcessCreation;
}

public async Task SendMail(MailData mailData)
{
var data = await _portalRepositories.GetInstance<IUserRepository>().GetUserMailData(mailData.Requester).ConfigureAwait(false);
var data = await portalRepositories.GetInstance<IUserRepository>().GetUserMailData(mailData.Requester).ConfigureAwait(false);
if (!data.Exists)
{
throw NotFoundException.Create(AdministrationMailErrors.USER_NOT_FOUND, new ErrorParameter[] { new("userId", mailData.Requester.ToString()) });
}

if (data.RecipientMail is not null)
{
_mailingProcessCreation.CreateMailProcess(data.RecipientMail, mailData.Template, mailData.MailParameters);
mailingProcessCreation.CreateMailProcess(data.RecipientMail, mailData.Template, mailData.MailParameters.ToDictionary(x => x.Key, x => x.Value));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,14 @@ public MailController(IMailBusinessLogic businessLogic)
}

/// <summary>
/// Creates a notification with the given data
/// Creates a mail from the given data
/// </summary>
/// <param name="data">Data for the notification</param>
/// <param name="data">Data for the mail</param>
/// <returns>Return NoContent</returns>
/// <remarks>Example: POST: /api/notification</remarks>
/// <response code="204">Count of the notifications.</response>
/// <response code="400">NotificationStatus does not exist.</response>
/// <response code="403">IamUserId is not assigned.</response>
[HttpDelete]
/// <remarks>Example: POST: /api/administration/mail</remarks>
[HttpPost]
[Route("")]
[Authorize(Roles = "send_mail")]
// [Authorize(Roles = "send_mail")]
[Authorize(Policy = PolicyTypes.ValidIdentity)]
[ProducesResponseType(typeof(int), StatusCodes.Status204NoContent)]
[ProducesResponseType(typeof(ErrorResponse), StatusCodes.Status400BadRequest)]
Expand Down
7 changes: 6 additions & 1 deletion src/administration/Administration.Service/Models/MailData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,10 @@ namespace Org.Eclipse.TractusX.Portal.Backend.Administration.Service.Models;
public record MailData(
[property: JsonPropertyName("requester")] Guid Requester,
[property: JsonPropertyName("template")] string Template,
[property: JsonPropertyName("mailParameters")] Dictionary<string, string> MailParameters
[property: JsonPropertyName("mailParameters")] IEnumerable<MailParameter> MailParameters
);

public record MailParameter(
[property: JsonPropertyName("key")] string Key,
[property: JsonPropertyName("value")] string Value
);
Original file line number Diff line number Diff line change
Expand Up @@ -30,29 +30,19 @@

namespace Org.Eclipse.TractusX.Portal.Backend.IssuerComponent.Library.BusinessLogic;

public class IssuerComponentBusinessLogic : IIssuerComponentBusinessLogic
public class IssuerComponentBusinessLogic(
IPortalRepositories repositories,
IIssuerComponentService service,
IApplicationChecklistService checklistService,
IOptions<IssuerComponentSettings> options)
: IIssuerComponentBusinessLogic
{
private readonly IPortalRepositories _repositories;
private readonly IIssuerComponentService _service;
private readonly IApplicationChecklistService _checklistService;
private readonly IssuerComponentSettings _settings;

public IssuerComponentBusinessLogic(
IPortalRepositories repositories,
IIssuerComponentService service,
IApplicationChecklistService checklistService,
IOptions<IssuerComponentSettings> options)
{
_repositories = repositories;
_service = service;
_checklistService = checklistService;
_settings = options.Value;
}
private readonly IssuerComponentSettings _settings = options.Value;

public async Task<IApplicationChecklistService.WorkerChecklistProcessStepExecutionResult> CreateBpnlCredential(IApplicationChecklistService.WorkerChecklistProcessStepData context, CancellationToken cancellationToken)
{
var applicationId = context.ApplicationId;
var (exists, holder, businessPartnerNumber, walletInformation) = await _repositories.GetInstance<IApplicationRepository>().GetBpnlCredentialIformationByApplicationId(applicationId).ConfigureAwait(false);
var (exists, holder, businessPartnerNumber, walletInformation) = await repositories.GetInstance<IApplicationRepository>().GetBpnlCredentialIformationByApplicationId(applicationId).ConfigureAwait(false);
if (!exists)
{
throw new NotFoundException($"CompanyApplication {applicationId} does not exist");
Expand All @@ -78,7 +68,7 @@ public IssuerComponentBusinessLogic(

var callbackUrl = $"{_settings.CallbackBaseUrl}/api/administration/registration/issuer/bpncredential";
var data = new CreateBpnCredentialRequest(holder, businessPartnerNumber, new TechnicalUserDetails(walletInformation.WalletUrl, walletInformation.ClientId, secret), callbackUrl);
await _service.CreateBpnlCredential(data, cancellationToken).ConfigureAwait(false);
await service.CreateBpnlCredential(data, cancellationToken).ConfigureAwait(false);
return new IApplicationChecklistService.WorkerChecklistProcessStepExecutionResult(
ProcessStepStatusId.DONE,
checklist =>
Expand All @@ -93,7 +83,7 @@ public IssuerComponentBusinessLogic(

public async Task StoreBpnlCredential(Guid applicationId, IssuerResponseData data)
{
var context = await _checklistService
var context = await checklistService
.VerifyChecklistEntryAndProcessSteps(
applicationId,
ApplicationChecklistEntryTypeId.BPNL_CREDENTIAL,
Expand All @@ -102,7 +92,7 @@ public async Task StoreBpnlCredential(Guid applicationId, IssuerResponseData dat
processStepTypeIds: new[] { ProcessStepTypeId.REQUEST_MEMBERSHIP_CREDENTIAL })
.ConfigureAwait(false);

_checklistService.FinalizeChecklistEntryAndProcessSteps(
checklistService.FinalizeChecklistEntryAndProcessSteps(
context,
null,
item =>
Expand All @@ -118,7 +108,7 @@ public async Task StoreBpnlCredential(Guid applicationId, IssuerResponseData dat
public async Task<IApplicationChecklistService.WorkerChecklistProcessStepExecutionResult> CreateMembershipCredential(IApplicationChecklistService.WorkerChecklistProcessStepData context, CancellationToken cancellationToken)
{
var applicationId = context.ApplicationId;
var (exists, holder, businessPartnerNumber, walletInformation) = await _repositories.GetInstance<IApplicationRepository>().GetBpnlCredentialIformationByApplicationId(applicationId).ConfigureAwait(false);
var (exists, holder, businessPartnerNumber, walletInformation) = await repositories.GetInstance<IApplicationRepository>().GetBpnlCredentialIformationByApplicationId(applicationId).ConfigureAwait(false);
if (!exists)
{
throw new NotFoundException($"CompanyApplication {applicationId} does not exist");
Expand All @@ -144,7 +134,7 @@ public async Task StoreBpnlCredential(Guid applicationId, IssuerResponseData dat

var callbackUrl = $"{_settings.CallbackBaseUrl}/api/administration/registration/issuer/membershipcredential";
var data = new CreateMembershipCredentialRequest(holder, businessPartnerNumber, "catena-x", new TechnicalUserDetails(walletInformation.WalletUrl, walletInformation.ClientId, secret), callbackUrl);
await _service.CreateMembershipCredential(data, cancellationToken).ConfigureAwait(false);
await service.CreateMembershipCredential(data, cancellationToken).ConfigureAwait(false);
return new IApplicationChecklistService.WorkerChecklistProcessStepExecutionResult(
ProcessStepStatusId.DONE,
checklist =>
Expand All @@ -159,7 +149,7 @@ public async Task StoreBpnlCredential(Guid applicationId, IssuerResponseData dat

public async Task StoreMembershipCredential(Guid applicationId, IssuerResponseData data)
{
var context = await _checklistService
var context = await checklistService
.VerifyChecklistEntryAndProcessSteps(
applicationId,
ApplicationChecklistEntryTypeId.MEMBERSHIP_CREDENTIAL,
Expand All @@ -168,7 +158,7 @@ public async Task StoreMembershipCredential(Guid applicationId, IssuerResponseDa
processStepTypeIds: new[] { ProcessStepTypeId.START_CLEARING_HOUSE })
.ConfigureAwait(false);

_checklistService.FinalizeChecklistEntryAndProcessSteps(
checklistService.FinalizeChecklistEntryAndProcessSteps(
context,
null,
item =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<AssemblyName>Org.Eclipse.TractusX.Portal.Backend.IssuerComponent.Library</AssemblyName>
Expand All @@ -35,8 +35,8 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="JsonSchema.Net" Version="6.0.3" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="7.0.1" />
<PackageReference Include="JsonSchema.Net" Version="6.0.5" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -27,30 +27,24 @@

namespace Org.Eclipse.TractusX.Portal.Backend.IssuerComponent.Library.Service;

public class IssuerComponentService : IIssuerComponentService
public class IssuerComponentService(ITokenService tokenService, IOptions<IssuerComponentSettings> options)
: IIssuerComponentService
{
private static readonly JsonSerializerOptions Options = new() { PropertyNamingPolicy = JsonNamingPolicy.CamelCase };
private readonly ITokenService _tokenService;
private readonly IssuerComponentSettings _settings;

public IssuerComponentService(ITokenService tokenService, IOptions<IssuerComponentSettings> options)
{
_tokenService = tokenService;
_settings = options.Value;
}
private readonly IssuerComponentSettings _settings = options.Value;

public async Task<bool> CreateBpnlCredential(CreateBpnCredentialRequest data, CancellationToken cancellationToken)
{
var httpClient = await _tokenService.GetAuthorizedClient<IssuerComponentService>(_settings, cancellationToken).ConfigureAwait(false);
await httpClient.PostAsJsonAsync("bpn", data, Options, cancellationToken)
var httpClient = await tokenService.GetAuthorizedClient<IssuerComponentService>(_settings, cancellationToken).ConfigureAwait(false);
await httpClient.PostAsJsonAsync("/api/issuer/bpn", data, Options, cancellationToken)
.CatchingIntoServiceExceptionFor("issuer-component-bpn-post", HttpAsyncResponseMessageExtension.RecoverOptions.INFRASTRUCTURE).ConfigureAwait(false);
return true;
}

public async Task<bool> CreateMembershipCredential(CreateMembershipCredentialRequest data, CancellationToken cancellationToken)
{
var httpClient = await _tokenService.GetAuthorizedClient<IssuerComponentService>(_settings, cancellationToken).ConfigureAwait(false);
await httpClient.PostAsJsonAsync("membership", data, Options, cancellationToken)
var httpClient = await tokenService.GetAuthorizedClient<IssuerComponentService>(_settings, cancellationToken).ConfigureAwait(false);
await httpClient.PostAsJsonAsync("/api/issuer/membership", data, Options, cancellationToken)
.CatchingIntoServiceExceptionFor("issuer-component-membership-post", HttpAsyncResponseMessageExtension.RecoverOptions.INFRASTRUCTURE).ConfigureAwait(false);
return true;
}
Expand Down
Loading

0 comments on commit 7dc6ca2

Please sign in to comment.