Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" PrivateAssets="All" />
<PackageReference Include="VirtoCommerce.CoreModule.Core" Version="3.821.0" />
<PackageReference Include="VirtoCommerce.EnvironmentsCompare.Core" Version="3.800.0-alpha.56-vcst-3900-environments-compare-initial" />
<PackageReference Include="VirtoCommerce.NotificationsModule.Core" Version="3.809.0" />
<PackageReference Include="VirtoCommerce.Platform.Core" Version="3.889.0" />
<PackageReference Include="VirtoCommerce.Seo.Core" Version="3.801.0" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VirtoCommerce.EnvironmentsCompare.Core.Models;
using VirtoCommerce.EnvironmentsCompare.Core.Services;
using VirtoCommerce.Platform.Core.Common;
using VirtoCommerce.Platform.Core.Settings;
using VirtoCommerce.StoreModule.Core.Model;
using VirtoCommerce.StoreModule.Core.Model.Search;
using VirtoCommerce.StoreModule.Core.Services;

namespace VirtoCommerce.StoreModule.Data.Services;

public class ComparableStoreSettingsProvider(IStoreSearchService storeSearchService, ILocalizableSettingService localizableSettingService) : IComparableSettingsProvider
{
protected const int MaxComparableStoresCount = 100;

public async Task<IList<ComparableSettingScope>> GetComparableSettingsAsync()
{
var result = new List<ComparableSettingScope>();

var storeSearchCriteria = AbstractTypeFactory<StoreSearchCriteria>.TryCreateInstance();
storeSearchCriteria.Take = MaxComparableStoresCount;

foreach (var store in await storeSearchService.SearchAllNoCloneAsync(storeSearchCriteria))
{
var resultScope = AbstractTypeFactory<ComparableSettingScope>.TryCreateInstance();
resultScope.ScopeName = $"StoreSettings: {store.Id}";
result.Add(resultScope);

await AddSettings(resultScope, store);

await AddLanguagesAndCurrencies(resultScope, store);
}

return result;
}

protected virtual async Task AddSettings(ComparableSettingScope resultScope, Store store)
{
foreach (var storeSettingGroup in store.Settings.GroupBy(x => x.GroupName))
{
var resultGroup = AbstractTypeFactory<ComparableSettingGroup>.TryCreateInstance();
resultGroup.GroupName = storeSettingGroup.Key.IsNullOrEmpty() ? "Without group" : storeSettingGroup.Key;
resultScope.SettingGroups.Add(resultGroup);

foreach (var storeSetting in storeSettingGroup)
{
var resultSetting = AbstractTypeFactory<ComparableSetting>.TryCreateInstance();
resultSetting.Name = storeSetting.Name;
resultSetting.Value = await GetSettingValue(storeSetting, store);
resultSetting.IsSecret = IsSettingSecret(storeSetting);
resultGroup.Settings.Add(resultSetting);
}
}
}

protected virtual async Task<object> GetSettingValue(ObjectSettingEntry setting, Store store)
{
if (!setting.IsLocalizable)
{
return setting.Value;
}
else
{
var localizedValuesBuilder = new StringBuilder();

foreach (var language in store.Languages.OrderBy(x => x))
{
localizedValuesBuilder.Append('[').Append(language).Append(']').Append(':');

foreach (var settingValue in await localizableSettingService.GetValuesAsync(setting.Name, language))
{
localizedValuesBuilder.Append(settingValue.Key).Append('=').Append(settingValue.Value).Append(';');
}

localizedValuesBuilder.Append(';');
}

return localizedValuesBuilder.ToString();
}
}

protected virtual bool IsSettingSecret(ObjectSettingEntry setting)
{
return setting.ValueType == SettingValueType.SecureString;
}

protected virtual Task AddLanguagesAndCurrencies(ComparableSettingScope resultScope, Store store)
{
var languagesAndCurrenciesGroup = AbstractTypeFactory<ComparableSettingGroup>.TryCreateInstance();
languagesAndCurrenciesGroup.GroupName = "Languages and Currencies";
resultScope.SettingGroups.Add(languagesAndCurrenciesGroup);

var languagesSetting = AbstractTypeFactory<ComparableSetting>.TryCreateInstance();
languagesSetting.Name = nameof(store.Languages);
languagesSetting.Value = string.Join(";", store.Languages.OrderBy(x => x));
languagesAndCurrenciesGroup.Settings.Add(languagesSetting);

var defaultLanguageSetting = AbstractTypeFactory<ComparableSetting>.TryCreateInstance();
defaultLanguageSetting.Name = nameof(store.DefaultLanguage);
defaultLanguageSetting.Value = store.DefaultLanguage;
languagesAndCurrenciesGroup.Settings.Add(defaultLanguageSetting);

var currenciesSetting = AbstractTypeFactory<ComparableSetting>.TryCreateInstance();
currenciesSetting.Name = nameof(store.Currencies);
currenciesSetting.Value = string.Join(";", store.Currencies.OrderBy(x => x));
languagesAndCurrenciesGroup.Settings.Add(currenciesSetting);

var defaultCurrencySetting = AbstractTypeFactory<ComparableSetting>.TryCreateInstance();
defaultCurrencySetting.Name = nameof(store.DefaultCurrency);
defaultCurrencySetting.Value = store.DefaultCurrency;
languagesAndCurrenciesGroup.Settings.Add(defaultCurrencySetting);

return Task.CompletedTask;
}
}
3 changes: 3 additions & 0 deletions src/VirtoCommerce.StoreModule.Web/Module.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using VirtoCommerce.CoreModule.Core.Seo;
using VirtoCommerce.EnvironmentsCompare.Core.Services;
using VirtoCommerce.NotificationsModule.Core.Services;
using VirtoCommerce.Platform.Core.Common;
using VirtoCommerce.Platform.Core.DynamicProperties;
Expand Down Expand Up @@ -81,6 +82,8 @@ public void Initialize(IServiceCollection serviceCollection)
serviceCollection.AddTransient<IStoreAuthenticationService, StoreAuthenticationService>();
serviceCollection.AddTransient<IStoreAuthenticationSchemeService, StoreAuthenticationSchemeService>();
serviceCollection.AddTransient<IStoreAuthenticationSchemeSearchService, StoreAuthenticationSchemeSearchService>();

serviceCollection.AddTransient<IComparableSettingsProvider, ComparableStoreSettingsProvider>();
}

public void PostInitialize(IApplicationBuilder appBuilder)
Expand Down
1 change: 1 addition & 0 deletions src/VirtoCommerce.StoreModule.Web/module.manifest
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<dependency id="VirtoCommerce.Core" version="3.821.0" />
<dependency id="VirtoCommerce.Notifications" version="3.809.0" />
<dependency id="VirtoCommerce.Seo" version="3.801.0" />
<dependency id="VirtoCommerce.EnvironmentsCompare" version="3.800.0-alpha.56-vcst-3900-environments-compare-initial" optional="true" />
</dependencies>

<title>Store</title>
Expand Down
Loading