Skip to content

Commit

Permalink
Loading stock quantity updater config from json
Browse files Browse the repository at this point in the history
  • Loading branch information
anion0278 committed Mar 13, 2023
1 parent 8fcda53 commit d811fa3
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 38 deletions.
17 changes: 15 additions & 2 deletions Mapp.DataAccess/JsonManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ public interface IJsonManager
Dictionary<string, string> DeserializeJsonDictionary(string fileName);
void SerializeDictionaryToJson(Dictionary<string, string> map, string fileName);
IEnumerable<MarketPlaceTransactionsConfigDTO> LoadTransactionsConfigs();
IEnumerable<StockDataXmlSourceDefinition> LoadStockQuantityUpdaterConfigs();
}

public class JsonManager: IJsonManager
public class JsonManager : IJsonManager
{

public IEnumerable<UpdateInfoEventArgs> DeserializeUpdates(string remoteJsonData)
Expand Down Expand Up @@ -47,7 +48,8 @@ public Dictionary<string, string> DeserializeJsonDictionary(string fileName)
{
string json = JsonSerializer.Serialize(map, new JsonSerializerOptions
{
IgnoreNullValues = true, WriteIndented = true
IgnoreNullValues = true,
WriteIndented = true
});
File.WriteAllText(fileName, json);
}
Expand All @@ -66,5 +68,16 @@ public IEnumerable<MarketPlaceTransactionsConfigDTO> LoadTransactionsConfigs()

return configDtos;
}

public IEnumerable<StockDataXmlSourceDefinition> LoadStockQuantityUpdaterConfigs()
{
var serializeOptions = new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
WriteIndented = true
};
string json = File.ReadAllText(Path.Join("StockQuantityUpdater", "config.json"));
return JsonSerializer.Deserialize<StockDataXmlSourceDefinition[]>(json, serializeOptions);
}
}
}
19 changes: 16 additions & 3 deletions Mapp.DataAccess/StockQuantityUpdater.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,29 @@

namespace Shmap.DataAccess;

public class StockQuantityUpdater
public interface IStockQuantityUpdater
{
Task<IEnumerable<StockData>> ConvertWarehouseData();
}

