OrderStockConfirmedDomainEvent implemented
This commit is contained in:
parent
2f41736f91
commit
d3d15f1de5
@ -1,4 +1,6 @@
|
||||
namespace Ordering.API.Application.DomainEventHandlers.OrderStartedEvent
|
||||
using System.Linq;
|
||||
|
||||
namespace Ordering.API.Application.DomainEventHandlers.OrderStartedEvent
|
||||
{
|
||||
using MediatR;
|
||||
using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.OrderAggregate;
|
||||
@ -9,14 +11,14 @@
|
||||
using Ordering.API.Application.IntegrationCommands.Commands;
|
||||
using Ordering.API.Application.IntegrationEvents;
|
||||
|
||||
public class UpdateOrderWhenOrderStockMethodVerifiedDomainEventHandler
|
||||
: IAsyncNotificationHandler<OrderStockMethodVerifiedDomainEvent>
|
||||
public class OrderStatusChangedWhenOrderStockConfirmedDomainEventHandler
|
||||
: IAsyncNotificationHandler<OrderStockConfirmedDomainEvent>
|
||||
{
|
||||
private readonly IOrderRepository _orderRepository;
|
||||
private readonly ILoggerFactory _logger;
|
||||
private readonly IOrderingIntegrationEventService _orderingIntegrationEventService;
|
||||
|
||||
public UpdateOrderWhenOrderStockMethodVerifiedDomainEventHandler(
|
||||
public OrderStatusChangedWhenOrderStockConfirmedDomainEventHandler(
|
||||
IOrderRepository orderRepository, ILoggerFactory logger,
|
||||
IOrderingIntegrationEventService orderingIntegrationEventService)
|
||||
{
|
||||
@ -25,23 +27,18 @@
|
||||
_orderingIntegrationEventService = orderingIntegrationEventService;
|
||||
}
|
||||
|
||||
public async Task Handle(OrderStockMethodVerifiedDomainEvent orderStockMethodVerifiedDomainEvent)
|
||||
public async Task Handle(OrderStockConfirmedDomainEvent orderStockMethodVerifiedDomainEvent)
|
||||
{
|
||||
var orderToUpdate = await _orderRepository.GetAsync(orderStockMethodVerifiedDomainEvent.OrderId);
|
||||
orderToUpdate.SetOrderStatusId(orderStockMethodVerifiedDomainEvent.OrderStatus.Id);
|
||||
|
||||
_orderRepository.Update(orderToUpdate);
|
||||
|
||||
await _orderRepository.UnitOfWork
|
||||
.SaveEntitiesAsync();
|
||||
|
||||
_logger.CreateLogger(nameof(UpdateOrderWhenOrderStockMethodVerifiedDomainEventHandler))
|
||||
_logger.CreateLogger(nameof(OrderStatusChangedWhenOrderStockConfirmedDomainEventHandler))
|
||||
.LogTrace($"Order with Id: {orderStockMethodVerifiedDomainEvent.OrderId} has been successfully updated with " +
|
||||
$"a status order id: { orderStockMethodVerifiedDomainEvent.OrderStatus.Id }");
|
||||
|
||||
var payOrderCommandMsg = new PayOrderCommandMsg(orderToUpdate.Id);
|
||||
if (orderStockMethodVerifiedDomainEvent.OrderStatus == OrderStatus.StockValidated)
|
||||
{
|
||||
var payOrderCommandMsg = new PayOrderCommandMsg(orderStockMethodVerifiedDomainEvent.OrderId);
|
||||
await _orderingIntegrationEventService.SaveEventAndOrderingContextChangesAsync(payOrderCommandMsg);
|
||||
await _orderingIntegrationEventService.PublishThroughEventBusAsync(payOrderCommandMsg);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -1,4 +1,5 @@
|
||||
using System.Linq;
|
||||
using Ordering.API.Application.IntegrationCommands.Commands;
|
||||
|
||||
namespace Ordering.API.Application.IntegrationEvents.EventHandling
|
||||
{
|
||||
@ -25,15 +26,18 @@ namespace Ordering.API.Application.IntegrationEvents.EventHandling
|
||||
|
||||
public async Task Handle(OrderStockNotConfirmedIntegrationEvent @event)
|
||||
{
|
||||
//TODO: must update the order state to cancelled and the CurrentOrderStateContextDescription with the reasons of no-stock confirm
|
||||
var order = await _orderRepository.GetAsync(@event.OrderId);
|
||||
CheckValidSagaId(order);
|
||||
var orderToUpdate = await _orderRepository.GetAsync(@event.OrderId);
|
||||
CheckValidSagaId(orderToUpdate);
|
||||
|
||||
var orderStockNotConfirmedItems = @event.OrderStockItems
|
||||
.FindAll(c => !c.Confirmed)
|
||||
.Select(c => c.ProductId);
|
||||
|
||||
order.SetOrderStockConfirmed(orderStockNotConfirmedItems);
|
||||
orderToUpdate.SetOrderStockConfirmed(orderStockNotConfirmedItems);
|
||||
|
||||
var payOrderCommandMsg = new PayOrderCommandMsg(orderToUpdate.Id);
|
||||
await _orderingIntegrationEventService.SaveEventAndOrderingContextChangesAsync(payOrderCommandMsg);
|
||||
await _orderingIntegrationEventService.PublishThroughEventBusAsync(payOrderCommandMsg);
|
||||
}
|
||||
|
||||
private void CheckValidSagaId(Order orderSaga)
|
||||
|
@ -14,7 +14,6 @@ namespace Ordering.API.Application.IntegrationEvents.EventHandling
|
||||
private readonly ILoggerFactory _logger;
|
||||
|
||||
public UserCheckoutAcceptedIntegrationEventHandler(IMediator mediator,
|
||||
IOrderingIntegrationEventService orderingIntegrationEventService,
|
||||
ILoggerFactory logger)
|
||||
{
|
||||
_mediator = mediator ?? throw new ArgumentNullException(nameof(mediator));
|
||||
|
@ -103,21 +103,22 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.O
|
||||
if(orderStockNotConfirmedItems is null)
|
||||
{
|
||||
OrderStatus = OrderStatus.StockValidated;
|
||||
|
||||
_description = "All the items were confirmed with available stock.";
|
||||
//AddDomainEvent(new OrderStockMethodVerifiedDomainEvent(Id, OrderStatus.StockValidated));
|
||||
}
|
||||
else
|
||||
{
|
||||
OrderStatus = OrderStatus.Cancelled;
|
||||
|
||||
var itemsStockNotConfirmedProductNames = OrderItems
|
||||
.Where(c => orderStockNotConfirmedItems.Contains(c.ProductId))
|
||||
.Select(c => c.GetOrderItemProductName());
|
||||
|
||||
var itemsStockNotConfirmedDescription = string.Join(", ", itemsStockNotConfirmedProductNames);
|
||||
|
||||
OrderStatus = OrderStatus.Cancelled;
|
||||
_description = $"The product items don't have stock: ({itemsStockNotConfirmedDescription}).";
|
||||
//AddDomainEvent(new OrderStockMethodVerifiedDomainEvent(Id, OrderStatus.Cancelled));
|
||||
}
|
||||
|
||||
AddDomainEvent(new OrderStockConfirmedDomainEvent(Id, OrderStatus));
|
||||
}
|
||||
|
||||
private void AddOrderStartedDomainEvent(int cardTypeId, string cardNumber,
|
||||
|
@ -1,18 +1,20 @@
|
||||
namespace Ordering.Domain.Events
|
||||
using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.OrderAggregate;
|
||||
|
||||
namespace Ordering.Domain.Events
|
||||
{
|
||||
using MediatR;
|
||||
using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.OrderAggregate;
|
||||
using System.Collections.Generic;
|
||||
|
||||
/// <summary>
|
||||
/// Event used when the order stock items are verified
|
||||
/// </summary>
|
||||
public class OrderStockMethodVerifiedDomainEvent
|
||||
public class OrderStockConfirmedDomainEvent
|
||||
: IAsyncNotification
|
||||
{
|
||||
public int OrderId { get; }
|
||||
public OrderStatus OrderStatus { get; }
|
||||
|
||||
public OrderStockMethodVerifiedDomainEvent(int orderId,
|
||||
public OrderStockConfirmedDomainEvent(int orderId,
|
||||
OrderStatus orderStatus)
|
||||
{
|
||||
OrderId = orderId;
|
||||
|
Loading…
x
Reference in New Issue
Block a user