Skip to content

Commit 889d3c8

Browse files
committed
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
1 parent 1218069 commit 889d3c8

14 files changed

Lines changed: 50 additions & 72 deletions

File tree

eShopOnContainers-ServicesAndWebApps.sln

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
33
# Visual Studio 15
4-
VisualStudioVersion = 15.0.26403.3
4+
VisualStudioVersion = 15.0.26430.6
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{932D8224-11F6-4D07-B109-DA28AD288A63}"
77
EndProject
@@ -80,9 +80,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Extensions.Health
8080
EndProject
8181
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EventBus.Tests", "src\BuildingBlocks\EventBus\EventBus.Tests\EventBus.Tests.csproj", "{4A980AC4-7205-46BF-8CCB-09E44D700FD4}"
8282
EndProject
83-
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "SagaManager", "SagaManager", "{F38B4FF0-0B49-405A-B1B4-F7A5E3BC4C4E}"
83+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "GracePeriod", "GracePeriod", "{F38B4FF0-0B49-405A-B1B4-F7A5E3BC4C4E}"
8484
EndProject
85-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SagaManager", "src\Services\SagaManager\SagaManager\SagaManager.csproj", "{F6E0F0DD-1400-43C3-B5E0-7CC325728C47}"
85+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GracePeriodManager", "src\Services\SagaManager\SagaManager\GracePeriodManager.csproj", "{F6E0F0DD-1400-43C3-B5E0-7CC325728C47}"
8686
EndProject
8787
Global
8888
GlobalSection(SolutionConfigurationPlatforms) = preSolution

src/Services/Ordering/Ordering.API/Application/DomainEventHandlers/BuyerAndPaymentMethodVerified/UpdateOrderWhenBuyerAndPaymentMethodVerifiedDomainEventHandler.cs

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,13 @@ namespace Ordering.API.Application.DomainEventHandlers.BuyerAndPaymentMethodVeri
1212
public class UpdateOrderWhenBuyerAndPaymentMethodVerifiedDomainEventHandler
1313
: IAsyncNotificationHandler<BuyerAndPaymentMethodVerifiedDomainEvent>
1414
{
15-
private readonly IOrderRepository _orderRepository;
16-
private readonly IOrderingIntegrationEventService _orderingIntegrationEventService;
15+
private readonly IOrderRepository _orderRepository;
1716
private readonly ILoggerFactory _logger;
1817

1918
public UpdateOrderWhenBuyerAndPaymentMethodVerifiedDomainEventHandler(
20-
IOrderRepository orderRepository, ILoggerFactory logger,
21-
IOrderingIntegrationEventService orderingIntegrationEventService)
19+
IOrderRepository orderRepository, ILoggerFactory logger)
2220
{
23-
_orderRepository = orderRepository ?? throw new ArgumentNullException(nameof(orderRepository));
24-
_orderingIntegrationEventService = orderingIntegrationEventService ?? throw new ArgumentNullException(nameof(orderingIntegrationEventService));
21+
_orderRepository = orderRepository ?? throw new ArgumentNullException(nameof(orderRepository));
2522
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
2623
}
2724

@@ -32,11 +29,7 @@ public async Task Handle(BuyerAndPaymentMethodVerifiedDomainEvent buyerPaymentMe
3229
{
3330
var orderToUpdate = await _orderRepository.GetAsync(buyerPaymentMethodVerifiedEvent.OrderId);
3431
orderToUpdate.SetBuyerId(buyerPaymentMethodVerifiedEvent.Buyer.Id);
35-
orderToUpdate.SetPaymentId(buyerPaymentMethodVerifiedEvent.Payment.Id);
36-
37-
var orderStartedIntegrationEvent = new OrderStartedIntegrationEvent(buyerPaymentMethodVerifiedEvent.Buyer.IdentityGuid);
38-
39-
await _orderingIntegrationEventService.PublishThroughEventBusAsync(orderStartedIntegrationEvent);
32+
orderToUpdate.SetPaymentId(buyerPaymentMethodVerifiedEvent.Payment.Id);
4033

4134
_logger.CreateLogger(nameof(UpdateOrderWhenBuyerAndPaymentMethodVerifiedDomainEventHandler))
4235
.LogTrace($"Order with Id: {buyerPaymentMethodVerifiedEvent.OrderId} has been successfully updated with a payment method id: { buyerPaymentMethodVerifiedEvent.Payment.Id }");

src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/UserCheckoutAcceptedIntegrationEventHandler.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@ public class UserCheckoutAcceptedIntegrationEventHandler : IIntegrationEventHand
1212
{
1313
private readonly IMediator _mediator;
1414
private readonly ILoggerFactory _logger;
15+
private readonly IOrderingIntegrationEventService _orderingIntegrationEventService;
1516

1617
public UserCheckoutAcceptedIntegrationEventHandler(IMediator mediator,
17-
ILoggerFactory logger)
18+
ILoggerFactory logger, IOrderingIntegrationEventService orderingIntegrationEventService)
1819
{
1920
_mediator = mediator ?? throw new ArgumentNullException(nameof(mediator));
2021
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
22+
_orderingIntegrationEventService = orderingIntegrationEventService ?? throw new ArgumentNullException(nameof(orderingIntegrationEventService));
2123
}
2224

2325
/// <summary>
@@ -32,6 +34,11 @@ public UserCheckoutAcceptedIntegrationEventHandler(IMediator mediator,
3234
public async Task Handle(UserCheckoutAcceptedIntegrationEvent eventMsg)
3335
{
3436
var result = false;
37+
38+
// Send Integration event to clean basket once basket is converted to Order and before starting with the order creation process
39+
var orderStartedIntegrationEvent = new OrderStartedIntegrationEvent(eventMsg.UserId);
40+
await _orderingIntegrationEventService.PublishThroughEventBusAsync(orderStartedIntegrationEvent);
41+
3542
if (eventMsg.RequestId != Guid.Empty)
3643
{
3744
var createOrderCommand = new CreateOrderCommand(eventMsg.Basket.Items, eventMsg.UserId, eventMsg.City, eventMsg.Street,
@@ -41,7 +48,7 @@ public async Task Handle(UserCheckoutAcceptedIntegrationEvent eventMsg)
4148

4249
var requestCreateOrder = new IdentifiedCommand<CreateOrderCommand, bool>(createOrderCommand, eventMsg.RequestId);
4350
result = await _mediator.SendAsync(requestCreateOrder);
44-
}
51+
}
4552

4653
_logger.CreateLogger(nameof(UserCheckoutAcceptedIntegrationEventHandler))
4754
.LogTrace(result ? $"UserCheckoutAccepted integration event has been received and a create new order process is started with requestId: {eventMsg.RequestId}" :

src/Services/SagaManager/SagaManager/SagaManager.csproj renamed to src/Services/SagaManager/SagaManager/GracePeriodManager.csproj

File renamed without changes.

src/Services/SagaManager/SagaManager/IntegrationEvents/Events/GracePeriodConfirmedIntegrationEvent.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace SagaManager.IntegrationEvents.Events
1+
namespace GracePeriodManager.IntegrationEvents.Events
22
{
33
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events;
44

src/Services/SagaManager/SagaManager/SagaManagerSettings.cs renamed to src/Services/SagaManager/SagaManager/ManagerSettings.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
namespace SagaManager
1+
namespace GracePeriodManager
22
{
3-
public class SagaManagerSettings
3+
public class ManagerSettings
44
{
55
public string ConnectionString { get; set; }
66

src/Services/SagaManager/SagaManager/Program.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace SagaManager
1+
namespace GracePeriodManager
22
{
33
using System.IO;
44
using System;
@@ -31,14 +31,14 @@ static async Task MainAsync()
3131
var logger = serviceProvider.GetService<ILoggerFactory>();
3232
Configure(logger);
3333

34-
var sagaManagerService = serviceProvider
35-
.GetRequiredService<ISagaManagerService>();
34+
var gracePeriodManagerService = serviceProvider
35+
.GetRequiredService<IManagerService>();
3636
var checkUpdateTime = serviceProvider
37-
.GetRequiredService<IOptions<SagaManagerSettings>>().Value.CheckUpdateTime;
37+
.GetRequiredService<IOptions<ManagerSettings>>().Value.CheckUpdateTime;
3838

3939
while (true)
4040
{
41-
sagaManagerService.CheckConfirmedGracePeriodOrders();
41+
gracePeriodManagerService.CheckConfirmedGracePeriodOrders();
4242
await Task.Delay(checkUpdateTime);
4343
}
4444
}
@@ -57,11 +57,11 @@ public static IServiceProvider ConfigureServices(IServiceCollection services)
5757
{
5858
services.AddLogging()
5959
.AddOptions()
60-
.Configure<SagaManagerSettings>(Configuration)
61-
.AddSingleton<ISagaManagerService, SagaManagerService>()
60+
.Configure<ManagerSettings>(Configuration)
61+
.AddSingleton<IManagerService, ManagerService>()
6262
.AddSingleton<IRabbitMQPersistentConnection>(sp =>
6363
{
64-
var settings = sp.GetRequiredService<IOptions<SagaManagerSettings>>().Value;
64+
var settings = sp.GetRequiredService<IOptions<ManagerSettings>>().Value;
6565
var logger = sp.GetRequiredService<ILogger<DefaultRabbitMQPersistentConnection>>();
6666
var factory = new ConnectionFactory()
6767
{
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace GracePeriodManager.Services
2+
{
3+
public interface IManagerService
4+
{
5+
void CheckConfirmedGracePeriodOrders();
6+
}
7+
}

src/Services/SagaManager/SagaManager/Services/ISagaManagerService.cs

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/Services/SagaManager/SagaManager/Services/SagaManagerService.cs renamed to src/Services/SagaManager/SagaManager/Services/ManagerService.cs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
1-
namespace SagaManager.Services
1+
namespace GracePeriodManager.Services
22
{
3-
using System.Collections.Generic;
4-
using System.Data.SqlClient;
5-
using Microsoft.Extensions.Options;
6-
using Microsoft.Extensions.Logging;
73
using Dapper;
8-
using IntegrationEvents.Events;
4+
using GracePeriodManager.IntegrationEvents.Events;
95
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions;
6+
using Microsoft.Extensions.Logging;
7+
using Microsoft.Extensions.Options;
8+
using System.Collections.Generic;
9+
using System.Data.SqlClient;
1010

11-
public class SagaManagerService : ISagaManagerService
11+
public class ManagerService : IManagerService
1212
{
13-
private readonly SagaManagerSettings _settings;
13+
private readonly ManagerSettings _settings;
1414
private readonly IEventBus _eventBus;
15-
private readonly ILogger<SagaManagerService> _logger;
15+
private readonly ILogger<ManagerService> _logger;
1616

17-
public SagaManagerService(IOptions<SagaManagerSettings> settings,
17+
public ManagerService(IOptions<ManagerSettings> settings,
1818
IEventBus eventBus,
19-
ILogger<SagaManagerService> logger)
19+
ILogger<ManagerService> logger)
2020
{
2121
_settings = settings.Value;
2222
_eventBus = eventBus;
@@ -41,11 +41,11 @@ private IEnumerable<int> GetConfirmedGracePeriodOrders()
4141
{
4242
try
4343
{
44-
_logger.LogInformation("SagaManager Client is trying to connect to database server");
44+
_logger.LogInformation("Grace Period Manager Client is trying to connect to database server");
4545
conn.Open();
4646
orderIds = conn.Query<int>(
4747
@"SELECT Id FROM [Microsoft.eShopOnContainers.Services.OrderingDb].[ordering].[orders]
48-
WHERE DATEDIFF(hour, [OrderDate], GETDATE()) >= @GracePeriodTime
48+
WHERE DATEDIFF(minute, [OrderDate], GETDATE()) >= @GracePeriodTime
4949
AND [OrderStatusId] = 1",
5050
new { GracePeriodTime = _settings.GracePeriodTime });
5151
}

0 commit comments

Comments
 (0)