Skip to content

Commit

Permalink
Merge pull request #1 from zuntio/add-user-config
Browse files Browse the repository at this point in the history
Add user config
  • Loading branch information
zuntio authored May 2, 2020
2 parents 7c88188 + 25fabbb commit 9485654
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace McfStockChangeTracker.Options
{
public class StockBusterOptions
public class McfStockChangeTrackerApiOptions
{
public string StoreName { get; set; }
public string ApiUser { get; set; }
Expand Down
11 changes: 11 additions & 0 deletions McfStockChangeTracker/Options/McfStockChangeTrackerUserOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace McfStockChangeTracker.Options
{
public class McfStockChangeTrackerUserOptions
{
public Dictionary<string, string> Users { get; set; }
}
}
9 changes: 6 additions & 3 deletions McfStockChangeTracker/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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.");
Expand Down Expand Up @@ -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.");
Expand Down
36 changes: 34 additions & 2 deletions McfStockChangeTracker/Services/FileIOService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,22 @@ 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()
{
WasInitialSetup = false;
CheckAndCreateFoldersAndConfigurationFiles();
}

public StockBusterOptions GetCredentials()
public McfStockChangeTrackerApiOptions GetCredentials()
{
var text = File.ReadAllLines(_storeConfigFile, Encoding.UTF8);
var firstLine = text.FirstOrDefault();
Expand All @@ -42,14 +44,39 @@ 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],
ApiKey = credentials[2]
};
}

public McfStockChangeTrackerUserOptions GetUsers()
{
var users = new Dictionary<string, string>();
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))
Expand Down Expand Up @@ -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<StockChangeDto> data)
Expand Down
8 changes: 5 additions & 3 deletions McfStockChangeTracker/Services/StockChangeTrackingService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<List<StockChangeDto>> SmartQueryForStockChanges(string productCode, DateTimeOffset? start,
Expand Down Expand Up @@ -125,7 +127,7 @@ private List<StockChangeDto> MapToStockChangeDtos(List<StockChangeData> 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();
Expand Down
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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\\\<youruser>\My Documents\StockChangeTracker\
* C:\Users\\\<youruser>\My Documents\StockChangeTracker\config\
* C:\Users\\\<youruser>\My Documents\StockChangeTracker\config\credentials.ini
* C:\Users\\\<youruser>\My Documents\StockChangeTracker\config\users.ini
* C:\Users\\\<youruser>\My Documents\StockChangeTracker\input\
* C:\Users\\\<youruser>\My Documents\StockChangeTracker\output\

Expand All @@ -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.
Expand Down

0 comments on commit 9485654

Please sign in to comment.