-
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.
feat: add changeCartCurrency mutation
- Loading branch information
1 parent
dc05f46
commit bbeca03
Showing
4 changed files
with
99 additions
and
0 deletions.
There are no files selected for viewing
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; } | ||
} | ||
} |
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"); | ||
} | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
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,59 @@ | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
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); | ||
|
||
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); | ||
|
||
// get items to convert | ||
var excludedProductsIds = newCurrencyCartAggregate.LineItems.Select(x => x.ProductId).ToArray(); | ||
|
||
var newCartItems = currentCurrencyCartAggregate.LineItems | ||
.Where(x => !excludedProductsIds.Contains(x.ProductId)) | ||
.Select(x => new NewCartItem(x.ProductId, x.Quantity) | ||
{ | ||
Comment = x.Note, | ||
}) | ||
.ToArray(); | ||
|
||
await newCurrencyCartAggregate.AddItemsAsync(newCartItems); | ||
|
||
await CartRepository.SaveAsync(newCurrencyCartAggregate); | ||
return newCurrencyCartAggregate; | ||
} | ||
} | ||
} |
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