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
5 changes: 5 additions & 0 deletions TransactionProcessor.Mobile.BusinessLogic.Tests/TestData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,11 @@ public static class TestData
TestData.Operator3Product_200KES
};

public static List<ContractOperatorModel> ContractOperatorList = new List<ContractOperatorModel>
{
ContractOperatorModel
};

public static List<ContractProductModel> ContractProductListEmpty = new List<ContractProductModel>();

public static ContractOperatorModel ContractOperatorModel = new ContractOperatorModel
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,26 +47,26 @@ public BillPaymentSelectOperatorPageViewModelTests() {
[Fact]
public async Task BillPaymentSelectOperatorPageViewModel_Initialise_IsInitialised()
{
this.Mediator.Setup(m => m.Send(It.IsAny<GetContractProductsRequest>(), It.IsAny<CancellationToken>())).ReturnsAsync(Result.Success(TestData.ContractProductList));
this.Mediator.Setup(m => m.Send(It.IsAny<GetProductOperators>(), It.IsAny<CancellationToken>())).ReturnsAsync(Result.Success(TestData.ContractOperatorList));

await this.ViewModel.Initialise(CancellationToken.None);
this.Mediator.Verify(x => x.Send(It.IsAny<GetContractProductsRequest>(), It.IsAny<CancellationToken>()), Times.Once);
this.Mediator.Verify(x => x.Send(It.IsAny<GetProductOperators>(), It.IsAny<CancellationToken>()), Times.Once);

this.ViewModel.Operators.Count.ShouldBe(3);
this.ViewModel.Operators.Count.ShouldBe(1);
}

[Fact]
public async Task BillPaymentSelectOperatorPageViewModel_OperatorSelectedCommand_Execute_IsExecuted()
{
this.Mediator.Setup(m => m.Send(It.IsAny<GetContractProductsRequest>(), It.IsAny<CancellationToken>())).ReturnsAsync(Result.Success(TestData.ContractProductList));
this.Mediator.Setup(m => m.Send(It.IsAny<GetProductOperators>(), It.IsAny<CancellationToken>())).ReturnsAsync(Result.Success(TestData.ContractOperatorList));

await this.ViewModel.Initialise(CancellationToken.None);

this.ViewModel.Operators.Count.ShouldBe(3);
this.ViewModel.Operators.Count.ShouldBe(1);

ItemSelected<ContractOperatorModel> selectedContractOperator = new ItemSelected<ContractOperatorModel>
{
SelectedItemIndex = 1,
SelectedItemIndex = 0,
SelectedItem = TestData.ContractOperatorModel
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,22 +46,22 @@ public MobileTopupSelectOperatorPageViewModelTests() {
[Fact]
public async Task MobileTopupSelectOperatorPageViewModel_Initialise_IsInitialised()
{
this.Mediator.Setup(m => m.Send(It.IsAny<GetContractProductsRequest>(), It.IsAny<CancellationToken>())).ReturnsAsync(Result.Success(TestData.ContractProductList));
this.Mediator.Setup(m => m.Send(It.IsAny<GetProductOperators>(), It.IsAny<CancellationToken>())).ReturnsAsync(Result.Success(TestData.ContractOperatorList));

await this.ViewModel.Initialise(CancellationToken.None);
this.Mediator.Verify(x => x.Send(It.IsAny<GetContractProductsRequest>(), It.IsAny<CancellationToken>()), Times.Once);
this.Mediator.Verify(x => x.Send(It.IsAny<GetProductOperators>(), It.IsAny<CancellationToken>()), Times.Once);

this.ViewModel.Operators.Count.ShouldBe(3);
this.ViewModel.Operators.Count.ShouldBe(1);
}

[Fact]
public async Task MobileTopupSelectOperatorPageViewModel_OperatorSelectedCommand_Execute_IsExecuted()
{
this.Mediator.Setup(m => m.Send(It.IsAny<GetContractProductsRequest>(), It.IsAny<CancellationToken>())).ReturnsAsync(Result.Success(TestData.ContractProductList));
this.Mediator.Setup(m => m.Send(It.IsAny<GetProductOperators>(), It.IsAny<CancellationToken>())).ReturnsAsync(Result.Success(TestData.ContractOperatorList));

await this.ViewModel.Initialise(CancellationToken.None);

this.ViewModel.Operators.Count.ShouldBe(3);
this.ViewModel.Operators.Count.ShouldBe(1);

ItemSelected<ContractOperatorModel> selectedContractOperator = new ItemSelected<ContractOperatorModel>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,23 +42,22 @@ public VoucherSelectOperatorPageViewModelTests() {
[Fact]
public async Task VoucherSelectOperatorPageViewModel_Initialise_IsInitialised()
{
this.Mediator.Setup(m => m.Send(It.IsAny<GetContractProductsRequest>(), It.IsAny<CancellationToken>())).ReturnsAsync(Result.Success(TestData.ContractProductList));

this.Mediator.Setup(m => m.Send(It.IsAny<GetProductOperators>(), It.IsAny<CancellationToken>())).ReturnsAsync(Result.Success(TestData.ContractOperatorList));

await this.ViewModel.Initialise(CancellationToken.None);
this.Mediator.Verify(x => x.Send(It.IsAny<GetContractProductsRequest>(), It.IsAny<CancellationToken>()), Times.Once);
this.Mediator.Verify(x => x.Send(It.IsAny<GetProductOperators>(), It.IsAny<CancellationToken>()), Times.Once);

this.ViewModel.Operators.Count.ShouldBe(3);
this.ViewModel.Operators.Count.ShouldBe(1);
}

[Fact]
public async Task VoucherSelectOperatorPageViewModel_OperatorSelectedCommand_Execute_IsExecuted()
{
this.Mediator.Setup(m => m.Send(It.IsAny<GetContractProductsRequest>(), It.IsAny<CancellationToken>())).ReturnsAsync(Result.Success(TestData.ContractProductList));
this.Mediator.Setup(m => m.Send(It.IsAny<GetProductOperators>(), It.IsAny<CancellationToken>())).ReturnsAsync(Result.Success(TestData.ContractOperatorList));

await this.ViewModel.Initialise(CancellationToken.None);

this.ViewModel.Operators.Count.ShouldBe(3);
this.ViewModel.Operators.Count.ShouldBe(1);

ItemSelected<ContractOperatorModel> selectedContractOperator = new ItemSelected<ContractOperatorModel>
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using MediatR;
using Shared.Results;
using SimpleResults;
using TransactionProcessor.Mobile.BusinessLogic.Models;
using TransactionProcessor.Mobile.BusinessLogic.Requests;
Expand All @@ -8,7 +9,8 @@ namespace TransactionProcessor.Mobile.BusinessLogic.RequestHandlers;

public class MerchantRequestHandler : IRequestHandler<GetContractProductsRequest, Result<List<ContractProductModel>>>,
IRequestHandler<GetMerchantBalanceRequest, Result<Decimal>>,
IRequestHandler<GetMerchantDetailsRequest, Result<MerchantDetailsModel>>
IRequestHandler<GetMerchantDetailsRequest, Result<MerchantDetailsModel>>,
IRequestHandler<GetProductOperators, Result<List<ContractOperatorModel>>>
{
#region Fields

Expand Down Expand Up @@ -74,4 +76,42 @@ public async Task<Result<MerchantDetailsModel>> Handle(GetMerchantDetailsRequest
}

#endregion

public async Task<Result<List<ContractOperatorModel>>> Handle(GetProductOperators request,
CancellationToken cancellationToken) {
List<ContractProductModel> products = this.ApplicationCache.GetContractProducts();

Boolean useTrainingMode = this.ApplicationCache.GetUseTrainingMode();
IMerchantService merchantService = this.MerchantServiceResolver(useTrainingMode);

if (products == null || products.Any() == false)
{
Result<List<ContractProductModel>> getProductsResult = await merchantService.GetContractProducts(cancellationToken);
if (getProductsResult.IsFailed) {
return ResultHelpers.CreateFailure(getProductsResult);
}

products = getProductsResult.Data;

if (request.ProductType.HasValue)
{
products = products.Where(p => p.ProductType == request.ProductType).ToList();
}
}

List<ContractOperatorModel> operators = products.GroupBy(c => new
{
c.OperatorName,
c.OperatorId,
c.OperatorIdentfier
}).Select(g => new ContractOperatorModel
{
OperatorId = g.Key.OperatorId,
OperatorName = g.Key.OperatorName,
OperatorIdentfier = g.Key.OperatorIdentfier
}).ToList();

return Result.Success(operators);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,6 @@ public static GetContractProductsRequest Create(ProductType? productType =null)
}

#endregion
}
}

public record GetProductOperators(ProductType? ProductType) : IRequest<Result<List<ContractOperatorModel>>>;
Original file line number Diff line number Diff line change
Expand Up @@ -45,26 +45,14 @@ public BillPaymentSelectOperatorPageViewModel(IMediator mediator, INavigationSer

public async Task Initialise(CancellationToken cancellationToken)
{
GetContractProductsRequest request = GetContractProductsRequest.Create(ProductType.BillPayment);

Result<List<ContractProductModel>> productsresult = await this.Mediator.Send(request, cancellationToken);
// TODO: Handle the failure result
List<ContractProductModel> products = productsresult.Data;

// TODO: Should this logic live in the Reqest handler ???
List<ContractOperatorModel> operators = products.GroupBy(c => new
{
c.OperatorName,
c.OperatorId,
c.OperatorIdentfier
}).Select(g => new ContractOperatorModel
{
OperatorId = g.Key.OperatorId,
OperatorName = g.Key.OperatorName,
OperatorIdentfier = g.Key.OperatorIdentfier
}).ToList();

this.Operators = operators;
GetProductOperators request = new GetProductOperators(ProductType.BillPayment);
Result<List<ContractOperatorModel>> operatorsResult = await this.Mediator.Send(request, cancellationToken);
if (operatorsResult.IsFailed)
{
await this.DialogService.ShowWarningToast("Unable to load operators. Please try again later.", cancellationToken: cancellationToken);
return;
}
this.Operators = operatorsResult.Data;
}

[RelayCommand]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,25 +43,14 @@ public MobileTopupSelectOperatorPageViewModel(IMediator mediator, INavigationSer

public async Task Initialise(CancellationToken cancellationToken)
{
GetContractProductsRequest request = GetContractProductsRequest.Create(ProductType.MobileTopup);

Result<List<ContractProductModel>> productsresult = await this.Mediator.Send(request, cancellationToken);
List<ContractProductModel> products = productsresult.Data;

// TODO: Should this logic live in the Request handler ???
List<ContractOperatorModel> operators = products.GroupBy(c => new
{
c.OperatorName,
c.OperatorId,
c.OperatorIdentfier
}).Select(g => new ContractOperatorModel
{
OperatorId = g.Key.OperatorId,
OperatorName = g.Key.OperatorName,
OperatorIdentfier = g.Key.OperatorIdentfier
}).ToList();

this.Operators = operators;
GetProductOperators request = new GetProductOperators(ProductType.MobileTopup);
Result<List<ContractOperatorModel>> operatorsResult = await this.Mediator.Send(request, cancellationToken);
if (operatorsResult.IsFailed)
{
await this.DialogService.ShowWarningToast("Unable to load operators. Please try again later.", cancellationToken: cancellationToken);
return;
}
this.Operators = operatorsResult.Data;
}

[RelayCommand]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
using System.Windows.Input;
using CommunityToolkit.Mvvm.Input;
using CommunityToolkit.Mvvm.Input;
using MediatR;
using MvvmHelpers.Commands;
using SimpleResults;
using System.Windows.Input;
using TransactionProcessor.Mobile.BusinessLogic.Common;
using TransactionProcessor.Mobile.BusinessLogic.Logging;
using TransactionProcessor.Mobile.BusinessLogic.Models;
Expand Down Expand Up @@ -43,25 +44,14 @@ public VoucherSelectOperatorPageViewModel(IMediator mediator, INavigationService

public async Task Initialise(CancellationToken cancellationToken)
{
GetContractProductsRequest request = GetContractProductsRequest.Create(ProductType.Voucher);

var productsresult = await this.Mediator.Send(request, cancellationToken);
List<ContractProductModel> products = productsresult.Data;

// TODO: Should this logic live in the Reqest handler ???
List<ContractOperatorModel> operators = products.GroupBy(c => new
{
c.OperatorName,
c.OperatorId,
c.OperatorIdentfier
}).Select(g => new ContractOperatorModel
{
OperatorId = g.Key.OperatorId,
OperatorName = g.Key.OperatorName,
OperatorIdentfier = g.Key.OperatorIdentfier
}).ToList();

this.Operators = operators;
GetProductOperators request = new GetProductOperators(ProductType.Voucher);
Result<List<ContractOperatorModel>> operatorsResult = await this.Mediator.Send(request, cancellationToken);
if (operatorsResult.IsFailed)
{
await this.DialogService.ShowWarningToast("Unable to load operators. Please try again later.", cancellationToken: cancellationToken);
return;
}
this.Operators = operatorsResult.Data;
}

[RelayCommand]
Expand Down
Loading
Loading