Skip to content

Commit 2e2caae

Browse files
final fixes
1 parent e6ea586 commit 2e2caae

2 files changed

Lines changed: 77 additions & 26 deletions

File tree

TransactionProcessing.MerchantPos/Runtime/MerchantRuntime.cs

Lines changed: 64 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,14 @@ public MerchantRuntime(IApiClient apiClient,
3232
}
3333

3434
private TokenResponse CurrentServiceToken;
35-
public async Task RunAsync((String clientId, String clientSecret) serviceClient, (String clientId, String clientSecret) posClient, MerchantConfig config, CancellationToken cancellationToken)
35+
private TokenResponse CurrentUserToken;
36+
private (String clientId, String clientSecret) ServiceClientCredentials;
37+
private (String clientId, String clientSecret) PosClientCredentials;
38+
public async Task RunAsync((String clientId, String clientSecret) serviceClient, (String clientId, String clientSecret) posClient , MerchantConfig config, CancellationToken cancellationToken)
3639
{
3740
Logger.LogInformation($"MerchantRuntime started for {config.MerchantName}");
38-
41+
this.ServiceClientCredentials = serviceClient;
42+
this.PosClientCredentials = posClient;
3943
while (!cancellationToken.IsCancellationRequested)
4044
{
4145
try
@@ -44,8 +48,8 @@ public async Task RunAsync((String clientId, String clientSecret) serviceClient,
4448
//Result<TokenResponse> serviceToken = await this.GetToken(this.CurrentServiceToken, serviceClient, cancellationToken);
4549
//this.CurrentServiceToken = serviceToken.Data;
4650

47-
await StartupSequence(posClient.clientId, posClient.clientSecret, config, cancellationToken);
48-
await RunMainLoop(posClient.clientId, posClient.clientSecret, config, cancellationToken);
51+
await StartupSequence(config, cancellationToken);
52+
await RunMainLoop( config, cancellationToken);
4953
}
5054
catch (Exception ex)
5155
{
@@ -55,12 +59,11 @@ public async Task RunAsync((String clientId, String clientSecret) serviceClient,
5559
}
5660
}
5761

58-
private async Task<Result> GetToken((String clientId, String clientSecret) serviceClient,
59-
CancellationToken cancellationToken)
62+
private async Task<Result> GetServiceToken(CancellationToken cancellationToken)
6063
{
6164
if (this.CurrentServiceToken == null)
6265
{
63-
Result<TokenResponse> tokenResult = await this.SecurityServiceClient.GetToken(serviceClient.clientId, serviceClient.clientSecret, cancellationToken);
66+
Result<TokenResponse> tokenResult = await this.SecurityServiceClient.GetToken(this.ServiceClientCredentials.clientId, this.ServiceClientCredentials.clientSecret, cancellationToken);
6467
if (tokenResult.IsFailed)
6568
return ResultHelpers.CreateFailure(tokenResult);
6669
TokenResponse token = tokenResult.Data;
@@ -72,7 +75,7 @@ private async Task<Result> GetToken((String clientId, String clientSecret) servi
7275
if (this.CurrentServiceToken.Expires.UtcDateTime.Subtract(DateTime.UtcNow) < TimeSpan.FromMinutes(2))
7376
{
7477
Logger.LogDebug($"Token is about to expire at {this.CurrentServiceToken.Expires.DateTime:O}");
75-
Result<TokenResponse> tokenResult = await this.SecurityServiceClient.GetToken(serviceClient.clientId, serviceClient.clientSecret, cancellationToken);
78+
Result<TokenResponse> tokenResult = await this.SecurityServiceClient.GetToken(this.ServiceClientCredentials.clientId, this.ServiceClientCredentials.clientSecret, cancellationToken);
7679
if (tokenResult.IsFailed)
7780
return ResultHelpers.CreateFailure(tokenResult);
7881
TokenResponse token = tokenResult.Data;
@@ -84,30 +87,62 @@ private async Task<Result> GetToken((String clientId, String clientSecret) servi
8487
return Result.Success();
8588
}
8689

87-
private async Task StartupSequence(String clientId,
88-
String clientSecret,
89-
MerchantConfig cfg,
90+
private async Task<Result> GetUserToken(MerchantConfig config,
91+
CancellationToken cancellationToken)
92+
{
93+
if (this.CurrentUserToken == null)
94+
{
95+
Result<TokenResponse> tokenResult = await this.SecurityServiceClient.GetToken(config.Username, config.Password, this.PosClientCredentials.clientId, this.PosClientCredentials.clientSecret, cancellationToken);
96+
if (tokenResult.IsFailed)
97+
return ResultHelpers.CreateFailure(tokenResult);
98+
TokenResponse token = tokenResult.Data;
99+
Logger.LogDebug($"Token is {token.AccessToken}");
100+
this.CurrentUserToken = token;
101+
return Result.Success();
102+
}
103+
104+
if (this.CurrentUserToken.Expires.UtcDateTime.Subtract(DateTime.UtcNow) < TimeSpan.FromMinutes(2))
105+
{
106+
Logger.LogDebug($"Token is about to expire at {this.CurrentUserToken.Expires.DateTime:O}");
107+
Result<TokenResponse> tokenResult = await this.SecurityServiceClient.GetToken(config.Username, config.Password, this.PosClientCredentials.clientId, this.PosClientCredentials.clientSecret, cancellationToken);
108+
if (tokenResult.IsFailed)
109+
return ResultHelpers.CreateFailure(tokenResult);
110+
TokenResponse token = tokenResult.Data;
111+
Logger.LogDebug($"Token is {token.AccessToken}");
112+
this.CurrentUserToken = token;
113+
return Result.Success();
114+
}
115+
116+
return Result.Success();
117+
}
118+
119+
private async Task StartupSequence(MerchantConfig cfg,
90120
CancellationToken cancellationToken) {
91121
// 1. Token
92-
Result tokenResult = await this.GetToken((clientId, clientSecret), cancellationToken);
122+
Result tokenResult = await this.GetServiceToken(cancellationToken);
123+
if (tokenResult.IsFailed)
124+
{
125+
Logger.LogWarning($"Failed to get service token during startup sequence.");
126+
return;
127+
}
128+
129+
tokenResult = await this.GetUserToken(cfg, cancellationToken);
93130

94131
if (tokenResult.IsFailed) {
95132
Logger.LogWarning($"Failed to get token for merchant {cfg.MerchantName} during startup sequence.");
96133
return;
97134
}
98135

99136
// 2. Load products
100-
List<Product> products = await ApiClient.GetProductList(cfg, this.CurrentServiceToken, cancellationToken);
137+
List<Product> products = await ApiClient.GetProductList(cfg, this.CurrentUserToken, cancellationToken);
101138
cfg.Products = products;
102139

103140
// 3. Balance
104141
decimal balance = await ApiClient.GetBalance(cfg, this.CurrentServiceToken, cancellationToken);
105142
await Repository.UpdateBalance(cfg.MerchantId,cfg.MerchantName, balance);
106143
}
107144

108-
private async Task RunMainLoop(String clientId,
109-
String clientSecret,
110-
MerchantConfig cfg,
145+
private async Task RunMainLoop(MerchantConfig cfg,
111146
CancellationToken token) {
112147
TimeSpan saleInterval = TimeSpan.FromSeconds(cfg.SaleIntervalSeconds);
113148

@@ -127,7 +162,7 @@ private async Task RunMainLoop(String clientId,
127162
// Get last end of day time
128163

129164
if (currentTime.Date > merchant.LastEndOfDayDateTime.Date) {
130-
await DoReconciliation(clientId, clientSecret, cfg, token);
165+
await DoReconciliation(cfg, token);
131166
}
132167

133168
TimeSpan delay = openingTime - currentTime.TimeOfDay;
@@ -149,32 +184,35 @@ private async Task RunMainLoop(String clientId,
149184
DateTime now = DateTime.Now;
150185
if (merchant.LastLogonDateTime.Date != now.Date)
151186
{
152-
Result tokenResult = await this.GetToken((clientId, clientSecret), token);
187+
Result tokenResult = await this.GetUserToken(cfg, token);
153188
if (tokenResult.IsFailed)
154189
{
155190
Logger.LogWarning($"Failed to obtain token for daily logon for merchant {cfg.MerchantName}");
156191
}
157192
else
158193
{
159-
await ApiClient.SendLogon(cfg, this.CurrentServiceToken, merchant.TransactionNumber, token);
194+
await ApiClient.SendLogon(cfg, this.CurrentUserToken, merchant.TransactionNumber, token);
160195
//_lastDailyLogonDate = now.Date;
161196
await this.Repository.UpdateLastLogon(cfg.MerchantId, cfg.MerchantName, now);
162197
Logger.LogInformation($"Performed daily logon for merchant {cfg.MerchantName} on {now:yyyy-MM-dd}");
163198
}
164199
}
165200
else {
166201
// Sell product
167-
await DoSaleCycle(clientId, clientSecret, cfg, merchant.TransactionNumber, token);
202+
await DoSaleCycle(cfg, merchant.TransactionNumber, token);
168203
}
169204

170205
await this.Repository.IncrementTransactionNumber(cfg.MerchantId, cfg.MerchantName);
171206
await Task.Delay(saleInterval, token);
172207
}
173208
}
174209

175-
private async Task DoSaleCycle(String clientId, String clientSecret, MerchantConfig cfg,Int32 transactionNumber, CancellationToken cancellationToken)
210+
private async Task DoSaleCycle(MerchantConfig cfg,Int32 transactionNumber, CancellationToken cancellationToken)
176211
{
177-
Result tokenResult = await this.GetToken((clientId, clientSecret), cancellationToken);
212+
Result tokenResult = await this.GetServiceToken(cancellationToken);
213+
if (tokenResult.IsFailed)
214+
return;
215+
tokenResult = await this.GetUserToken(cfg, cancellationToken);
178216
if (tokenResult.IsFailed)
179217
return;
180218

@@ -193,7 +231,7 @@ private async Task DoSaleCycle(String clientId, String clientSecret, MerchantCon
193231
bool induceFail = _rng.NextDouble() < cfg.FailureInjectionProbability;
194232
decimal saleValue = induceFail ? balance + 10 : value;
195233

196-
SaleResponse result = await ApiClient.SendSale(cfg, this.CurrentServiceToken, product, saleValue, transactionNumber, cancellationToken);
234+
SaleResponse result = await ApiClient.SendSale(cfg, this.CurrentUserToken, product, saleValue, transactionNumber, cancellationToken);
197235

198236
if (result.Authorised)
199237
{
@@ -217,14 +255,14 @@ private async Task DoSaleCycle(String clientId, String clientSecret, MerchantCon
217255
}
218256
}
219257

220-
private async Task DoReconciliation(String clientId, String clientSecret, MerchantConfig cfg, CancellationToken cancellationToken)
258+
private async Task DoReconciliation(MerchantConfig cfg, CancellationToken cancellationToken)
221259
{
222-
Result tokenResult = await this.GetToken((clientId, clientSecret), cancellationToken);
260+
Result tokenResult = await this.GetUserToken(cfg, cancellationToken);
223261
if (tokenResult.IsFailed)
224262
return;
225263

226264
List<OperatorTotal> totals = await Repository.GetTotals(cfg.MerchantId);
227-
await ApiClient.SendReconciliation(cfg, this.CurrentServiceToken, totals, cancellationToken);
265+
await ApiClient.SendReconciliation(cfg, this.CurrentUserToken, totals, cancellationToken);
228266

229267
// Clear totals
230268
await this.Repository.UpdateLastEndOfDay(cfg.MerchantId, cfg.MerchantName, DateTime.Now);
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"version": 1,
3+
"isRoot": true,
4+
"tools": {
5+
"dotnet-ef": {
6+
"version": "10.0.3",
7+
"commands": [
8+
"dotnet-ef"
9+
],
10+
"rollForward": false
11+
}
12+
}
13+
}

0 commit comments

Comments
 (0)