Skip to content

Commit 0ac757d

Browse files
Merge pull request #848 from TransactionProcessing/task/#845_update_gets_for_balanceaggregate
Updated GET methods to read the balance aggregate
2 parents 2415f7d + fafe035 commit 0ac757d

File tree

14 files changed

+90
-62
lines changed

14 files changed

+90
-62
lines changed

TransactionProcessor.BusinessLogic.Tests/Manager/TransactionProcessorManagerTests.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,5 +479,23 @@ public async Task TransactionProcessorManager_GetMerchants_RepoCallFails_Excepti
479479
Result<List<Merchant>> getMerchantsResult = await this.TransactionProcessorManager.GetMerchants(TestData.EstateId, CancellationToken.None);
480480
getMerchantsResult.IsFailed.ShouldBeTrue();
481481
}
482+
483+
[Fact]
484+
public async Task TransactionProcessorManager_GetMerchantLiveBalance_ResultSuccess()
485+
{
486+
this.AggregateService.Setup(a => a.GetLatest<MerchantBalanceAggregate>(It.IsAny<Guid>(), It.IsAny<CancellationToken>())).ReturnsAsync(Result.Success(TestData.Aggregates.MerchantBalanceAggregateWithCredit()));
487+
488+
Result<Decimal> getLiveMerchantBalanceResult = await this.TransactionProcessorManager.GetMerchantLiveBalance(TestData.EstateId, TestData.MerchantId, CancellationToken.None);
489+
getLiveMerchantBalanceResult.IsSuccess.ShouldBeTrue();
490+
}
491+
492+
[Fact]
493+
public async Task TransactionProcessorManager_GetMerchantLiveBalance_GetFailed_ResultFailed()
494+
{
495+
this.AggregateService.Setup(a => a.GetLatest<MerchantBalanceAggregate>(It.IsAny<Guid>(), It.IsAny<CancellationToken>())).ReturnsAsync(Result.Failure());
496+
497+
Result<Decimal> getLiveMerchantBalanceResult = await this.TransactionProcessorManager.GetMerchantLiveBalance(TestData.EstateId, TestData.MerchantId, CancellationToken.None);
498+
getLiveMerchantBalanceResult.IsFailed.ShouldBeTrue();
499+
}
482500
}
483501
}

TransactionProcessor.BusinessLogic.Tests/Mediator/DummyEstateManagementManager.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@
1212
namespace TransactionProcessor.BusinessLogic.Tests.Mediator;
1313

