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
21 changes: 17 additions & 4 deletions EstateReportingAPI.BusinessLogic/ReportingManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -960,12 +960,20 @@ public async Task<Result<List<Merchant>>> GetMerchants(MerchantQueries.GetMercha
return ResultHelpers.CreateFailure(queryResults);

var merchants = queryResults.Data;

var merchantBalancesQuery = context.MerchantBalanceProjectionState.Where(mb => merchants.Select(m => m.Merchant.MerchantId).Contains(mb.MerchantId));

var balanceQueryResults = await ExecuteQuerySafeToList(merchantBalancesQuery, cancellationToken, "Error retrieving merchant balances");

if (balanceQueryResults.IsFailed)
return ResultHelpers.CreateFailure(balanceQueryResults);

// Ok now enumerate the results
List<Merchant> response = new();
foreach (var queryResult in merchants) {
response.Add(new Merchant {
Balance = 0,
response.Add(new Merchant
{
Balance = balanceQueryResults.Data.Single(b => b.MerchantId == queryResult.Merchant.MerchantId).Balance,
CreatedDateTime = queryResult.Merchant.CreatedDateTime,
Name = queryResult.Merchant.Name,
Region = queryResult.Address.Region,
Expand Down Expand Up @@ -1017,9 +1025,14 @@ public async Task<Result<Merchant>> GetMerchant(MerchantQueries.GetMerchantQuery

var merchant = merchantQueryResult.Data;

// Get the merchant state to get the balance
var merchantStateQueryResult = await ExecuteQuerySafeSingleOrDefault(context.MerchantBalanceProjectionState.Where(ms => ms.MerchantId == request.MerchantId), cancellationToken, "Error getting merchant state");
if (merchantStateQueryResult.IsFailed) return ResultHelpers.CreateFailure(merchantStateQueryResult);
var merchantState = merchantStateQueryResult.Data;

// Ok now enumerate the results
Merchant result = new Merchant {
Balance = 0,
Merchant result = new() {
Balance = merchantState.Balance,
CreatedDateTime = merchant.CreatedDateTime,
Name = merchant.Name,
Reference = merchant.Reference,
Expand Down
9 changes: 9 additions & 0 deletions EstateReportingAPI.IntegrationTests/DatabaseHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Shared.Logger;
using TransactionProcessor.Database.Contexts;
using TransactionProcessor.Database.Entities;
using TransactionProcessor.ProjectionEngine.Database.Database.Entities;

namespace EstateReportingAPI.IntegrationTests;

Expand Down Expand Up @@ -361,6 +362,7 @@ public async Task<Guid> AddOperator(String estateName, String operatorName)

public async Task<Guid> AddMerchant(String estateName,
String merchantName,
Decimal balance,
DateTime createDateTime,
DateTime lastSaleDateTime,
(String addressLine1, String town, String postCode, String region) address,
Expand Down Expand Up @@ -447,6 +449,13 @@ await this.Context.MerchantContracts.AddAsync(new MerchantContract {
}
}

MerchantBalanceProjectionState state = new MerchantBalanceProjectionState {
Balance = balance,
MerchantId = savedMerchant.MerchantId,
MerchantName = savedMerchant.Name
};
await this.Context.MerchantBalanceProjectionState.AddAsync(state);

await this.Context.SaveChangesAsync(CancellationToken.None);

return merchant.MerchantId;
Expand Down
30 changes: 16 additions & 14 deletions EstateReportingAPI.IntegrationTests/MerchantEndpointTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class MerchantEndpointTests : ControllerTestsBase {
public async Task MerchantEndpoint_GetMerchants_MerchantsReturned() {
await this.helper.AddEstate("Test Estate", "Ref1");
for (int i = 0; i < 10; i++) {
await this.helper.AddMerchant("Test Estate", $"Test Merchant {i}", DateTime.Now, DateTime.Now,
await this.helper.AddMerchant("Test Estate", $"Test Merchant {i}",100 * i, DateTime.Now, DateTime.Now,
("Address Line 1", $"Test Town {i}", $"TE57 {i}NG", $"Region {i}"),
($"Contact {i}", @"{i}@2.com", $"{i}23456"));
}
Expand All @@ -32,14 +32,15 @@ await this.helper.AddMerchant("Test Estate", $"Test Merchant {i}", DateTime.Now,
for (int i = 0; i < 10; i++) {
Merchant? expected = merchants.SingleOrDefault(m => m.Name == $"Test Merchant {i}");
expected.ShouldNotBeNull();
expected.Balance.ShouldBe(100 * i);
}
}

[Fact]
public async Task MerchantEndpoint_GetMerchant_MerchantReturned()
{
await this.helper.AddEstate("Test Estate", "Ref1");
var merchantId = await this.helper.AddMerchant("Test Estate", $"Test Merchant 1", DateTime.Now, DateTime.Now,
var merchantId = await this.helper.AddMerchant("Test Estate", $"Test Merchant 1",100, DateTime.Now, DateTime.Now,
("Address Line 1", $"Test Town", $"TE57 1NG", $"Region"),
("Contact 1", "1@2.com", "123456"));

Expand All @@ -49,6 +50,7 @@ public async Task MerchantEndpoint_GetMerchant_MerchantReturned()

merchant.ShouldNotBeNull();
merchant.Name.ShouldBe("Test Merchant 1");
merchant.Balance.ShouldBe(100);
}

[Fact]
Expand All @@ -57,7 +59,7 @@ public async Task MerchantEndpoint_GetRecentMerchants_MerchantsReturned()
await this.helper.AddEstate("Test Estate", "Ref1");
for (int i = 0; i < 10; i++)
{
await this.helper.AddMerchant("Test Estate", $"Test Merchant {i}", DateTime.Now.AddDays(i*-1), DateTime.Now,
await this.helper.AddMerchant("Test Estate", $"Test Merchant {i}",100, DateTime.Now.AddDays(i*-1), DateTime.Now,
("Address Line 1", $"Test Town {i}", $"TE57 {i}NG", $"Region {i}"),
($"Contact {i}", @"{i}@2.com", $"{i}23456"));
}
Expand All @@ -82,7 +84,7 @@ public async Task MerchantEndpoint_GetMerchantOperators_MerchantOperatorsReturne
await this.helper.AddOperator("Test Estate", "Safaricom");
await this.helper.AddOperator("Test Estate", "Voucher");

var merchantId = await this.helper.AddMerchant("Test Estate", $"Test Merchant 1", DateTime.Now, DateTime.Now,
var merchantId = await this.helper.AddMerchant("Test Estate", $"Test Merchant 1",100, DateTime.Now, DateTime.Now,
("Address Line 1", $"Test Town", $"TE57 1NG", $"Region"),
("Contact 1", "1@2.com", "123456"), operators: ["Safaricom", "Voucher"]);

Expand Down Expand Up @@ -110,7 +112,7 @@ public async Task MerchantEndpoint_GetMerchantContracts_MerchantContractsReturne
List<(string productName, int productType, decimal? value)> voucherProductList = new() { ("10 KES Voucher", 0, 10.00m), ("Custom", 0, null) };
await this.helper.AddContractWithProducts("Test Estate", "Healthcare Centre 1 Contract", "Voucher", voucherProductList);

var merchantId = await this.helper.AddMerchant("Test Estate", $"Test Merchant 1", DateTime.Now, DateTime.Now,
var merchantId = await this.helper.AddMerchant("Test Estate", $"Test Merchant 1", 100, DateTime.Now, DateTime.Now,
("Address Line 1", $"Test Town", $"TE57 1NG", $"Region"),
("Contact 1", "1@2.com", "123456"), operators: ["Safaricom", "Voucher"],
["Safaricom Contract", "Healthcare Centre 1 Contract"]);
Expand All @@ -130,7 +132,7 @@ public async Task MerchantEndpoint_GetMerchantDevices_MerchantDevicesReturned()
{
await this.helper.AddEstate("Test Estate", "Ref1");

var merchantId = await this.helper.AddMerchant("Test Estate", $"Test Merchant 1", DateTime.Now, DateTime.Now,
var merchantId = await this.helper.AddMerchant("Test Estate", $"Test Merchant 1",100, DateTime.Now, DateTime.Now,
("Address Line 1", $"Test Town", $"TE57 1NG", $"Region"),
("Contact 1", "1@2.com", "123456"), devices: ["123456"]);

Expand All @@ -148,28 +150,28 @@ public async Task MerchantEndpoint_GetMerchantKpis_MerchantKpisReturned()
{
await this.helper.AddEstate("Test Estate", "Ref1");

await this.helper.AddMerchant("Test Estate", $"Test Merchant 1", DateTime.Now, DateTime.Now,
await this.helper.AddMerchant("Test Estate", $"Test Merchant 1",100, DateTime.Now, DateTime.Now,
("Address Line 1", $"Test Town", $"TE57 1NG", $"Region"),
("Contact 1", "1@2.com", "123456"), devices: ["123456"]);
await this.helper.AddMerchant("Test Estate", $"Test Merchant 2", DateTime.Now, DateTime.Now.AddMinutes(-10),
await this.helper.AddMerchant("Test Estate", $"Test Merchant 2", 200, DateTime.Now, DateTime.Now.AddMinutes(-10),
("Address Line 1", $"Test Town", $"TE57 1NG", $"Region"),
("Contact 1", "1@2.com", "123456"), devices: ["123456"]);
await this.helper.AddMerchant("Test Estate", $"Test Merchant 3", DateTime.Now, DateTime.Now.AddHours(-2),
await this.helper.AddMerchant("Test Estate", $"Test Merchant 3",300, DateTime.Now, DateTime.Now.AddHours(-2),
("Address Line 1", $"Test Town", $"TE57 1NG", $"Region"),
("Contact 1", "1@2.com", "123456"), devices: ["123456"]);
await this.helper.AddMerchant("Test Estate", $"Test Merchant 4", DateTime.Now, DateTime.Now.AddHours(-3),
await this.helper.AddMerchant("Test Estate", $"Test Merchant 4",400, DateTime.Now, DateTime.Now.AddHours(-3),
("Address Line 1", $"Test Town", $"TE57 1NG", $"Region"),
("Contact 1", "1@2.com", "123456"), devices: ["123456"]);
await this.helper.AddMerchant("Test Estate", $"Test Merchant 5", DateTime.Now, DateTime.Now.AddDays(-2),
await this.helper.AddMerchant("Test Estate", $"Test Merchant 5",500, DateTime.Now, DateTime.Now.AddDays(-2),
("Address Line 1", $"Test Town", $"TE57 1NG", $"Region"),
("Contact 1", "1@2.com", "123456"), devices: ["123456"]);
await this.helper.AddMerchant("Test Estate", $"Test Merchant 6", DateTime.Now, DateTime.Now.AddDays(-1),
await this.helper.AddMerchant("Test Estate", $"Test Merchant 6",600, DateTime.Now, DateTime.Now.AddDays(-1),
("Address Line 1", $"Test Town", $"TE57 1NG", $"Region"),
("Contact 1", "1@2.com", "123456"), devices: ["123456"]);
await this.helper.AddMerchant("Test Estate", $"Test Merchant 7", DateTime.Now, DateTime.Now.AddDays(-3),
await this.helper.AddMerchant("Test Estate", $"Test Merchant 7",700, DateTime.Now, DateTime.Now.AddDays(-3),
("Address Line 1", $"Test Town", $"TE57 1NG", $"Region"),
("Contact 1", "1@2.com", "123456"), devices: ["123456"]);
await this.helper.AddMerchant("Test Estate", $"Test Merchant 8", DateTime.Now, DateTime.Now.AddDays(-10),
await this.helper.AddMerchant("Test Estate", $"Test Merchant 8",800, DateTime.Now, DateTime.Now.AddDays(-10),
("Address Line 1", $"Test Town", $"TE57 1NG", $"Region"),
("Contact 1", "1@2.com", "123456"), devices: ["123456"]);

Expand Down
16 changes: 8 additions & 8 deletions EstateReportingAPI.IntegrationTests/TransactionsEndpointTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ protected override async Task SetupStandingData() {
this.TestOutputHelper.WriteLine($"Setup Operators {sw.ElapsedMilliseconds}ms");
sw.Restart();
// Merchants
await this.helper.AddMerchant("Test Estate", "Test Merchant 1", DateTime.MinValue, DateTime.MinValue, default, default);
await this.helper.AddMerchant("Test Estate", "Test Merchant 2", DateTime.MinValue, DateTime.MinValue, default, default);
await this.helper.AddMerchant("Test Estate", "Test Merchant 3", DateTime.MinValue, DateTime.MinValue, default, default);
await this.helper.AddMerchant("Test Estate", "Test Merchant 4", DateTime.MinValue, DateTime.MinValue, default, default);
await this.helper.AddMerchant("Test Estate", "Test Merchant 1",100, DateTime.MinValue, DateTime.MinValue, default, default);
await this.helper.AddMerchant("Test Estate", "Test Merchant 2", 100, DateTime.MinValue, DateTime.MinValue, default, default);
await this.helper.AddMerchant("Test Estate", "Test Merchant 3", 100, DateTime.MinValue, DateTime.MinValue, default, default);
await this.helper.AddMerchant("Test Estate", "Test Merchant 4", 100, DateTime.MinValue, DateTime.MinValue, default, default);
sw.Stop();
this.TestOutputHelper.WriteLine($"Setup Merchants {sw.ElapsedMilliseconds}ms");
sw.Restart();
Expand Down Expand Up @@ -1106,10 +1106,10 @@ protected override async Task SetupStandingData() {
this.TestOutputHelper.WriteLine($"Setup Operators {sw.ElapsedMilliseconds}ms");
sw.Restart();
// Merchants
await this.helper.AddMerchant("Test Estate", "Test Merchant 1", DateTime.MinValue, DateTime.MinValue, default, default);
await this.helper.AddMerchant("Test Estate", "Test Merchant 2", DateTime.MinValue, DateTime.MinValue, default, default);
await this.helper.AddMerchant("Test Estate", "Test Merchant 3", DateTime.MinValue, DateTime.MinValue, default, default);
await this.helper.AddMerchant("Test Estate", "Test Merchant 4", DateTime.MinValue, DateTime.MinValue, default, default);
await this.helper.AddMerchant("Test Estate", "Test Merchant 1",100, DateTime.MinValue, DateTime.MinValue, default, default);
await this.helper.AddMerchant("Test Estate", "Test Merchant 2",100, DateTime.MinValue, DateTime.MinValue, default, default);
await this.helper.AddMerchant("Test Estate", "Test Merchant 3",100, DateTime.MinValue, DateTime.MinValue, default, default);
await this.helper.AddMerchant("Test Estate", "Test Merchant 4",100, DateTime.MinValue, DateTime.MinValue, default, default);
sw.Stop();
this.TestOutputHelper.WriteLine($"Setup Merchants {sw.ElapsedMilliseconds}ms");
sw.Restart();
Expand Down
Loading