Skip to content

Commit

Permalink
Merge branch 'grandnode:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikhil13x authored Jan 1, 2022
2 parents 843e233 + 1aee3a1 commit a4cb5b1
Show file tree
Hide file tree
Showing 11 changed files with 75 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ public partial class ShippingService : IShippingService
{
#region Fields

private readonly IWarehouseService _warehouseService;
private readonly ILogger _logger;
private readonly ITranslationService _translationService;
private readonly ICountryService _countryService;
Expand All @@ -41,15 +40,13 @@ public partial class ShippingService : IShippingService
/// Ctor
/// </summary>
public ShippingService(
IWarehouseService warehouseService,
ILogger logger,
ITranslationService translationService,
ICountryService countryService,
IEnumerable<IShippingRateCalculationProvider> shippingRateCalculationProvider,
ShippingProviderSettings shippingProviderSettings,
ShippingSettings shippingSettings)
{
_warehouseService = warehouseService;
_logger = logger;
_translationService = translationService;
_countryService = countryService;
Expand Down
6 changes: 3 additions & 3 deletions src/Core/Grand.SharedKernel/Extensions/CommonHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,10 @@ public static bool IsValidEmail(string email)
/// <returns>Result string</returns>
public static string GenerateRandomDigitCode(int length)
{
string str = string.Empty;
RNGCryptoServiceProvider provider = new RNGCryptoServiceProvider();
var str = string.Empty;
using var rng = RandomNumberGenerator.Create();
var byteArray = new byte[length];
provider.GetBytes(byteArray);
rng.GetBytes(byteArray);
for (var i = 0; i < length; i++)
str = string.Concat(str, byteArray[i].ToString());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ namespace Grand.Business.Checkout.Tests.Services.Shipping
[TestClass]
public class ShippingServiceTests
{
private Mock<IWarehouseService> _warehouseMock;
private Mock<ILogger> _loggerMock;
private Mock<ITranslationService> _translationServiceMock;
private Mock<ICountryService> _countryServiceMokc;
Expand All @@ -31,14 +30,13 @@ public class ShippingServiceTests
[TestInitialize]
public void Init()
{
_warehouseMock = new Mock<IWarehouseService>();
_loggerMock = new Mock<ILogger>();
_translationServiceMock = new Mock<ITranslationService>();
_countryServiceMokc = new Mock<ICountryService>();
_shippingProviderSettings = new ShippingProviderSettings();
_shippingSettings = new ShippingSettings();
_rateProviderMock = new Mock<IShippingRateCalculationProvider>();
_service = new ShippingService(_warehouseMock.Object, _loggerMock.Object, _translationServiceMock.Object, _countryServiceMokc.Object,
_service = new ShippingService(_loggerMock.Object, _translationServiceMock.Object, _countryServiceMokc.Object,
new List<IShippingRateCalculationProvider>() { _rateProviderMock.Object }, _shippingProviderSettings, _shippingSettings);
}

Expand Down Expand Up @@ -92,8 +90,7 @@ public async Task CreateShippingOptionRequests_ReturnExpectedResults()
var warehouse = new Warehouse() {
Address = null
};
_warehouseMock.Setup(c => c.GetWarehouseById(It.IsAny<string>())).ReturnsAsync(warehouse);


var result = await _service.CreateShippingOptionRequests(customer, cart, shippingAddress, store);

Assert.AreEqual(result.ShippingAddress, shippingAddress);
Expand All @@ -119,7 +116,6 @@ public async Task CreateShippingOptionRequests_ShipNotEnable_ReturnEmptyList()
var warehouse = new Warehouse() {
Address = null
};
_warehouseMock.Setup(c => c.GetWarehouseById(It.IsAny<string>())).ReturnsAsync(warehouse);

var result = await _service.CreateShippingOptionRequests(customer, cart, shippingAddress, store);
Assert.IsTrue(result != null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@
@Loc["Admin.Configuration.Payment.Methods"]
</div>
</div>
<div class="x_content">
<div class="form-horizontal">
<div class="form-body">
@await Component.InvokeAsync("StoreScope")
</div>
</div>
</div>
<div class="x_content form">
<vc:admin-widget widget-zone="payment_method_list_top" additional-data="Model" />
<div id="paymentmethods-grid"></div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@
</div>
<vc:admin-widget widget-zone="payment_settings_buttons" additional-data="null" />
</div>
<div class="x_content">
<div class="form-horizontal">
<div class="form-body">
@await Component.InvokeAsync("StoreScope")
</div>
</div>
</div>
<div class="x_content form">
<vc:admin-widget widget-zone="payment_settings_top" additional-data="null" />
<div class="form-horizontal">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@
</div>
<vc:admin-widget widget-zone="shipping_provider_list_buttons" additional-data="null" />
</div>
<div class="x_content">
<div class="form-horizontal">
<div class="form-body">
@await Component.InvokeAsync("StoreScope")
</div>
</div>
</div>
<div class="x_content form">
<div class="form-horizontal">
<div class="form-body">
Expand Down
14 changes: 8 additions & 6 deletions src/Web/Grand.Web.Admin/Components/StoreScope.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
using System.Threading.Tasks;
using Grand.Business.Common.Interfaces.Directory;
using System.Linq;
using System.Collections.Generic;
using Grand.Domain.Stores;

namespace Grand.Web.Admin.Components
{
Expand Down Expand Up @@ -52,22 +54,22 @@ public async Task<IViewComponentResult> InvokeAsync()
Name = s.Shortcut
});
}
model.StoreId = await GetActiveStore(_storeService, _workContext);
model.StoreId = await GetActiveStore(allStores);
return View(model);
}

#endregion

#region Methods

private async Task<string> GetActiveStore(IStoreService storeService, IWorkContext workContext)
private async Task<string> GetActiveStore(IList<Store> stores)
{
//ensure that we have 2 (or more) stores
if ((await storeService.GetAllStores()).Count < 2)
return string.Empty;
if (stores.Count < 2)
return stores.FirstOrDefault().Id;

var storeId = workContext.CurrentCustomer.GetUserFieldFromEntity<string>(SystemCustomerFieldNames.AdminAreaStoreScopeConfiguration);
var store = await storeService.GetStoreById(storeId);
var storeId = _workContext.CurrentCustomer.GetUserFieldFromEntity<string>(SystemCustomerFieldNames.AdminAreaStoreScopeConfiguration);
var store = await _storeService.GetStoreById(storeId);

return store != null ? store.Id : "";
}
Expand Down
19 changes: 15 additions & 4 deletions src/Web/Grand.Web.Admin/Controllers/BaseAdminController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Grand.Business.Common.Interfaces.Directory;

namespace Grand.Web.Admin.Controllers
{
Expand Down Expand Up @@ -66,15 +67,25 @@ protected virtual async Task<string> GetActiveStore()
{
var storeService = HttpContext.RequestServices.GetRequiredService<IStoreService>();
var workContext = HttpContext.RequestServices.GetRequiredService<IWorkContext>();
var groupService = HttpContext.RequestServices.GetRequiredService<IGroupService>();

var stores = await storeService.GetAllStores();
if (stores.Count < 2)
return stores.FirstOrDefault().Id;

var storeId = workContext.CurrentCustomer.GetUserFieldFromEntity<string>(SystemCustomerFieldNames.AdminAreaStoreScopeConfiguration);
var store = await storeService.GetStoreById(storeId);
if (await groupService.IsStaff(workContext.CurrentCustomer))
{
return workContext.CurrentCustomer.StaffStoreId;
}

return store != null ? store.Id : workContext.CurrentStore.Id;
var storeId = workContext.CurrentCustomer.GetUserFieldFromEntity<string>(SystemCustomerFieldNames.AdminAreaStoreScopeConfiguration);
if(!string.IsNullOrEmpty(storeId))
{
var store = await storeService.GetStoreById(storeId);
if (store != null)
return store.Id;
}
return stores.FirstOrDefault().Id;
}
/// <summary>
/// Creates a <see cref="T:System.Web.Mvc.JsonResult"/> object that serializes the specified object to JavaScript Object Notation (JSON) format using the content type, content encoding, and the JSON request behavior.
Expand All @@ -90,7 +101,7 @@ protected virtual async Task<string> GetActiveStore()
public override JsonResult Json(object data)
{
var serializerSettings = new JsonSerializerSettings {
DateFormatHandling = DateFormatHandling.IsoDateFormat
DateFormatHandling = DateFormatHandling.IsoDateFormat
};
return base.Json(data, serializerSettings);
}
Expand Down
28 changes: 16 additions & 12 deletions src/Web/Grand.Web.Admin/Controllers/PaymentController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ public partial class PaymentController : BaseAdminController
private readonly IPaymentService _paymentService;
private readonly ISettingService _settingService;
private readonly ICountryService _countryService;
private readonly IGroupService _groupService;
private readonly IShippingMethodService _shippingMethodService;
private readonly ITranslationService _translationService;
private readonly IServiceProvider _serviceProvider;
Expand All @@ -43,7 +42,6 @@ public partial class PaymentController : BaseAdminController
public PaymentController(IPaymentService paymentService,
ISettingService settingService,
ICountryService countryService,
IGroupService groupService,
IShippingMethodService shippingMethodService,
ITranslationService translationService,
IServiceProvider serviceProvider,
Expand All @@ -52,7 +50,6 @@ public PaymentController(IPaymentService paymentService,
_paymentService = paymentService;
_settingService = settingService;
_countryService = countryService;
_groupService = groupService;
_shippingMethodService = shippingMethodService;
_translationService = translationService;
_serviceProvider = serviceProvider;
Expand All @@ -68,7 +65,9 @@ public PaymentController(IPaymentService paymentService,
[HttpPost]
public async Task<IActionResult> Methods()
{
var _paymentSettings = _settingService.LoadSetting<PaymentSettings>();
var storeScope = await GetActiveStore();

var _paymentSettings = _settingService.LoadSetting<PaymentSettings>(storeScope);

var paymentMethodsModel = new List<PaymentMethodModel>();
var paymentMethods = _paymentService.LoadAllPaymentMethods();
Expand Down Expand Up @@ -100,7 +99,8 @@ public async Task<IActionResult> Methods()
[HttpPost]
public async Task<IActionResult> MethodUpdate(PaymentMethodModel model)
{
var _paymentSettings = _settingService.LoadSetting<PaymentSettings>();
var storeScope = await GetActiveStore();
var _paymentSettings = _settingService.LoadSetting<PaymentSettings>(storeScope);

var pm = _paymentService.LoadPaymentMethodBySystemName(model.SystemName);
if (pm.IsPaymentMethodActive(_paymentSettings))
Expand All @@ -109,7 +109,7 @@ public async Task<IActionResult> MethodUpdate(PaymentMethodModel model)
{
//mark as disabled
_paymentSettings.ActivePaymentProviderSystemNames.Remove(pm.SystemName);
await _settingService.SaveSetting(_paymentSettings);
await _settingService.SaveSetting(_paymentSettings, storeScope);
}
}
else
Expand All @@ -118,7 +118,7 @@ public async Task<IActionResult> MethodUpdate(PaymentMethodModel model)
{
//mark as active
_paymentSettings.ActivePaymentProviderSystemNames.Add(pm.SystemName);
await _settingService.SaveSetting(_paymentSettings);
await _settingService.SaveSetting(_paymentSettings, storeScope);
}
}

Expand All @@ -135,7 +135,7 @@ public async Task<IActionResult> ConfigureMethod(string systemName)
var model = await pm.ToModel();
//TODO
/*
model.LogoUrl = pm.PluginInfo.GetLogoUrl(_webHelper);
model.LogoUrl = "";
*/
model.ConfigurationUrl = pm.ConfigurationUrl;
return View(model);
Expand Down Expand Up @@ -236,9 +236,11 @@ public async Task<IActionResult> MethodRestrictionsSave(IFormCollection form)

#region Shipping Settings

public IActionResult Settings()
public async Task<IActionResult> Settings()
{
var paymentSettings = _settingService.LoadSetting<PaymentSettings>();
var storeScope = await GetActiveStore();

var paymentSettings = _settingService.LoadSetting<PaymentSettings>(storeScope);
var model = paymentSettings.ToModel();

return View(model);
Expand All @@ -247,10 +249,12 @@ public IActionResult Settings()
public async Task<IActionResult> Settings(PaymentSettingsModel model,
[FromServices] ICustomerActivityService customerActivityService)
{
var paymentSettings = _settingService.LoadSetting<PaymentSettings>();
var storeScope = await GetActiveStore();

var paymentSettings = _settingService.LoadSetting<PaymentSettings>(storeScope);
paymentSettings = model.ToEntity(paymentSettings);

await _settingService.SaveSetting(paymentSettings);
await _settingService.SaveSetting(paymentSettings, storeScope);

//activity log
_ = customerActivityService.InsertActivity("EditSettings", "",
Expand Down
14 changes: 9 additions & 5 deletions src/Web/Grand.Web.Admin/Controllers/ShippingController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,11 @@ protected virtual async Task PreparePickupPointModel(PickupPointModel model)
public IActionResult Providers() => View();

[HttpPost]
public IActionResult Providers(DataSourceRequest command)
public async Task<IActionResult> Providers(DataSourceRequest command)
{
var _shippingProviderSettings = _settingService.LoadSetting<ShippingProviderSettings>();
var storeScope = await GetActiveStore();

var _shippingProviderSettings = _settingService.LoadSetting<ShippingProviderSettings>(storeScope);
var shippingProvidersModel = new List<ShippingRateComputationMethodModel>();
var shippingProviders = _shippingService.LoadAllShippingRateCalculationProviders();
foreach (var shippingProvider in shippingProviders)
Expand All @@ -172,7 +174,9 @@ public IActionResult Providers(DataSourceRequest command)
[HttpPost]
public async Task<IActionResult> ProviderUpdate(ShippingRateComputationMethodModel model)
{
var _shippingProviderSettings = _settingService.LoadSetting<ShippingProviderSettings>();
var storeScope = await GetActiveStore();

var _shippingProviderSettings = _settingService.LoadSetting<ShippingProviderSettings>(storeScope);

var srcm = _shippingService.LoadShippingRateCalculationProviderBySystemName(model.SystemName);
if (srcm.IsShippingRateMethodActive(_shippingProviderSettings))
Expand All @@ -181,7 +185,7 @@ public async Task<IActionResult> ProviderUpdate(ShippingRateComputationMethodMod
{
//mark as disabled
_shippingProviderSettings.ActiveSystemNames.Remove(srcm.SystemName);
await _settingService.SaveSetting(_shippingProviderSettings);
await _settingService.SaveSetting(_shippingProviderSettings, storeScope);
}
}
else
Expand All @@ -190,7 +194,7 @@ public async Task<IActionResult> ProviderUpdate(ShippingRateComputationMethodMod
{
//mark as active
_shippingProviderSettings.ActiveSystemNames.Add(srcm.SystemName);
await _settingService.SaveSetting(_shippingProviderSettings);
await _settingService.SaveSetting(_shippingProviderSettings, storeScope);
}
}
return new JsonResult("");
Expand Down
2 changes: 1 addition & 1 deletion src/Web/Grand.Web.Admin/Models/News/NewsItemModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public NewsItemModel()

}

public partial class NewsLocalizedModel : ILocalizedModelLocal
public partial class NewsLocalizedModel : ILocalizedModelLocal, ISlugModelLocal
{
public string LanguageId { get; set; }

Expand Down

0 comments on commit a4cb5b1

Please sign in to comment.