-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
VCST-2303: add changeCartCurrency mutation (#24)
- Loading branch information
1 parent
692a6db
commit 2a264d5
Showing
6 changed files
with
241 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
src/VirtoCommerce.XCart.Core/Commands/ChangeCartCurrencyCommand.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
using VirtoCommerce.XCart.Core.Commands.BaseCommands; | ||
|
||
namespace VirtoCommerce.XCart.Core.Commands | ||
{ | ||
public class ChangeCartCurrencyCommand : CartCommand | ||
{ | ||
public string NewCurrencyCode { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
src/VirtoCommerce.XCart.Core/Schemas/InputChangeCartCurrencyType.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using GraphQL.Types; | ||
|
||
namespace VirtoCommerce.XCart.Core.Schemas | ||
{ | ||
public class InputChangeCartCurrencyType : InputCartBaseType | ||
{ | ||
public InputChangeCartCurrencyType() | ||
{ | ||
Field<NonNullGraphType<StringGraphType>>("newCurrencyCode", "Second cart currency"); | ||
} | ||
} | ||
} |
143 changes: 143 additions & 0 deletions
143
src/VirtoCommerce.XCart.Data/Commands/ChangeCartCurrencyCommandHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using VirtoCommerce.CartModule.Core.Model; | ||
using VirtoCommerce.Platform.Core.Common; | ||
using VirtoCommerce.Xapi.Core.Models; | ||
using VirtoCommerce.XCart.Core; | ||
using VirtoCommerce.XCart.Core.Commands; | ||
using VirtoCommerce.XCart.Core.Commands.BaseCommands; | ||
using VirtoCommerce.XCart.Core.Models; | ||
using VirtoCommerce.XCart.Core.Services; | ||
|
||
namespace VirtoCommerce.XCart.Data.Commands | ||
{ | ||
public class ChangeCartCurrencyCommandHandler : CartCommandHandler<ChangeCartCurrencyCommand> | ||
{ | ||
private readonly ICartProductService _cartProductService; | ||
|
||
public ChangeCartCurrencyCommandHandler( | ||
ICartAggregateRepository cartAggregateRepository, | ||
ICartProductService cartProductService) | ||
: base(cartAggregateRepository) | ||
{ | ||
_cartProductService = cartProductService; | ||
} | ||
|
||
public override async Task<CartAggregate> Handle(ChangeCartCurrencyCommand request, CancellationToken cancellationToken) | ||
{ | ||
// get (or create) both carts | ||
var currentCurrencyCartAggregate = await GetOrCreateCartFromCommandAsync(request) | ||
?? throw new OperationCanceledException("Cart not found"); | ||
|
||
var newCurrencyCartRequest = new ChangeCartCurrencyCommand | ||
{ | ||
StoreId = request.StoreId ?? currentCurrencyCartAggregate.Cart.StoreId, | ||
CartName = request.CartName ?? currentCurrencyCartAggregate.Cart.Name, | ||
CartType = request.CartType ?? currentCurrencyCartAggregate.Cart.Type, | ||
UserId = request.UserId ?? currentCurrencyCartAggregate.Cart.CustomerId, | ||
OrganizationId = request.OrganizationId ?? currentCurrencyCartAggregate.Cart.OrganizationId, | ||
CultureName = request.CultureName ?? currentCurrencyCartAggregate.Cart.LanguageCode, | ||
CurrencyCode = request.NewCurrencyCode, | ||
}; | ||
|
||
var newCurrencyCartAggregate = await GetOrCreateCartFromCommandAsync(newCurrencyCartRequest); | ||
|
||
// clear (old) cart items and add items from the currency cart | ||
newCurrencyCartAggregate.Cart.Items.Clear(); | ||
|
||
await CopyItems(currentCurrencyCartAggregate, newCurrencyCartAggregate); | ||
|
||
await CartRepository.SaveAsync(newCurrencyCartAggregate); | ||
return newCurrencyCartAggregate; | ||
} | ||
|
||
protected virtual async Task CopyItems(CartAggregate currentCurrencyCartAggregate, CartAggregate newCurrencyCartAggregate) | ||
{ | ||
var ordinaryItems = currentCurrencyCartAggregate.LineItems | ||
.Where(x => !x.IsConfigured) | ||
.ToArray(); | ||
|
||
if (ordinaryItems.Length > 0) | ||
{ | ||
var newCartItems = ordinaryItems | ||
.Select(x => new NewCartItem(x.ProductId, x.Quantity) | ||
{ | ||
IgnoreValidationErrors = true, | ||
Comment = x.Note, | ||
IsSelectedForCheckout = x.SelectedForCheckout, | ||
DynamicProperties = x.DynamicProperties.SelectMany(x => x.Values.Select(y => new DynamicPropertyValue() | ||
{ | ||
Name = x.Name, | ||
Value = y.Value, | ||
Locale = y.Locale, | ||
})).ToArray(), | ||
}) | ||
.ToArray(); | ||
|
||
await newCurrencyCartAggregate.AddItemsAsync(newCartItems); | ||
} | ||
|
||
// copy configured items | ||
var configuredItems = currentCurrencyCartAggregate.LineItems | ||
.Where(x => x.IsConfigured) | ||
.ToArray(); | ||
|
||
await CopyConfiguredItems(newCurrencyCartAggregate, configuredItems); | ||
} | ||
|
||
protected virtual async Task CopyConfiguredItems(CartAggregate newCurrencyCartAggregate, IList<LineItem> configuredItems) | ||
{ | ||
if (configuredItems.Count == 0) | ||
{ | ||
return; | ||
} | ||
|
||
var configProductsIds = configuredItems | ||
.Where(x => !x.ConfigurationItems.IsNullOrEmpty()) | ||
.SelectMany(x => x.ConfigurationItems.Select(x => x.ProductId)) | ||
.Distinct() | ||
.ToList(); | ||
|
||
configProductsIds.AddRange(configuredItems.Select(x => x.ProductId)); | ||
|
||
var configProducts = await _cartProductService.GetCartProductsByIdsAsync(newCurrencyCartAggregate, configProductsIds); | ||
|
||
foreach (var configurationLineItem in configuredItems) | ||
{ | ||
var contaner = AbstractTypeFactory<ConfiguredLineItemContainer>.TryCreateInstance(); | ||
contaner.Currency = newCurrencyCartAggregate.Currency; | ||
contaner.Store = newCurrencyCartAggregate.Store; | ||
|
||
contaner.ConfigurableProduct = configProducts.FirstOrDefault(x => x.Product.Id == configurationLineItem.ProductId); | ||
|
||
foreach (var configurationItem in configurationLineItem.ConfigurationItems ?? []) | ||
{ | ||
var product = configProducts.FirstOrDefault(x => x.Product.Id == configurationItem.ProductId); | ||
if (product != null) | ||
{ | ||
contaner.AddItem(product, configurationItem.Quantity, configurationItem.SectionId); | ||
} | ||
} | ||
|
||
var expItem = contaner.CreateConfiguredLineItem(configurationLineItem.Quantity); | ||
|
||
await newCurrencyCartAggregate.AddConfiguredItemAsync(new NewCartItem(configurationLineItem.ProductId, configurationLineItem.Quantity) | ||
{ | ||
CartProduct = contaner.ConfigurableProduct, | ||
IgnoreValidationErrors = true, | ||
Comment = configurationLineItem.Note, | ||
IsSelectedForCheckout = configurationLineItem.SelectedForCheckout, | ||
DynamicProperties = configurationLineItem.DynamicProperties.SelectMany(x => x.Values.Select(y => new DynamicPropertyValue() | ||
{ | ||
Name = x.Name, | ||
Value = y.Value, | ||
Locale = y.Locale, | ||
})).ToArray(), | ||
}, expItem.Item); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters