Skip to content

Commit

Permalink
feat: add changeCartCurrency mutation
Browse files Browse the repository at this point in the history
  • Loading branch information
ksavosteev committed Dec 3, 2024
1 parent dc05f46 commit bbeca03
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 0 deletions.
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; }
}
}
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");
}
}
}
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;
}
}
}
19 changes: 19 additions & 0 deletions src/VirtoCommerce.XCart.Data/Schemas/PurchaseSchema.cs
Original file line number Diff line number Diff line change
Expand Up @@ -829,6 +829,25 @@ public void Build(ISchema schema)

schema.Mutation.AddField(margeCartField);

var changeCartCurrency = FieldBuilder.Create<CartAggregate, CartAggregate>(GraphTypeExtenstionHelper.GetActualType<CartType>())
.Name("changeCartCurrency")
.Argument(GraphTypeExtenstionHelper.GetActualComplexType<NonNullGraphType<InputChangeCartCurrencyType>>(), SchemaConstants.CommandName)
.ResolveSynchronizedAsync(CartPrefix, "userId", _distributedLockService, async context =>
{
var cartCommand = context.GetCartCommand<ChangeCartCurrencyCommand>();

await CheckAuthByCartCommandAsync(context, cartCommand);

//We need to add cartAggregate to the context to be able use it on nested cart types resolvers (e.g for currency)
var cartAggregate = await _mediator.Send(cartCommand);

//store cart aggregate in the user context for future usage in the graph types resolvers
context.SetExpandedObjectGraph(cartAggregate);
return cartAggregate;
}).FieldType;

schema.Mutation.AddField(changeCartCurrency);

/// <example>
/// This is an example JSON request for a mutation
/// {
Expand Down

0 comments on commit bbeca03

Please sign in to comment.