From cf9a337f1a5f54af0fcb20b25bd9578db986de1f Mon Sep 17 00:00:00 2001 From: Stuart Ferguson Date: Wed, 31 Dec 2025 19:48:49 +0000 Subject: [PATCH 1/3] refactor Record Callback method --- .../Services/CallbackDomainService.cs | 42 ++++++++++++------- 1 file changed, 27 insertions(+), 15 deletions(-) diff --git a/CallbackHandler.BusinessLogic/Services/CallbackDomainService.cs b/CallbackHandler.BusinessLogic/Services/CallbackDomainService.cs index e11ed04..0aa2660 100644 --- a/CallbackHandler.BusinessLogic/Services/CallbackDomainService.cs +++ b/CallbackHandler.BusinessLogic/Services/CallbackDomainService.cs @@ -17,6 +17,7 @@ namespace CallbackHandler.BusinessLogic.Services; using System; using System.Threading; using System.Threading.Tasks; +using static Microsoft.EntityFrameworkCore.DbLoggerCategory.Database; public interface ICallbackDomainService { @@ -41,14 +42,33 @@ public CallbackDomainService(IAggregateRepository RecordCallback(CallbackCommands.RecordCallbackCommand command, CancellationToken cancellationToken) { - // split the reference string into an array of strings - String[] referenceData = command.Reference?.Split(['-'], StringSplitOptions.RemoveEmptyEntries) ?? []; + // validate the reference + Result<(Guid estateId, Guid merchantId)> validateResult = await ValidateReference(command.Reference); + if (validateResult.IsFailed) + return ResultHelpers.CreateFailure(validateResult); - if (referenceData.Length == 0) { + Result getResult = await this.AggregateRepository.GetLatestVersion(command.CallbackId, cancellationToken); + Result callbackMessageAggregateResult = + DomainServiceHelper.HandleGetAggregateResult(getResult, command.CallbackId, false); + + CallbackMessageAggregate aggregate = callbackMessageAggregateResult.Data; + Result stateResult = aggregate.RecordCallback(command.CallbackId, command.TypeString, command.MessageFormat, command.CallbackMessage, command.Destinations, + (command.Reference, Guid.Parse(estateReference), Guid.Parse(merchantReference))); + if (stateResult.IsFailed) + return stateResult; + return await this.AggregateRepository.SaveChanges(aggregate, cancellationToken); + } + + private async Task ValidateReference(String reference) { + String[] referenceData = reference?.Split(['-'], StringSplitOptions.RemoveEmptyEntries) ?? []; + + if (referenceData.Length == 0) + { return Result.Invalid("Reference cannot be empty."); } - if (referenceData.Length != 2) { + if (referenceData.Length != 2) + { return Result.Invalid("Reference must contain estate and merchant references separated by a hyphen."); } @@ -58,7 +78,8 @@ public async Task RecordCallback(CallbackCommands.RecordCallbackCommand // Validate the reference data // Validate the estate and merchant references are valid GUIDs - if (!Guid.TryParse(estateReference, out Guid estateId) || !Guid.TryParse(merchantReference, out Guid merchantId)) { + if (!Guid.TryParse(estateReference, out Guid estateId) || !Guid.TryParse(merchantReference, out Guid merchantId)) + { return Result.Invalid("Estate or Merchant reference is not a valid GUID."); } @@ -71,16 +92,7 @@ public async Task RecordCallback(CallbackCommands.RecordCallbackCommand if (result.IsFailed) return ResultHelpers.CreateFailure(result); - Result getResult = await this.AggregateRepository.GetLatestVersion(command.CallbackId, cancellationToken); - Result callbackMessageAggregateResult = - DomainServiceHelper.HandleGetAggregateResult(getResult, command.CallbackId, false); - - CallbackMessageAggregate aggregate = callbackMessageAggregateResult.Data; - Result stateResult = aggregate.RecordCallback(command.CallbackId, command.TypeString, command.MessageFormat, command.CallbackMessage, command.Destinations, - (command.Reference, Guid.Parse(estateReference), Guid.Parse(merchantReference))); - if (stateResult.IsFailed) - return stateResult; - return await this.AggregateRepository.SaveChanges(aggregate, cancellationToken); + return Result.Success(); } private TokenResponse TokenResponse; From dd61ca407baa2633cc876f7733640a7e919f18c7 Mon Sep 17 00:00:00 2001 From: Stuart Ferguson Date: Wed, 31 Dec 2025 19:53:01 +0000 Subject: [PATCH 2/3] fix build failure --- .../Services/CallbackDomainService.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/CallbackHandler.BusinessLogic/Services/CallbackDomainService.cs b/CallbackHandler.BusinessLogic/Services/CallbackDomainService.cs index 0aa2660..7c11664 100644 --- a/CallbackHandler.BusinessLogic/Services/CallbackDomainService.cs +++ b/CallbackHandler.BusinessLogic/Services/CallbackDomainService.cs @@ -43,7 +43,7 @@ public async Task RecordCallback(CallbackCommands.RecordCallbackCommand CancellationToken cancellationToken) { // validate the reference - Result<(Guid estateId, Guid merchantId)> validateResult = await ValidateReference(command.Reference); + Result<(Guid estateId, Guid merchantId)> validateResult = await ValidateReference(command.Reference, cancellationToken); if (validateResult.IsFailed) return ResultHelpers.CreateFailure(validateResult); @@ -53,13 +53,13 @@ public async Task RecordCallback(CallbackCommands.RecordCallbackCommand CallbackMessageAggregate aggregate = callbackMessageAggregateResult.Data; Result stateResult = aggregate.RecordCallback(command.CallbackId, command.TypeString, command.MessageFormat, command.CallbackMessage, command.Destinations, - (command.Reference, Guid.Parse(estateReference), Guid.Parse(merchantReference))); + (command.Reference,validateResult.Data.estateId, validateResult.Data.merchantId)); if (stateResult.IsFailed) return stateResult; return await this.AggregateRepository.SaveChanges(aggregate, cancellationToken); } - private async Task ValidateReference(String reference) { + private async Task> ValidateReference(String reference, CancellationToken cancellationToken) { String[] referenceData = reference?.Split(['-'], StringSplitOptions.RemoveEmptyEntries) ?? []; if (referenceData.Length == 0) @@ -92,7 +92,7 @@ private async Task ValidateReference(String reference) { if (result.IsFailed) return ResultHelpers.CreateFailure(result); - return Result.Success(); + return Result.Success((estateId,merchantId)); } private TokenResponse TokenResponse; From 01b5ceb34d5e43d24c5b016aa084a7cff4b27341 Mon Sep 17 00:00:00 2001 From: Stuart Ferguson Date: Wed, 31 Dec 2025 20:01:49 +0000 Subject: [PATCH 3/3] :| --- .../Services/CallbackDomainService.cs | 21 +++++++++---------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/CallbackHandler.BusinessLogic/Services/CallbackDomainService.cs b/CallbackHandler.BusinessLogic/Services/CallbackDomainService.cs index 7c11664..41b3d1c 100644 --- a/CallbackHandler.BusinessLogic/Services/CallbackDomainService.cs +++ b/CallbackHandler.BusinessLogic/Services/CallbackDomainService.cs @@ -62,15 +62,14 @@ public async Task RecordCallback(CallbackCommands.RecordCallbackCommand private async Task> ValidateReference(String reference, CancellationToken cancellationToken) { String[] referenceData = reference?.Split(['-'], StringSplitOptions.RemoveEmptyEntries) ?? []; - if (referenceData.Length == 0) - { - return Result.Invalid("Reference cannot be empty."); - } - if (referenceData.Length != 2) - { - return Result.Invalid("Reference must contain estate and merchant references separated by a hyphen."); - } + var result = referenceData.Length switch { + 0 => Result.Invalid("Reference cannot be empty."), + _ when referenceData.Length != 2 => Result.Invalid("Reference must contain estate and merchant references separated by a hyphen."), + _ => Result.Success() + }; + if (result.IsFailed) + return ResultHelpers.CreateFailure(result); // Element 0 is estate reference, Element 1 is merchant reference String estateReference = referenceData[0]; @@ -88,9 +87,9 @@ public async Task RecordCallback(CallbackCommands.RecordCallbackCommand return ResultHelpers.CreateFailure(getTokenResult); this.TokenResponse = getTokenResult.Data; - Result result = await this.TransactionProcessorClient.GetMerchant(this.TokenResponse.AccessToken, estateId, merchantId, cancellationToken); - if (result.IsFailed) - return ResultHelpers.CreateFailure(result); + Result getMerchantResult = await this.TransactionProcessorClient.GetMerchant(this.TokenResponse.AccessToken, estateId, merchantId, cancellationToken); + if (getMerchantResult.IsFailed) + return ResultHelpers.CreateFailure(getMerchantResult); return Result.Success((estateId,merchantId)); }