-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
OCC-181: Inventory warnings when listing content items in admin dashb…
…oard (#338) * Create extension methods for error message checking. * Add extension documentation. * Add PriceEstimationWithMinimumOrderQuantityShouldNotShowWarning test (TDD). * Create an abstraction to update the context used for product price estimation. * Remove copy-paste error. * Add Feature ID for "Orchard Core Commerce - Inventory". * Add inventory product estimation context updater. * Typo
- Loading branch information
1 parent
4e6beaa
commit 75102d3
Showing
14 changed files
with
169 additions
and
23 deletions.
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
src/Modules/OrchardCore.Commerce.Inventory/Constants/FeatureIds.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,7 @@ | ||
namespace OrchardCore.Commerce.Inventory.Constants; | ||
|
||
public static class FeatureIds | ||
{ | ||
public const string Area = "OrchardCore.Commerce.Inventory"; | ||
public const string Inventory = Area; | ||
} |
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
11 changes: 11 additions & 0 deletions
11
src/Modules/OrchardCore.Commerce/Abstractions/IProductEstimationContextUpdater.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,11 @@ | ||
using OrchardCore.Commerce.Models; | ||
|
||
namespace OrchardCore.Commerce.Abstractions; | ||
|
||
/// <summary> | ||
/// A service that updates the <see cref="ProductEstimationContext"/> used by the <see | ||
/// cref="IShoppingCartHelpers.EstimateProductAsync"/> method. | ||
/// </summary> | ||
public interface IProductEstimationContextUpdater : ISortableUpdaterProvider<ProductEstimationContext> | ||
{ | ||
} |
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
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
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
9 changes: 9 additions & 0 deletions
9
src/Modules/OrchardCore.Commerce/Models/ProductEstimationContext.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 OrchardCore.Commerce.AddressDataType; | ||
|
||
namespace OrchardCore.Commerce.Models; | ||
|
||
public record ProductEstimationContext( | ||
string ShoppingCartId, | ||
ShoppingCartItem ShoppingCartItem, | ||
Address ShippingAddress, | ||
Address BillingAddress); |
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
42 changes: 42 additions & 0 deletions
42
src/Modules/OrchardCore.Commerce/Settings/InventoryProductEstimationContextUpdater.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,42 @@ | ||
using OrchardCore.Commerce.Abstractions; | ||
using OrchardCore.Commerce.Inventory.Models; | ||
using OrchardCore.Commerce.Models; | ||
using OrchardCore.ContentManagement; | ||
using System.Threading.Tasks; | ||
|
||
namespace OrchardCore.Commerce.Settings; | ||
|
||
public class InventoryProductEstimationContextUpdater : IProductEstimationContextUpdater | ||
{ | ||
private readonly IProductService _productService; | ||
private readonly IShoppingCartPersistence _shoppingCartPersistence; | ||
public int Order => 0; | ||
|
||
public InventoryProductEstimationContextUpdater( | ||
IProductService productService, | ||
IShoppingCartPersistence shoppingCartPersistence) | ||
{ | ||
_productService = productService; | ||
_shoppingCartPersistence = shoppingCartPersistence; | ||
} | ||
|
||
public async Task<ProductEstimationContext> UpdateAsync(ProductEstimationContext model) | ||
{ | ||
var product = await _productService.GetProductAsync(model.ShoppingCartItem.ProductSku); | ||
if (product.As<InventoryPart>() is not { } inventory) return model; | ||
|
||
var cart = await _shoppingCartPersistence.RetrieveAsync(model.ShoppingCartId); | ||
var item = cart.AddItem(model.ShoppingCartItem.WithQuantity(0)); | ||
var newQuantity = item.Quantity + model.ShoppingCartItem.Quantity; | ||
|
||
var minimum = inventory.MinimumOrderQuantity.Value is { } minimumDecimal ? (int)minimumDecimal : int.MinValue; | ||
if (newQuantity < minimum) | ||
{ | ||
model = model with { ShoppingCartItem = model.ShoppingCartItem.WithQuantity(minimum - item.Quantity) }; | ||
} | ||
|
||
return model; | ||
} | ||
|
||
public Task<bool> IsApplicableAsync(ProductEstimationContext model) => Task.FromResult(true); | ||
} |
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
29 changes: 29 additions & 0 deletions
29
test/OrchardCore.Commerce.Tests.UI/Extension/NotificationUITestContextExtensions.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,29 @@ | ||
using Atata; | ||
using Lombiq.Tests.UI.Extensions; | ||
using OpenQA.Selenium; | ||
using Shouldly; | ||
|
||
namespace Lombiq.Tests.UI.Services; | ||
|
||
public static class NotificationUITestContextExtensions | ||
{ | ||
/// <summary> | ||
/// Returns the text of the element with the <c>message-error</c> class if one exists. | ||
/// </summary> | ||
/// <param name="safely"> | ||
/// If the element is found then this doesn't matter. Otherwise if it's <see langword="true"/> then <see | ||
/// langword="null"/> is returned and if it's <see langword="false"/> an exception is thrown. | ||
/// </param> | ||
public static string GetErrorMessage(this UITestContext context, bool safely = false) | ||
{ | ||
var by = By.ClassName("message-error").Safely(safely); | ||
return context.Get(by)?.Text?.Trim(); | ||
} | ||
|
||
/// <summary> | ||
/// Looks for the element with the <c>message-error</c> class, it shouldn't exist or its content should be empty. If | ||
/// that's not true an exception will be thrown containing the element text. | ||
/// </summary> | ||
public static void ErrorMessageShouldNotExist(this UITestContext context) => | ||
context.GetErrorMessage(safely: true).ShouldBeNullOrEmpty(); | ||
} |
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
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
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