Skip to content

Commit

Permalink
Added notification sending
Browse files Browse the repository at this point in the history
  • Loading branch information
FirestarJes committed Oct 28, 2024
1 parent 25b8000 commit 1ada669
Show file tree
Hide file tree
Showing 11 changed files with 236 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ limitations under the License.
<EmbeddedResource Include="Scripts\202409101100_Add_EndedDateTime_to_SettlementReport.sql" />
<EmbeddedResource Include="Scripts\202406210900_Add_IsHiddenFromActor_to_SettlementReport.sql" />
<EmbeddedResource Include="Scripts\202409081607_Add_JobId_To_SettlementReport.sql" />
<EmbeddedResource Include="Scripts\202410261405_Add_IsNotificationSent_to_SettlementReport.sql" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ALTER TABLE [settlementreports].[SettlementReport]
ADD [IsNotficationSent] [bit] NOT NULL DEFAULT(1); --For all existing records, we will assume that notification has been sent.
GO
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright 2020 Energinet DataHub A/S
//
// Licensed under the Apache License, Version 2.0 (the "License2");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using Energinet.DataHub.Core.Messaging.Communication.Publisher;
using Microsoft.Azure.Functions.Worker;

namespace Energinet.DataHub.SettlementReport.Orchestration.SettlementReports.Functions.SettlementReports;

