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
14 changes: 3 additions & 11 deletions TransactionProcessor.Aggregates/MerchantAggregate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,7 @@ public static Result AddAddress(this MerchantAggregate aggregate, AddressModel a
if (result.IsFailed)
return result;

if (IsDuplicateAddress(aggregate, address.AddressLine1, address.AddressLine2, address.AddressLine3, address.AddressLine4,
address.Town,address.Region, address.PostalCode, address.Country))
if (IsDuplicateAddress(aggregate, address))
return Result.Success(); // No need to throw an error, just ignore as we already have this address

MerchantDomainEvents.AddressAddedEvent addressAddedEvent = new(aggregate.AggregateId, aggregate.EstateId, Guid.NewGuid(),
Expand Down Expand Up @@ -544,17 +543,10 @@ private static Result EnsureMerchantHasBeenCreated(this MerchantAggregate aggreg
return Result.Success();
}

private static Boolean IsDuplicateAddress(this MerchantAggregate aggregate, String addressLine1,
String addressLine2,
String addressLine3,
String addressLine4,
String town,
String region,
String postalCode,
String country)
private static Boolean IsDuplicateAddress(this MerchantAggregate aggregate, AddressModel address)
{
// create record of "new" address
Address newAddress = new Address(addressLine1, addressLine2, addressLine3, addressLine4, town, region, postalCode, country);
Address newAddress = new Address(address.AddressLine1, address.AddressLine2, address.AddressLine3, address.AddressLine4, address.Town, address.Region, address.PostalCode, address.Country);

foreach (KeyValuePair<Guid, Address> aggregateAddress in aggregate.Addresses){
if (newAddress == aggregateAddress.Value){
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using SimpleResults;
using TransactionProcessor.BusinessLogic.Manager;
using TransactionProcessor.BusinessLogic.Requests;

namespace TransactionProcessor.BusinessLogic.Tests.Mediator;

Expand All @@ -11,14 +12,7 @@ namespace TransactionProcessor.BusinessLogic.Tests.Mediator;

public class DummyVoucherDomainService : IVoucherDomainService
{
public async Task<Result<IssueVoucherResponse>> IssueVoucher(Guid voucherId,
Guid operatorId,
Guid estateId,
Guid transactionId,
DateTime issuedDateTime,
Decimal value,
String recipientEmail,
String recipientMobile,
public async Task<Result<IssueVoucherResponse>> IssueVoucher(VoucherCommands.IssueVoucherCommand command,
CancellationToken cancellationToken) {
return Result.Success(new IssueVoucherResponse());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Shouldly;
using TransactionProcessor.BusinessLogic.Manager;
using TransactionProcessor.BusinessLogic.RequestHandlers;
using TransactionProcessor.BusinessLogic.Requests;
using TransactionProcessor.BusinessLogic.Services;
using Xunit;

Expand All @@ -19,9 +20,7 @@ public async Task VoucherManagementRequestHandler_IssueVoucherRequest_IsHandled(
{
Mock<IVoucherDomainService> voucherDomainService = new Mock<IVoucherDomainService>();
Mock<IVoucherManagementManager> voucherManagementManager = new Mock<IVoucherManagementManager>();
voucherDomainService.Setup(v => v.IssueVoucher(It.IsAny<Guid>(), It.IsAny<Guid>(), It.IsAny<Guid>(),
It.IsAny<Guid>(), It.IsAny<DateTime>(),
It.IsAny<Decimal>(), It.IsAny<String>(), It.IsAny<String>(),
voucherDomainService.Setup(v => v.IssueVoucher(It.IsAny<VoucherCommands.IssueVoucherCommand>(),
It.IsAny<CancellationToken>())).ReturnsAsync(TestData.IssueVoucherResponse);

VoucherManagementRequestHandler handler = new VoucherManagementRequestHandler(voucherDomainService.Object, voucherManagementManager.Object);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,7 @@ public async Task VoucherDomainService_IssueVoucher_EstateWithNoOperators_ErrorT

this.AggregateService.Setup(v => v.GetLatest<VoucherAggregate>(It.IsAny<Guid>(), It.IsAny<CancellationToken>())).ReturnsAsync(Result.Success(new VoucherAggregate()));
this.AggregateService.Setup(f => f.Get<EstateAggregate>(It.IsAny<Guid>(), It.IsAny<CancellationToken>())).ReturnsAsync(TestData.Aggregates.CreatedEstateAggregate());
var result = await this.VoucherDomainService.IssueVoucher(TestData.VoucherId,
TestData.OperatorId,
TestData.EstateId,
TestData.TransactionId,
TestData.IssuedDateTime,
TestData.Value,
TestData.RecipientEmail,
TestData.RecipientMobile,
var result = await this.VoucherDomainService.IssueVoucher(TestData.IssueVoucherCommand,
CancellationToken.None);
result.IsFailed.ShouldBeTrue();
}
Expand Down Expand Up @@ -111,14 +104,7 @@ public async Task VoucherDomainService_IssueVoucher_InvalidEstate_ErrorThrown()
this.AggregateService.Setup(v => v.GetLatest<VoucherAggregate>(It.IsAny<Guid>(), It.IsAny<CancellationToken>())).ReturnsAsync(Result.Success(new VoucherAggregate()));
this.AggregateService.Setup(f => f.Get<EstateAggregate>(It.IsAny<Guid>(), It.IsAny<CancellationToken>())).ReturnsAsync(TestData.Aggregates.EmptyEstateAggregate);

Result<IssueVoucherResponse> result = await this.VoucherDomainService.IssueVoucher(TestData.VoucherId,
TestData.OperatorId,
TestData.EstateId,
TestData.TransactionId,
TestData.IssuedDateTime,
TestData.Value,
TestData.RecipientEmail,
TestData.RecipientMobile,
Result<IssueVoucherResponse> result = await this.VoucherDomainService.IssueVoucher(TestData.IssueVoucherCommand,
CancellationToken.None);
result.IsFailed.ShouldBeTrue();
}
Expand All @@ -128,14 +114,7 @@ public async Task VoucherDomainService_IssueVoucher_OperatorNotSupportedByEstate
this.AggregateService.Setup(v => v.GetLatest<VoucherAggregate>(It.IsAny<Guid>(), It.IsAny<CancellationToken>())).ReturnsAsync(Result.Success(new VoucherAggregate()));
this.AggregateService.Setup(f => f.Get<EstateAggregate>(It.IsAny<Guid>(), It.IsAny<CancellationToken>())).ReturnsAsync(Result.Success(TestData.Aggregates.EstateAggregateWithOperator()));

Result<IssueVoucherResponse> result = await this.VoucherDomainService.IssueVoucher(TestData.VoucherId,
TestData.OperatorId2,
TestData.EstateId,
TestData.TransactionId,
TestData.IssuedDateTime,
TestData.Value,
TestData.RecipientEmail,
TestData.RecipientMobile,
Result<IssueVoucherResponse> result = await this.VoucherDomainService.IssueVoucher(TestData.IssueVoucherCommand,
CancellationToken.None);
result.IsFailed.ShouldBeTrue();
}
Expand All @@ -147,14 +126,7 @@ public async Task VoucherDomainService_IssueVoucher_VoucherIssued() {
this.AggregateService.Setup(v => v.Save(It.IsAny<VoucherAggregate>(), It.IsAny<CancellationToken>())).ReturnsAsync(Result.Success);
this.AggregateService.Setup(f => f.Get<EstateAggregate>(It.IsAny<Guid>(), It.IsAny<CancellationToken>())).ReturnsAsync(Result.Success(TestData.Aggregates.EstateAggregateWithOperator()));

Result<IssueVoucherResponse> result = await this.VoucherDomainService.IssueVoucher(TestData.VoucherId,
TestData.OperatorId,
TestData.EstateId,
TestData.TransactionId,
TestData.IssuedDateTime,
TestData.Value,
TestData.RecipientEmail,
TestData.RecipientMobile,
Result<IssueVoucherResponse> result = await this.VoucherDomainService.IssueVoucher(TestData.IssueVoucherCommand,
CancellationToken.None);

result.IsSuccess.ShouldBeTrue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,7 @@ public VoucherManagementRequestHandler(IVoucherDomainService voucherDomainServic
public async Task<Result<IssueVoucherResponse>> Handle(VoucherCommands.IssueVoucherCommand command,
CancellationToken cancellationToken)
{
return await this.VoucherDomainService.IssueVoucher(command.VoucherId,
command.OperatorId,
command.EstateId,
command.TransactionId,
command.IssuedDateTime,
command.Value,
command.RecipientEmail,
command.RecipientMobile,
return await this.VoucherDomainService.IssueVoucher(command,
cancellationToken);
}

Expand Down
51 changes: 10 additions & 41 deletions TransactionProcessor.BusinessLogic/Services/VoucherDomainService.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Shared.Results;
using TransactionProcessor.Aggregates;
using TransactionProcessor.BusinessLogic.Common;
using TransactionProcessor.BusinessLogic.Requests;
using TransactionProcessor.Database.Contexts;
using TransactionProcessor.DataTransferObjects.Responses.Estate;
using TransactionProcessor.Models.Estate;
Expand All @@ -23,37 +24,9 @@ public interface IVoucherDomainService
{
#region Methods

/// <summary>
/// Issues the voucher.
/// </summary>
/// <param name="voucherId">The voucher identifier.</param>
/// <param name="operatorId">The operator identifier.</param>
/// <param name="estateId">The estate identifier.</param>
/// <param name="transactionId">The transaction identifier.</param>
/// <param name="issuedDateTime">The issued date time.</param>
/// <param name="value">The value.</param>
/// <param name="recipientEmail">The recipient email.</param>
/// <param name="recipientMobile">The recipient mobile.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns></returns>
Task<Result<IssueVoucherResponse>> IssueVoucher(Guid voucherId,
Guid operatorId,
Guid estateId,
Guid transactionId,
DateTime issuedDateTime,
Decimal value,
String recipientEmail,
String recipientMobile,
CancellationToken cancellationToken);

/// <summary>
/// Redeems the voucher.
/// </summary>
/// <param name="estateId">The estate identifier.</param>
/// <param name="voucherCode">The voucher code.</param>
/// <param name="redeemedDateTime">The redeemed date time.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns></returns>
Task<Result<IssueVoucherResponse>> IssueVoucher(VoucherCommands.IssueVoucherCommand command,
CancellationToken cancellationToken);

Task<Result<RedeemVoucherResponse>> RedeemVoucher(Guid estateId,
String voucherCode,
DateTime redeemedDateTime,
Expand Down Expand Up @@ -81,23 +54,19 @@ public VoucherDomainService(Func<IAggregateService> aggregateService,

#region Methods

public async Task<Result<IssueVoucherResponse>> IssueVoucher(Guid voucherId, Guid operatorId, Guid estateId,
Guid transactionId,
DateTime issuedDateTime,
Decimal value,
String recipientEmail, String recipientMobile, CancellationToken cancellationToken) {
public async Task<Result<IssueVoucherResponse>> IssueVoucher(VoucherCommands.IssueVoucherCommand command, CancellationToken cancellationToken) {
try{
Result<EstateResponse> validateResult = await this.ValidateVoucherIssue(estateId, operatorId, cancellationToken);
Result<EstateResponse> validateResult = await this.ValidateVoucherIssue(command.EstateId, command.OperatorId, cancellationToken);
if (validateResult.IsFailed)
return ResultHelpers.CreateFailure(validateResult);

Result<VoucherAggregate> voucherResult = await DomainServiceHelper.GetAggregateOrFailure(ct => this.AggregateService.GetLatest<VoucherAggregate>(voucherId, ct), voucherId, cancellationToken, false);
Result<VoucherAggregate> voucherResult = await DomainServiceHelper.GetAggregateOrFailure(ct => this.AggregateService.GetLatest<VoucherAggregate>(command.VoucherId, ct), command.VoucherId, cancellationToken, false);
if (voucherResult.IsFailed)
return ResultHelpers.CreateFailure(voucherResult);

VoucherAggregate voucherAggregate = voucherResult.Data;

Result stateResult = voucherAggregate.Generate(operatorId, estateId, transactionId, issuedDateTime, value);
Result stateResult = voucherAggregate.Generate(command.OperatorId, command.EstateId, command.TransactionId, command.IssuedDateTime, command.Value);
if (stateResult.IsFailed)
return ResultHelpers.CreateFailure(stateResult);

Expand All @@ -109,7 +78,7 @@ public async Task<Result<IssueVoucherResponse>> IssueVoucher(Guid voucherId, Gui
if (stateResult.IsFailed)
return ResultHelpers.CreateFailure(stateResult);

stateResult = voucherAggregate.Issue(recipientEmail, recipientMobile, issuedDateTime);
stateResult = voucherAggregate.Issue(command.RecipientEmail, command.RecipientMobile, command.IssuedDateTime);
if (stateResult.IsFailed)
return ResultHelpers.CreateFailure(stateResult);

Expand All @@ -122,7 +91,7 @@ public async Task<Result<IssueVoucherResponse>> IssueVoucher(Guid voucherId, Gui
ExpiryDate = voucherModel.ExpiryDate,
Message = voucherModel.Message,
VoucherCode = voucherModel.VoucherCode,
VoucherId = voucherId
VoucherId = command.VoucherId
});
}
catch (Exception ex)
Expand Down
Loading