Skip to content

Commit

Permalink
feat: added notification trigger for reissuanse when credential is re…
Browse files Browse the repository at this point in the history
…voked
  • Loading branch information
leandro-cavalcante committed Aug 20, 2024
1 parent 54a2a73 commit b9692e9
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 13 deletions.
4 changes: 2 additions & 2 deletions src/credentials/SsiCredentialIssuer.Reissuance.App/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
using Org.Eclipse.TractusX.SsiCredentialIssuer.Portal.Service.DependencyInjection;

LoggingExtensions.EnsureInitialized();
Log.Information("Building worker");
Log.Information("Building Reissuance App");
try
{
var host = Host
Expand All @@ -47,7 +47,7 @@
})
.AddLogging()
.Build();
Log.Information("Building worker completed");
Log.Information("Building Reissuance App completed");

using var tokenSource = new CancellationTokenSource();
Console.CancelKeyPress += (s, e) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,6 @@ public interface IReissuanceRepository
Guid GetCompanySsiDetailId(Guid companySsiDetaillId);

bool IsReissuedCredential(Guid companySsiDetaillId);

bool IsCredentialRevokedByReissuance(Guid companySsiDetaillId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,11 @@ public bool IsReissuedCredential(Guid companySsiDetaillId)
.Where(ssi => ssi.ReissuedCredentialId == companySsiDetaillId)
.Select(ssi => true).SingleOrDefault();
}

