Skip to content

Commit

Permalink
VCST-1562: update cart organization name on order create (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
ksavosteev authored Jul 29, 2024
1 parent e249af1 commit a383b35
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using GraphQL;
using MediatR;
using VirtoCommerce.CartModule.Core.Model;
using VirtoCommerce.CartModule.Core.Services;
using VirtoCommerce.CustomerModule.Core.Services;
using VirtoCommerce.Platform.Core.Common;
using VirtoCommerce.Xapi.Core.Helpers;
using VirtoCommerce.XCart.Core;
Expand All @@ -23,33 +22,30 @@ public class CreateOrderFromCartCommandHandler : IRequestHandler<CreateOrderFrom
private readonly ICustomerOrderAggregateRepository _customerOrderAggregateRepository;
private readonly ICartAggregateRepository _cartRepository;
private readonly ICartValidationContextFactory _cartValidationContextFactory;
private readonly IMemberService _memberService;

public string ValidationRuleSet { get; set; } = "*";

public CreateOrderFromCartCommandHandler(
IShoppingCartService cartService,
ICustomerOrderAggregateRepository customerOrderAggregateRepository,
ICartAggregateRepository cartRepository,
ICartValidationContextFactory cartValidationContextFactory)
ICartValidationContextFactory cartValidationContextFactory,
IMemberService memberService)
{
_cartService = cartService;
_customerOrderAggregateRepository = customerOrderAggregateRepository;
_cartRepository = cartRepository;
_cartValidationContextFactory = cartValidationContextFactory;
_memberService = memberService;
}

public virtual async Task<CustomerOrderAggregate> Handle(CreateOrderFromCartCommand request, CancellationToken cancellationToken)
{
var cart = await _cartService.GetByIdAsync(request.CartId);
var cartAggregate = await _cartRepository.GetCartForShoppingCartAsync(cart);

// remove unselected gifts before order create
var unselectedGifts = cartAggregate.GiftItems.Where(x => !x.SelectedForCheckout).ToList();
if (unselectedGifts.Count != 0)
{
unselectedGifts.ForEach(x => cartAggregate.Cart.Items.Remove(x));
}

await UpdateCart(cartAggregate);
await ValidateCart(cartAggregate);

// need to check for unsaved gift items before creating an order and resave the cart, otherwise an exception will be thrown on order create
Expand All @@ -59,7 +55,7 @@ public virtual async Task<CustomerOrderAggregate> Handle(CreateOrderFromCartComm
await _cartRepository.SaveAsync(cartAggregate);
}

var result = await _customerOrderAggregateRepository.CreateOrderFromCart(cart);
var result = await _customerOrderAggregateRepository.CreateOrderFromCart(cartAggregate.Cart);

// remove selected items after order create
var selectedLineItemIds = cartAggregate.SelectedLineItems.Select(x => x.Id).ToArray();
Expand All @@ -80,6 +76,23 @@ public virtual async Task<CustomerOrderAggregate> Handle(CreateOrderFromCartComm
return result;
}

private async Task UpdateCart(CartAggregate cartAggregate)
{
// remove unselected gifts before order create
var unselectedGifts = cartAggregate.GiftItems.Where(x => !x.SelectedForCheckout).ToList();
if (unselectedGifts.Count != 0)
{
unselectedGifts.ForEach(x => cartAggregate.Cart.Items.Remove(x));
}

// update organization name
if (!string.IsNullOrEmpty(cartAggregate.Cart.OrganizationId))
{
var organization = await _memberService.GetByIdAsync(cartAggregate.Cart.OrganizationId);
cartAggregate.Cart.OrganizationName = organization?.Name;
}
}

protected virtual async Task ValidateCart(CartAggregate cartAggregate)
{
var context = await _cartValidationContextFactory.CreateValidationContextAsync(cartAggregate);
Expand All @@ -92,12 +105,5 @@ protected virtual async Task ValidateCart(CartAggregate cartAggregate)
throw new ExecutionError("The cart has validation errors", dictionary) { Code = Constants.ValidationErrorCode };
}
}

[Obsolete("Use ValidateCart(CartAggregate cartAggregate)()", DiagnosticId = "VC0005", UrlFormat = "https://docs.virtocommerce.org/products/products-virto3-versions/")]
protected virtual async Task ValidateCart(ShoppingCart cart)
{
var cartAggregate = await _cartRepository.GetCartForShoppingCartAsync(cart);
await ValidateCart(cartAggregate);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,16 @@ public Task Handle_CartHasValidationErrors_ExceptionThrown()

var orderAggregateRepositoryMock = new Mock<ICustomerOrderAggregateRepository>();

var memberService = new Mock<IMemberService>();

var request = new CreateOrderFromCartCommand(cart.Id);

var handler = new CreateOrderFromCartCommandHandler(
cartService.Object,
orderAggregateRepositoryMock.Object,
aggregationService.Object,
validationContextMock.Object);
validationContextMock.Object,
memberService.Object);

// Assert
return Assert.ThrowsAsync<ExecutionError>(() => handler.Handle(request, CancellationToken.None));
Expand Down Expand Up @@ -119,7 +122,9 @@ public async Task Handle_CreateOrder_EnsureSelectedLineItemsDeleted()
contextFactory.Setup(x => x.CreateValidationContextAsync(It.IsAny<CartAggregate>()))
.ReturnsAsync(new CartValidationContext());

var handler = new CreateOrderFromCartCommandHandler(cartService.Object, customerAggrRep.Object, cartAggrRep.Object, contextFactory.Object)
var memberService = new Mock<IMemberService>();

var handler = new CreateOrderFromCartCommandHandler(cartService.Object, customerAggrRep.Object, cartAggrRep.Object, contextFactory.Object, memberService.Object)
{
ValidationRuleSet = "default"
};
Expand Down

0 comments on commit a383b35

Please sign in to comment.