Skip to content

Commit

Permalink
VCST-2223: update to Configurable Products (#23)
Browse files Browse the repository at this point in the history
* feat: update to Configurable Products (#23)

---------

Co-authored-by: Евгений Колосов <[email protected]>
  • Loading branch information
ksavosteev and Ljutyj authored Dec 3, 2024
1 parent ba3118c commit 5d4f971
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using System.Collections.Generic;
using VirtoCommerce.Xapi.Core.Infrastructure;

namespace VirtoCommerce.XCatalog.Core.Queries;
public class GetProductConfigurationsQuery : IQuery<Dictionary<string, bool>>
{
public string[] ProductIds { get; set; } = [];
}
24 changes: 23 additions & 1 deletion src/VirtoCommerce.XCatalog.Core/Schemas/ProductType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using GraphQL;
using GraphQL.Builders;
using GraphQL.DataLoader;
using GraphQL.Resolvers;
using GraphQL.Types;
using MediatR;
using VirtoCommerce.CatalogModule.Core.Model;
Expand Down Expand Up @@ -76,7 +77,28 @@ public ProductType(IMediator mediator, IDataLoaderContextAccessor dataLoader)
Field(d => d.IndexedProduct.MaxQuantity, nullable: true).Description("Max. quantity");
Field(d => d.IndexedProduct.PackSize, nullable: false).Description("Defines the number of items in a package. Quantity step for your product's.");
Field(d => d.RelevanceScore, nullable: true).Description("Product relevance score");
Field(d => d.IndexedProduct.IsConfigurable, nullable: false).Description("Product is configurable");

var productField = new FieldType
{
Name = "isConfigurable",
Type = typeof(NonNullGraphType<BooleanGraphType>),
Description = "Product is configurable",
Resolver = new FuncFieldResolver<ExpProduct, IDataLoaderResult<bool>>(context =>
{
var loader = dataLoader.Context.GetOrAddBatchLoader<string, bool>("products_active_configurations", async (ids) =>
{
var query = new GetProductConfigurationsQuery
{
ProductIds = ids.ToArray()
};

return await mediator.Send(query);
});
return loader.LoadAsync(context.Source.Id);
})
};
AddField(productField);


FieldAsync<StringGraphType>("outline", resolve: async context =>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" PrivateAssets="All" />
<PackageReference Include="VirtoCommerce.Xapi.Core" Version="3.809.0" />
<PackageReference Include="VirtoCommerce.CatalogModule.Core" Version="3.829.0" />
<PackageReference Include="VirtoCommerce.CatalogModule.Core" Version="3.830.0" />
<PackageReference Include="VirtoCommerce.InventoryModule.Core" Version="3.805.0" />
<PackageReference Include="VirtoCommerce.MarketingModule.Core" Version="3.812.0" />
<PackageReference Include="VirtoCommerce.PricingModule.Core" Version="3.809.0" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using VirtoCommerce.CatalogModule.Core.Model.Search;
using VirtoCommerce.CatalogModule.Core.Search;
using VirtoCommerce.Platform.Core.Common;
using VirtoCommerce.Xapi.Core.Infrastructure;
using VirtoCommerce.XCatalog.Core.Queries;

namespace VirtoCommerce.XCatalog.Data.Queries;
public class GetProductConfigurationsQueryHandler : IQueryHandler<GetProductConfigurationsQuery, Dictionary<string, bool>>
{
private readonly IProductConfigurationSearchService _productConfigurationSearchService;

public GetProductConfigurationsQueryHandler(IProductConfigurationSearchService productConfigurationSearchService)
{
_productConfigurationSearchService = productConfigurationSearchService;
}

public async Task<Dictionary<string, bool>> Handle(GetProductConfigurationsQuery request, CancellationToken cancellationToken)
{
var criteria = AbstractTypeFactory<ProductConfigurationSearchCriteria>.TryCreateInstance();
criteria.ProductIds = request.ProductIds;

var configurations = await _productConfigurationSearchService.SearchNoCloneAsync(criteria);

var result = configurations.Results.ToDictionary(x => x.ProductId, x => x.IsActive);

var existingConfigurations = result.Select(result => result.Key).ToArray();
foreach (var nonExistingConfiguration in request.ProductIds.Except(existingConfigurations))
{
result.TryAdd(nonExistingConfiguration, false);
}

return result;
}
}
2 changes: 1 addition & 1 deletion src/VirtoCommerce.XCatalog.Web/module.manifest
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<platformVersion>3.853.0</platformVersion>
<dependencies>
<dependency id="VirtoCommerce.Catalog" version="3.829.0" />
<dependency id="VirtoCommerce.Catalog" version="3.830.0" />
<dependency id="VirtoCommerce.Inventory" version="3.805.0" optional="true" />
<dependency id="VirtoCommerce.Marketing" version="3.812.0" optional="true" />
<dependency id="VirtoCommerce.Pricing" version="3.809.0" optional="true" />
Expand Down

0 comments on commit 5d4f971

Please sign in to comment.