Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VCST-2303: add changeCartCurrency mutation #24

Merged
merged 12 commits into from
Dec 20, 2024
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
Loading