Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,6 @@ public async Task FloatDomainService_CreateFloatForContractProduct_FloatCreated(
this.EstateClient.Setup(e => e.GetContract(It.IsAny<String>(),
It.IsAny<Guid>(),
It.IsAny<Guid>(),
It.IsAny<Boolean>(),
It.IsAny<Boolean>(),
It.IsAny<CancellationToken>())).ReturnsAsync(new ContractResponse{
EstateId = TestData.EstateId,
ContractId = TestData.ContractId,
Expand Down Expand Up @@ -90,8 +88,6 @@ public async Task FloatDomainService_CreateFloatForContractProduct_InvalidEstate
this.EstateClient.Setup(e => e.GetContract(It.IsAny<String>(),
It.IsAny<Guid>(),
It.IsAny<Guid>(),
It.IsAny<Boolean>(),
It.IsAny<Boolean>(),
It.IsAny<CancellationToken>())).ReturnsAsync(new ContractResponse
{
EstateId = TestData.EstateId,
Expand Down Expand Up @@ -146,8 +142,6 @@ public async Task FloatDomainService_CreateFloatForContractProduct_InvalidContra
this.EstateClient.Setup(e => e.GetContract(It.IsAny<String>(),
It.IsAny<Guid>(),
It.IsAny<Guid>(),
It.IsAny<Boolean>(),
It.IsAny<Boolean>(),
It.IsAny<CancellationToken>())).ReturnsAsync(new ContractResponse
{
EstateId = TestData.EstateId,
Expand Down
30 changes: 30 additions & 0 deletions TransactionProcessor.BusinessLogic/Common/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ namespace TransactionProcessor.BusinessLogic.Common
using System.Diagnostics.CodeAnalysis;
using Shared.General;
using Type = System.Type;
using System.Threading;
using SecurityService.Client;
using SecurityService.DataTransferObjects.Responses;
using Shared.Logger;

public static class Helpers
{
Expand All @@ -20,6 +24,32 @@ public static Guid CalculateSettlementAggregateId(DateTime settlementDate,
Guid aggregateId = GuidCalculator.Combine(estateId,merchantId, settlementDate.ToGuid());
return aggregateId;
}

public static async Task<TokenResponse> GetToken(TokenResponse currentToken, ISecurityServiceClient securityServiceClient, CancellationToken cancellationToken)
{
// Get a token to talk to the estate service
String clientId = ConfigurationReader.GetValue("AppSettings", "ClientId");
String clientSecret = ConfigurationReader.GetValue("AppSettings", "ClientSecret");
Logger.LogDebug($"Client Id is {clientId}");
Logger.LogDebug($"Client Secret is {clientSecret}");

if (currentToken == null)
{
TokenResponse token = await securityServiceClient.GetToken(clientId, clientSecret, cancellationToken);
Logger.LogDebug($"Token is {token.AccessToken}");
return token;
}

if (currentToken.Expires.UtcDateTime.Subtract(DateTime.UtcNow) < TimeSpan.FromMinutes(2))
{
Logger.LogDebug($"Token is about to expire at {currentToken.Expires.DateTime:O}");
TokenResponse token = await securityServiceClient.GetToken(clientId, clientSecret, cancellationToken);
Logger.LogDebug($"Token is {token.AccessToken}");
return token;
}

return currentToken;
}
}

public static class Extensions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,36 +120,7 @@ private DateTime CalculateSettlementDate(SettlementSchedule merchantSettlementSc

return completeDateTime.Date;
}

/// <summary>
/// Gets the token.
/// </summary>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns></returns>
[ExcludeFromCodeCoverage]
private async Task<TokenResponse> GetToken(CancellationToken cancellationToken) {
// Get a token to talk to the estate service
String clientId = ConfigurationReader.GetValue("AppSettings", "ClientId");
String clientSecret = ConfigurationReader.GetValue("AppSettings", "ClientSecret");
Logger.LogInformation($"Client Id is {clientId}");
Logger.LogInformation($"Client Secret is {clientSecret}");

if (this.TokenResponse == null) {
TokenResponse token = await this.SecurityServiceClient.GetToken(clientId, clientSecret, cancellationToken);
Logger.LogInformation($"Token is {token.AccessToken}");
return token;
}

if (this.TokenResponse.Expires.UtcDateTime.Subtract(DateTime.UtcNow) < TimeSpan.FromMinutes(2)) {
Logger.LogInformation($"Token is about to expire at {this.TokenResponse.Expires.DateTime:O}");
TokenResponse token = await this.SecurityServiceClient.GetToken(clientId, clientSecret, cancellationToken);
Logger.LogInformation($"Token is {token.AccessToken}");
return token;
}

return this.TokenResponse;
}


private Boolean RequireFeeCalculation(TransactionAggregate transactionAggregate){
return transactionAggregate switch{
_ when transactionAggregate.IsAuthorised == false => false,
Expand Down Expand Up @@ -197,7 +168,7 @@ private async Task HandleSpecificDomainEvent(TransactionHasBeenCompletedEvent do
if (RequireFeeCalculation(transactionAggregate) == false)
return;

this.TokenResponse = await this.GetToken(cancellationToken);
this.TokenResponse = await Helpers.GetToken(this.TokenResponse, this.SecurityServiceClient, cancellationToken);

List<TransactionFeeToCalculate> feesForCalculation = await this.GetTransactionFeesForCalculation(transactionAggregate, cancellationToken);

Expand Down Expand Up @@ -345,7 +316,7 @@ private async Task<List<TransactionFeeToCalculate>> GetTransactionFeesForCalcula

private async Task HandleSpecificDomainEvent(CustomerEmailReceiptRequestedEvent domainEvent,
CancellationToken cancellationToken) {
this.TokenResponse = await this.GetToken(cancellationToken);
this.TokenResponse = await Helpers.GetToken(this.TokenResponse, this.SecurityServiceClient, cancellationToken);

TransactionAggregate transactionAggregate =
await this.TransactionAggregateRepository.GetLatestVersion(domainEvent.TransactionId, cancellationToken);
Expand All @@ -368,7 +339,7 @@ await this.SendEmailMessage(this.TokenResponse.AccessToken,

private async Task HandleSpecificDomainEvent(CustomerEmailReceiptResendRequestedEvent domainEvent,
CancellationToken cancellationToken) {
this.TokenResponse = await this.GetToken(cancellationToken);
this.TokenResponse = await Helpers.GetToken(this.TokenResponse, this.SecurityServiceClient, cancellationToken);

// Send the message
await this.ResendEmailMessage(this.TokenResponse.AccessToken, domainEvent.EventId, domainEvent.EstateId, cancellationToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using Common;
using EstateManagement.Database.Contexts;
using EstateManagement.Database.Entities;
using MessagingService.Client;
Expand Down Expand Up @@ -125,39 +126,7 @@ public async Task Handle(IDomainEvent domainEvent,
{
await this.HandleSpecificDomainEvent((dynamic)domainEvent, cancellationToken);
}

/// <summary>
/// Gets the token.
/// </summary>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns></returns>
[ExcludeFromCodeCoverage]
private async Task<TokenResponse> GetToken(CancellationToken cancellationToken)
{
// Get a token to talk to the estate service
String clientId = ConfigurationReader.GetValue("AppSettings", "ClientId");
String clientSecret = ConfigurationReader.GetValue("AppSettings", "ClientSecret");
Logger.LogInformation($"Client Id is {clientId}");
Logger.LogInformation($"Client Secret is {clientSecret}");

if (this.TokenResponse == null)
{
TokenResponse token = await this.SecurityServiceClient.GetToken(clientId, clientSecret, cancellationToken);
Logger.LogInformation($"Token is {token.AccessToken}");
return token;
}

if (this.TokenResponse.Expires.UtcDateTime.Subtract(DateTime.UtcNow) < TimeSpan.FromMinutes(2))
{
Logger.LogInformation($"Token is about to expire at {this.TokenResponse.Expires.DateTime:O}");
TokenResponse token = await this.SecurityServiceClient.GetToken(clientId, clientSecret, cancellationToken);
Logger.LogInformation($"Token is {token.AccessToken}");
return token;
}

return this.TokenResponse;
}


/// <summary>
/// Gets the voucher operator.
/// </summary>
Expand Down Expand Up @@ -186,7 +155,7 @@ private async Task HandleSpecificDomainEvent(VoucherIssuedEvent domainEvent,
// Get the voucher aggregate
VoucherAggregate voucherAggregate = await this.VoucherAggregateRepository.GetLatestVersion(domainEvent.AggregateId, cancellationToken);
Models.Voucher voucherModel = voucherAggregate.GetVoucher();
this.TokenResponse = await this.GetToken(cancellationToken);
this.TokenResponse = await Helpers.GetToken(this.TokenResponse, this.SecurityServiceClient, cancellationToken);
if (string.IsNullOrEmpty(voucherModel.RecipientEmail) == false)
{
String message = await this.GetEmailVoucherMessage(voucherModel, cancellationToken);
Expand Down
33 changes: 4 additions & 29 deletions TransactionProcessor.BusinessLogic/Services/IFloatDomainService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace TransactionProcessor.BusinessLogic.Services
using System.Diagnostics.CodeAnalysis;
using System.Runtime.InteropServices;
using System.Threading;
using Common;
using EstateManagement.Client;
using EstateManagement.Database.Entities;
using EstateManagement.DataTransferObjects.Responses;
Expand Down Expand Up @@ -48,36 +49,10 @@ public FloatDomainService(IAggregateRepository<FloatAggregate, DomainEvent> floa

private TokenResponse TokenResponse;

[ExcludeFromCodeCoverage]
private async Task<TokenResponse> GetToken(CancellationToken cancellationToken)
{
// Get a token to talk to the estate service
String clientId = ConfigurationReader.GetValue("AppSettings", "ClientId");
String clientSecret = ConfigurationReader.GetValue("AppSettings", "ClientSecret");
Logger.LogInformation($"Client Id is {clientId}");
Logger.LogInformation($"Client Secret is {clientSecret}");

if (this.TokenResponse == null)
{
TokenResponse token = await this.SecurityServiceClient.GetToken(clientId, clientSecret, cancellationToken);
Logger.LogInformation($"Token is {token.AccessToken}");
return token;
}

if (this.TokenResponse.Expires.UtcDateTime.Subtract(DateTime.UtcNow) < TimeSpan.FromMinutes(2))
{
Logger.LogInformation($"Token is about to expire at {this.TokenResponse.Expires.DateTime:O}");
TokenResponse token = await this.SecurityServiceClient.GetToken(clientId, clientSecret, cancellationToken);
Logger.LogInformation($"Token is {token.AccessToken}");
return token;
}

return this.TokenResponse;
}

private async Task ValidateEstate(Guid estateId, CancellationToken cancellationToken)
{
this.TokenResponse = await this.GetToken(cancellationToken);
this.TokenResponse = await Helpers.GetToken(this.TokenResponse, this.SecurityServiceClient, cancellationToken);

EstateResponse estate = await this.EstateClient.GetEstate(this.TokenResponse.AccessToken, estateId, cancellationToken);

Expand All @@ -88,10 +63,10 @@ private async Task ValidateEstate(Guid estateId, CancellationToken cancellationT

private async Task ValidateContractProduct(Guid estateId, Guid contractId, Guid productId, CancellationToken cancellationToken)
{
this.TokenResponse = await this.GetToken(cancellationToken);
this.TokenResponse = await Helpers.GetToken(this.TokenResponse, this.SecurityServiceClient, cancellationToken);

// TODO: validate the estate, contract and product
ContractResponse contract = await this.EstateClient.GetContract(this.TokenResponse.AccessToken, estateId, contractId, true, false, cancellationToken);
ContractResponse contract = await this.EstateClient.GetContract(this.TokenResponse.AccessToken, estateId, contractId, cancellationToken);

if (contract == null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public async Task<ProcessSettlementResponse> ProcessSettlement(DateTime settleme
return response;
}

this.TokenResponse = await this.GetToken(cancellationToken);
this.TokenResponse = await Helpers.GetToken(this.TokenResponse, this.SecurityServiceClient, cancellationToken);

MerchantResponse merchant = await this.EstateClient.GetMerchant(this.TokenResponse.AccessToken,
estateId,
Expand Down Expand Up @@ -108,32 +108,5 @@ public SettlementDomainService(IAggregateRepository<TransactionAggregate, Domain
}

private TokenResponse TokenResponse;

[ExcludeFromCodeCoverage]
private async Task<TokenResponse> GetToken(CancellationToken cancellationToken)
{
// Get a token to talk to the estate service
String clientId = ConfigurationReader.GetValue("AppSettings", "ClientId");
String clientSecret = ConfigurationReader.GetValue("AppSettings", "ClientSecret");
Logger.LogInformation($"Client Id is {clientId}");
Logger.LogInformation($"Client Secret is {clientSecret}");

if (this.TokenResponse == null)
{
TokenResponse token = await this.SecurityServiceClient.GetToken(clientId, clientSecret, cancellationToken);
Logger.LogInformation($"Token is {token.AccessToken}");
return token;
}

if (this.TokenResponse.Expires.UtcDateTime.Subtract(DateTime.UtcNow) < TimeSpan.FromMinutes(2))
{
Logger.LogInformation($"Token is about to expire at {this.TokenResponse.Expires.DateTime:O}");
TokenResponse token = await this.SecurityServiceClient.GetToken(clientId, clientSecret, cancellationToken);
Logger.LogInformation($"Token is {token.AccessToken}");
return token;
}

return this.TokenResponse;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ private async Task AddDeviceToMerchant(Guid estateId,
Guid merchantId,
String deviceIdentifier,
CancellationToken cancellationToken){
this.TokenResponse = await this.GetToken(cancellationToken);
this.TokenResponse = await Helpers.GetToken(this.TokenResponse, this.SecurityServiceClient, cancellationToken);

// Add the device to the merchant
await this.EstateClient.AddDeviceToMerchant(this.TokenResponse.AccessToken,
Expand Down Expand Up @@ -367,37 +367,13 @@ private String GenerateTransactionReference(){
private async Task<MerchantResponse> GetMerchant(Guid estateId,
Guid merchantId,
CancellationToken cancellationToken){
this.TokenResponse = await this.GetToken(cancellationToken);
this.TokenResponse = await Helpers.GetToken(this.TokenResponse, this.SecurityServiceClient, cancellationToken);

MerchantResponse merchant = await this.EstateClient.GetMerchant(this.TokenResponse.AccessToken, estateId, merchantId, cancellationToken);

return merchant;
}

[ExcludeFromCodeCoverage]
private async Task<TokenResponse> GetToken(CancellationToken cancellationToken){
// Get a token to talk to the estate service
String clientId = ConfigurationReader.GetValue("AppSettings", "ClientId");
String clientSecret = ConfigurationReader.GetValue("AppSettings", "ClientSecret");
Logger.LogInformation($"Client Id is {clientId}");
Logger.LogInformation($"Client Secret is {clientSecret}");

if (this.TokenResponse == null){
TokenResponse token = await this.SecurityServiceClient.GetToken(clientId, clientSecret, cancellationToken);
Logger.LogInformation($"Token is {token.AccessToken}");
return token;
}

if (this.TokenResponse.Expires.UtcDateTime.Subtract(DateTime.UtcNow) < TimeSpan.FromMinutes(2)){
Logger.LogInformation($"Token is about to expire at {this.TokenResponse.Expires.DateTime:O}");
TokenResponse token = await this.SecurityServiceClient.GetToken(clientId, clientSecret, cancellationToken);
Logger.LogInformation($"Token is {token.AccessToken}");
return token;
}

return this.TokenResponse;
}


private async Task<OperatorResponse> ProcessMessageWithOperator(MerchantResponse merchant,
Guid transactionId,
DateTime transactionDateTime,
Expand Down
Loading