diff --git a/docker-compose.override.yml b/docker-compose.override.yml index 6a9a6448e..8f408c722 100644 --- a/docker-compose.override.yml +++ b/docker-compose.override.yml @@ -63,6 +63,11 @@ services: - "5115:80" tenantacustomisation: + environment: + - EventBusConnection=${ESHOP_AZURE_SERVICE_BUS:-rabbitmq} + - EventBusUserName=${ESHOP_SERVICE_BUS_USERNAME} + - EventBusPassword=${ESHOP_SERVICE_BUS_PASSWORD} + - AzureServiceBusEnabled=False ports: - "5116:80" diff --git a/src/BuildingBlocks/EventBus/EventBus/Abstractions/AbstractIntegrationEventHandler.cs b/src/BuildingBlocks/EventBus/EventBus/Abstractions/AbstractIntegrationEventHandler.cs index c67e082cf..947b1c406 100644 --- a/src/BuildingBlocks/EventBus/EventBus/Abstractions/AbstractIntegrationEventHandler.cs +++ b/src/BuildingBlocks/EventBus/EventBus/Abstractions/AbstractIntegrationEventHandler.cs @@ -13,9 +13,19 @@ namespace Ordering.API.Application.IntegrationEvents.EventHandling { private static String url = @"http://tenantmanager/"; private readonly IEventBus _eventBus; + //private readonly ILogger> _logger; + + protected AbstractIntegrationEventHandler(IEventBus eventBus) + { + _eventBus = eventBus ?? throw new ArgumentNullException(nameof(eventBus)); + } public async Task CheckIfCustomised(IntegrationEvent @event) { + if (!@event.CheckForCustomisation) + { + return false; + } Boolean result = Get(@event); if (result) { @@ -23,7 +33,6 @@ namespace Ordering.API.Application.IntegrationEvents.EventHandling try { //_logger.LogInformation("----- Publishing integration event: {IntegrationEventId} from {AppName} - ({@IntegrationEvent})", eventMessage.Id, Program.AppName, eventMessage); - _eventBus.Publish(customisationEvent); } catch (Exception ex) @@ -58,7 +67,7 @@ namespace Ordering.API.Application.IntegrationEvents.EventHandling } } - return false; + return true; } } diff --git a/src/BuildingBlocks/EventBus/EventBus/Events/IntegrationEvent.cs b/src/BuildingBlocks/EventBus/EventBus/Events/IntegrationEvent.cs index ef09911fe..6536ba721 100644 --- a/src/BuildingBlocks/EventBus/EventBus/Events/IntegrationEvent.cs +++ b/src/BuildingBlocks/EventBus/EventBus/Events/IntegrationEvent.cs @@ -9,6 +9,14 @@ namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events { Id = Guid.NewGuid(); CreationDate = DateTime.UtcNow; + CheckForCustomisation = true; + } + + public IntegrationEvent(Boolean checkForCustomisation) + { + Id = Guid.NewGuid(); + CreationDate = DateTime.UtcNow; + CheckForCustomisation = checkForCustomisation; } [JsonConstructor] @@ -23,5 +31,8 @@ namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events [JsonProperty] public DateTime CreationDate { get; private set; } + + [JsonProperty] + public Boolean CheckForCustomisation { get; set; } } } diff --git a/src/BuildingBlocks/EventBus/EventBusRabbitMQ/EventBusRabbitMQ.cs b/src/BuildingBlocks/EventBus/EventBusRabbitMQ/EventBusRabbitMQ.cs index 5623549d5..05af15cf6 100644 --- a/src/BuildingBlocks/EventBus/EventBusRabbitMQ/EventBusRabbitMQ.cs +++ b/src/BuildingBlocks/EventBus/EventBusRabbitMQ/EventBusRabbitMQ.cs @@ -82,12 +82,12 @@ namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBusRabbitMQ var eventName = @event.GetType().Name; - _logger.LogTrace("Creating RabbitMQ channel to publish event: {EventId} ({EventName})", @event.Id, eventName); + _logger.LogWarning("Creating RabbitMQ channel to publish event: {EventId} ({EventName})", @event.Id, eventName); using (var channel = _persistentConnection.CreateModel()) { - _logger.LogTrace("Declaring RabbitMQ exchange to publish event: {EventId}", @event.Id); + _logger.LogWarning("Declaring RabbitMQ exchange to publish event: {EventId}", @event.Id); channel.ExchangeDeclare(exchange: BROKER_NAME, type: "direct"); @@ -99,7 +99,7 @@ namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBusRabbitMQ var properties = channel.CreateBasicProperties(); properties.DeliveryMode = 2; // persistent - _logger.LogTrace("Publishing event to RabbitMQ: {EventId}", @event.Id); + _logger.LogWarning("Publishing event to RabbitMQ: {EventId}", @event.Id); channel.BasicPublish( exchange: BROKER_NAME, @@ -260,7 +260,7 @@ namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBusRabbitMQ private async Task ProcessEvent(string eventName, string message) { - _logger.LogTrace("Processing RabbitMQ event: {EventName}", eventName); + _logger.LogWarning("Processing RabbitMQ event: {EventName}", eventName); if (_subsManager.HasSubscriptionsForEvent(eventName)) { diff --git a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/UserCheckoutAcceptedIntegrationEventHandler.cs b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/UserCheckoutAcceptedIntegrationEventHandler.cs index 5782930f3..7c2615c25 100644 --- a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/UserCheckoutAcceptedIntegrationEventHandler.cs +++ b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/UserCheckoutAcceptedIntegrationEventHandler.cs @@ -15,14 +15,16 @@ namespace Ordering.API.Application.IntegrationEvents.EventHandling public class UserCheckoutAcceptedIntegrationEventHandler : AbstractIntegrationEventHandler, IIntegrationEventHandler { private readonly IMediator _mediator; + private readonly IEventBus _eventBus; private readonly ILogger _logger; public UserCheckoutAcceptedIntegrationEventHandler( IMediator mediator, - ILogger logger) + ILogger logger, IEventBus eventBus) : base(eventBus) { _mediator = mediator ?? throw new ArgumentNullException(nameof(mediator)); _logger = logger ?? throw new ArgumentNullException(nameof(logger)); + _eventBus = eventBus ?? throw new ArgumentNullException(nameof(eventBus)); } /// diff --git a/src/Services/TenantCustomisations/TenantACustomisations/IntegrationEvents/EventHandling/CustomisationEventHandler.cs b/src/Services/TenantCustomisations/TenantACustomisations/IntegrationEvents/EventHandling/CustomisationEventHandler.cs new file mode 100644 index 000000000..990afa51c --- /dev/null +++ b/src/Services/TenantCustomisations/TenantACustomisations/IntegrationEvents/EventHandling/CustomisationEventHandler.cs @@ -0,0 +1,46 @@ +using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions; +using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events; +using Microsoft.Extensions.Logging; +using Ordering.API.Application.IntegrationEvents.Events; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using TenantACustomisations.Services; + +namespace TenantACustomisations.IntegrationEvents.EventHandling +{ + public class CustomisationEventHandler : IIntegrationEventHandler + { + + private readonly ILogger _logger; + private readonly IEventBus _eventBus; + private readonly IValidationService validationService; + + public CustomisationEventHandler(ILogger logger, IEventBus eventBus) + { + _logger = logger ?? throw new ArgumentNullException(nameof(logger)); + _eventBus = eventBus ?? throw new ArgumentNullException(nameof(eventBus)); + validationService = new ValidationService(); + } + + public async Task Handle(CustomisationEvent @event) + { + _logger.LogInformation("----- Handling integration event: {IntegrationEventId} - ({@IntegrationEvent})", @event.Id, @event); + IntegrationEvent integrationEvent = @event.@event; + + switch (integrationEvent.GetType().Name) + { + case "UserCheckoutAcceptedIntegrationEvent": + if (validationService.Validate((UserCheckoutAcceptedIntegrationEvent)integrationEvent)) + { + integrationEvent.CheckForCustomisation = false; + _eventBus.Publish(integrationEvent); + } + break; + default: + break; + } + } + } +} diff --git a/src/Services/TenantCustomisations/TenantACustomisations/Services/IValidationService.cs b/src/Services/TenantCustomisations/TenantACustomisations/Services/IValidationService.cs new file mode 100644 index 000000000..cdf5d8172 --- /dev/null +++ b/src/Services/TenantCustomisations/TenantACustomisations/Services/IValidationService.cs @@ -0,0 +1,14 @@ +using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events; +using Ordering.API.Application.IntegrationEvents.Events; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace TenantACustomisations.Services +{ + interface IValidationService + { + Boolean Validate(UserCheckoutAcceptedIntegrationEvent @event); + } +} diff --git a/src/Services/TenantCustomisations/TenantACustomisations/Services/ValidationService.cs b/src/Services/TenantCustomisations/TenantACustomisations/Services/ValidationService.cs new file mode 100644 index 000000000..618e9c257 --- /dev/null +++ b/src/Services/TenantCustomisations/TenantACustomisations/Services/ValidationService.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events; +using Ordering.API.Application.IntegrationEvents.Events; + +namespace TenantACustomisations.Services +{ + public class ValidationService : IValidationService + { + public bool Validate(UserCheckoutAcceptedIntegrationEvent @event) + { + return @event.State == "Oslo"; + } + } +} diff --git a/src/Services/TenantCustomisations/TenantACustomisations/Startup.cs b/src/Services/TenantCustomisations/TenantACustomisations/Startup.cs index afdb96f30..8aad4f6ee 100644 --- a/src/Services/TenantCustomisations/TenantACustomisations/Startup.cs +++ b/src/Services/TenantCustomisations/TenantACustomisations/Startup.cs @@ -33,6 +33,8 @@ using Infrastructure.AutofacModules; using Microsoft.eShopOnContainers.Services.TenantACustomisations.Infrastructure.Filters; using global::TenantACustomisations.Infrastructure.Filters; + using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events; + using global::TenantACustomisations.IntegrationEvents.EventHandling; public class Startup { @@ -112,7 +114,7 @@ { var eventBus = app.ApplicationServices.GetRequiredService(); - //eventBus.Subscribe>(); + eventBus.Subscribe(); } protected virtual void ConfigureAuth(IApplicationBuilder app) @@ -385,6 +387,7 @@ } services.AddSingleton(); + services.AddTransient(); return services; } diff --git a/src/Services/TenantCustomisations/TenantACustomisations/TenantACustomisations.csproj b/src/Services/TenantCustomisations/TenantACustomisations/TenantACustomisations.csproj index 272a27355..44c3fe7ff 100644 --- a/src/Services/TenantCustomisations/TenantACustomisations/TenantACustomisations.csproj +++ b/src/Services/TenantCustomisations/TenantACustomisations/TenantACustomisations.csproj @@ -44,7 +44,6 @@ - @@ -52,6 +51,7 @@ + diff --git a/src/Services/TenantCustomisations/TenantACustomisations/appsettings.json b/src/Services/TenantCustomisations/TenantACustomisations/appsettings.json index def9159a7..76cd21c29 100644 --- a/src/Services/TenantCustomisations/TenantACustomisations/appsettings.json +++ b/src/Services/TenantCustomisations/TenantACustomisations/appsettings.json @@ -1,8 +1,28 @@ { - "Logging": { - "LogLevel": { - "Default": "Warning" + "Serilog": { + "SeqServerUrl": null, + "LogstashgUrl": null, + "MinimumLevel": { + "Default": "Information", + "Override": { + "Microsoft": "Warning", + "Microsoft.eShopOnContainers": "Information", + "System": "Warning" + } } }, - "AllowedHosts": "*" -} + "IdentityUrl": "http://localhost:5105", + "ConnectionString": "127.0.0.1", + "AzureServiceBusEnabled": false, + "SubscriptionClientName": "TenantACustomisation", + "ApplicationInsights": { + "InstrumentationKey": "" + }, + "EventBusRetryCount": 5, + "UseVault": false, + "Vault": { + "Name": "eshop", + "ClientId": "your-clien-id", + "ClientSecret": "your-client-secret" + } +} \ No newline at end of file