diff --git a/TransactionProcessor.Aggregates/MerchantAggregate.cs b/TransactionProcessor.Aggregates/MerchantAggregate.cs index 310f4d82..5e081fa6 100644 --- a/TransactionProcessor.Aggregates/MerchantAggregate.cs +++ b/TransactionProcessor.Aggregates/MerchantAggregate.cs @@ -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(), @@ -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 aggregateAddress in aggregate.Addresses){ if (newAddress == aggregateAddress.Value){ diff --git a/TransactionProcessor.BusinessLogic.Tests/Mediator/DummyVoucherDomainService.cs b/TransactionProcessor.BusinessLogic.Tests/Mediator/DummyVoucherDomainService.cs index 5e1e0b14..c9763b15 100644 --- a/TransactionProcessor.BusinessLogic.Tests/Mediator/DummyVoucherDomainService.cs +++ b/TransactionProcessor.BusinessLogic.Tests/Mediator/DummyVoucherDomainService.cs @@ -1,5 +1,6 @@ using SimpleResults; using TransactionProcessor.BusinessLogic.Manager; +using TransactionProcessor.BusinessLogic.Requests; namespace TransactionProcessor.BusinessLogic.Tests.Mediator; @@ -11,14 +12,7 @@ namespace TransactionProcessor.BusinessLogic.Tests.Mediator; public class DummyVoucherDomainService : IVoucherDomainService { - public async Task> IssueVoucher(Guid voucherId, - Guid operatorId, - Guid estateId, - Guid transactionId, - DateTime issuedDateTime, - Decimal value, - String recipientEmail, - String recipientMobile, + public async Task> IssueVoucher(VoucherCommands.IssueVoucherCommand command, CancellationToken cancellationToken) { return Result.Success(new IssueVoucherResponse()); } diff --git a/TransactionProcessor.BusinessLogic.Tests/RequestHandler/VoucherManagementRequestHandlerTests.cs b/TransactionProcessor.BusinessLogic.Tests/RequestHandler/VoucherManagementRequestHandlerTests.cs index a9bb0481..bb50aba7 100644 --- a/TransactionProcessor.BusinessLogic.Tests/RequestHandler/VoucherManagementRequestHandlerTests.cs +++ b/TransactionProcessor.BusinessLogic.Tests/RequestHandler/VoucherManagementRequestHandlerTests.cs @@ -4,6 +4,7 @@ using Shouldly; using TransactionProcessor.BusinessLogic.Manager; using TransactionProcessor.BusinessLogic.RequestHandlers; +using TransactionProcessor.BusinessLogic.Requests; using TransactionProcessor.BusinessLogic.Services; using Xunit; @@ -19,9 +20,7 @@ public async Task VoucherManagementRequestHandler_IssueVoucherRequest_IsHandled( { Mock voucherDomainService = new Mock(); Mock voucherManagementManager = new Mock(); - voucherDomainService.Setup(v => v.IssueVoucher(It.IsAny(), It.IsAny(), It.IsAny(), - It.IsAny(), It.IsAny(), - It.IsAny(), It.IsAny(), It.IsAny(), + voucherDomainService.Setup(v => v.IssueVoucher(It.IsAny(), It.IsAny())).ReturnsAsync(TestData.IssueVoucherResponse); VoucherManagementRequestHandler handler = new VoucherManagementRequestHandler(voucherDomainService.Object, voucherManagementManager.Object); diff --git a/TransactionProcessor.BusinessLogic.Tests/Services/VoucherDomainServiceTests.cs b/TransactionProcessor.BusinessLogic.Tests/Services/VoucherDomainServiceTests.cs index c1f4de1e..a78babc1 100644 --- a/TransactionProcessor.BusinessLogic.Tests/Services/VoucherDomainServiceTests.cs +++ b/TransactionProcessor.BusinessLogic.Tests/Services/VoucherDomainServiceTests.cs @@ -56,14 +56,7 @@ public async Task VoucherDomainService_IssueVoucher_EstateWithNoOperators_ErrorT this.AggregateService.Setup(v => v.GetLatest(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(new VoucherAggregate())); this.AggregateService.Setup(f => f.Get(It.IsAny(), It.IsAny())).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(); } @@ -111,14 +104,7 @@ public async Task VoucherDomainService_IssueVoucher_InvalidEstate_ErrorThrown() this.AggregateService.Setup(v => v.GetLatest(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(new VoucherAggregate())); this.AggregateService.Setup(f => f.Get(It.IsAny(), It.IsAny())).ReturnsAsync(TestData.Aggregates.EmptyEstateAggregate); - Result result = await this.VoucherDomainService.IssueVoucher(TestData.VoucherId, - TestData.OperatorId, - TestData.EstateId, - TestData.TransactionId, - TestData.IssuedDateTime, - TestData.Value, - TestData.RecipientEmail, - TestData.RecipientMobile, + Result result = await this.VoucherDomainService.IssueVoucher(TestData.IssueVoucherCommand, CancellationToken.None); result.IsFailed.ShouldBeTrue(); } @@ -128,14 +114,7 @@ public async Task VoucherDomainService_IssueVoucher_OperatorNotSupportedByEstate this.AggregateService.Setup(v => v.GetLatest(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(new VoucherAggregate())); this.AggregateService.Setup(f => f.Get(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.Aggregates.EstateAggregateWithOperator())); - Result result = await this.VoucherDomainService.IssueVoucher(TestData.VoucherId, - TestData.OperatorId2, - TestData.EstateId, - TestData.TransactionId, - TestData.IssuedDateTime, - TestData.Value, - TestData.RecipientEmail, - TestData.RecipientMobile, + Result result = await this.VoucherDomainService.IssueVoucher(TestData.IssueVoucherCommand, CancellationToken.None); result.IsFailed.ShouldBeTrue(); } @@ -147,14 +126,7 @@ public async Task VoucherDomainService_IssueVoucher_VoucherIssued() { this.AggregateService.Setup(v => v.Save(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success); this.AggregateService.Setup(f => f.Get(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.Aggregates.EstateAggregateWithOperator())); - Result result = await this.VoucherDomainService.IssueVoucher(TestData.VoucherId, - TestData.OperatorId, - TestData.EstateId, - TestData.TransactionId, - TestData.IssuedDateTime, - TestData.Value, - TestData.RecipientEmail, - TestData.RecipientMobile, + Result result = await this.VoucherDomainService.IssueVoucher(TestData.IssueVoucherCommand, CancellationToken.None); result.IsSuccess.ShouldBeTrue(); diff --git a/TransactionProcessor.BusinessLogic/RequestHandlers/VoucherManagementRequestHandler.cs b/TransactionProcessor.BusinessLogic/RequestHandlers/VoucherManagementRequestHandler.cs index 3853f21e..47b4f263 100644 --- a/TransactionProcessor.BusinessLogic/RequestHandlers/VoucherManagementRequestHandler.cs +++ b/TransactionProcessor.BusinessLogic/RequestHandlers/VoucherManagementRequestHandler.cs @@ -53,14 +53,7 @@ public VoucherManagementRequestHandler(IVoucherDomainService voucherDomainServic public async Task> 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); } diff --git a/TransactionProcessor.BusinessLogic/Services/VoucherDomainService.cs b/TransactionProcessor.BusinessLogic/Services/VoucherDomainService.cs index 93f26a95..ddc0fc1f 100644 --- a/TransactionProcessor.BusinessLogic/Services/VoucherDomainService.cs +++ b/TransactionProcessor.BusinessLogic/Services/VoucherDomainService.cs @@ -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; @@ -23,37 +24,9 @@ public interface IVoucherDomainService { #region Methods - /// - /// Issues the voucher. - /// - /// The voucher identifier. - /// The operator identifier. - /// The estate identifier. - /// The transaction identifier. - /// The issued date time. - /// The value. - /// The recipient email. - /// The recipient mobile. - /// The cancellation token. - /// - Task> IssueVoucher(Guid voucherId, - Guid operatorId, - Guid estateId, - Guid transactionId, - DateTime issuedDateTime, - Decimal value, - String recipientEmail, - String recipientMobile, - CancellationToken cancellationToken); - - /// - /// Redeems the voucher. - /// - /// The estate identifier. - /// The voucher code. - /// The redeemed date time. - /// The cancellation token. - /// + Task> IssueVoucher(VoucherCommands.IssueVoucherCommand command, + CancellationToken cancellationToken); + Task> RedeemVoucher(Guid estateId, String voucherCode, DateTime redeemedDateTime, @@ -81,23 +54,19 @@ public VoucherDomainService(Func aggregateService, #region Methods - public async Task> IssueVoucher(Guid voucherId, Guid operatorId, Guid estateId, - Guid transactionId, - DateTime issuedDateTime, - Decimal value, - String recipientEmail, String recipientMobile, CancellationToken cancellationToken) { + public async Task> IssueVoucher(VoucherCommands.IssueVoucherCommand command, CancellationToken cancellationToken) { try{ - Result validateResult = await this.ValidateVoucherIssue(estateId, operatorId, cancellationToken); + Result validateResult = await this.ValidateVoucherIssue(command.EstateId, command.OperatorId, cancellationToken); if (validateResult.IsFailed) return ResultHelpers.CreateFailure(validateResult); - Result voucherResult = await DomainServiceHelper.GetAggregateOrFailure(ct => this.AggregateService.GetLatest(voucherId, ct), voucherId, cancellationToken, false); + Result voucherResult = await DomainServiceHelper.GetAggregateOrFailure(ct => this.AggregateService.GetLatest(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); @@ -109,7 +78,7 @@ public async Task> 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); @@ -122,7 +91,7 @@ public async Task> IssueVoucher(Guid voucherId, Gui ExpiryDate = voucherModel.ExpiryDate, Message = voucherModel.Message, VoucherCode = voucherModel.VoucherCode, - VoucherId = voucherId + VoucherId = command.VoucherId }); } catch (Exception ex)