public sealed class DispatchIntegrationEventsTrigger
{
private readonly IPublisher _publisher;

public DispatchIntegrationEventsTrigger(IPublisher publisher)
{
_publisher = publisher;
}

[Function(nameof(DispatchIntegrationEventsTrigger))]
public Task RunAsync([TimerTrigger("* */1 * * *")] FunctionContext context)
{
ArgumentNullException.ThrowIfNull(context);
return _publisher.PublishAsync(context.CancellationToken);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,6 @@ public interface ISettlementReportRepository
Task<IEnumerable<SettlementReport>> GetForJobsAsync();

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

Task<IEnumerable<SettlementReport>> GetNeedsNotificationSent();
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ public sealed class SettlementReport

public long? JobId { get; init; }

public bool IsNotficationSent { get; private set; }

public SettlementReport(
IClock clock,
Guid userId,
Expand All @@ -80,6 +82,7 @@ public SettlementReport(
SplitReportPerGridArea = request.SplitReportPerGridArea;
IncludeMonthlyAmount = request.IncludeMonthlyAmount;
GridAreas = JsonSerializer.Serialize(request.Filter.GridAreas);
IsNotficationSent = false;
}

public SettlementReport(
Expand All @@ -106,6 +109,7 @@ public SettlementReport(
SplitReportPerGridArea = request.SplitReportPerGridArea;
IncludeMonthlyAmount = request.IncludeMonthlyAmount;
GridAreas = JsonSerializer.Serialize(request.Filter.GridAreas);
IsNotficationSent = false;
}

// EF Core Constructor.
Expand All @@ -132,4 +136,9 @@ public void MarkAsFailed()
{
Status = SettlementReportStatus.Failed;
}

public void MarkAsNotificationSent()
{
IsNotficationSent = true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright 2020 Energinet DataHub A/S
//
// Licensed under the Apache License, Version 2.0 (the "License2");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace Energinet.DataHub.SettlementReport.Infrastructure.Contracts;

public partial class UserNotificationTriggered
{
public const string EventName = "UserNotificationTriggered";
public const int CurrentMinorVersion = 1;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/* Copyright 2020 Energinet DataHub A/S
*
* Licensed under the Apache License, Version 2.0 (the "License2");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

syntax = "proto3";
import "google/protobuf/timestamp.proto";

option csharp_namespace = "Energinet.DataHub.SettlementReport.Infrastructure.Contracts";

/*
* UserNotificationTriggered Integration Event.
*
* Occurs when some action triggers the notification of a set of users.
*/
message UserNotificationTriggered {

/*
* An identifier specifying the reason for the notification.
*/
string reason_identifier = 1;

/*
* The unique identifier of the actor whose users will receive the notification.
*/
string target_actor_id = 2;

oneof target {

/*
* The unique identifier of the user that should receive the notification.
*/
string target_user_id = 3;

/*
* The unique identifier of the group of users having the specified permission that should receive the notification.
*/
string target_permissions = 4;
}

/*
* A reason-specific id of an entity that the notification targets.
*/
string related_id = 5;

/*
* A timestamp for when the notification was generated.
*/
google.protobuf.Timestamp occurred_at = 6;

/*
* A timestamp for when the notification expires by itself.
*/
google.protobuf.Timestamp expires_at = 7;
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
using Energinet.DataHub.SettlementReport.Common.Infrastructure.Extensions.Options;
using Energinet.DataHub.SettlementReport.Common.Infrastructure.HealthChecks;
using Energinet.DataHub.SettlementReport.Common.Infrastructure.Options;
using Energinet.DataHub.SettlementReport.Infrastructure.Notifications;
using Energinet.DataHub.SettlementReport.Infrastructure.Persistence;
using Energinet.DataHub.SettlementReport.Infrastructure.Persistence.Databricks;
using Energinet.DataHub.SettlementReport.Infrastructure.Persistence.SettlementReportRequest;
Expand All @@ -42,10 +43,8 @@ public static IServiceCollection AddSettlementReportsV2Module(this IServiceColle
{
ArgumentNullException.ThrowIfNull(configuration);

services
.AddOptions<ServiceBusNamespaceOptions>()
.BindConfiguration(ServiceBusNamespaceOptions.SectionName)
.ValidateDataAnnotations();
services.AddOptions<IntegrationEventsOptions>().BindConfiguration(IntegrationEventsOptions.SectionName).ValidateDataAnnotations();
services.AddOptions<ServiceBusNamespaceOptions>().BindConfiguration(ServiceBusNamespaceOptions.SectionName).ValidateDataAnnotations();

services.AddDatabricksSqlStatementForApplication(configuration);

Expand Down Expand Up @@ -74,7 +73,6 @@ public static IServiceCollection AddSettlementReportsV2Module(this IServiceColle
services.AddScoped<ISettlementReportChargePriceRepository, SettlementReportChargePriceRepository>();
services.AddScoped<ISettlementReportMonthlyAmountTotalRepository, SettlementReportMonthlyAmountTotalRepository>();
services.AddSettlementReportBlobStorage();
services.AddServiceBusClientForApplication(configuration);
services.AddScoped<ISettlementReportDatabaseContext, SettlementReportDatabaseContext>();
services.AddDbContext<SettlementReportDatabaseContext>(
options => options.UseSqlServer(
Expand Down Expand Up @@ -105,6 +103,9 @@ public static IServiceCollection AddSettlementReportsV2Module(this IServiceColle
// Used by sql statements (queries)
services.AddOptions<DeltaTableOptions>().Bind(configuration);

services.AddServiceBusClientForApplication(configuration);
services.AddIntegrationEventsPublisher<IntegrationEventProvider>(configuration);

return services;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Copyright 2020 Energinet DataHub A/S
//
// Licensed under the Apache License, Version 2.0 (the "License2");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using Energinet.DataHub.Core.Messaging.Communication;
using Energinet.DataHub.Core.Messaging.Communication.Publisher;
using Energinet.DataHub.SettlementReport.Application.SettlementReports_v2;
using Google.Protobuf.WellKnownTypes;

namespace Energinet.DataHub.SettlementReport.Infrastructure.Notifications;

public sealed class IntegrationEventProvider : IIntegrationEventProvider
{
private readonly ISettlementReportRepository _settlementReportRepository;

public IntegrationEventProvider(ISettlementReportRepository settlementReportRepository)
{
_settlementReportRepository = settlementReportRepository;
}

public async IAsyncEnumerable<IntegrationEvent> GetAsync()
{
var reportsForNotifications = await _settlementReportRepository
.GetNeedsNotificationSent()
.ConfigureAwait(false);

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

reportForNotification.MarkAsNotificationSent();

await _settlementReportRepository.AddOrUpdateAsync(reportForNotification)
.ConfigureAwait(false);
}
}

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

var now = DateTime.UtcNow;

var integrationEvent = new IntegrationEvent(
Guid.Parse(reportForNotification.RequestId),
Contracts.UserNotificationTriggered.EventName,
Contracts.UserNotificationTriggered.CurrentMinorVersion,
new Contracts.UserNotificationTriggered
{
ReasonIdentifier = "SettlementReportFinished",
TargetActorId = reportForNotification.ActorId.ToString(),
RelatedId = reportForNotification.Id.ToString(),
OccurredAt = now.ToTimestamp(),
ExpiresAt = now.AddHours(23).ToTimestamp(),
});

return Task.FromResult(integrationEvent);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,13 @@ public async Task DeleteAsync(Application.SettlementReports_v2.SettlementReport
.ToListAsync()
.ConfigureAwait(false);
}

public async Task<IEnumerable<Application.SettlementReports_v2.SettlementReport>> GetNeedsNotificationSent()
{
return await _context.SettlementReports
.Where(x => x.IsNotficationSent == false)
.OrderBy(x => x.EndedDateTime)
.ToListAsync()
.ConfigureAwait(false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
<PackageReference Include="Energinet.DataHub.Core.Databricks.SqlStatementExecution" Version="11.2.4" />
<PackageReference Include="Energinet.DataHub.Core.JsonSerialization" Version="2.2.12" />
<PackageReference Include="Energinet.DataHub.Core.Messaging" Version="6.1.0" />
<PackageReference Include="Google.Protobuf" Version="3.28.3" />
<PackageReference Include="Grpc.Tools" Version="2.67.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.10" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="8.0.10" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.10" />
Expand All @@ -23,4 +28,15 @@
<ProjectReference Include="..\SettlementReports.Application\SettlementReports.Application.csproj" />
<ProjectReference Include="..\Common.Infrastructure\Common.Infrastructure.csproj" />
</ItemGroup>

<ItemGroup>
<Protobuf Include="Contracts/*.proto">
<GrpcServices>None</GrpcServices>
<Access>Public</Access>
<ProtoCompile>True</ProtoCompile>
<CompileOutputs>True</CompileOutputs>
<OutputDir>obj\contracts</OutputDir>
<Generator>MSBuild:Compile</Generator>
</Protobuf>
</ItemGroup>
</Project>

0 comments on commit 1ada669

Please sign in to comment.