diff --git a/BuildingBlocks/src/BuildingBlocks.Application/PushNotifications/IPushNotificationSender.cs b/BuildingBlocks/src/BuildingBlocks.Application/PushNotifications/IPushNotificationSender.cs new file mode 100644 index 0000000000..6c77d890ba --- /dev/null +++ b/BuildingBlocks/src/BuildingBlocks.Application/PushNotifications/IPushNotificationSender.cs @@ -0,0 +1,9 @@ +using Backbone.DevelopmentKit.Identity.ValueObjects; + +namespace Backbone.BuildingBlocks.Application.PushNotifications; + +public interface IPushNotificationSender +{ + Task SendNotification(IdentityAddress recipient, object notification, CancellationToken cancellationToken); +} + diff --git a/ConsumerApi/Program.cs b/ConsumerApi/Program.cs index 09ec9fed7b..d687f2b871 100644 --- a/ConsumerApi/Program.cs +++ b/ConsumerApi/Program.cs @@ -16,6 +16,7 @@ using Backbone.Modules.Challenges.Infrastructure.Persistence.Database; using Backbone.Modules.Devices.ConsumerApi; using Backbone.Modules.Devices.Infrastructure.Persistence.Database; +using Backbone.Modules.Devices.Infrastructure.PushNotifications; using Backbone.Modules.Files.ConsumerApi; using Backbone.Modules.Files.Infrastructure.Persistence.Database; using Backbone.Modules.Messages.ConsumerApi; @@ -29,7 +30,6 @@ using Backbone.Modules.Tokens.ConsumerApi; using Backbone.Modules.Tokens.Infrastructure.Persistence.Database; using Backbone.Tooling.Extensions; -using MediatR; using Microsoft.AspNetCore.Diagnostics.HealthChecks; using Microsoft.AspNetCore.HttpOverrides; using Microsoft.Extensions.Options; @@ -38,6 +38,7 @@ using Serilog.Exceptions.Core; using Serilog.Exceptions.EntityFrameworkCore.Destructurers; using Serilog.Settings.Configuration; +using DevicesConfiguration = Backbone.Modules.Devices.ConsumerApi.Configuration; Log.Logger = new LoggerConfiguration() .WriteTo.Console() @@ -168,6 +169,11 @@ static void ConfigureServices(IServiceCollection services, IConfiguration config }); services.AddEventBus(parsedConfiguration.Infrastructure.EventBus); + + var devicesConfiguration = new DevicesConfiguration(); + configuration.GetSection("Modules:Devices").Bind(devicesConfiguration); + + services.AddPushNotifications(devicesConfiguration.Infrastructure.PushNotifications); } static void Configure(WebApplication app) diff --git a/Modules/Devices/src/Devices.Application/Infrastructure/PushNotifications/IPushService.cs b/Modules/Devices/src/Devices.Application/Infrastructure/PushNotifications/IPushNotificationRegistrationService.cs similarity index 79% rename from Modules/Devices/src/Devices.Application/Infrastructure/PushNotifications/IPushService.cs rename to Modules/Devices/src/Devices.Application/Infrastructure/PushNotifications/IPushNotificationRegistrationService.cs index 4d5c5b7cb3..3557abb6fb 100644 --- a/Modules/Devices/src/Devices.Application/Infrastructure/PushNotifications/IPushService.cs +++ b/Modules/Devices/src/Devices.Application/Infrastructure/PushNotifications/IPushNotificationRegistrationService.cs @@ -4,9 +4,9 @@ namespace Backbone.Modules.Devices.Application.Infrastructure.PushNotifications; -public interface IPushService +public interface IPushNotificationRegistrationService { Task UpdateRegistration(IdentityAddress address, DeviceId deviceId, PnsHandle handle, string appId, Environment environment, CancellationToken cancellationToken); Task DeleteRegistration(DeviceId deviceId, CancellationToken cancellationToken); - Task SendNotification(IdentityAddress recipient, object notification, CancellationToken cancellationToken); } + diff --git a/Modules/Devices/src/Devices.Application/IntegrationEvents/Incoming/DatawalletModificationCreated/DatawalletModifiedIntegrationEventHandler.cs b/Modules/Devices/src/Devices.Application/IntegrationEvents/Incoming/DatawalletModificationCreated/DatawalletModifiedIntegrationEventHandler.cs index 1cd8b91256..1d6333cbcd 100644 --- a/Modules/Devices/src/Devices.Application/IntegrationEvents/Incoming/DatawalletModificationCreated/DatawalletModifiedIntegrationEventHandler.cs +++ b/Modules/Devices/src/Devices.Application/IntegrationEvents/Incoming/DatawalletModificationCreated/DatawalletModifiedIntegrationEventHandler.cs @@ -1,20 +1,20 @@ using Backbone.BuildingBlocks.Application.Abstractions.Infrastructure.EventBus; -using Backbone.Modules.Devices.Application.Infrastructure.PushNotifications; +using Backbone.BuildingBlocks.Application.PushNotifications; using Backbone.Modules.Devices.Application.Infrastructure.PushNotifications.Datawallet; namespace Backbone.Modules.Devices.Application.IntegrationEvents.Incoming.DatawalletModificationCreated; public class DatawalletModifiedIntegrationEventHandler : IIntegrationEventHandler { - private readonly IPushService _pushService; + private readonly IPushNotificationSender _pushSenderService; - public DatawalletModifiedIntegrationEventHandler(IPushService pushService) + public DatawalletModifiedIntegrationEventHandler(IPushNotificationSender pushSenderService) { - _pushService = pushService; + _pushSenderService = pushSenderService; } public async Task Handle(DatawalletModifiedIntegrationEvent integrationEvent) { - await _pushService.SendNotification(integrationEvent.Identity, new DatawalletModificationsCreatedPushNotification(integrationEvent.ModifiedByDevice), CancellationToken.None); + await _pushSenderService.SendNotification(integrationEvent.Identity, new DatawalletModificationsCreatedPushNotification(integrationEvent.ModifiedByDevice), CancellationToken.None); } } diff --git a/Modules/Devices/src/Devices.Application/IntegrationEvents/Incoming/ExternalEventCreated/ExternalEventCreatedIntegrationEventHandler.cs b/Modules/Devices/src/Devices.Application/IntegrationEvents/Incoming/ExternalEventCreated/ExternalEventCreatedIntegrationEventHandler.cs index f5c8b57f23..790ffbce47 100644 --- a/Modules/Devices/src/Devices.Application/IntegrationEvents/Incoming/ExternalEventCreated/ExternalEventCreatedIntegrationEventHandler.cs +++ b/Modules/Devices/src/Devices.Application/IntegrationEvents/Incoming/ExternalEventCreated/ExternalEventCreatedIntegrationEventHandler.cs @@ -1,20 +1,20 @@ using Backbone.BuildingBlocks.Application.Abstractions.Infrastructure.EventBus; -using Backbone.Modules.Devices.Application.Infrastructure.PushNotifications; +using Backbone.BuildingBlocks.Application.PushNotifications; using Backbone.Modules.Devices.Application.Infrastructure.PushNotifications.ExternalEvents; namespace Backbone.Modules.Devices.Application.IntegrationEvents.Incoming.ExternalEventCreated; public class ExternalEventCreatedIntegrationEventHandler : IIntegrationEventHandler { - private readonly IPushService _pushService; + private readonly IPushNotificationSender _pushSenderService; - public ExternalEventCreatedIntegrationEventHandler(IPushService pushService) + public ExternalEventCreatedIntegrationEventHandler(IPushNotificationSender pushSenderService) { - _pushService = pushService; + _pushSenderService = pushSenderService; } public async Task Handle(ExternalEventCreatedIntegrationEvent @event) { - await _pushService.SendNotification(@event.Owner, new ExternalEventCreatedPushNotification(), CancellationToken.None); + await _pushSenderService.SendNotification(@event.Owner, new ExternalEventCreatedPushNotification(), CancellationToken.None); } } diff --git a/Modules/Devices/src/Devices.Application/PushNotifications/Commands/DeleteDeviceRegistration/Handler.cs b/Modules/Devices/src/Devices.Application/PushNotifications/Commands/DeleteDeviceRegistration/Handler.cs index 35ebe8e0e3..d82fd9e141 100644 --- a/Modules/Devices/src/Devices.Application/PushNotifications/Commands/DeleteDeviceRegistration/Handler.cs +++ b/Modules/Devices/src/Devices.Application/PushNotifications/Commands/DeleteDeviceRegistration/Handler.cs @@ -6,18 +6,18 @@ namespace Backbone.Modules.Devices.Application.PushNotifications.Commands.DeleteDeviceRegistration; internal class Handler : IRequestHandler { - private readonly IPushService _pushService; + private readonly IPushNotificationRegistrationService _pushRegistrationService; private readonly DeviceId _activeDevice; - public Handler(IPushService pushService, IUserContext userContext) + public Handler(IPushNotificationRegistrationService pushRegistrationService, IUserContext userContext) { - _pushService = pushService; + _pushRegistrationService = pushRegistrationService; _activeDevice = userContext.GetDeviceId(); } public async Task Handle(DeleteDeviceRegistrationCommand request, CancellationToken cancellationToken) { - await _pushService.DeleteRegistration(_activeDevice, cancellationToken); + await _pushRegistrationService.DeleteRegistration(_activeDevice, cancellationToken); } } diff --git a/Modules/Devices/src/Devices.Application/PushNotifications/Commands/SendTestNotification/Handler.cs b/Modules/Devices/src/Devices.Application/PushNotifications/Commands/SendTestNotification/Handler.cs index 3f009b8edd..901a7adb9b 100644 --- a/Modules/Devices/src/Devices.Application/PushNotifications/Commands/SendTestNotification/Handler.cs +++ b/Modules/Devices/src/Devices.Application/PushNotifications/Commands/SendTestNotification/Handler.cs @@ -1,6 +1,6 @@ using Backbone.BuildingBlocks.Application.Abstractions.Infrastructure.UserContext; +using Backbone.BuildingBlocks.Application.PushNotifications; using Backbone.DevelopmentKit.Identity.ValueObjects; -using Backbone.Modules.Devices.Application.Infrastructure.PushNotifications; using MediatR; namespace Backbone.Modules.Devices.Application.PushNotifications.Commands.SendTestNotification; @@ -8,17 +8,17 @@ namespace Backbone.Modules.Devices.Application.PushNotifications.Commands.SendTe public class Handler : IRequestHandler { private readonly IdentityAddress _activeIdentity; - private readonly IPushService _pushService; + private readonly IPushNotificationSender _pushSenderService; - public Handler(IUserContext userContext, IPushService pushService) + public Handler(IUserContext userContext, IPushNotificationSender pushSenderService) { - _pushService = pushService; + _pushSenderService = pushSenderService; _activeIdentity = userContext.GetAddress(); } public async Task Handle(SendTestNotificationCommand request, CancellationToken cancellationToken) { - await _pushService.SendNotification(_activeIdentity, request.Data, cancellationToken); + await _pushSenderService.SendNotification(_activeIdentity, request.Data, cancellationToken); return Unit.Value; } } diff --git a/Modules/Devices/src/Devices.Application/PushNotifications/Commands/UpdateDeviceRegistration/Handler.cs b/Modules/Devices/src/Devices.Application/PushNotifications/Commands/UpdateDeviceRegistration/Handler.cs index 72381b40bf..3687c6c265 100644 --- a/Modules/Devices/src/Devices.Application/PushNotifications/Commands/UpdateDeviceRegistration/Handler.cs +++ b/Modules/Devices/src/Devices.Application/PushNotifications/Commands/UpdateDeviceRegistration/Handler.cs @@ -13,14 +13,14 @@ namespace Backbone.Modules.Devices.Application.PushNotifications.Commands.Update public class Handler : IRequestHandler { private readonly IdentityAddress _activeIdentity; - private readonly IPushService _pushService; + private readonly IPushNotificationRegistrationService _pushRegistrationService; private readonly DeviceId _activeDevice; private const string PRODUCTION_ENVIRONMENT = "Production"; private const string DEVELOPMENT_ENVIRONMENT = "Development"; - public Handler(IPushService pushService, IUserContext userContext) + public Handler(IPushNotificationRegistrationService pushRegistrationService, IUserContext userContext) { - _pushService = pushService; + _pushRegistrationService = pushRegistrationService; _activeIdentity = userContext.GetAddress(); _activeDevice = userContext.GetDeviceId(); } @@ -30,7 +30,7 @@ public async Task Handle(UpdateDeviceRegistrationCommand request, Cancella var parseHandleResult = PnsHandle.Parse(request.Handle, DeserializePlatform(request.Platform)); if (parseHandleResult.IsSuccess) { - await _pushService.UpdateRegistration(_activeIdentity, _activeDevice, parseHandleResult.Value, request.AppId, DeserializeEnvironment(request.Environment ?? PRODUCTION_ENVIRONMENT), cancellationToken); + await _pushRegistrationService.UpdateRegistration(_activeIdentity, _activeDevice, parseHandleResult.Value, request.AppId, DeserializeEnvironment(request.Environment ?? PRODUCTION_ENVIRONMENT), cancellationToken); } else { diff --git a/Modules/Devices/src/Devices.ConsumerApi/DevicesModule.cs b/Modules/Devices/src/Devices.ConsumerApi/DevicesModule.cs index 3c598d72b7..4a34fbeabd 100644 --- a/Modules/Devices/src/Devices.ConsumerApi/DevicesModule.cs +++ b/Modules/Devices/src/Devices.ConsumerApi/DevicesModule.cs @@ -35,8 +35,6 @@ public override void ConfigureServices(IServiceCollection services, IConfigurati options.Provider = parsedConfiguration.Infrastructure.SqlDatabase.Provider; }); - services.AddPushNotifications(parsedConfiguration.Infrastructure.PushNotifications); - services.AddSingleton(_ => SignatureHelper.CreateEd25519WithRawKeyFormat()); services.AddSqlDatabaseHealthCheck(Name, parsedConfiguration.Infrastructure.SqlDatabase.Provider, parsedConfiguration.Infrastructure.SqlDatabase.ConnectionString); diff --git a/Modules/Devices/src/Devices.Infrastructure/PushNotifications/DirectPush/DirectPushService.cs b/Modules/Devices/src/Devices.Infrastructure/PushNotifications/DirectPush/DirectPushService.cs index e5cd18a4b5..d4975ad588 100644 --- a/Modules/Devices/src/Devices.Infrastructure/PushNotifications/DirectPush/DirectPushService.cs +++ b/Modules/Devices/src/Devices.Infrastructure/PushNotifications/DirectPush/DirectPushService.cs @@ -1,4 +1,5 @@ -using Backbone.BuildingBlocks.Infrastructure.Exceptions; +using Backbone.BuildingBlocks.Application.PushNotifications; +using Backbone.BuildingBlocks.Infrastructure.Exceptions; using Backbone.DevelopmentKit.Identity.ValueObjects; using Backbone.Modules.Devices.Application.Infrastructure.Persistence.Repository; using Backbone.Modules.Devices.Application.Infrastructure.PushNotifications; @@ -10,7 +11,7 @@ namespace Backbone.Modules.Devices.Infrastructure.PushNotifications.DirectPush; -public class DirectPushService : IPushService +public class DirectPushService : IPushNotificationRegistrationService, IPushNotificationSender { private readonly IPnsRegistrationRepository _pnsRegistrationRepository; private readonly ILogger _logger; diff --git a/Modules/Devices/src/Devices.Infrastructure/PushNotifications/DirectPush/IServiceCollectionExtensions.cs b/Modules/Devices/src/Devices.Infrastructure/PushNotifications/DirectPush/IServiceCollectionExtensions.cs index 007d0e1034..5db27c1edf 100644 --- a/Modules/Devices/src/Devices.Infrastructure/PushNotifications/DirectPush/IServiceCollectionExtensions.cs +++ b/Modules/Devices/src/Devices.Infrastructure/PushNotifications/DirectPush/IServiceCollectionExtensions.cs @@ -1,4 +1,5 @@ -using Backbone.Modules.Devices.Application.Infrastructure.PushNotifications; +using Backbone.BuildingBlocks.Application.PushNotifications; +using Backbone.Modules.Devices.Application.Infrastructure.PushNotifications; using Backbone.Modules.Devices.Infrastructure.PushNotifications.DirectPush.ApplePushNotificationService; using Backbone.Modules.Devices.Infrastructure.PushNotifications.DirectPush.FirebaseCloudMessaging; using Microsoft.Extensions.DependencyInjection; @@ -28,7 +29,8 @@ private static void AddApns(this IServiceCollection services) { services.AddHttpClient(); services.AddTransient(); - services.AddTransient(); + services.AddTransient(); + services.AddTransient(); services.AddTransient(); services.AddSingleton(); } diff --git a/Modules/Devices/src/Devices.Infrastructure/PushNotifications/Dummy/DummyPushService.cs b/Modules/Devices/src/Devices.Infrastructure/PushNotifications/Dummy/DummyPushService.cs index f066a3c1f2..df6d4a9adb 100644 --- a/Modules/Devices/src/Devices.Infrastructure/PushNotifications/Dummy/DummyPushService.cs +++ b/Modules/Devices/src/Devices.Infrastructure/PushNotifications/Dummy/DummyPushService.cs @@ -1,4 +1,5 @@ -using Backbone.DevelopmentKit.Identity.ValueObjects; +using Backbone.BuildingBlocks.Application.PushNotifications; +using Backbone.DevelopmentKit.Identity.ValueObjects; using Backbone.Modules.Devices.Application.Infrastructure.PushNotifications; using Backbone.Modules.Devices.Domain.Aggregates.PushNotifications.Handles; using Microsoft.Extensions.Logging; @@ -6,7 +7,7 @@ namespace Backbone.Modules.Devices.Infrastructure.PushNotifications.Dummy; -public class DummyPushService : IPushService +public class DummyPushService : IPushNotificationRegistrationService, IPushNotificationSender { private readonly ILogger _logger; diff --git a/Modules/Devices/src/Devices.Infrastructure/PushNotifications/Dummy/IServiceCollectionExtensions.cs b/Modules/Devices/src/Devices.Infrastructure/PushNotifications/Dummy/IServiceCollectionExtensions.cs index 9d5eaab899..2893a61460 100644 --- a/Modules/Devices/src/Devices.Infrastructure/PushNotifications/Dummy/IServiceCollectionExtensions.cs +++ b/Modules/Devices/src/Devices.Infrastructure/PushNotifications/Dummy/IServiceCollectionExtensions.cs @@ -1,4 +1,5 @@ -using Backbone.Modules.Devices.Application.Infrastructure.PushNotifications; +using Backbone.BuildingBlocks.Application.PushNotifications; +using Backbone.Modules.Devices.Application.Infrastructure.PushNotifications; using Microsoft.Extensions.DependencyInjection; namespace Backbone.Modules.Devices.Infrastructure.PushNotifications.Dummy; @@ -7,6 +8,7 @@ public static class IServiceCollectionExtensions { public static void AddDummyPushNotifications(this IServiceCollection services) { - services.AddTransient(); + services.AddTransient(); + services.AddTransient(); } }