diff --git a/McfStockChangeTracker/Options/StockBusterOptions.cs b/McfStockChangeTracker/Options/McfStockChangeTrackerApiOptions.cs similarity index 79% rename from McfStockChangeTracker/Options/StockBusterOptions.cs rename to McfStockChangeTracker/Options/McfStockChangeTrackerApiOptions.cs index 96ca97a..35f4c33 100644 --- a/McfStockChangeTracker/Options/StockBusterOptions.cs +++ b/McfStockChangeTracker/Options/McfStockChangeTrackerApiOptions.cs @@ -1,6 +1,6 @@ namespace McfStockChangeTracker.Options { - public class StockBusterOptions + public class McfStockChangeTrackerApiOptions { public string StoreName { get; set; } public string ApiUser { get; set; } diff --git a/McfStockChangeTracker/Options/McfStockChangeTrackerUserOptions.cs b/McfStockChangeTracker/Options/McfStockChangeTrackerUserOptions.cs new file mode 100644 index 0000000..86b7c53 --- /dev/null +++ b/McfStockChangeTracker/Options/McfStockChangeTrackerUserOptions.cs @@ -0,0 +1,11 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace McfStockChangeTracker.Options +{ + public class McfStockChangeTrackerUserOptions + { + public Dictionary Users { get; set; } + } +} diff --git a/McfStockChangeTracker/Program.cs b/McfStockChangeTracker/Program.cs index f103734..98cfcc2 100644 --- a/McfStockChangeTracker/Program.cs +++ b/McfStockChangeTracker/Program.cs @@ -19,6 +19,8 @@ static async Task Main(string[] args) Console.WriteLine($"Tarvittavat kansiot, alikansiot ja asetustiedostot luotu hakemistoon {io.AppRootFolder}."); Console.WriteLine($"Ennen seuraavaa ajoa, tallenna kaupan nimi, apikäyttäjätunnus ja apiavain tiedostoon {io.StoreConfigFile} muodossa:" + $"\n\nkaupannimi:sposti:apiavain123abc\n\n"); + Console.WriteLine($"Halutessasi syötä käyttäjä-id:t ja niihin yhdistettävät nimet tiedostoon {io.UserConfigFile} muodossa:" + + $"\n\n1:Varasto\n2:Matti\n\n"); } else { @@ -31,8 +33,9 @@ static async Task Main(string[] args) private static async Task GetStockChangesInteractively(FileIOService io) { - var settings = io.GetCredentials(); - var service = new StockChangeTrackingService(settings); + var apiSettings = io.GetCredentials(); + var userSettings = io.GetUsers(); + var service = new StockChangeTrackingService(apiSettings, userSettings); Console.WriteLine("Tervetuloa StockBuster5000000:aan!\n\n"); Console.WriteLine("Anna tuotteen tai tuotevariaation yksilöllinen tuotekoodi."); Console.WriteLine("Jos haluat hakea tietyn tuotteen kaikkien variaatioiden tapahtumat, anna päätuotteen tuotekoodi."); @@ -72,7 +75,7 @@ private static async Task GetStockChangesInteractively(FileIOService io) continue; } Console.WriteLine($"Löydetty {data.Count} varastotapahtumaa tuotteelle {data.First().Product} annetulla aikavälillä."); - var filename = $"{DateTime.Now:yyyyMMddhhmmss}-{givenLine}.csv"; + var filename = $"{DateTime.Now:yyyyMMddHHmmss}-{givenLine}.csv"; Console.WriteLine($"Kirjoitetaan tapahtumat tiedostoon {Path.Combine(io.OutputFolder, filename)}"); await io.WriteToCsv(filename, data); Console.WriteLine("Kirjoitettu."); diff --git a/McfStockChangeTracker/Services/FileIOService.cs b/McfStockChangeTracker/Services/FileIOService.cs index d4d9def..e1e538c 100644 --- a/McfStockChangeTracker/Services/FileIOService.cs +++ b/McfStockChangeTracker/Services/FileIOService.cs @@ -20,12 +20,14 @@ public class FileIOService private readonly string _inputFolder = Path.Combine(_appRootFolder, "input"); private readonly string _outputFolder = Path.Combine(_appRootFolder, "output"); private readonly string _storeConfigFile = Path.Combine(_configFolder, "credentials.ini"); + private readonly string _userConfigFile = Path.Combine(_configFolder, "users.ini"); public bool WasInitialSetup { get; private set; } public string AppRootFolder => _appRootFolder; public string OutputFolder => _outputFolder; public string StoreConfigFile => _storeConfigFile; + public string UserConfigFile => _userConfigFile; public FileIOService() { @@ -33,7 +35,7 @@ public FileIOService() CheckAndCreateFoldersAndConfigurationFiles(); } - public StockBusterOptions GetCredentials() + public McfStockChangeTrackerApiOptions GetCredentials() { var text = File.ReadAllLines(_storeConfigFile, Encoding.UTF8); var firstLine = text.FirstOrDefault(); @@ -42,7 +44,7 @@ public StockBusterOptions GetCredentials() var credentials = firstLine.Split(":"); if (credentials.Length != 3) throw new Exception($"Ohjelma tarvitsee kaikki kolme asiaa: Verkkokaupan nimen, käyttäjätunnuksen ja api-avaimen. Yksi tai useampi puuttuu. Tarkista {_storeConfigFile}"); - return new StockBusterOptions + return new McfStockChangeTrackerApiOptions { StoreName = credentials[0], ApiUser = credentials[1], @@ -50,6 +52,31 @@ public StockBusterOptions GetCredentials() }; } + public McfStockChangeTrackerUserOptions GetUsers() + { + var users = new Dictionary(); + var text = File.ReadAllLines(_userConfigFile, Encoding.UTF8); + var filteredLines = text.Where(x => !string.IsNullOrWhiteSpace(x)).ToList(); + var splitLines = filteredLines.Select(x => x.Split(":")).Where(x => x.Length == 2).ToList(); + splitLines.ForEach(x => + { + try + { + users.Add(x[0].Trim(), x[1].Trim()); + } + catch (Exception ex) + { + throw new Exception($"Sama käyttäjätunnus syötetty useaan kertaan. Tarkista {_userConfigFile}"); + } + }); + + return new McfStockChangeTrackerUserOptions + { + Users = users + }; + + } + private void CheckAndCreateFoldersAndConfigurationFiles() { if (!Directory.Exists(_appRootFolder)) @@ -77,6 +104,11 @@ private void CheckAndCreateFoldersAndConfigurationFiles() WasInitialSetup = true; File.CreateText(_storeConfigFile); } + if (!File.Exists(_userConfigFile)) + { + WasInitialSetup = true; + File.CreateText(_userConfigFile); + } } public async Task WriteToCsv(string filename, List data) diff --git a/McfStockChangeTracker/Services/StockChangeTrackingService.cs b/McfStockChangeTracker/Services/StockChangeTrackingService.cs index 1b02fd3..8ae01f9 100644 --- a/McfStockChangeTracker/Services/StockChangeTrackingService.cs +++ b/McfStockChangeTracker/Services/StockChangeTrackingService.cs @@ -14,10 +14,12 @@ namespace McfStockChangeTracker.Services public class StockChangeTrackingService { private readonly McfClient _client; + private readonly McfStockChangeTrackerUserOptions _userOptions; - public StockChangeTrackingService(StockBusterOptions options) + public StockChangeTrackingService(McfStockChangeTrackerApiOptions apiOptions, McfStockChangeTrackerUserOptions userOptions) { - _client = new McfClient(options.StoreName, options.ApiUser, options.ApiKey); + _client = new McfClient(apiOptions.StoreName, apiOptions.ApiUser, apiOptions.ApiKey); + _userOptions = userOptions; } public async Task> SmartQueryForStockChanges(string productCode, DateTimeOffset? start, @@ -125,7 +127,7 @@ private List MapToStockChangeDtos(List data, st Variation = variationName, StockChangeType = x.SourceType, TimeStamp = x.ChangedAt.ToString("d.M.yyyy hh:mm:ss"), - User = x.UserId.ToString(), + User = _userOptions.Users.ContainsKey(x.UserId.ToString()) ? _userOptions.Users[x.UserId.ToString()] : x.UserId.ToString(), Differential = x.QuantityChange, TargetAmount = x.Quantity }).ToList(); diff --git a/README.md b/README.md index 16c565b..1dab16b 100644 --- a/README.md +++ b/README.md @@ -20,14 +20,13 @@ This application is provided "as is". If you download binaries or compile this b Tracker will be updated to use Dependecy Injection. Also soon you will be able to provide list of trackable items in input-folder. Graphical User Interface is also under consideration. -For now, output only shows user ids. You can track down the actual user by highlightin user in MCF's User Administration. There will be possibility to se these up so application can translate id to user. - ## Getting started After downloading/compiling, run the Executable. In the first run it will create you following folder and files: * C:\Users\\\\My Documents\StockChangeTracker\ * C:\Users\\\\My Documents\StockChangeTracker\config\ * C:\Users\\\\My Documents\StockChangeTracker\config\credentials.ini +* C:\Users\\\\My Documents\StockChangeTracker\config\users.ini * C:\Users\\\\My Documents\StockChangeTracker\input\ * C:\Users\\\\My Documents\StockChangeTracker\output\ @@ -44,6 +43,17 @@ Then navigate to My Documents -> StockChangeTracker -> config and open credentia Save the file. Application is ready to use. +Optionally you can also define users. Find out user id from MCF User Administration page and on your computer navigate to My Documents -> StockChangeTracker -> config and open users.ini. Add lines in following format: +`userid:user_in_human_readable_format` + +In example: + +`1:Boss` + +`2:The Warehouse Guy` + +`3:Mike` + ## Usage Application will ask you unique product code of the stock item you are about to track. Provide either code of variation, product or product which has variations.