public bool IsCredentialRevokedByReissuance(Guid companySsiDetaillId)
{
return _dbContext.Reissuances
.Where(ssi => ssi.Id == companySsiDetaillId)
.Select(ssi => true).SingleOrDefault();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
"verified_credential_external_type_id": 6,
"verified_credential_type_id": 6
},
{
"verified_credential_external_type_id": 7,
"verified_credential_type_id": 7
},
{
"verified_credential_external_type_id": 8,
"verified_credential_type_id": 8
Expand All @@ -35,6 +39,10 @@
"verified_credential_external_type_id": 10,
"verified_credential_type_id": 10
},
{
"verified_credential_external_type_id": 11,
"verified_credential_type_id": 11
},
{
"verified_credential_external_type_id": 12,
"verified_credential_type_id": 12
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,5 +152,10 @@ public enum NotificationTypeId
/// <summary>
/// Notification when a credential got rejected
/// </summary>
CREDENTIAL_EXPIRY = 26
CREDENTIAL_EXPIRY = 26,

/// <summary>
/// Notification when a credential got Reissued
/// </summary>
CREDENTIAL_RENEWAL = 27
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,12 @@ public CredentialExpiryProcessHandler(IIssuerRepositories repositories, IWalletS
public async Task<(IEnumerable<ProcessStepTypeId>? nextStepTypeIds, ProcessStepStatusId stepStatusId, bool modified, string? processMessage)> TriggerNotification(Guid credentialId, CancellationToken cancellationToken)
{
var (typeId, requesterId) = await _repositories.GetInstance<ICredentialRepository>().GetCredentialNotificationData(credentialId).ConfigureAwait(ConfigureAwaitOptions.None);
var notificationTypeId = await GetNotificationTypeId(credentialId);

if (Guid.TryParse(requesterId, out var companyUserId))
{
var content = JsonSerializer.Serialize(new { Type = typeId, CredentialId = credentialId }, Options);
await _portalService.AddNotification(content, companyUserId, NotificationTypeId.CREDENTIAL_REJECTED, cancellationToken);
await _portalService.AddNotification(content, companyUserId, notificationTypeId, cancellationToken);
}

return (
Expand All @@ -96,18 +98,30 @@ public CredentialExpiryProcessHandler(IIssuerRepositories repositories, IWalletS
null);
}

private Task<NotificationTypeId> GetNotificationTypeId(Guid credentialId)
{
var isRevokedByReissuance = _repositories.GetInstance<IReissuanceRepository>().IsCredentialRevokedByReissuance(credentialId);
return Task.FromResult(isRevokedByReissuance ? NotificationTypeId.CREDENTIAL_RENEWAL : NotificationTypeId.CREDENTIAL_REJECTED);
}

public async Task<(IEnumerable<ProcessStepTypeId>? nextStepTypeIds, ProcessStepStatusId stepStatusId, bool modified, string? processMessage)> TriggerMail(Guid credentialId, CancellationToken cancellationToken)
{
var (typeId, requesterId) = await _repositories.GetInstance<ICredentialRepository>().GetCredentialNotificationData(credentialId).ConfigureAwait(ConfigureAwaitOptions.None);

var typeValue = typeId.GetEnumValue() ?? throw new UnexpectedConditionException($"VerifiedCredentialType {typeId} does not exists");
var isRevokedByReissuance = _repositories.GetInstance<IReissuanceRepository>().IsCredentialRevokedByReissuance(credentialId);

if (Guid.TryParse(requesterId, out var companyUserId))
{
var mailParameters = new MailParameter[]
if (isRevokedByReissuance)
{
var mailParameters = CreateEmailParameters(typeValue, "The credential about to expiry is revoked and new credential was reissued");
await _portalService.TriggerMail("CredentialRenewal", companyUserId, mailParameters, cancellationToken);
}
else
{
new("requestName", typeValue), new("reason", "The credential is already expired")
};
await _portalService.TriggerMail("CredentialRejected", companyUserId, mailParameters, cancellationToken);
var mailParameters = CreateEmailParameters(typeValue, "The credential is already expired");
await _portalService.TriggerMail("CredentialRejected", companyUserId, mailParameters, cancellationToken);
}
}

return (
Expand All @@ -116,4 +130,12 @@ public CredentialExpiryProcessHandler(IIssuerRepositories repositories, IWalletS
false,
null);
}

private static MailParameter[] CreateEmailParameters(string typeValue, string reason)
{
return
[
new("requestName", typeValue), new("reason", reason)
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
********************************************************************************/

using Microsoft.Extensions.DependencyInjection;
using Org.Eclipse.TractusX.SsiCredentialIssuer.CredentialProcess.Library;
using Org.Eclipse.TractusX.SsiCredentialIssuer.CredentialProcess.Library.DependencyInjection;
using Org.Eclipse.TractusX.SsiCredentialIssuer.CredentialProcess.Worker.Expiry;
using Org.Eclipse.TractusX.SsiCredentialIssuer.Processes.Worker.Library;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public async Task SignCredential_WithValidData_ReturnsExpected()
result.modified.Should().BeFalse();
result.processMessage.Should().BeNull();
result.stepStatusId.Should().Be(ProcessStepStatusId.DONE);
result.nextStepTypeIds.Should().ContainSingle().Which.Should().Be(ProcessStepTypeId.SAVE_CREDENTIAL_DOCUMENT);
result.nextStepTypeIds.Should().ContainSingle().Which.Should().Be(ProcessStepTypeId.REVOKE_REISSUED_CREDENTIAL);
}

#endregion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public class CredentialExpiryProcessHandlerTests
private readonly IWalletService _walletService;
private readonly IIssuerRepositories _issuerRepositories;
private readonly ICredentialRepository _credentialRepository;
private readonly IReissuanceRepository _reissuanceRepository;
private readonly IPortalService _portalService;

private readonly CredentialExpiryProcessHandler _sut;
Expand All @@ -58,9 +59,11 @@ public CredentialExpiryProcessHandlerTests()
_issuerRepositories = A.Fake<IIssuerRepositories>();
_credentialRepository = A.Fake<ICredentialRepository>();
_documentRepository = A.Fake<IDocumentRepository>();
_reissuanceRepository = A.Fake<IReissuanceRepository>();

A.CallTo(() => _issuerRepositories.GetInstance<ICredentialRepository>()).Returns(_credentialRepository);
A.CallTo(() => _issuerRepositories.GetInstance<IDocumentRepository>()).Returns(_documentRepository);
A.CallTo(() => _issuerRepositories.GetInstance<IReissuanceRepository>()).Returns(_reissuanceRepository);

_walletService = A.Fake<IWalletService>();
_portalService = A.Fake<IPortalService>();
Expand Down Expand Up @@ -162,7 +165,7 @@ public async Task RevokeCredential_WithEmptyExternalCredentialId_ThrowsConflictE
#region TriggerNotification

[Fact]
public async Task TriggerNotification_WithValid_CallsExpected()
public async Task TriggerNotification_CredentialRejected_WithValid_CallsExpected()
{
// Arrange
var requesterId = Guid.NewGuid();
Expand All @@ -181,12 +184,32 @@ public async Task TriggerNotification_WithValid_CallsExpected()
result.nextStepTypeIds.Should().ContainSingle().Which.Should().Be(ProcessStepTypeId.TRIGGER_MAIL);
}

[Fact]
public async Task TriggerNotification_CredentialRenewal_WithValid_CallsExpected()
{
// Arrange
var requesterId = Guid.NewGuid();
A.CallTo(() => _reissuanceRepository.IsCredentialRevokedByReissuance(_credentialId)).Returns(true);
A.CallTo(() => _credentialRepository.GetCredentialNotificationData(_credentialId))
.Returns((VerifiedCredentialExternalTypeId.BUSINESS_PARTNER_NUMBER, requesterId.ToString()));

// Act
var result = await _sut.TriggerNotification(_credentialId, CancellationToken.None);

// Assert
A.CallTo(() => _portalService.AddNotification(A<string>._, requesterId, NotificationTypeId.CREDENTIAL_RENEWAL, A<CancellationToken>._))
.MustHaveHappenedOnceExactly();
result.modified.Should().BeFalse();
result.processMessage.Should().BeNull();
result.stepStatusId.Should().Be(ProcessStepStatusId.DONE);
result.nextStepTypeIds.Should().ContainSingle().Which.Should().Be(ProcessStepTypeId.TRIGGER_MAIL);
}
#endregion

#region TriggerMail

[Fact]
public async Task TriggerMail_WithValid_CallsExpected()
public async Task TriggerMail_CredentialRejected_WithValid_CallsExpected()
{
// Arrange
var requesterId = Guid.NewGuid();
Expand All @@ -205,5 +228,25 @@ public async Task TriggerMail_WithValid_CallsExpected()
result.nextStepTypeIds.Should().BeNull();
}

[Fact]
public async Task TriggerMail_CredentialRenewal_WithValid_CallsExpected()
{
// Arrange
var requesterId = Guid.NewGuid();
A.CallTo(() => _reissuanceRepository.IsCredentialRevokedByReissuance(_credentialId)).Returns(true);
A.CallTo(() => _credentialRepository.GetCredentialNotificationData(_credentialId))
.Returns((VerifiedCredentialExternalTypeId.MEMBERSHIP_CREDENTIAL, requesterId.ToString()));

// Act
var result = await _sut.TriggerMail(_credentialId, CancellationToken.None);

// Assert
A.CallTo(() => _portalService.TriggerMail("CredentialRenewal", requesterId, A<IEnumerable<MailParameter>>._, A<CancellationToken>._))
.MustHaveHappenedOnceExactly();
result.modified.Should().BeFalse();
result.processMessage.Should().BeNull();
result.stepStatusId.Should().Be(ProcessStepStatusId.DONE);
result.nextStepTypeIds.Should().BeNull();
}
#endregion
}

0 comments on commit b9692e9

Please sign in to comment.