From 8c2af8c048f2f3ac35b07a39e9f8ce3354f92ac1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ram=C3=B3n=20Tom=C3=A1s?= Date: Wed, 24 May 2017 16:09:03 +0200 Subject: [PATCH] Clean Basket event is launched once basket is converted to order and before starting creating order. Change grace period time to 1 minute Remove unecessary clean basket methods on client --- eShopOnContainers-ServicesAndWebApps.sln | 6 ++--- ...PaymentMethodVerifiedDomainEventHandler.cs | 15 +++-------- ...CheckoutAcceptedIntegrationEventHandler.cs | 11 ++++++-- ...nager.csproj => GracePeriodManager.csproj} | 0 .../GracePeriodConfirmedIntegrationEvent.cs | 2 +- ...aManagerSettings.cs => ManagerSettings.cs} | 4 +-- .../SagaManager/SagaManager/Program.cs | 16 ++++++------ .../SagaManager/Services/IManagerService.cs | 7 +++++ .../Services/ISagaManagerService.cs | 7 ----- ...agaManagerService.cs => ManagerService.cs} | 26 +++++++++---------- .../SagaManager/SagaManager/appsettings.json | 2 +- src/Web/WebMVC/Services/BasketService.cs | 13 +--------- src/Web/WebMVC/Services/IBasketService.cs | 1 - .../Client/modules/basket/basket.service.ts | 12 +-------- 14 files changed, 50 insertions(+), 72 deletions(-) rename src/Services/SagaManager/SagaManager/{SagaManager.csproj => GracePeriodManager.csproj} (100%) rename src/Services/SagaManager/SagaManager/{SagaManagerSettings.cs => ManagerSettings.cs} (77%) create mode 100644 src/Services/SagaManager/SagaManager/Services/IManagerService.cs delete mode 100644 src/Services/SagaManager/SagaManager/Services/ISagaManagerService.cs rename src/Services/SagaManager/SagaManager/Services/{SagaManagerService.cs => ManagerService.cs} (74%) diff --git a/eShopOnContainers-ServicesAndWebApps.sln b/eShopOnContainers-ServicesAndWebApps.sln index 043d577b3..f502cf2a0 100644 --- a/eShopOnContainers-ServicesAndWebApps.sln +++ b/eShopOnContainers-ServicesAndWebApps.sln @@ -1,7 +1,7 @@  Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio 15 -VisualStudioVersion = 15.0.26403.3 +VisualStudioVersion = 15.0.26430.6 MinimumVisualStudioVersion = 10.0.40219.1 Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{932D8224-11F6-4D07-B109-DA28AD288A63}" EndProject @@ -80,9 +80,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Extensions.Health EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EventBus.Tests", "src\BuildingBlocks\EventBus\EventBus.Tests\EventBus.Tests.csproj", "{4A980AC4-7205-46BF-8CCB-09E44D700FD4}" EndProject -Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "SagaManager", "SagaManager", "{F38B4FF0-0B49-405A-B1B4-F7A5E3BC4C4E}" +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "GracePeriod", "GracePeriod", "{F38B4FF0-0B49-405A-B1B4-F7A5E3BC4C4E}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SagaManager", "src\Services\SagaManager\SagaManager\SagaManager.csproj", "{F6E0F0DD-1400-43C3-B5E0-7CC325728C47}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GracePeriodManager", "src\Services\SagaManager\SagaManager\GracePeriodManager.csproj", "{F6E0F0DD-1400-43C3-B5E0-7CC325728C47}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution diff --git a/src/Services/Ordering/Ordering.API/Application/DomainEventHandlers/BuyerAndPaymentMethodVerified/UpdateOrderWhenBuyerAndPaymentMethodVerifiedDomainEventHandler.cs b/src/Services/Ordering/Ordering.API/Application/DomainEventHandlers/BuyerAndPaymentMethodVerified/UpdateOrderWhenBuyerAndPaymentMethodVerifiedDomainEventHandler.cs index 08897fe46..35e14dd68 100644 --- a/src/Services/Ordering/Ordering.API/Application/DomainEventHandlers/BuyerAndPaymentMethodVerified/UpdateOrderWhenBuyerAndPaymentMethodVerifiedDomainEventHandler.cs +++ b/src/Services/Ordering/Ordering.API/Application/DomainEventHandlers/BuyerAndPaymentMethodVerified/UpdateOrderWhenBuyerAndPaymentMethodVerifiedDomainEventHandler.cs @@ -12,16 +12,13 @@ namespace Ordering.API.Application.DomainEventHandlers.BuyerAndPaymentMethodVeri public class UpdateOrderWhenBuyerAndPaymentMethodVerifiedDomainEventHandler : IAsyncNotificationHandler { - private readonly IOrderRepository _orderRepository; - private readonly IOrderingIntegrationEventService _orderingIntegrationEventService; + private readonly IOrderRepository _orderRepository; private readonly ILoggerFactory _logger; public UpdateOrderWhenBuyerAndPaymentMethodVerifiedDomainEventHandler( - IOrderRepository orderRepository, ILoggerFactory logger, - IOrderingIntegrationEventService orderingIntegrationEventService) + IOrderRepository orderRepository, ILoggerFactory logger) { - _orderRepository = orderRepository ?? throw new ArgumentNullException(nameof(orderRepository)); - _orderingIntegrationEventService = orderingIntegrationEventService ?? throw new ArgumentNullException(nameof(orderingIntegrationEventService)); + _orderRepository = orderRepository ?? throw new ArgumentNullException(nameof(orderRepository)); _logger = logger ?? throw new ArgumentNullException(nameof(logger)); } @@ -32,11 +29,7 @@ namespace Ordering.API.Application.DomainEventHandlers.BuyerAndPaymentMethodVeri { var orderToUpdate = await _orderRepository.GetAsync(buyerPaymentMethodVerifiedEvent.OrderId); orderToUpdate.SetBuyerId(buyerPaymentMethodVerifiedEvent.Buyer.Id); - orderToUpdate.SetPaymentId(buyerPaymentMethodVerifiedEvent.Payment.Id); - - var orderStartedIntegrationEvent = new OrderStartedIntegrationEvent(buyerPaymentMethodVerifiedEvent.Buyer.IdentityGuid); - - await _orderingIntegrationEventService.PublishThroughEventBusAsync(orderStartedIntegrationEvent); + orderToUpdate.SetPaymentId(buyerPaymentMethodVerifiedEvent.Payment.Id); _logger.CreateLogger(nameof(UpdateOrderWhenBuyerAndPaymentMethodVerifiedDomainEventHandler)) .LogTrace($"Order with Id: {buyerPaymentMethodVerifiedEvent.OrderId} has been successfully updated with a payment method id: { buyerPaymentMethodVerifiedEvent.Payment.Id }"); 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 720d7a8d4..8e4823646 100644 --- a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/UserCheckoutAcceptedIntegrationEventHandler.cs +++ b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/UserCheckoutAcceptedIntegrationEventHandler.cs @@ -12,12 +12,14 @@ namespace Ordering.API.Application.IntegrationEvents.EventHandling { private readonly IMediator _mediator; private readonly ILoggerFactory _logger; + private readonly IOrderingIntegrationEventService _orderingIntegrationEventService; public UserCheckoutAcceptedIntegrationEventHandler(IMediator mediator, - ILoggerFactory logger) + ILoggerFactory logger, IOrderingIntegrationEventService orderingIntegrationEventService) { _mediator = mediator ?? throw new ArgumentNullException(nameof(mediator)); _logger = logger ?? throw new ArgumentNullException(nameof(logger)); + _orderingIntegrationEventService = orderingIntegrationEventService ?? throw new ArgumentNullException(nameof(orderingIntegrationEventService)); } /// @@ -32,6 +34,11 @@ namespace Ordering.API.Application.IntegrationEvents.EventHandling public async Task Handle(UserCheckoutAcceptedIntegrationEvent eventMsg) { var result = false; + + // Send Integration event to clean basket once basket is converted to Order and before starting with the order creation process + var orderStartedIntegrationEvent = new OrderStartedIntegrationEvent(eventMsg.UserId); + await _orderingIntegrationEventService.PublishThroughEventBusAsync(orderStartedIntegrationEvent); + if (eventMsg.RequestId != Guid.Empty) { var createOrderCommand = new CreateOrderCommand(eventMsg.Basket.Items, eventMsg.UserId, eventMsg.City, eventMsg.Street, @@ -41,7 +48,7 @@ namespace Ordering.API.Application.IntegrationEvents.EventHandling var requestCreateOrder = new IdentifiedCommand(createOrderCommand, eventMsg.RequestId); result = await _mediator.SendAsync(requestCreateOrder); - } + } _logger.CreateLogger(nameof(UserCheckoutAcceptedIntegrationEventHandler)) .LogTrace(result ? $"UserCheckoutAccepted integration event has been received and a create new order process is started with requestId: {eventMsg.RequestId}" : diff --git a/src/Services/SagaManager/SagaManager/SagaManager.csproj b/src/Services/SagaManager/SagaManager/GracePeriodManager.csproj similarity index 100% rename from src/Services/SagaManager/SagaManager/SagaManager.csproj rename to src/Services/SagaManager/SagaManager/GracePeriodManager.csproj diff --git a/src/Services/SagaManager/SagaManager/IntegrationEvents/Events/GracePeriodConfirmedIntegrationEvent.cs b/src/Services/SagaManager/SagaManager/IntegrationEvents/Events/GracePeriodConfirmedIntegrationEvent.cs index 0eb422d6f..fce04cd24 100644 --- a/src/Services/SagaManager/SagaManager/IntegrationEvents/Events/GracePeriodConfirmedIntegrationEvent.cs +++ b/src/Services/SagaManager/SagaManager/IntegrationEvents/Events/GracePeriodConfirmedIntegrationEvent.cs @@ -1,4 +1,4 @@ -namespace SagaManager.IntegrationEvents.Events +namespace GracePeriodManager.IntegrationEvents.Events { using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events; diff --git a/src/Services/SagaManager/SagaManager/SagaManagerSettings.cs b/src/Services/SagaManager/SagaManager/ManagerSettings.cs similarity index 77% rename from src/Services/SagaManager/SagaManager/SagaManagerSettings.cs rename to src/Services/SagaManager/SagaManager/ManagerSettings.cs index 3b6d3bd08..44e0b56f7 100644 --- a/src/Services/SagaManager/SagaManager/SagaManagerSettings.cs +++ b/src/Services/SagaManager/SagaManager/ManagerSettings.cs @@ -1,6 +1,6 @@ -namespace SagaManager +namespace GracePeriodManager { - public class SagaManagerSettings + public class ManagerSettings { public string ConnectionString { get; set; } diff --git a/src/Services/SagaManager/SagaManager/Program.cs b/src/Services/SagaManager/SagaManager/Program.cs index 29c382f50..eecdd80ea 100644 --- a/src/Services/SagaManager/SagaManager/Program.cs +++ b/src/Services/SagaManager/SagaManager/Program.cs @@ -1,4 +1,4 @@ -namespace SagaManager +namespace GracePeriodManager { using System.IO; using System; @@ -31,14 +31,14 @@ var logger = serviceProvider.GetService(); Configure(logger); - var sagaManagerService = serviceProvider - .GetRequiredService(); + var gracePeriodManagerService = serviceProvider + .GetRequiredService(); var checkUpdateTime = serviceProvider - .GetRequiredService>().Value.CheckUpdateTime; + .GetRequiredService>().Value.CheckUpdateTime; while (true) { - sagaManagerService.CheckConfirmedGracePeriodOrders(); + gracePeriodManagerService.CheckConfirmedGracePeriodOrders(); await Task.Delay(checkUpdateTime); } } @@ -57,11 +57,11 @@ { services.AddLogging() .AddOptions() - .Configure(Configuration) - .AddSingleton() + .Configure(Configuration) + .AddSingleton() .AddSingleton(sp => { - var settings = sp.GetRequiredService>().Value; + var settings = sp.GetRequiredService>().Value; var logger = sp.GetRequiredService>(); var factory = new ConnectionFactory() { diff --git a/src/Services/SagaManager/SagaManager/Services/IManagerService.cs b/src/Services/SagaManager/SagaManager/Services/IManagerService.cs new file mode 100644 index 000000000..2362ccd00 --- /dev/null +++ b/src/Services/SagaManager/SagaManager/Services/IManagerService.cs @@ -0,0 +1,7 @@ +namespace GracePeriodManager.Services +{ + public interface IManagerService + { + void CheckConfirmedGracePeriodOrders(); + } +} \ No newline at end of file diff --git a/src/Services/SagaManager/SagaManager/Services/ISagaManagerService.cs b/src/Services/SagaManager/SagaManager/Services/ISagaManagerService.cs deleted file mode 100644 index 6ee012c37..000000000 --- a/src/Services/SagaManager/SagaManager/Services/ISagaManagerService.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace SagaManager.Services -{ - public interface ISagaManagerService - { - void CheckConfirmedGracePeriodOrders(); - } -} \ No newline at end of file diff --git a/src/Services/SagaManager/SagaManager/Services/SagaManagerService.cs b/src/Services/SagaManager/SagaManager/Services/ManagerService.cs similarity index 74% rename from src/Services/SagaManager/SagaManager/Services/SagaManagerService.cs rename to src/Services/SagaManager/SagaManager/Services/ManagerService.cs index fd5ab1438..24207ea2b 100644 --- a/src/Services/SagaManager/SagaManager/Services/SagaManagerService.cs +++ b/src/Services/SagaManager/SagaManager/Services/ManagerService.cs @@ -1,22 +1,22 @@ -namespace SagaManager.Services +namespace GracePeriodManager.Services { - using System.Collections.Generic; - using System.Data.SqlClient; - using Microsoft.Extensions.Options; - using Microsoft.Extensions.Logging; using Dapper; - using IntegrationEvents.Events; + using GracePeriodManager.IntegrationEvents.Events; using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions; + using Microsoft.Extensions.Logging; + using Microsoft.Extensions.Options; + using System.Collections.Generic; + using System.Data.SqlClient; - public class SagaManagerService : ISagaManagerService + public class ManagerService : IManagerService { - private readonly SagaManagerSettings _settings; + private readonly ManagerSettings _settings; private readonly IEventBus _eventBus; - private readonly ILogger _logger; + private readonly ILogger _logger; - public SagaManagerService(IOptions settings, + public ManagerService(IOptions settings, IEventBus eventBus, - ILogger logger) + ILogger logger) { _settings = settings.Value; _eventBus = eventBus; @@ -41,11 +41,11 @@ { try { - _logger.LogInformation("SagaManager Client is trying to connect to database server"); + _logger.LogInformation("Grace Period Manager Client is trying to connect to database server"); conn.Open(); orderIds = conn.Query( @"SELECT Id FROM [Microsoft.eShopOnContainers.Services.OrderingDb].[ordering].[orders] - WHERE DATEDIFF(hour, [OrderDate], GETDATE()) >= @GracePeriodTime + WHERE DATEDIFF(minute, [OrderDate], GETDATE()) >= @GracePeriodTime AND [OrderStatusId] = 1", new { GracePeriodTime = _settings.GracePeriodTime }); } diff --git a/src/Services/SagaManager/SagaManager/appsettings.json b/src/Services/SagaManager/SagaManager/appsettings.json index 0a514a514..273437a8e 100644 --- a/src/Services/SagaManager/SagaManager/appsettings.json +++ b/src/Services/SagaManager/SagaManager/appsettings.json @@ -8,6 +8,6 @@ } }, "ConnectionString": "Server=tcp:127.0.0.1,5433;Database=Microsoft.eShopOnContainers.Services.OrderingDb;User Id=sa;Password=Pass@word;", - "GracePeriodTime": "15", + "GracePeriodTime": "1", "CheckUpdateTime": "30000" } diff --git a/src/Web/WebMVC/Services/BasketService.cs b/src/Web/WebMVC/Services/BasketService.cs index 84c3756a7..55cec1bb9 100644 --- a/src/Web/WebMVC/Services/BasketService.cs +++ b/src/Web/WebMVC/Services/BasketService.cs @@ -120,18 +120,7 @@ namespace Microsoft.eShopOnContainers.WebMVC.Services basket.Items.Add(product); await UpdateBasket(basket); - } - - public async Task CleanBasket(ApplicationUser user) - { - var token = await GetUserTokenAsync(); - var cleanBasketUri = API.Basket.CleanBasket(_remoteServiceBaseUrl, user.Id); - - var response = await _apiClient.DeleteAsync(cleanBasketUri, token); - - //CCE: response status code... - - } + } async Task GetUserTokenAsync() { diff --git a/src/Web/WebMVC/Services/IBasketService.cs b/src/Web/WebMVC/Services/IBasketService.cs index 215a958f1..13921909a 100644 --- a/src/Web/WebMVC/Services/IBasketService.cs +++ b/src/Web/WebMVC/Services/IBasketService.cs @@ -15,6 +15,5 @@ namespace Microsoft.eShopOnContainers.WebMVC.Services Task Checkout(BasketDTO basket); Task SetQuantities(ApplicationUser user, Dictionary quantities); Order MapBasketToOrder(Basket basket); - Task CleanBasket(ApplicationUser user); } } diff --git a/src/Web/WebSPA/Client/modules/basket/basket.service.ts b/src/Web/WebSPA/Client/modules/basket/basket.service.ts index 37cecdc5d..5aa03c182 100644 --- a/src/Web/WebSPA/Client/modules/basket/basket.service.ts +++ b/src/Web/WebSPA/Client/modules/basket/basket.service.ts @@ -51,10 +51,6 @@ export class BasketService { } } } - - this.basketEvents.orderCreated$.subscribe(x => { - this.dropBasket(); - }); } addItemToBasket(item): Observable { @@ -83,13 +79,7 @@ export class BasketService { return response.json(); }); - } - - dropBasket() { - this.basket.items = []; - this.service.delete(this.basketUrl + '/' + this.basket.buyerId); - this.basketDropedSource.next(); - } + } mapBasketInfoCheckout(order: IOrder): IBasketCheckout { let basketCheckout = {};