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
Keep every store with an available currency (#836)
Krzysztof Pajakac02b1648904
diff --git a/src/Tests/Grand.Web.Admin.Tests/Services/CurrencyViewModelServiceTests.cs b/src/Tests/Grand.Web.Admin.Tests/Services/CurrencyViewModelServiceTests.csindex 9e4caa617..e0504913a 100644--- a/src/Tests/Grand.Web.Admin.Tests/Services/CurrencyViewModelServiceTests.cs+++ b/src/Tests/Grand.Web.Admin.Tests/Services/CurrencyViewModelServiceTests.cs@@ -1,8 +1,11 @@using Grand.Business.Core.Interfaces.Common.Configuration;using Grand.Business.Core.Interfaces.Common.Directory;using Grand.Business.Core.Interfaces.Common.Localization;+using Grand.Business.Core.Interfaces.Common.Stores;using Grand.Domain.Directory;+using Grand.Domain.Stores;using Grand.Infrastructure.Caching;+using Grand.Web.AdminShared.Models.Directory;using Grand.Web.AdminShared.Services;using Microsoft.VisualStudio.TestTools.UnitTesting;using Moq;@@ -14,6 +17,7 @@ namespace Grand.Web.Admin.Tests.Services;public class CurrencyViewModelServiceTests{private Mock<ICurrencyService> _currencyServiceMock;+ private Mock<IStoreService> _storeServiceMock;private CurrencySettings _currencySettings;private Mock<ISettingService> _settingServiceMock;private Mock<ITranslationService> _translationServiceMock;@@ -24,6 +28,7 @@ public class CurrencyViewModelServiceTestspublic void Setup(){_currencyServiceMock = new Mock<ICurrencyService>();+ _storeServiceMock = new Mock<IStoreService>();_currencySettings = new CurrencySettings();_settingServiceMock = new Mock<ISettingService>();_translationServiceMock = new Mock<ITranslationService>();@@ -31,6 +36,7 @@ public class CurrencyViewModelServiceTests_service = new CurrencyViewModelService(_currencyServiceMock.Object,+ _storeServiceMock.Object,_currencySettings,_settingServiceMock.Object,_translationServiceMock.Object,@@ -90,6 +96,68 @@ public class CurrencyViewModelServiceTestsAssert.IsTrue(canProceed);}+ [TestMethod]+ public async Task ValidateCurrencyStoreMapping_PublishedAndNotLimited_CanProceed()+ {+ var (canProceed, message) = await _service.ValidateCurrencyStoreMapping(new Currency { Id = "currency-1" },+ new CurrencyModel { Published = true, Stores = [] });++ Assert.IsTrue(canProceed);+ Assert.AreEqual(string.Empty, message);+ _storeServiceMock.Verify(s => s.GetAllStores(), Times.Never);+ }++ [TestMethod]+ public async Task ValidateCurrencyStoreMapping_StoreLeftWithoutCurrency_CannotProceed()+ {+ _currencyServiceMock.Setup(c => c.GetAllCurrencies(It.IsAny<bool>(), It.IsAny<string>()))+ .ReturnsAsync(new List<Currency> {+ new() { Id = "currency-1" },+ new() { Id = "currency-2", LimitedToStores = true, Stores = ["store-1"] }+ });+ _storeServiceMock.Setup(s => s.GetAllStores())+ .ReturnsAsync(new List<Store> { new() { Id = "store-1" }, new() { Id = "store-2", Name = "Second" } });+ _translationServiceMock.Setup(t => t.GetResource("Admin.Configuration.Currencies.CantLimitStores"))+ .Returns("Store '{0}' has no currency");++ var (canProceed, message) = await _service.ValidateCurrencyStoreMapping(new Currency { Id = "currency-1" },+ new CurrencyModel { Published = true, Stores = ["store-1"] });++ Assert.IsFalse(canProceed);+ Assert.AreEqual("Store 'Second' has no currency", message);+ }++ [TestMethod]+ public async Task ValidateCurrencyStoreMapping_AnotherGlobalCurrencyExists_CanProceed()+ {+ _currencyServiceMock.Setup(c => c.GetAllCurrencies(It.IsAny<bool>(), It.IsAny<string>()))+ .ReturnsAsync(new List<Currency> { new() { Id = "currency-1" }, new() { Id = "currency-2" } });+ _storeServiceMock.Setup(s => s.GetAllStores())+ .ReturnsAsync(new List<Store> { new() { Id = "store-1" }, new() { Id = "store-2" } });++ var (canProceed, _) = await _service.ValidateCurrencyStoreMapping(new Currency { Id = "currency-1" },+ new CurrencyModel { Published = true, Stores = ["store-1"] });++ Assert.IsTrue(canProceed);+ }++ [TestMethod]+ public async Task ValidateCurrencyStoreMapping_UnpublishingLastCurrencyOfStore_CannotProceed()+ {+ _currencyServiceMock.Setup(c => c.GetAllCurrencies(It.IsAny<bool>(), It.IsAny<string>()))+ .ReturnsAsync(new List<Currency> { new() { Id = "currency-1" } });+ _storeServiceMock.Setup(s => s.GetAllStores())+ .ReturnsAsync(new List<Store> { new() { Id = "store-1", Name = "First" } });+ _translationServiceMock.Setup(t => t.GetResource("Admin.Configuration.Currencies.CantLimitStores"))+ .Returns("Store '{0}' has no currency");++ var (canProceed, message) = await _service.ValidateCurrencyStoreMapping(new Currency { Id = "currency-1" },+ new CurrencyModel { Published = false, Stores = [] });++ Assert.IsFalse(canProceed);+ Assert.AreEqual("Store 'First' has no currency", message);+ }+[TestMethod]public async Task ValidateCurrencyDelete_PrimaryStoreCurrency_CannotDelete(){diff --git a/src/Tests/Grand.Web.Store.Tests/Controllers/CurrencyControllerTests.cs b/src/Tests/Grand.Web.Store.Tests/Controllers/CurrencyControllerTests.csnew file mode 100644index 000000000..6850d3847--- /dev/null+++ b/src/Tests/Grand.Web.Store.Tests/Controllers/CurrencyControllerTests.cs@@ -0,0 +1,94 @@+using Grand.Business.Core.Interfaces.Common.Directory;+using Grand.Business.Core.Interfaces.Common.Localization;+using Grand.Business.Core.Interfaces.Common.Stores;+using Grand.Domain.Customers;+using Grand.Domain.Directory;+using Grand.Domain.Stores;+using Grand.Infrastructure;+using Grand.Web.Store.Controllers;+using Microsoft.AspNetCore.Http;+using Microsoft.AspNetCore.Mvc;+using Microsoft.AspNetCore.Mvc.ViewFeatures;+using Microsoft.VisualStudio.TestTools.UnitTesting;+using Moq;+using DomainStore = Grand.Domain.Stores.Store;++namespace Grand.Web.Store.Tests.Controllers;++[TestClass]+public class CurrencyControllerTests+{+ private const string StoreId = "storeId";++ private CurrencyController _controller;+ private Mock<ICurrencyService> _currencyServiceMock;+ private Mock<IStoreService> _storeServiceMock;+ private Mock<ITranslationService> _translationServiceMock;+ private CurrencySettings _currencySettings;++ [TestInitialize]+ public void Setup()+ {+ _currencyServiceMock = new Mock<ICurrencyService>();+ _storeServiceMock = new Mock<IStoreService>();+ _translationServiceMock = new Mock<ITranslationService>();+ _translationServiceMock.Setup(t => t.GetResource(It.IsAny<string>())).Returns<string>(x => x);+ _currencySettings = new CurrencySettings();++ var workContextMock = new Mock<IWorkContext>();+ workContextMock.Setup(w => w.CurrentCustomer).Returns(new Customer { StaffStoreId = StoreId });+ var contextAccessorMock = new Mock<IContextAccessor>();+ contextAccessorMock.Setup(c => c.WorkContext).Returns(workContextMock.Object);++ _controller = new CurrencyController(+ _currencyServiceMock.Object,+ _currencySettings,+ _translationServiceMock.Object,+ _storeServiceMock.Object,+ contextAccessorMock.Object);++ var httpContext = new DefaultHttpContext();+ _controller.ControllerContext = new ControllerContext { HttpContext = httpContext };+ _controller.TempData = new TempDataDictionary(httpContext, new Mock<ITempDataProvider>().Object);+ }++ private static Currency StoreCurrency(string id)+ {+ return new Currency { Id = id, Published = true, LimitedToStores = true, Stores = [StoreId] };+ }++ [TestMethod]+ public async Task UnassignStore_LastAvailableCurrency_IsRejected()+ {+ var currency = StoreCurrency("currency-1");+ _currencyServiceMock.Setup(c => c.GetCurrencyById(currency.Id)).ReturnsAsync(currency);+ _storeServiceMock.Setup(s => s.GetStoreById(StoreId)).ReturnsAsync(new DomainStore { Id = StoreId });+ _currencyServiceMock.Setup(c => c.GetAllCurrencies(It.IsAny<bool>(), StoreId))+ .ReturnsAsync(new List<Currency> { currency });++ var result = await _controller.UnassignStore(currency.Id) as JsonResult;++ Assert.IsNotNull(result);+ Assert.AreEqual("Admin.Configuration.Currencies.CantUnassignLast",+ result.Value.GetType().GetProperty("message")!.GetValue(result.Value));+ Assert.IsTrue(currency.Stores.Contains(StoreId));+ _currencyServiceMock.Verify(c => c.UpdateCurrency(It.IsAny<Currency>()), Times.Never);+ }++ [TestMethod]+ public async Task UnassignStore_AnotherCurrencyRemains_IsUnassigned()+ {+ var currency = StoreCurrency("currency-1");+ _currencyServiceMock.Setup(c => c.GetCurrencyById(currency.Id)).ReturnsAsync(currency);+ _storeServiceMock.Setup(s => s.GetStoreById(StoreId)).ReturnsAsync(new DomainStore { Id = StoreId });+ _currencyServiceMock.Setup(c => c.GetAllCurrencies(It.IsAny<bool>(), StoreId))+ .ReturnsAsync(new List<Currency> { currency, StoreCurrency("currency-2") });++ var result = await _controller.UnassignStore(currency.Id) as JsonResult;++ Assert.IsNotNull(result);+ Assert.IsTrue((bool)result.Value.GetType().GetProperty("success")!.GetValue(result.Value)!);+ Assert.IsFalse(currency.Stores.Contains(StoreId));+ _currencyServiceMock.Verify(c => c.UpdateCurrency(currency), Times.Once);+ }+}diff --git a/src/Web/Grand.Web.Admin/Controllers/CurrencyController.cs b/src/Web/Grand.Web.Admin/Controllers/CurrencyController.csindex f6630714b..ac62df16c 100644--- a/src/Web/Grand.Web.Admin/Controllers/CurrencyController.cs+++ b/src/Web/Grand.Web.Admin/Controllers/CurrencyController.cs@@ -225,6 +225,15 @@ public class CurrencyController : BaseAdminControllerreturn RedirectToAction("Edit", new { id = currency.Id });}+ //ensure no store is left without an available currency+ var (canMap, mappingMessage) =+ await _currencyViewModelService.ValidateCurrencyStoreMapping(currency, model);+ if (!canMap)+ {+ Error(mappingMessage);+ return RedirectToAction("Edit", new { id = currency.Id });+ }+currency = await _currencyViewModelService.UpdateCurrencyModel(currency, model);Success(_translationService.GetResource("Admin.Configuration.Currencies.Updated"));if (continueEditing)diff --git a/src/Web/Grand.Web.AdminShared/Interfaces/ICurrencyViewModelService.cs b/src/Web/Grand.Web.AdminShared/Interfaces/ICurrencyViewModelService.csindex 4cabc6706..3fadf012a 100644--- a/src/Web/Grand.Web.AdminShared/Interfaces/ICurrencyViewModelService.cs+++ b/src/Web/Grand.Web.AdminShared/Interfaces/ICurrencyViewModelService.cs@@ -11,5 +11,6 @@ public interface ICurrencyViewModelServiceTask MarkAsPrimaryExchangeRateCurrency(string id);Task MarkAsPrimaryStoreCurrency(string id);Task<(bool canProceed, string message)> ValidateCurrencyUnpublish(string currencyId, bool published);+ Task<(bool canProceed, string message)> ValidateCurrencyStoreMapping(Currency currency, CurrencyModel model);Task<(bool canDelete, string message)> ValidateCurrencyDelete(Currency currency);}\ No newline at end of filediff --git a/src/Web/Grand.Web.AdminShared/Services/CurrencyViewModelService.cs b/src/Web/Grand.Web.AdminShared/Services/CurrencyViewModelService.csindex d33f6092d..9e9c5f2bc 100644--- a/src/Web/Grand.Web.AdminShared/Services/CurrencyViewModelService.cs+++ b/src/Web/Grand.Web.AdminShared/Services/CurrencyViewModelService.cs@@ -1,6 +1,7 @@using Grand.Business.Core.Interfaces.Common.Configuration;using Grand.Business.Core.Interfaces.Common.Directory;using Grand.Business.Core.Interfaces.Common.Localization;+using Grand.Business.Core.Interfaces.Common.Stores;using Grand.Domain.Directory;using Grand.Infrastructure.Caching;using Grand.Web.AdminShared.Extensions.Mapping;@@ -14,6 +15,7 @@ public class CurrencyViewModelService : ICurrencyViewModelService#region Fieldsprivate readonly ICurrencyService _currencyService;+ private readonly IStoreService _storeService;private readonly CurrencySettings _currencySettings;private readonly ISettingService _settingService;private readonly ITranslationService _translationService;@@ -24,12 +26,14 @@ public class CurrencyViewModelService : ICurrencyViewModelService#region Constructorspublic CurrencyViewModelService(ICurrencyService currencyService,+ IStoreService storeService,CurrencySettings currencySettings,ISettingService settingService,ITranslationService translationService,ICacheBase cacheBase){_currencyService = currencyService;+ _storeService = storeService;_currencySettings = currencySettings;_settingService = settingService;_translationService = translationService;@@ -90,6 +94,33 @@ public class CurrencyViewModelService : ICurrencyViewModelServicereturn (true, string.Empty);}+ public virtual async Task<(bool canProceed, string message)> ValidateCurrencyStoreMapping(Currency currency,+ CurrencyModel model)+ {+ var limitedToStores = model.Stores is { Length: > 0 };++ //a published currency available to every store can never leave a store without one+ if (model.Published && !limitedToStores)+ return (true, string.Empty);++ var otherCurrencies = (await _currencyService.GetAllCurrencies())+ .Where(c => c.Id != currency.Id).ToList();++ foreach (var store in await _storeService.GetAllStores())+ {+ if (otherCurrencies.Any(c => !c.LimitedToStores || c.Stores.Contains(store.Id)))+ continue;++ if (model.Published && (!limitedToStores || model.Stores.Contains(store.Id)))+ continue;++ return (false, string.Format(+ _translationService.GetResource("Admin.Configuration.Currencies.CantLimitStores"), store.Name));+ }++ return (true, string.Empty);+ }+public virtual async Task<(bool canDelete, string message)> ValidateCurrencyDelete(Currency currency){if (currency.Id == _currencySettings.PrimaryStoreCurrencyId)diff --git a/src/Web/Grand.Web.Common/WorkContextSetter.cs b/src/Web/Grand.Web.Common/WorkContextSetter.csindex 805ace465..7c32b24a1 100644--- a/src/Web/Grand.Web.Common/WorkContextSetter.cs+++ b/src/Web/Grand.Web.Common/WorkContextSetter.cs@@ -397,6 +397,12 @@ public class WorkContextSetter : IWorkContextSettervar allStoreCurrencies = await _currencyService.GetAllCurrencies(storeId: store.Id);+ //no published currency is mapped to the store - fall back to the primary store currency+ //rather than failing every request for that store+ if (!allStoreCurrencies.Any())+ return await _currencyService.GetPrimaryStoreCurrency() ??+ throw new Exception("No currency could be loaded");+if (allStoreCurrencies.Count == 1)return allStoreCurrencies.FirstOrDefault();diff --git a/src/Web/Grand.Web.Store/Controllers/CurrencyController.cs b/src/Web/Grand.Web.Store/Controllers/CurrencyController.csindex 3004601f1..494e23b13 100644--- a/src/Web/Grand.Web.Store/Controllers/CurrencyController.cs+++ b/src/Web/Grand.Web.Store/Controllers/CurrencyController.cs@@ -108,6 +108,11 @@ public class CurrencyController(if (store?.DefaultCurrencyId == currency.Id)return Json(new { success = false, message = translationService.GetResource("Admin.Configuration.Currencies.CantUnassignDefault") });+ //the store must keep at least one available currency, otherwise its work context cannot be built+ var storeCurrencies = await currencyService.GetAllCurrencies(storeId: storeId);+ if (!storeCurrencies.Any(c => c.Id != currency.Id))+ return Json(new { success = false, message = translationService.GetResource("Admin.Configuration.Currencies.CantUnassignLast") });+if (currency.Stores.Remove(storeId))await currencyService.UpdateCurrency(currency);diff --git a/src/Web/Grand.Web/App_Data/Resources/DefaultLanguage.xml b/src/Web/Grand.Web/App_Data/Resources/DefaultLanguage.xmlindex 09de03d11..38ceab54f 100644Binary files a/src/Web/Grand.Web/App_Data/Resources/DefaultLanguage.xml and b/src/Web/Grand.Web/App_Data/Resources/DefaultLanguage.xml differdiff --git a/src/Web/Grand.Web/App_Data/Resources/Upgrade/en_240.xml b/src/Web/Grand.Web/App_Data/Resources/Upgrade/en_240.xmlindex 9f51c3550..175f8def6 100644--- a/src/Web/Grand.Web/App_Data/Resources/Upgrade/en_240.xml+++ b/src/Web/Grand.Web/App_Data/Resources/Upgrade/en_240.xml@@ -81,9 +81,15 @@<Resource Name="admin.configuration.currencies.cannotmodifyglobal" Area="Admin"><Value>Global currencies cannot be modified by store owners.</Value></Resource>+ <Resource Name="admin.configuration.currencies.cantlimitstores" Area="Admin">+ <Value>The store '{0}' would be left without an available currency.</Value>+ </Resource><Resource Name="admin.configuration.currencies.cantunassigndefault" Area="Admin"><Value>The default store currency can't be unassigned.</Value></Resource>+ <Resource Name="admin.configuration.currencies.cantunassignlast" Area="Admin">+ <Value>At least one currency must remain available for your store.</Value>+ </Resource><Resource Name="admin.configuration.currencies.notassignedtostore" Area="Admin"><Value>This currency is not assigned to your store.</Value></Resource>