Skip to content

Open-source eCommerce platform for C# and ASP.NET Core 10 with MongoDB. Build multi-store shops and multi-vendor marketplaces with B2B/B2C, REST API, plugins, Vue 3 and Docker.

feat(arch001): add RoutedOrderDataScope and register IAdminDataScope<Order>

KrzysztofPajak2e1d72e59e1d
diff --git a/src/Tests/Grand.Web.Admin.Tests/Controllers/RoutedOrderDataScopeTests.cs b/src/Tests/Grand.Web.Admin.Tests/Controllers/RoutedOrderDataScopeTests.cs
new file mode 100644
index 000000000..82a47c649
--- /dev/null
+++ b/src/Tests/Grand.Web.Admin.Tests/Controllers/RoutedOrderDataScopeTests.cs
@@ -0,0 +1,95 @@
+#nullable enable
+
+using Grand.Business.Core.Interfaces.Common.Directory;
+using Grand.Domain.Customers;
+using Grand.Domain.Orders;
+using Grand.Domain.Vendors;
+using Grand.Infrastructure;
+using Grand.Web.AdminShared.Services;
+using Microsoft.AspNetCore.Http;
+using Microsoft.VisualStudio.TestTools.UnitTesting;
+using Moq;
+
+namespace Grand.Web.Admin.Tests.Controllers;
+
+/// <summary>
+/// Covers the routing decision itself - which concrete IAdminDataScope&lt;Order&gt; the resolver
+/// delegates to for each area, and that an unrecognized or missing area fails closed rather than
+/// falling back to any concrete scope. Mirrors RoutedProductDataScopeTests: real concrete scopes
+/// built with mocked IContextAccessor/IGroupService dependencies, since AdminOrderDataScope/
+/// StoreOrderDataScope/VendorOrderDataScope have no virtual members for Moq to intercept.
+/// </summary>
+[TestClass]
+public class RoutedOrderDataScopeTests
+{
+ private const string StaffStoreId = "store-1";
+ private const string VendorId = "vendor-1";
+
+ private AdminOrderDataScope _adminScope = null!;
+ private StoreOrderDataScope _storeScope = null!;
+ private VendorOrderDataScope _vendorScope = null!;
+
+ [TestInitialize]
+ public void Setup()
+ {
+ var workContext = new Mock<IWorkContext>();
+ workContext.Setup(x => x.CurrentCustomer).Returns(new Customer { StaffStoreId = StaffStoreId });
+ workContext.Setup(x => x.CurrentVendor).Returns(new Vendor { Id = VendorId });
+ var contextAccessor = new Mock<IContextAccessor>();
+ contextAccessor.Setup(x => x.WorkContext).Returns(workContext.Object);
+
+ var groupService = new Mock<IGroupService>();
+ groupService.Setup(g => g.IsSalesManager(It.IsAny<Customer>())).ReturnsAsync(false);
+
+ _adminScope = new AdminOrderDataScope(contextAccessor.Object, groupService.Object);
+ _storeScope = new StoreOrderDataScope(contextAccessor.Object);
+ _vendorScope = new VendorOrderDataScope(contextAccessor.Object);
+ }
+
+ private RoutedOrderDataScope ResolverForArea(string? area)
+ {
+ var httpContext = new DefaultHttpContext();
+ if (area is not null) httpContext.Request.RouteValues["area"] = area;
+ var httpContextAccessor = new Mock<IHttpContextAccessor>();
+ httpContextAccessor.Setup(x => x.HttpContext).Returns(httpContext);
+ return new RoutedOrderDataScope(httpContextAccessor.Object, _adminScope, _storeScope, _vendorScope);
+ }
+
+ [TestMethod]
+ public void AdminArea_ResolvesToAdminScope()
+ {
+ var resolver = ResolverForArea("Admin");
+ Assert.AreEqual("Admin", resolver.ResourceKeyPrefix);
+ Assert.IsNull(resolver.DefaultStoreId);
+ Assert.IsNull(resolver.DefaultVendorId);
+ Assert.IsTrue(resolver.CanFeatureOnHomepage);
+ }
+
+ [TestMethod]
+ public void StoreArea_ResolvesToStoreScope()
+ {
+ var resolver = ResolverForArea("Store");
+ Assert.AreEqual(StaffStoreId, resolver.DefaultStoreId);
+ Assert.IsNull(resolver.DefaultVendorId);
+ }
+
+ [TestMethod]
+ public void VendorArea_ResolvesToVendorScope()
+ {
+ var resolver = ResolverForArea("Vendor");
+ Assert.AreEqual("Vendor", resolver.ResourceKeyPrefix);
+ Assert.AreEqual(VendorId, resolver.DefaultVendorId);
+ Assert.IsFalse(resolver.ShowStoreSelector);
+ Assert.IsFalse(resolver.CanFeatureOnHomepage);
+ }
+
+ [TestMethod]
+ public void UnrecognizedOrMissingArea_ThrowsFailClosed()
+ {
+ var resolver = ResolverForArea("Vue");
+ Assert.Throws<InvalidOperationException>(() => _ = resolver.ResourceKeyPrefix);
+
+ var resolverNoArea = ResolverForArea(null);
+ Assert.Throws<InvalidOperationException>(() => _ = resolverNoArea.ResourceKeyPrefix);
+ }
+}
diff --git a/src/Web/Grand.Web.AdminShared/Services/RoutedOrderDataScope.cs b/src/Web/Grand.Web.AdminShared/Services/RoutedOrderDataScope.cs
new file mode 100644
index 000000000..e3b148ba8
--- /dev/null
+++ b/src/Web/Grand.Web.AdminShared/Services/RoutedOrderDataScope.cs
@@ -0,0 +1,52 @@
+#nullable enable
+
+using Grand.Domain.Orders;
+using Grand.Web.AdminShared.Interfaces;
+using Microsoft.AspNetCore.Http;
+
+namespace Grand.Web.AdminShared.Services;
+
+/// <summary>
+/// Resolves the correct per-host <see cref="IAdminDataScope{Order}" /> implementation at
+/// request time, based on the current request's "area" route value — same fix and same reason
+/// as <see cref="RoutedProductDataScope"/>/<see cref="RoutedCategoryDataScope"/>/
+/// <see cref="RoutedCollectionDataScope"/>: <c>Grand.Web</c> (the combined host) loads all
+/// three hosts into one DI container, so a plain per-host registration would let whichever
+/// host's StartupApplication ran last win for every area in that process.
+///
+/// First 3-branch routed scope in ARCH-001 — every prior entity's Vendor branch either didn't
+/// exist (Category/Collection) or reused the same scope shape as Product's. Order genuinely
+/// needs all three.
+/// </summary>
+public class RoutedOrderDataScope(
+ IHttpContextAccessor httpContextAccessor,
+ AdminOrderDataScope adminScope,
+ StoreOrderDataScope storeScope,
+ VendorOrderDataScope vendorScope) : IAdminDataScope<Order>
+{
+ private IAdminDataScope<Order> Resolved
+ {
+ get
+ {
+ var area = httpContextAccessor.HttpContext?.Request.RouteValues["area"] as string;
+ return area switch {
+ "Admin" => adminScope,
+ "Store" => storeScope,
+ "Vendor" => vendorScope,
+ //fail closed: this object fronts store/vendor/Sales-Manager tenant isolation, so an
+ //unrecognized or missing area must never silently resolve to any concrete scope
+ _ => throw new InvalidOperationException(
+ $"RoutedOrderDataScope: unrecognized or missing area '{area}'.")
+ };
+ }
+ }
+
+ public Task<bool> HasAccess(Order entity) => Resolved.HasAccess(entity);
+ public IEnumerable<OrderItem> FilterOrderItems(IEnumerable<OrderItem> orderItems) =>
+ Resolved.FilterOrderItems(orderItems);
+ public string? DefaultStoreId => Resolved.DefaultStoreId;
+ public string ResourceKeyPrefix => Resolved.ResourceKeyPrefix;
+ public bool ShowStoreSelector => Resolved.ShowStoreSelector;
+ public string? DefaultVendorId => Resolved.DefaultVendorId;
+ public bool CanFeatureOnHomepage => Resolved.CanFeatureOnHomepage;
+}
diff --git a/src/Web/Grand.Web.AdminShared/Startup/StartupApplication.cs b/src/Web/Grand.Web.AdminShared/Startup/StartupApplication.cs
index bd84eb0ac..cd4355fcc 100644
--- a/src/Web/Grand.Web.AdminShared/Startup/StartupApplication.cs
+++ b/src/Web/Grand.Web.AdminShared/Startup/StartupApplication.cs
@@ -1,6 +1,7 @@
using elFinder.Net.AspNetCore.Extensions;
using elFinder.Net.Drivers.FileSystem.Extensions;
using Grand.Domain.Catalog;
+using Grand.Domain.Orders;
using Grand.Infrastructure;
using Grand.Web.AdminShared.Interfaces;
using Grand.Web.AdminShared.Services;
@@ -82,6 +83,13 @@ public class StartupApplication : IStartupApplication
services.AddScoped<GlobalAdminDataScope<Collection>>();
services.AddScoped<StoreAdminDataScope<Collection>>();
services.AddScoped<IAdminDataScope<Collection>, RoutedCollectionDataScope>();
+
+ // IAdminDataScope<Order>: three bespoke implementations, none reusing the generic Global/Store
+ // scopes — see AdminOrderDataScope/StoreOrderDataScope/VendorOrderDataScope doc comments.
+ services.AddScoped<AdminOrderDataScope>();
+ services.AddScoped<StoreOrderDataScope>();
+ services.AddScoped<VendorOrderDataScope>();
+ services.AddScoped<IAdminDataScope<Order>, RoutedOrderDataScope>();
}
public void Configure(WebApplication application, IWebHostEnvironment webHostEnvironment)