Skip to content
Merged
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
55 changes: 33 additions & 22 deletions CallbackHandler.BusinessLogic/Services/CallbackDomainService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -41,24 +42,43 @@ public CallbackDomainService(IAggregateRepository<CallbackMessageAggregate, Doma
public async Task<Result> 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, cancellationToken);
if (validateResult.IsFailed)
return ResultHelpers.CreateFailure(validateResult);

if (referenceData.Length == 0) {
return Result.Invalid("Reference cannot be empty.");
}
Result<CallbackMessageAggregate> getResult = await this.AggregateRepository.GetLatestVersion(command.CallbackId, cancellationToken);
Result<CallbackMessageAggregate> callbackMessageAggregateResult =
DomainServiceHelper.HandleGetAggregateResult(getResult, command.CallbackId, false);

if (referenceData.Length != 2) {
return Result.Invalid("Reference must contain estate and merchant references separated by a hyphen.");
}
CallbackMessageAggregate aggregate = callbackMessageAggregateResult.Data;
Result stateResult = aggregate.RecordCallback(command.CallbackId, command.TypeString, command.MessageFormat, command.CallbackMessage, command.Destinations,
(command.Reference,validateResult.Data.estateId, validateResult.Data.merchantId));
if (stateResult.IsFailed)
return stateResult;
return await this.AggregateRepository.SaveChanges(aggregate, cancellationToken);
}

private async Task<Result<(Guid estateId, Guid merchantId)>> ValidateReference(String reference, CancellationToken cancellationToken) {
String[] referenceData = reference?.Split(['-'], StringSplitOptions.RemoveEmptyEntries) ?? [];


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];
String merchantReference = referenceData[1];

// 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.");
}

Expand All @@ -67,20 +87,11 @@ public async Task<Result> RecordCallback(CallbackCommands.RecordCallbackCommand
return ResultHelpers.CreateFailure(getTokenResult);
this.TokenResponse = getTokenResult.Data;

Result<MerchantResponse> result = await this.TransactionProcessorClient.GetMerchant(this.TokenResponse.AccessToken, estateId, merchantId, cancellationToken);
if (result.IsFailed)
return ResultHelpers.CreateFailure(result);

Result<CallbackMessageAggregate> getResult = await this.AggregateRepository.GetLatestVersion(command.CallbackId, cancellationToken);
Result<CallbackMessageAggregate> callbackMessageAggregateResult =
DomainServiceHelper.HandleGetAggregateResult(getResult, command.CallbackId, false);
Result<MerchantResponse> getMerchantResult = await this.TransactionProcessorClient.GetMerchant(this.TokenResponse.AccessToken, estateId, merchantId, cancellationToken);
if (getMerchantResult.IsFailed)
return ResultHelpers.CreateFailure(getMerchantResult);

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((estateId,merchantId));
}

private TokenResponse TokenResponse;
Expand Down
Loading