Skip to content

Commit

Permalink
Update to ensure we only send when completed or failed
Browse files Browse the repository at this point in the history
  • Loading branch information
FirestarJes committed Oct 28, 2024
1 parent 1ada669 commit 2bccd43
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@ public interface ISettlementReportRepository

Task<IEnumerable<SettlementReport>> GetForJobsAsync(Guid actorId);

Task<IEnumerable<SettlementReport>> GetNeedsNotificationSent();
Task<IEnumerable<SettlementReport>> GetNeedsNotificationSentForCompletedAndFailed();
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using Energinet.DataHub.Core.Messaging.Communication;
using Energinet.DataHub.Core.Messaging.Communication.Publisher;
using Energinet.DataHub.SettlementReport.Application.SettlementReports_v2;
using Energinet.DataHub.SettlementReport.Interfaces.SettlementReports_v2.Models;
using Google.Protobuf.WellKnownTypes;

namespace Energinet.DataHub.SettlementReport.Infrastructure.Notifications;
Expand All @@ -31,12 +32,12 @@ public IntegrationEventProvider(ISettlementReportRepository settlementReportRepo
public async IAsyncEnumerable<IntegrationEvent> GetAsync()
{
var reportsForNotifications = await _settlementReportRepository
.GetNeedsNotificationSent()
.GetNeedsNotificationSentForCompletedAndFailed()
.ConfigureAwait(false);

foreach (var reportForNotification in reportsForNotifications)
{
yield return await CreateAsync(reportForNotification).ConfigureAwait(false);
yield return await CreateAsync(reportForNotification, reportForNotification.Status).ConfigureAwait(false);

reportForNotification.MarkAsNotificationSent();

Expand All @@ -45,7 +46,7 @@ await _settlementReportRepository.AddOrUpdateAsync(reportForNotification)
}
}

private Task<IntegrationEvent> CreateAsync(Application.SettlementReports_v2.SettlementReport reportForNotification)
private Task<IntegrationEvent> CreateAsync(Application.SettlementReports_v2.SettlementReport reportForNotification, SettlementReportStatus status)
{
ArgumentNullException.ThrowIfNull(reportForNotification);

Expand All @@ -57,7 +58,12 @@ private Task<IntegrationEvent> CreateAsync(Application.SettlementReports_v2.Sett
Contracts.UserNotificationTriggered.CurrentMinorVersion,
new Contracts.UserNotificationTriggered
{
ReasonIdentifier = "SettlementReportFinished",
ReasonIdentifier = status switch
{
SettlementReportStatus.Completed => "SettlementReportCompleted",
SettlementReportStatus.Failed => "SettlementReportFailed",
_ => throw new InvalidOperationException("Sending notification for settlement report with status other than Completed or Failed is not supported"),
},
TargetActorId = reportForNotification.ActorId.ToString(),
RelatedId = reportForNotification.Id.ToString(),
OccurredAt = now.ToTimestamp(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// limitations under the License.

using Energinet.DataHub.SettlementReport.Application.SettlementReports_v2;
using Energinet.DataHub.SettlementReport.Interfaces.SettlementReports_v2.Models;
using Microsoft.EntityFrameworkCore;

namespace Energinet.DataHub.SettlementReport.Infrastructure.Persistence.SettlementReportRequest;
Expand Down Expand Up @@ -90,10 +91,10 @@ public async Task DeleteAsync(Application.SettlementReports_v2.SettlementReport
.ConfigureAwait(false);
}

public async Task<IEnumerable<Application.SettlementReports_v2.SettlementReport>> GetNeedsNotificationSent()
public async Task<IEnumerable<Application.SettlementReports_v2.SettlementReport>> GetNeedsNotificationSentForCompletedAndFailed()
{
return await _context.SettlementReports
.Where(x => x.IsNotficationSent == false)
.Where(x => x.IsNotficationSent == false && (x.Status == SettlementReportStatus.Completed || x.Status == SettlementReportStatus.Failed))
.OrderBy(x => x.EndedDateTime)
.ToListAsync()
.ConfigureAwait(false);
Expand Down

0 comments on commit 2bccd43

Please sign in to comment.