1414
public class DummyTransactionProcessorManager : ITransactionProcessorManager {
15+
public async Task<Result<Decimal>> GetMerchantLiveBalance(Guid estateId,
16+
Guid merchantId,
17+
CancellationToken cancellationToken) {
18+
return Result.Success(1000.00m); // Dummy balance
19+
}
20+
1521
public async Task<Result<List<Contract>>> GetMerchantContracts(Guid estateId,
1622
Guid merchantId,
1723
CancellationToken cancellationToken) {

TransactionProcessor.BusinessLogic.Tests/Services/MerchantDomainServiceTests.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -607,7 +607,9 @@ public async Task MerchantDomainService_MakeMerchantWithdrawal_WithdrawalIsMade(
607607
.Setup(s => s.GetToken(It.IsAny<String>(), It.IsAny<String>(), It.IsAny<CancellationToken>()))
608608
.ReturnsAsync(Result.Success(TestData.TokenResponse()));
609609

610-
this.EventStoreContext.Setup(e => e.GetPartitionStateFromProjection(It.IsAny<String>(), It.IsAny<String>(), It.IsAny<CancellationToken>())).ReturnsAsync(Result.Success<String>(JsonConvert.SerializeObject(TestData.MerchantBalanceProjectionState)));
610+
this.AggregateService
611+
.Setup(m => m.GetLatest<MerchantBalanceAggregate>(It.IsAny<Guid>(), It.IsAny<CancellationToken>()))
612+
.ReturnsAsync(Result.Success(TestData.Aggregates.MerchantBalanceAggregateWithCredit()));
611613

612614
var result = await this.DomainService.MakeMerchantWithdrawal(TestData.Commands.MakeMerchantWithdrawalCommand, CancellationToken.None);
613615
result.IsSuccess.ShouldBeTrue();

TransactionProcessor.BusinessLogic/Manager/ITransactionProcessorManager.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ public interface ITransactionProcessorManager
1414
{
1515
#region Methods
1616

17+
Task<Result<Decimal>> GetMerchantLiveBalance(Guid estateId,
18+
Guid merchantId,
19+
CancellationToken cancellationToken);
20+
1721
Task<Result<List<Contract>>> GetMerchantContracts(Guid estateId,
1822
Guid merchantId,
1923
CancellationToken cancellationToken);

TransactionProcessor.BusinessLogic/Manager/TransactionProcessorManager.cs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,24 @@ public async Task<Result<Merchant>> GetMerchant(Guid estateId,
147147
return Result.Success(merchantModel);
148148
}
149149

150+
public async Task<Result<Decimal>> GetMerchantLiveBalance(Guid estateId,
151+
Guid merchantId,
152+
CancellationToken cancellationToken) {
153+
Result<MerchantBalanceAggregate> getMerchantBalanceResult = await this.AggregateService.GetLatest<MerchantBalanceAggregate>(merchantId, cancellationToken);
154+
155+
if (getMerchantBalanceResult.Status == ResultStatus.NotFound)
156+
return Result.Success(0.0m);
157+
158+
if (getMerchantBalanceResult.IsFailed)
159+
return ResultHelpers.CreateFailure(getMerchantBalanceResult);
160+
161+
MerchantBalanceAggregate aggregate = getMerchantBalanceResult.Data;
162+
return Result.Success(aggregate.Balance);
163+
}
164+
150165
public async Task<Result<List<Contract>>> GetMerchantContracts(Guid estateId,
151-
Guid merchantId,
152-
CancellationToken cancellationToken)
166+
Guid merchantId,
167+
CancellationToken cancellationToken)
153168
{
154169
Result<List<Contract>> getMerchantContractsResult = await this.TransactionProcessorReadModelRepository.GetMerchantContracts(estateId, merchantId, cancellationToken);
155170
if (getMerchantContractsResult.IsFailed)

TransactionProcessor.BusinessLogic/RequestHandlers/MerchantRequestHandler.cs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ namespace TransactionProcessor.BusinessLogic.RequestHandlers;
1919

2020
public class MerchantRequestHandler :
2121
IRequestHandler<MerchantQueries.GetMerchantBalanceQuery, Result<MerchantBalanceState>>,
22-
IRequestHandler<MerchantQueries.GetMerchantLiveBalanceQuery, Result<MerchantBalanceProjectionState1>>,
22+
IRequestHandler<MerchantQueries.GetMerchantLiveBalanceQuery, Result<Decimal>>,
2323
IRequestHandler<MerchantQueries.GetMerchantBalanceHistoryQuery, Result<List<MerchantBalanceChangedEntry>>>,
2424
IRequestHandler<MerchantCommands.SwapMerchantDeviceCommand, Result>,
2525
IRequestHandler<MerchantCommands.AddMerchantContractCommand, Result>,
@@ -64,17 +64,9 @@ public async Task<Result<MerchantBalanceState>> Handle(MerchantQueries.GetMercha
6464
return await this.MerchantBalanceStateRepository.Load(query.EstateId, query.MerchantId, cancellationToken);
6565
}
6666

67-
public async Task<Result<MerchantBalanceProjectionState1>> Handle(MerchantQueries.GetMerchantLiveBalanceQuery query,
67+
public async Task<Result<Decimal>> Handle(MerchantQueries.GetMerchantLiveBalanceQuery query,
6868
CancellationToken cancellationToken) {
69-
70-
Result<String> result = await this.EventStoreContext.GetPartitionStateFromProjection("MerchantBalanceProjection", $"MerchantBalance-{query.MerchantId:N}", cancellationToken);
71-
if (result.IsFailed)
72-
return Result.NotFound(
73-
$"Merchant Balance not found for Merchant {query.MerchantId} on MerchantBalanceProjection");
74-
75-
MerchantBalanceProjectionState1 projectionState = JsonConvert.DeserializeObject<MerchantBalanceProjectionState1>(result.Data);
76-
77-
return Result.Success(projectionState);
69+
return await this.TransactionProcessorManager.GetMerchantLiveBalance(query.EstateId, query.MerchantId, cancellationToken);
7870
}
7971

8072
public async Task<Result<List<MerchantBalanceChangedEntry>>> Handle(MerchantQueries.GetMerchantBalanceHistoryQuery query,

TransactionProcessor.BusinessLogic/Requests/MerchantQueries.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace TransactionProcessor.BusinessLogic.Requests;
1212
public record MerchantQueries {
1313
public record GetMerchantBalanceQuery(Guid EstateId, Guid MerchantId) : IRequest<Result<MerchantBalanceState>>;
1414

15-
public record GetMerchantLiveBalanceQuery(Guid MerchantId) : IRequest<Result<MerchantBalanceProjectionState1>>;
15+
public record GetMerchantLiveBalanceQuery(Guid EstateId, Guid MerchantId) : IRequest<Result<Decimal>>;
1616

1717
public record GetMerchantBalanceHistoryQuery(Guid EstateId, Guid MerchantId, DateTime StartDate, DateTime EndDate)
1818
: IRequest<Result<List<MerchantBalanceChangedEntry>>>;

TransactionProcessor.BusinessLogic/Services/MerchantDomainService.cs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -398,19 +398,18 @@ public async Task<Result> MakeMerchantWithdrawal(MerchantCommands.MakeMerchantWi
398398
{
399399
return Result.Invalid($"Merchant [{command.MerchantId}] has not made any deposits yet");
400400
}
401+
402+
Result<MerchantBalanceAggregate> getMerchantBalanceResult = await this.AggregateService.GetLatest<MerchantBalanceAggregate>(command.MerchantId, cancellationToken);
403+
Result<MerchantBalanceAggregate> merchantBalanceAggregateResult =
404+
DomainServiceHelper.HandleGetAggregateResult(getMerchantBalanceResult, command.MerchantId, false);
405+
if (merchantBalanceAggregateResult.IsFailed)
406+
return ResultHelpers.CreateFailure(merchantBalanceAggregateResult);
401407

402-
// Now we need to check the merchants balance to ensure they have funds to withdraw
403-
Result<String> getBalanceResult = await this.EventStoreContext.GetPartitionStateFromProjection("MerchantBalanceProjection", $"MerchantBalance-{command.MerchantId:N}", cancellationToken);
404-
if (getBalanceResult.IsFailed)
405-
{
406-
Result.Invalid($"Failed to get Merchant Balance.");
407-
}
408-
409-
MerchantBalanceProjectionState1 projectionState = JsonConvert.DeserializeObject<MerchantBalanceProjectionState1>(getBalanceResult.Data);
408+
MerchantBalanceAggregate merchantBalanceAggregate = merchantBalanceAggregateResult.Data;
410409

411-
if (command.RequestDto.Amount > projectionState.merchant.balance)
410+
if (command.RequestDto.Amount > merchantBalanceAggregate.Balance)
412411
{
413-
return Result.Invalid($"Not enough credit available for withdrawal of [{command.RequestDto.Amount}]. Balance is {projectionState.merchant.balance}");
412+
return Result.Invalid($"Not enough credit available for withdrawal of [{command.RequestDto.Amount}]. Balance is {merchantBalanceAggregate.Balance}");
414413
}
415414

416415
// If we are here we have enough credit to withdraw

TransactionProcessor.IntegrationTesting.Helpers/TransactionProcessorSteps.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -279,21 +279,21 @@ await Retry.For(async () => {
279279
MerchantResponse merchant = getMerchantResult.Data;
280280
responses.Add(merchant);
281281

282-
string projectionName = "MerchantBalanceProjection";
283-
String partitionId = $"MerchantBalance-{m.Item2:N}";
282+
//string projectionName = "MerchantBalanceProjection";
283+
//String partitionId = $"MerchantBalance-{m.Item2:N}";
284284

285-
dynamic gg = await this.ProjectionManagementClient.GetStateAsync<dynamic>(
286-
projectionName, partitionId);
287-
JsonElement x = (JsonElement)gg;
285+
//dynamic gg = await this.ProjectionManagementClient.GetStateAsync<dynamic>(
286+
// projectionName, partitionId);
287+
//JsonElement x = (JsonElement)gg;
288288

289-
Result<MerchantBalanceResponse>? getMerchantBalanceResult = await this.TransactionProcessorClient.GetMerchantBalance(accessToken, m.Item1, m.Item2, CancellationToken.None);
290-
getMerchantBalanceResult.IsSuccess.ShouldBeTrue();
291-
getMerchantBalanceResult.Data.ShouldNotBeNull();
289+
//Result<MerchantBalanceResponse>? getMerchantBalanceResult = await this.TransactionProcessorClient.GetMerchantBalance(accessToken, m.Item1, m.Item2, CancellationToken.None);
290+
//getMerchantBalanceResult.IsSuccess.ShouldBeTrue();
291+
//getMerchantBalanceResult.Data.ShouldNotBeNull();
292292

293-
// Force a read model database hit
294-
Result<MerchantBalanceResponse>? getMerchantBalanceResult2 = await this.TransactionProcessorClient.GetMerchantBalance(accessToken, m.Item1, m.Item2, CancellationToken.None, liveBalance: false);
295-
getMerchantBalanceResult2.IsSuccess.ShouldBeTrue();
296-
getMerchantBalanceResult2.Data.ShouldNotBeNull();
293+
//// Force a read model database hit
294+
//Result<MerchantBalanceResponse>? getMerchantBalanceResult2 = await this.TransactionProcessorClient.GetMerchantBalance(accessToken, m.Item1, m.Item2, CancellationToken.None, liveBalance: false);
295+
//getMerchantBalanceResult2.IsSuccess.ShouldBeTrue();
296+
//getMerchantBalanceResult2.Data.ShouldNotBeNull();
297297
});
298298
}
299299

TransactionProcessor.IntegrationTests/Common/DockerHelper.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -224,11 +224,10 @@ protected override List<String> GetRequiredProjections()
224224
{
225225
List<String> requiredProjections = new List<String>();
226226

227-
requiredProjections.Add("CallbackHandlerEnricher.js");
228-
requiredProjections.Add("EstateAggregator.js");
229-
requiredProjections.Add("MerchantAggregator.js");
230-
requiredProjections.Add("MerchantBalanceCalculator.js");
231-
requiredProjections.Add("MerchantBalanceProjection.js");
227+
//requiredProjections.Add("EstateAggregator.js");
228+
//requiredProjections.Add("MerchantAggregator.js");
229+
//requiredProjections.Add("MerchantBalanceCalculator.js");
230+
//requiredProjections.Add("MerchantBalanceProjection.js");
232231

233232
return requiredProjections;
234233
}

0 commit comments

Comments
 (0)