diff --git a/TransactionProcessor.BusinessLogic/Services/MerchantDomainService.cs b/TransactionProcessor.BusinessLogic/Services/MerchantDomainService.cs index 935d4934..d882635f 100644 --- a/TransactionProcessor.BusinessLogic/Services/MerchantDomainService.cs +++ b/TransactionProcessor.BusinessLogic/Services/MerchantDomainService.cs @@ -79,20 +79,11 @@ public MerchantDomainService(Func aggregateService, public async Task AddDeviceToMerchant(MerchantCommands.AddMerchantDeviceCommand command, CancellationToken cancellationToken) { try { - Result estateResult = await DomainServiceHelper.GetAggregateOrFailure(ct => this.AggregateService.Get(command.EstateId, ct), command.EstateId, cancellationToken); - if (estateResult.IsFailed) - return ResultHelpers.CreateFailure(estateResult); + Result contextResult = await this.GetValidatedMerchantContext(command.EstateId, command.MerchantId, cancellationToken); + if (contextResult.IsFailed) + return ResultHelpers.CreateFailure(contextResult); - Result merchantResult = await DomainServiceHelper.GetAggregateOrFailure(ct => this.AggregateService.GetLatest(command.MerchantId, ct), command.MerchantId, cancellationToken); - if (merchantResult.IsFailed) - return ResultHelpers.CreateFailure(merchantResult); - - EstateAggregate estateAggregate = estateResult.Data; - MerchantAggregate merchantAggregate = merchantResult.Data; - - Result result = this.ValidateEstateAndMerchant(estateAggregate, merchantAggregate); - if (result.IsFailed) - return ResultHelpers.CreateFailure(result); + MerchantAggregate merchantAggregate = contextResult.Data.MerchantAggregate; Guid deviceId = Guid.NewGuid(); Result stateResult = merchantAggregate.AddDevice(deviceId, command.RequestDto.DeviceIdentifier); @@ -111,23 +102,15 @@ public async Task AddDeviceToMerchant(MerchantCommands.AddMerchantDevice } public async Task AssignOperatorToMerchant(MerchantCommands.AssignOperatorToMerchantCommand command, - CancellationToken cancellationToken) + CancellationToken cancellationToken) { try { - Result estateResult = await DomainServiceHelper.GetAggregateOrFailure(ct => this.AggregateService.Get(command.EstateId, ct), command.EstateId, cancellationToken); - if (estateResult.IsFailed) - return ResultHelpers.CreateFailure(estateResult); - - Result merchantResult = await DomainServiceHelper.GetAggregateOrFailure(ct => this.AggregateService.GetLatest(command.MerchantId, ct), command.MerchantId, cancellationToken); - if (merchantResult.IsFailed) - return ResultHelpers.CreateFailure(merchantResult); + Result contextResult = await this.GetValidatedMerchantContext(command.EstateId, command.MerchantId, cancellationToken); + if (contextResult.IsFailed) + return ResultHelpers.CreateFailure(contextResult); - EstateAggregate estateAggregate = estateResult.Data; - MerchantAggregate merchantAggregate = merchantResult.Data; - - Result result = this.ValidateEstateAndMerchant(estateAggregate, merchantAggregate); - if (result.IsFailed) - return ResultHelpers.CreateFailure(result); + EstateAggregate estateAggregate = contextResult.Data.EstateAggregate; + MerchantAggregate merchantAggregate = contextResult.Data.MerchantAggregate; (Result validationResult, String operatorName) = await this.GetOperatorAssignmentDetails(command, estateAggregate, cancellationToken); if (validationResult.IsFailed) @@ -265,20 +248,11 @@ public async Task CreateMerchantUser(MerchantCommands.CreateMerchantUser try { CreateUserRequest createUserRequest = this.BuildMerchantUserRequest(command); - Result estateResult = await DomainServiceHelper.GetAggregateOrFailure(ct => this.AggregateService.Get(command.EstateId, ct), command.EstateId, cancellationToken); - if (estateResult.IsFailed) - return ResultHelpers.CreateFailure(estateResult); + Result contextResult = await this.GetValidatedMerchantContext(command.EstateId, command.MerchantId, cancellationToken); + if (contextResult.IsFailed) + return ResultHelpers.CreateFailure(contextResult); - Result merchantResult = await DomainServiceHelper.GetAggregateOrFailure(ct => this.AggregateService.GetLatest(command.MerchantId, ct), command.MerchantId, cancellationToken); - if (merchantResult.IsFailed) - return ResultHelpers.CreateFailure(merchantResult); - - EstateAggregate estateAggregate = estateResult.Data; - MerchantAggregate merchantAggregate = merchantResult.Data; - - Result validateResult = this.ValidateEstateAndMerchant(estateAggregate, merchantAggregate); - if (validateResult.IsFailed) - return ResultHelpers.CreateFailure(validateResult); + MerchantAggregate merchantAggregate = contextResult.Data.MerchantAggregate; Result getUserResult = await this.CreateMerchantSecurityUser(createUserRequest, cancellationToken); if (getUserResult.IsFailed) @@ -343,21 +317,11 @@ public async Task MakeMerchantDeposit(MerchantCommands.MakeMerchantDepos { try { - Result estateResult = await DomainServiceHelper.GetAggregateOrFailure(ct => this.AggregateService.Get(command.EstateId, ct), command.EstateId, cancellationToken); - if (estateResult.IsFailed) - return ResultHelpers.CreateFailure(estateResult); + Result contextResult = await this.GetValidatedMerchantContext(command.EstateId, command.MerchantId, cancellationToken, false); + if (contextResult.IsFailed) + return ResultHelpers.CreateFailure(contextResult); - Result merchantResult = await DomainServiceHelper.GetAggregateOrFailure(ct => this.AggregateService.Get(command.MerchantId, ct), command.MerchantId, cancellationToken); - if (merchantResult.IsFailed) - return ResultHelpers.CreateFailure(merchantResult); - - EstateAggregate estateAggregate = estateResult.Data; - MerchantAggregate merchantAggregate = merchantResult.Data; - - Result validateResult = - this.ValidateEstateAndMerchant(estateAggregate, merchantAggregate); - if (validateResult.IsFailed) - return ResultHelpers.CreateFailure(validateResult); + MerchantAggregate merchantAggregate = contextResult.Data.MerchantAggregate; Result depositListResult = await this.GetOrCreateMerchantDepositList(command, merchantAggregate, cancellationToken); if (depositListResult.IsFailed) @@ -418,20 +382,11 @@ public async Task MakeMerchantWithdrawal(MerchantCommands.MakeMerchantWi public async Task AddContractToMerchant(MerchantCommands.AddMerchantContractCommand command, CancellationToken cancellationToken) { try { - Result estateResult = await DomainServiceHelper.GetAggregateOrFailure(ct => this.AggregateService.Get(command.EstateId, ct), command.EstateId, cancellationToken); - if (estateResult.IsFailed) - return ResultHelpers.CreateFailure(estateResult); + Result contextResult = await this.GetValidatedMerchantContext(command.EstateId, command.MerchantId, cancellationToken); + if (contextResult.IsFailed) + return ResultHelpers.CreateFailure(contextResult); - Result merchantResult = await DomainServiceHelper.GetAggregateOrFailure(ct => this.AggregateService.GetLatest(command.MerchantId, ct), command.MerchantId, cancellationToken); - if (merchantResult.IsFailed) - return ResultHelpers.CreateFailure(merchantResult); - - EstateAggregate estateAggregate = estateResult.Data; - MerchantAggregate merchantAggregate = merchantResult.Data; - - Result validateResult = this.ValidateEstateAndMerchant(estateAggregate, merchantAggregate); - if (validateResult.IsFailed) - return ResultHelpers.CreateFailure(validateResult); + MerchantAggregate merchantAggregate = contextResult.Data.MerchantAggregate; Result contractResult = await this.GetCreatedContract(command.RequestDto.ContractId, cancellationToken); if (contractResult.IsFailed) @@ -455,20 +410,11 @@ public async Task AddContractToMerchant(MerchantCommands.AddMerchantCont public async Task UpdateMerchant(MerchantCommands.UpdateMerchantCommand command, CancellationToken cancellationToken) { try { - Result estateResult = await DomainServiceHelper.GetAggregateOrFailure(ct => this.AggregateService.Get(command.EstateId, ct), command.EstateId, cancellationToken); - if (estateResult.IsFailed) - return ResultHelpers.CreateFailure(estateResult); - - Result merchantResult = await DomainServiceHelper.GetAggregateOrFailure(ct => this.AggregateService.GetLatest(command.MerchantId, ct), command.MerchantId, cancellationToken); - if (merchantResult.IsFailed) - return ResultHelpers.CreateFailure(merchantResult); - - EstateAggregate estateAggregate = estateResult.Data; - MerchantAggregate merchantAggregate = merchantResult.Data; + Result contextResult = await this.GetValidatedMerchantContext(command.EstateId, command.MerchantId, cancellationToken); + if (contextResult.IsFailed) + return ResultHelpers.CreateFailure(contextResult); - Result result = this.ValidateEstateAndMerchant(estateAggregate, merchantAggregate); - if (result.IsFailed) - return ResultHelpers.CreateFailure(result); + MerchantAggregate merchantAggregate = contextResult.Data.MerchantAggregate; Result stateResult = merchantAggregate.UpdateMerchant(command.RequestDto.Name); if (stateResult.IsFailed) @@ -493,21 +439,11 @@ public async Task AddMerchantAddress(MerchantCommands.AddMerchantAddress { try { - Result estateResult = await DomainServiceHelper.GetAggregateOrFailure(ct => this.AggregateService.Get(command.EstateId, ct), command.EstateId, cancellationToken); - if (estateResult.IsFailed) - return ResultHelpers.CreateFailure(estateResult); - - Result merchantResult = await DomainServiceHelper.GetAggregateOrFailure(ct => this.AggregateService.GetLatest(command.MerchantId, ct), command.MerchantId, cancellationToken); - if (merchantResult.IsFailed) - return ResultHelpers.CreateFailure(merchantResult); + Result contextResult = await this.GetValidatedMerchantContext(command.EstateId, command.MerchantId, cancellationToken); + if (contextResult.IsFailed) + return ResultHelpers.CreateFailure(contextResult); - EstateAggregate estateAggregate = estateResult.Data; - MerchantAggregate merchantAggregate = merchantResult.Data; - - Result result = - this.ValidateEstateAndMerchant(estateAggregate, merchantAggregate); - if (result.IsFailed) - return ResultHelpers.CreateFailure(result); + MerchantAggregate merchantAggregate = contextResult.Data.MerchantAggregate; Address address = Address.Create(Guid.Empty, command.RequestDto.AddressLine1, command.RequestDto.AddressLine2, command.RequestDto.AddressLine3, command.RequestDto.AddressLine4, command.RequestDto.Town, command.RequestDto.Region, command.RequestDto.PostalCode, command.RequestDto.Country); @@ -531,21 +467,11 @@ public async Task UpdateMerchantAddress(MerchantCommands.UpdateMerchantA { try { - Result estateResult = await DomainServiceHelper.GetAggregateOrFailure(ct => this.AggregateService.Get(command.EstateId, ct), command.EstateId, cancellationToken); - if (estateResult.IsFailed) - return ResultHelpers.CreateFailure(estateResult); + Result contextResult = await this.GetValidatedMerchantContext(command.EstateId, command.MerchantId, cancellationToken); + if (contextResult.IsFailed) + return ResultHelpers.CreateFailure(contextResult); - Result merchantResult = await DomainServiceHelper.GetAggregateOrFailure(ct => this.AggregateService.GetLatest(command.MerchantId, ct), command.MerchantId, cancellationToken); - if (merchantResult.IsFailed) - return ResultHelpers.CreateFailure(merchantResult); - - EstateAggregate estateAggregate = estateResult.Data; - MerchantAggregate merchantAggregate = merchantResult.Data; - - Result result = - this.ValidateEstateAndMerchant(estateAggregate, merchantAggregate); - if (result.IsFailed) - return ResultHelpers.CreateFailure(result); + MerchantAggregate merchantAggregate = contextResult.Data.MerchantAggregate; Address address = Address.Create(command.AddressId, command.RequestDto.AddressLine1, command.RequestDto.AddressLine2, command.RequestDto.AddressLine3, command.RequestDto.AddressLine4, command.RequestDto.Town, command.RequestDto.Region, command.RequestDto.PostalCode, command.RequestDto.Country); @@ -569,21 +495,11 @@ public async Task AddMerchantContact(MerchantCommands.AddMerchantContact { try { - Result estateResult = await DomainServiceHelper.GetAggregateOrFailure(ct => this.AggregateService.Get(command.EstateId, ct), command.EstateId, cancellationToken); - if (estateResult.IsFailed) - return ResultHelpers.CreateFailure(estateResult); + Result contextResult = await this.GetValidatedMerchantContext(command.EstateId, command.MerchantId, cancellationToken); + if (contextResult.IsFailed) + return ResultHelpers.CreateFailure(contextResult); - Result merchantResult = await DomainServiceHelper.GetAggregateOrFailure(ct => this.AggregateService.GetLatest(command.MerchantId, ct), command.MerchantId, cancellationToken); - if (merchantResult.IsFailed) - return ResultHelpers.CreateFailure(merchantResult); - - EstateAggregate estateAggregate = estateResult.Data; - MerchantAggregate merchantAggregate = merchantResult.Data; - - Result result = - this.ValidateEstateAndMerchant(estateAggregate, merchantAggregate); - if (result.IsFailed) - return ResultHelpers.CreateFailure(result); + MerchantAggregate merchantAggregate = contextResult.Data.MerchantAggregate; Result stateResult = merchantAggregate.AddContact(command.RequestDto.ContactName, command.RequestDto.PhoneNumber, @@ -606,20 +522,11 @@ public async Task AddMerchantContact(MerchantCommands.AddMerchantContact public async Task UpdateMerchantContact(MerchantCommands.UpdateMerchantContactCommand command, CancellationToken cancellationToken) { try { - Result estateResult = await DomainServiceHelper.GetAggregateOrFailure(ct => this.AggregateService.Get(command.EstateId, ct), command.EstateId, cancellationToken); - if (estateResult.IsFailed) - return ResultHelpers.CreateFailure(estateResult); + Result contextResult = await this.GetValidatedMerchantContext(command.EstateId, command.MerchantId, cancellationToken); + if (contextResult.IsFailed) + return ResultHelpers.CreateFailure(contextResult); - Result merchantResult = await DomainServiceHelper.GetAggregateOrFailure(ct => this.AggregateService.GetLatest(command.MerchantId, ct), command.MerchantId, cancellationToken); - if (merchantResult.IsFailed) - return ResultHelpers.CreateFailure(merchantResult); - - EstateAggregate estateAggregate = estateResult.Data; - MerchantAggregate merchantAggregate = merchantResult.Data; - - Result result = this.ValidateEstateAndMerchant(estateAggregate, merchantAggregate); - if (result.IsFailed) - return ResultHelpers.CreateFailure(result); + MerchantAggregate merchantAggregate = contextResult.Data.MerchantAggregate; Result stateResult = merchantAggregate.UpdateContact(command.ContactId, command.RequestDto.ContactName, command.RequestDto.EmailAddress, command.RequestDto.PhoneNumber); if (stateResult.IsFailed) @@ -639,20 +546,11 @@ public async Task UpdateMerchantContact(MerchantCommands.UpdateMerchantC public async Task RemoveOperatorFromMerchant(MerchantCommands.RemoveOperatorFromMerchantCommand command, CancellationToken cancellationToken) { try { - Result estateResult = await DomainServiceHelper.GetAggregateOrFailure(ct => this.AggregateService.Get(command.EstateId, ct), command.EstateId, cancellationToken); - if (estateResult.IsFailed) - return ResultHelpers.CreateFailure(estateResult); + Result contextResult = await this.GetValidatedMerchantContext(command.EstateId, command.MerchantId, cancellationToken); + if (contextResult.IsFailed) + return ResultHelpers.CreateFailure(contextResult); - Result merchantResult = await DomainServiceHelper.GetAggregateOrFailure(ct => this.AggregateService.GetLatest(command.MerchantId, ct), command.MerchantId, cancellationToken); - if (merchantResult.IsFailed) - return ResultHelpers.CreateFailure(merchantResult); - - EstateAggregate estateAggregate = estateResult.Data; - MerchantAggregate merchantAggregate = merchantResult.Data; - - Result result = this.ValidateEstateAndMerchant(estateAggregate, merchantAggregate); - if (result.IsFailed) - return ResultHelpers.CreateFailure(result); + MerchantAggregate merchantAggregate = contextResult.Data.MerchantAggregate; Result stateResult = merchantAggregate.RemoveOperator(command.OperatorId); if (stateResult.IsFailed) @@ -673,21 +571,11 @@ public async Task RemoveContractFromMerchant(MerchantCommands.RemoveMerc { try { - Result estateResult = await DomainServiceHelper.GetAggregateOrFailure(ct => this.AggregateService.Get(command.EstateId, ct), command.EstateId, cancellationToken); - if (estateResult.IsFailed) - return ResultHelpers.CreateFailure(estateResult); - - Result merchantResult = await DomainServiceHelper.GetAggregateOrFailure(ct => this.AggregateService.GetLatest(command.MerchantId, ct), command.MerchantId, cancellationToken); - if (merchantResult.IsFailed) - return ResultHelpers.CreateFailure(merchantResult); - - EstateAggregate estateAggregate = estateResult.Data; - MerchantAggregate merchantAggregate = merchantResult.Data; + Result contextResult = await this.GetValidatedMerchantContext(command.EstateId, command.MerchantId, cancellationToken); + if (contextResult.IsFailed) + return ResultHelpers.CreateFailure(contextResult); - Result result = - this.ValidateEstateAndMerchant(estateAggregate, merchantAggregate); - if (result.IsFailed) - return ResultHelpers.CreateFailure(result); + MerchantAggregate merchantAggregate = contextResult.Data.MerchantAggregate; Result stateResult = merchantAggregate.RemoveContract(command.ContractId); if (stateResult.IsFailed) @@ -709,21 +597,11 @@ public async Task UpdateMerchantOpeningHours(MerchantCommands.UpdateMerc CancellationToken cancellationToken) { try { - Result estateResult = await DomainServiceHelper.GetAggregateOrFailure(ct => this.AggregateService.Get(command.EstateId, ct), command.EstateId, cancellationToken); - if (estateResult.IsFailed) - return ResultHelpers.CreateFailure(estateResult); - - Result merchantResult = await DomainServiceHelper.GetAggregateOrFailure(ct => this.AggregateService.GetLatest(command.MerchantId, ct), command.MerchantId, cancellationToken); - if (merchantResult.IsFailed) - return ResultHelpers.CreateFailure(merchantResult); - - EstateAggregate estateAggregate = estateResult.Data; - MerchantAggregate merchantAggregate = merchantResult.Data; + Result contextResult = await this.GetValidatedMerchantContext(command.EstateId, command.MerchantId, cancellationToken); + if (contextResult.IsFailed) + return ResultHelpers.CreateFailure(contextResult); - Result result = - this.ValidateEstateAndMerchant(estateAggregate, merchantAggregate); - if (result.IsFailed) - return ResultHelpers.CreateFailure(result); + MerchantAggregate merchantAggregate = contextResult.Data.MerchantAggregate; var dayMappings = new List<(DayOfWeek Day, TransactionProcessor.DataTransferObjects.Requests.Merchant.OpeningHours Hours)>() { (DayOfWeek.Monday, command.RequestDto.Monday), (DayOfWeek.Tuesday, command.RequestDto.Tuesday), @@ -756,20 +634,9 @@ public async Task UpdateMerchantOpeningHours(MerchantCommands.UpdateMerc public async Task CreateMerchantSchedule(MerchantCommands.CreateMerchantScheduleCommand command, CancellationToken cancellationToken) { try { - Result estateResult = await DomainServiceHelper.GetAggregateOrFailure(ct => this.AggregateService.Get(command.EstateId, ct), command.EstateId, cancellationToken); - if (estateResult.IsFailed) - return ResultHelpers.CreateFailure(estateResult); - - Result merchantResult = await DomainServiceHelper.GetAggregateOrFailure(ct => this.AggregateService.GetLatest(command.MerchantId, ct), command.MerchantId, cancellationToken); - if (merchantResult.IsFailed) - return ResultHelpers.CreateFailure(merchantResult); - - EstateAggregate estateAggregate = estateResult.Data; - MerchantAggregate merchantAggregate = merchantResult.Data; - - Result result = this.ValidateEstateAndMerchant(estateAggregate, merchantAggregate); - if (result.IsFailed) - return ResultHelpers.CreateFailure(result); + Result contextResult = await this.GetValidatedMerchantContext(command.EstateId, command.MerchantId, cancellationToken); + if (contextResult.IsFailed) + return ResultHelpers.CreateFailure(contextResult); Guid merchantScheduleId = IdGenerationService.GenerateMerchantScheduleAggregateId(command.EstateId, command.MerchantId, command.RequestDto.Year); Result merchantScheduleResult = await DomainServiceHelper.GetAggregateOrFailure(ct => this.AggregateService.GetLatest(merchantScheduleId, ct), merchantScheduleId, cancellationToken, false); @@ -795,20 +662,9 @@ public async Task CreateMerchantSchedule(MerchantCommands.CreateMerchant public async Task UpdateMerchantSchedule(MerchantCommands.UpdateMerchantScheduleCommand command, CancellationToken cancellationToken) { try { - Result estateResult = await DomainServiceHelper.GetAggregateOrFailure(ct => this.AggregateService.Get(command.EstateId, ct), command.EstateId, cancellationToken); - if (estateResult.IsFailed) - return ResultHelpers.CreateFailure(estateResult); - - Result merchantResult = await DomainServiceHelper.GetAggregateOrFailure(ct => this.AggregateService.GetLatest(command.MerchantId, ct), command.MerchantId, cancellationToken); - if (merchantResult.IsFailed) - return ResultHelpers.CreateFailure(merchantResult); - - EstateAggregate estateAggregate = estateResult.Data; - MerchantAggregate merchantAggregate = merchantResult.Data; - - Result result = this.ValidateEstateAndMerchant(estateAggregate, merchantAggregate); - if (result.IsFailed) - return ResultHelpers.CreateFailure(result); + Result contextResult = await this.GetValidatedMerchantContext(command.EstateId, command.MerchantId, cancellationToken); + if (contextResult.IsFailed) + return ResultHelpers.CreateFailure(contextResult); Guid merchantScheduleId = IdGenerationService.GenerateMerchantScheduleAggregateId(command.EstateId, command.MerchantId, command.Year); Result merchantScheduleResult = await DomainServiceHelper.GetAggregateOrFailure(ct => this.AggregateService.GetLatest(merchantScheduleId, ct), merchantScheduleId, cancellationToken, false); @@ -840,6 +696,32 @@ private Result ApplyOpeningHoursIfPresent(MerchantAggregate merchantAggregate, D return merchantAggregate.SetDayOpeningHours(day, hours.Opening, hours.Closing); } + private sealed record ValidatedMerchantContext(EstateAggregate EstateAggregate, + MerchantAggregate MerchantAggregate); + + private async Task> GetValidatedMerchantContext(Guid estateId, + Guid merchantId, + CancellationToken cancellationToken, + Boolean useLatestMerchant = true) { + Result estateResult = await DomainServiceHelper.GetAggregateOrFailure(ct => this.AggregateService.Get(estateId, ct), estateId, cancellationToken); + if (estateResult.IsFailed) + return ResultHelpers.CreateFailure(estateResult); + + Func>> getMerchant = useLatestMerchant + ? ct => this.AggregateService.GetLatest(merchantId, ct) + : ct => this.AggregateService.Get(merchantId, ct); + + Result merchantResult = await DomainServiceHelper.GetAggregateOrFailure(getMerchant, merchantId, cancellationToken); + if (merchantResult.IsFailed) + return ResultHelpers.CreateFailure(merchantResult); + + Result validateResult = this.ValidateEstateAndMerchant(estateResult.Data, merchantResult.Data); + if (validateResult.IsFailed) + return ResultHelpers.CreateFailure(validateResult); + + return Result.Success(new ValidatedMerchantContext(estateResult.Data, merchantResult.Data)); + } + private Result ValidateEstateAndMerchant(EstateAggregate estateAggregate, MerchantAggregate merchantAggregate) { @@ -896,17 +778,9 @@ private async Task> GetOrCreateMerchantDepo private async Task> GetMerchantDepositListForWithdrawal(MerchantCommands.MakeMerchantWithdrawalCommand command, CancellationToken cancellationToken) { - Result estateResult = await DomainServiceHelper.GetAggregateOrFailure(ct => this.AggregateService.Get(command.EstateId, ct), command.EstateId, cancellationToken); - if (estateResult.IsFailed) - return ResultHelpers.CreateFailure(estateResult); - - Result merchantResult = await DomainServiceHelper.GetAggregateOrFailure(ct => this.AggregateService.Get(command.MerchantId, ct), command.MerchantId, cancellationToken); - if (merchantResult.IsFailed) - return ResultHelpers.CreateFailure(merchantResult); - - Result validateResult = this.ValidateEstateAndMerchant(estateResult.Data, merchantResult.Data); - if (validateResult.IsFailed) - return ResultHelpers.CreateFailure(validateResult); + Result contextResult = await this.GetValidatedMerchantContext(command.EstateId, command.MerchantId, cancellationToken, false); + if (contextResult.IsFailed) + return ResultHelpers.CreateFailure(contextResult); Result getDepositListResult = await DomainServiceHelper.GetAggregateOrFailure(ct => this.AggregateService.GetLatest(command.MerchantId, ct), command.MerchantId, cancellationToken); if (getDepositListResult.IsFailed) @@ -949,21 +823,11 @@ public async Task SwapMerchantDevice(MerchantCommands.SwapMerchantDevice { try { - Result estateResult = await DomainServiceHelper.GetAggregateOrFailure(ct => this.AggregateService.Get(command.EstateId, ct), command.EstateId, cancellationToken); - if (estateResult.IsFailed) - return ResultHelpers.CreateFailure(estateResult); - - Result merchantResult = await DomainServiceHelper.GetAggregateOrFailure(ct => this.AggregateService.GetLatest(command.MerchantId, ct), command.MerchantId, cancellationToken); - if (merchantResult.IsFailed) - return ResultHelpers.CreateFailure(merchantResult); - - EstateAggregate estateAggregate = estateResult.Data; - MerchantAggregate merchantAggregate = merchantResult.Data; + Result contextResult = await this.GetValidatedMerchantContext(command.EstateId, command.MerchantId, cancellationToken); + if (contextResult.IsFailed) + return ResultHelpers.CreateFailure(contextResult); - Result result = - this.ValidateEstateAndMerchant(estateAggregate, merchantAggregate); - if (result.IsFailed) - return ResultHelpers.CreateFailure(result); + MerchantAggregate merchantAggregate = contextResult.Data.MerchantAggregate; Result stateResult = merchantAggregate.SwapDevice(command.DeviceIdentifier, command.RequestDto.NewDeviceIdentifier); if (stateResult.IsFailed)