public class StockQuantityUpdater : IStockQuantityUpdater
{
private readonly IJsonManager _jsonManager;
private readonly IEnumerable<StockDataXmlSourceDefinition> _sourceDefinitions;

public StockQuantityUpdater(IJsonManager jsonManager)
{
_jsonManager = jsonManager;
_sourceDefinitions = _jsonManager.LoadStockQuantityUpdaterConfigs();
}

public async Task<IEnumerable<StockData>> ConvertWarehouseData(IEnumerable<StockDataXmlSourceDefinition> stockDataXmlSources)
public async Task<IEnumerable<StockData>> ConvertWarehouseData()
{

var httpClient = new HttpClient();

var stockData = new List<StockData>();
foreach (var source in stockDataXmlSources)
foreach (var source in _sourceDefinitions)
{
var stream = await (await httpClient.GetAsync(source.Url)).Content.ReadAsStreamAsync();
stockData.AddRange(ExtractStockData(stream, source));
Expand Down
1 change: 1 addition & 0 deletions Mapp.UI/Bootstrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ private void ConfigureContainer()
Container.RegisterTypeAsSingleton<IFileOperationService, FileOperationsService>();
Container.RegisterTypeAsSingleton<IMainWindowViewModel, MainWindowViewModel>();
Container.RegisterTypeAsSingleton<IDialogService, DialogService>();
Container.RegisterTypeAsSingleton<IStockQuantityUpdater, StockQuantityUpdater>();

Container.RegisterType<IManualChangeWindowViewModel, ManualChangeWindowViewModel>();

Expand Down
3 changes: 3 additions & 0 deletions Mapp.UI/Mapp.UI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,9 @@
<None Update="ShippingNameByItemName.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="StockQuantityUpdater\config.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Transactions Configs\TransactionsConfigAmazonAU.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
Expand Down
34 changes: 34 additions & 0 deletions Mapp.UI/StockQuantityUpdater/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
[
{
"url": "https://www.rappa.cz/export/vo.xml",
"itemNodeName": "SHOPITEM",
"skuNodeParsingOptions": [
{
"elementName": "EAN"
}
],
"stockQuantityNodeParsingOptions": [
{
"elementName": "STOCK"
}
]
},
{
"url": "https://en.bushman.eu/content/feeds/uQ5TueFNQh_expando_4.xml",
"itemNodeName": "item",
"skuNodeParsingOptions": [
{
"elementName": "g:gtin"
},
{
"elementName": "g:sku_with_ean",
"substringPattern": "\\d{13}"
}
],
"stockQuantityNodeParsingOptions": [
{
"elementName": "g:quantity"
}
]
}
]
69 changes: 36 additions & 33 deletions Mapp.UI/ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ internal class MainWindowViewModel : ViewModelWithErrorValidationBase, IMainWind
private readonly ITransactionsReader _transactionsReader;
private readonly IGpcGenerator _gpcGenerator;
private readonly IAutocompleteData _autocompleteData;
private readonly IStockQuantityUpdater _quantityUpdater;
private readonly IDialogService _dialogService;
private int _windowWidth;
private int _windowHeight;
Expand Down Expand Up @@ -216,6 +217,7 @@ public MainWindowViewModel(IConfigProvider configProvider,
ITransactionsReader transactionsReader,
IGpcGenerator gpcGenerator,
IAutocompleteData autocompleteData,
IStockQuantityUpdater quantityUpdater,
IDialogService dialogService)
{
_invoiceConverter = invoiceConverter;
Expand All @@ -224,6 +226,7 @@ public MainWindowViewModel(IConfigProvider configProvider,
_transactionsReader = transactionsReader;
_gpcGenerator = gpcGenerator;
_autocompleteData = autocompleteData;
_quantityUpdater = quantityUpdater;
_dialogService = dialogService;

SelectAmazonInvoicesCommand = new RelayCommand(SelectAmazonInvoices, () => !HasErrors);
Expand Down Expand Up @@ -253,39 +256,39 @@ private async void ConvertWarehouseData()
IsReadyForProcessing = false;
try
{
StockDataXmlSourceDefinition[] sources =
{
new()
{
Url = "https://www.rappa.cz/export/vo.xml",
ItemNodeName = "SHOPITEM",
SkuNodeParsingOptions = new[]
{
new ValueParsingOption("EAN", null),
},
StockQuantityNodeParsingOptions = new[]
{
new ValueParsingOption("STOCK", null),
},
},
new()
{
Url = "https://en.bushman.eu/content/feeds/uQ5TueFNQh_expando_4.xml",
ItemNodeName = "item",
SkuNodeParsingOptions = new[]
{
new ValueParsingOption("g:gtin", null),
new ValueParsingOption("g:sku_with_ean", @"\d{13}"),
},
StockQuantityNodeParsingOptions = new[]
{
new ValueParsingOption("g:quantity", null),
},
}
};

var stockQuantityUpdater = new StockQuantityUpdater();
var stockData = await stockQuantityUpdater.ConvertWarehouseData(sources);
//StockDataXmlSourceDefinition[] sources =
//{
// new()
// {
// Url = "https://www.rappa.cz/export/vo.xml",
// ItemNodeName = "SHOPITEM",
// SkuNodeParsingOptions = new[]
// {
// new ValueParsingOption("EAN", null),
// },
// StockQuantityNodeParsingOptions = new[]
// {
// new ValueParsingOption("STOCK", null),
// },
// },
// new()
// {
// Url = "https://en.bushman.eu/content/feeds/uQ5TueFNQh_expando_4.xml",
// ItemNodeName = "item",
// SkuNodeParsingOptions = new[]
// {
// new ValueParsingOption("g:gtin", null),
// new ValueParsingOption("g:sku_with_ean", @"\d{13}"),
// },
// StockQuantityNodeParsingOptions = new[]
// {
// new ValueParsingOption("g:quantity", null),
// },
// }
//};


var stockData = await _quantityUpdater.ConvertWarehouseData();
var columnNamesLine =
"sku\tprice\tminimum-seller-allowed-price\tmaximum-seller-allowed-price\tquantity\thandling-time\tfulfillment-channel";
var lines = new List<string>(stockData.Count() + 1) { columnNamesLine };
Expand Down

0 comments on commit d811fa3

Please sign in to comment.