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-1698: add Shipping Address Policy #7

Merged
merged 5 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions src/VirtoCommerce.XOrder.Core/ModuleConstants.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System.Collections.Generic;
using VirtoCommerce.Platform.Core.Settings;

namespace VirtoCommerce.XOrder.Core
{
public static class ModuleConstants
{
public static class Settings
{
public static class General
{
public const string ShippingAddressPolicyDisabled = "Disabled";
public const string ShippingAddressPolicyPreviousOrder = "Previous Order Address";

public static SettingDescriptor ShippingAddressPolicy { get; } = new SettingDescriptor
{
Name = "XOrder.ShippingAddressPolicy",
ValueType = SettingValueType.ShortText,
GroupName = "Orders|General",
DefaultValue = ShippingAddressPolicyDisabled,
AllowedValues = new[] { ShippingAddressPolicyDisabled, ShippingAddressPolicyPreviousOrder }
};

public static IEnumerable<SettingDescriptor> AllSettings
{
get
{
yield return ShippingAddressPolicy;
}
}
}

public static IEnumerable<SettingDescriptor> StoreLevelSettings
{
get
{
yield return General.ShippingAddressPolicy;
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="8.0.0" />
<PackageReference Include="VirtoCommerce.Xapi.Core" Version="3.802.0" />
<PackageReference Include="VirtoCommerce.XCatalog.Core" Version="3.800.0" />
<PackageReference Include="VirtoCommerce.XCart.Core" Version="3.800.0" />
<PackageReference Include="VirtoCommerce.XCart.Core" Version="3.805.0-alpha.19-vcst-1698" />
<PackageReference Include="VirtoCommerce.OrdersModule.Core" Version="3.803.0" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ public static IServiceCollection AddXOrder(this IServiceCollection services, IGr
builder.AddMiddleware(typeof(EvalPromoContextOrderMiddleware));
});

services.AddPipeline<ShipmentContextCartMap>(builder =>
{
builder.AddMiddleware(typeof(ShipmentContextMiddleware));
});

return services;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using PipelineNet.Middleware;
using VirtoCommerce.CartModule.Core.Model;
using VirtoCommerce.OrdersModule.Core.Model.Search;
using VirtoCommerce.OrdersModule.Core.Services;
using VirtoCommerce.Platform.Core.Common;
using VirtoCommerce.Platform.Core.Settings;
using VirtoCommerce.XCart.Core.Models;
using static VirtoCommerce.CoreModule.Core.Common.AddressType;
using XOrderSetting = VirtoCommerce.XOrder.Core.ModuleConstants.Settings.General;

namespace VirtoCommerce.XOrder.Data.Middlewares
{
public class ShipmentContextMiddleware : IAsyncMiddleware<ShipmentContextCartMap>
{
private readonly ICustomerOrderSearchService _customerOrderSearchService;

public ShipmentContextMiddleware(ICustomerOrderSearchService customerOrderSearchService)
{
_customerOrderSearchService = customerOrderSearchService;
}

public async Task Run(ShipmentContextCartMap parameter, Func<ShipmentContextCartMap, Task> next)
{
ArgumentNullException.ThrowIfNull(parameter);

await RunInternal(parameter);
await next(parameter);
}

protected virtual async Task RunInternal(ShipmentContextCartMap parameter)
{
var shipment = parameter.Shipment;
var shippingAddressPolicy = GetShippingPolicy(parameter);

if (shipment?.DeliveryAddress != null || !shippingAddressPolicy.EqualsInvariant(XOrderSetting.ShippingAddressPolicyPreviousOrder))
{
return;
}

var lastOrderCriteria = new CustomerOrderSearchCriteria
{
CustomerId = parameter.CartAggregate.Cart.CustomerId,
Take = 1
};

var lastOrderResult = await _customerOrderSearchService.SearchNoCloneAsync(lastOrderCriteria);
if (lastOrderResult.Results.Count == 0)
{
return;
}

var order = lastOrderResult.Results[0];
var address = order.Addresses?.FirstOrDefault(x => x.AddressType == BillingAndShipping || x.AddressType == Shipping);

if (address != null)
{
var cartShipmentAddress = CreateCartShipmentAddress(address);
parameter.Shipment.DeliveryAddress = cartShipmentAddress;
}
}

private static string GetShippingPolicy(ShipmentContextCartMap parameter)
{
return parameter.CartAggregate?.Store?.Settings?.GetValue<string>(XOrderSetting.ShippingAddressPolicy);
}

protected virtual Address CreateCartShipmentAddress(OrdersModule.Core.Model.Address address)
{
var cartShipmentAddress = AbstractTypeFactory<Address>.TryCreateInstance();

cartShipmentAddress.Key = null;
cartShipmentAddress.Name = address.Name;
cartShipmentAddress.City = address.City;
cartShipmentAddress.CountryCode = address.CountryCode;
cartShipmentAddress.CountryName = address.CountryName;
cartShipmentAddress.Phone = address.Phone;
cartShipmentAddress.PostalCode = address.PostalCode;
cartShipmentAddress.RegionId = address.RegionId;
cartShipmentAddress.RegionName = address.RegionName;
cartShipmentAddress.City = address.City;
cartShipmentAddress.Email = address.Email;
cartShipmentAddress.FirstName = address.FirstName;
cartShipmentAddress.LastName = address.LastName;
cartShipmentAddress.Line1 = address.Line1;
cartShipmentAddress.Line2 = address.Line2;
cartShipmentAddress.AddressType = address.AddressType;
cartShipmentAddress.Organization = address.Organization;
cartShipmentAddress.OuterId = address.OuterId;
cartShipmentAddress.Description = address.Description;

return cartShipmentAddress;
}
}
}
10 changes: 9 additions & 1 deletion src/VirtoCommerce.XOrder.Web/Module.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using VirtoCommerce.Platform.Core.Modularity;
using VirtoCommerce.Platform.Core.Settings;
using VirtoCommerce.StoreModule.Core.Model;
using VirtoCommerce.Xapi.Core.Infrastructure;
using VirtoCommerce.XOrder.Core;
using VirtoCommerce.XOrder.Data.Extensions;

namespace VirtoCommerce.XOrder.Web;
Expand All @@ -20,7 +23,12 @@ public void Initialize(IServiceCollection serviceCollection)

public void PostInitialize(IApplicationBuilder appBuilder)
{
// Nothing to do here
var serviceProvider = appBuilder.ApplicationServices;

// settings
var settingsRegistrar = serviceProvider.GetRequiredService<ISettingsRegistrar>();
settingsRegistrar.RegisterSettings(ModuleConstants.Settings.General.AllSettings, ModuleInfo.Id);
settingsRegistrar.RegisterSettingsForType(ModuleConstants.Settings.StoreLevelSettings, nameof(Store));
}

public void Uninstall()
Expand Down
Loading