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.
C# aspnet-corecsharpdockerdotnetecommerceecommerce-platformmongodbmulti-storemulti-vendornosqlonline-shopshoppingcart
Add VendorProductDataScope for the Vendor host (ARCH-001 Phase 1)
KrzysztofPajak3a5e4a78ca88
diff --git a/src/Tests/Grand.Web.Vendor.Tests/Services/VendorProductDataScopeTests.cs b/src/Tests/Grand.Web.Vendor.Tests/Services/VendorProductDataScopeTests.csnew file mode 100644index 000000000..cbbd94866--- /dev/null+++ b/src/Tests/Grand.Web.Vendor.Tests/Services/VendorProductDataScopeTests.cs@@ -0,0 +1,76 @@+using Grand.Domain.Catalog;+using Grand.Domain.Customers;+using Grand.Domain.Vendors;+using Grand.Infrastructure;+using Grand.Web.AdminShared.Services;+using Microsoft.VisualStudio.TestTools.UnitTesting;+using Moq;++namespace Grand.Web.Vendor.Tests.Services;++[TestClass]+public class VendorProductDataScopeTests+{+ private Mock<IContextAccessor> _contextAccessor = null!;+ private const string VendorId = "vendor-1";++ [TestInitialize]+ public void Setup()+ {+ var workContext = new Mock<IWorkContext>();+ workContext.Setup(x => x.CurrentVendor).Returns(new Domain.Vendors.Vendor { Id = VendorId });+ _contextAccessor = new Mock<IContextAccessor>();+ _contextAccessor.Setup(x => x.WorkContext).Returns(workContext.Object);+ }++ [TestMethod]+ public async Task HasAccess_OwnProduct_ReturnsTrue()+ {+ var scope = new VendorProductDataScope(_contextAccessor.Object);+ Assert.IsTrue(await scope.HasAccess(new Product { VendorId = VendorId }));+ }++ [TestMethod]+ public async Task HasAccess_OtherVendorsProduct_ReturnsFalse()+ {+ var scope = new VendorProductDataScope(_contextAccessor.Object);+ Assert.IsFalse(await scope.HasAccess(new Product { VendorId = "vendor-2" }));+ }++ [TestMethod]+ public async Task HasAccess_NullProduct_ReturnsFalse()+ {+ var scope = new VendorProductDataScope(_contextAccessor.Object);+ Assert.IsFalse(await scope.HasAccess(null!));+ }++ [TestMethod]+ public void ApplyScope_FiltersToOwnVendorId()+ {+ var scope = new VendorProductDataScope(_contextAccessor.Object);+ var query = new[]+ {+ new Product { Id = "1", VendorId = VendorId },+ new Product { Id = "2", VendorId = "vendor-2" }+ }.AsQueryable();++ var result = scope.ApplyScope(query).ToList();++ Assert.AreEqual(1, result.Count);+ Assert.AreEqual("1", result[0].Id);+ }++ [TestMethod]+ public void DefaultStoreId_IsNull()+ {+ var scope = new VendorProductDataScope(_contextAccessor.Object);+ Assert.IsNull(scope.DefaultStoreId);+ }++ [TestMethod]+ public void ResourceKeyPrefix_IsVendor()+ {+ var scope = new VendorProductDataScope(_contextAccessor.Object);+ Assert.AreEqual("Vendor", scope.ResourceKeyPrefix);+ }+}diff --git a/src/Web/Grand.Web.AdminShared/Services/VendorProductDataScope.cs b/src/Web/Grand.Web.AdminShared/Services/VendorProductDataScope.csnew file mode 100644index 000000000..86e7cce3c--- /dev/null+++ b/src/Web/Grand.Web.AdminShared/Services/VendorProductDataScope.cs@@ -0,0 +1,26 @@+#nullable enable++using Grand.Domain.Catalog;+using Grand.Infrastructure;+using Grand.Web.AdminShared.Interfaces;++namespace Grand.Web.AdminShared.Services;++public class VendorProductDataScope(IContextAccessor contextAccessor) : IAdminDataScope<Product>+{+ public Task<bool> HasAccess(Product entity)+ {+ if (entity is null) return Task.FromResult(false);+ return Task.FromResult(entity.VendorId == contextAccessor.WorkContext.CurrentVendor.Id);+ }++ public IQueryable<Product> ApplyScope(IQueryable<Product> query)+ {+ var vendorId = contextAccessor.WorkContext.CurrentVendor.Id;+ return query.Where(x => x.VendorId == vendorId);+ }++ public string? DefaultStoreId => null;++ public string ResourceKeyPrefix => "Vendor";+}