-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProgram.cs
More file actions
188 lines (160 loc) · 6.4 KB
/
Copy pathProgram.cs
File metadata and controls
188 lines (160 loc) · 6.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
using System;
using System.Collections.Generic;
using PayrollEngine.Client.Exchange;
using PayrollEngine.Client.Model;
using PayrollEngine.Client.Scripting.Script;
using PayrollEngine.Client.Service.Api;
using Tasks = System.Threading.Tasks;
namespace PayrollEngine.Client.Tutorial.ImportExchangeData;
/// <summary>Import exchange data tutorial program</summary>
internal class Program : ConsoleProgram<Program>
{
private const string MonthWageCaseFieldName = "Monatslohn";
private const string MonthWageMinRequestAttribute = "MonthWageMinRequest";
private const decimal MonthWageMin = 4000;
/// <summary>Mandatory arguments: tenant and absences json file</summary>
protected override int MandatoryArgumentCount => 2;
/// <inheritdoc />
protected override async Tasks.Task RunAsync()
{
// tenant
var tenant = await GetTenantAsync(ConsoleArguments.Get(1));
if (tenant == null)
{
return;
}
// exchange
var exchange = await FileReader.ReadAsync<Model.Exchange>(ConsoleArguments.Get(2));
if (exchange == null)
{
return;
}
// case values
var caseValues = await SetupCaseValuesAsync(tenant, exchange);
// import case changes
await ImportAsync(exchange);
// user notification
DisplayCaseChanges(tenant, caseValues);
PressAnyKey();
}
#region Domain
/// <summary>Import exchange to backend</summary>
/// <param name="exchange">The exchange model</param>
private async Tasks.Task ImportAsync(Model.Exchange exchange) =>
await new ExchangeImport(HttpClient, exchange, new ScriptParser()).ImportAsync();
/// <summary>Setup case values</summary>
/// <param name="tenant">The tenant</param>
/// <param name="exchange">The exchange</param>
/// <returns>List with case change setup and case value setup</returns>
private static async Tasks.Task<List<Tuple<ICaseChangeSetup, ICaseValueSetup>>> SetupCaseValuesAsync(Tenant tenant, Model.Exchange exchange)
{
var caseValueSetups = new List<Tuple<ICaseChangeSetup, ICaseValueSetup>>();
// visitor
await new Visitor(exchange)
{
VisitCaseValue = (exchangeTenant, _, caseChangeSetup, _, caseValueSetup) =>
{
if (!string.Equals(exchangeTenant.Identifier, tenant.Identifier))
{
return;
}
// min month wage
if (string.Equals(MonthWageCaseFieldName, caseValueSetup.CaseFieldName))
{
// month wage from case value
var wage = ValueConvert.ToDecimal(caseValueSetup.Value);
if (wage < MonthWageMin)
{
// store min wage request value as case value attribute
caseValueSetup.SetAttribute(MonthWageMinRequestAttribute, wage);
// update case value
caseValueSetup.Value = ValueConvert.ToJson(MonthWageMin);
}
}
// collect case value setup
caseValueSetups.Add(new(caseChangeSetup, caseValueSetup));
}
}.ExecuteAsync();
return caseValueSetups;
}
/// <summary>Get tenant by console argument</summary>
/// <param name="tenantIdentifier">The tenant identifier</param>
private async Tasks.Task<Tenant> GetTenantAsync(string tenantIdentifier)
{
// tenant argument
if (string.IsNullOrWhiteSpace(tenantIdentifier))
{
WriteErrorLine("Missing argument tenant identifier.");
PressAnyKey();
return null;
}
// tenant request
var tenant = await new TenantService(HttpClient).GetAsync<Tenant>(new(), tenantIdentifier);
if (tenant == null)
{
WriteErrorLine($"Invalid tenant identifier {tenantIdentifier}.");
PressAnyKey();
return null;
}
return tenant;
}
#endregion
#region Output
/// <summary>Display the case changes</summary>
/// <param name="tenant">The tenant</param>
/// <param name="caseValueSetups">The case value setups</param>
private static void DisplayCaseChanges(Tenant tenant, List<Tuple<ICaseChangeSetup, ICaseValueSetup>> caseValueSetups)
{
// title
WriteTitleLine($"Case changes {tenant.Identifier}");
// table
var line = new string('-', 25 + 20 + 20 + 15 + 15 + 20 + 30);
WriteLine();
WriteLine(line);
WriteLine($"{"Employee",-25}{"Case",-20}{"Case field",-20}{"Start",-15}{"End",-15}{"Value",-20}Notes");
WriteLine(line);
foreach (var setup in caseValueSetups)
{
var caseSetup = setup.Item1;
var valueSetup = setup.Item2;
// compact start/end date
var start = valueSetup.Start.HasValue ?
valueSetup.Start.Value.ToCompactString() : string.Empty;
var end = valueSetup.End.HasValue ?
valueSetup.End.Value.ToCompactString() : string.Empty;
// case value display
Write($"{caseSetup.EmployeeIdentifier,-25}{caseSetup.Case.CaseName,-20}{valueSetup.CaseFieldName,-20}{start,-15}{end,-15}{valueSetup.Value,-20}");
// wage
if (valueSetup.ContainsAttribute(MonthWageMinRequestAttribute))
{
WriteError($"min. month wage > {valueSetup.GetAttribute(MonthWageMinRequestAttribute)}");
}
WriteLine();
}
WriteLine(line);
}
/// <inheritdoc />
protected override Tasks.Task HelpAsync()
{
WriteLine("Usage: ImportExchangeData Tenant CaseValues.json");
WriteLine();
WriteLine("Arguments:");
WriteLine(" 1. Tenant identifier");
WriteLine(" 2. JSON file containing the case values");
WriteLine();
WriteLine("Examples:");
WriteLine(" ImportExchangeData MyTenant");
WriteLine(" ImportExchangeData MyTenant MyCaseValues.json");
return Tasks.Task.CompletedTask;
}
#endregion
/// <summary>Program entry point</summary>
static async Tasks.Task Main()
{
// init logger
Log.SetLogger(new Serilog.PayrollLog());
// execute program
using var program = new Program();
await program.ExecuteAsync();
}
}