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
100 changes: 100 additions & 0 deletions TransactionProcessor.BusinessLogic/Common/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,106 @@ namespace TransactionProcessor.BusinessLogic.Common
{
using System.Diagnostics.CodeAnalysis;

public static class Helpers
{
public static Guid CalculateSettlementAggregateId(DateTime settlementDate,
Guid estateId)
{
Guid aggregateId = GuidCalculator.Combine(estateId, settlementDate.ToGuid());
return aggregateId;
}
}

public static class GuidCalculator
{
#region Methods

/// <summary>
/// Combines the specified GUIDs into a new GUID.
/// </summary>
/// <param name="firstGuid">The first unique identifier.</param>
/// <param name="secondGuid">The second unique identifier.</param>
/// <param name="offset">The offset.</param>
/// <returns>Guid.</returns>
public static Guid Combine(Guid firstGuid,
Guid secondGuid,
Byte offset)
{
Byte[] firstAsBytes = firstGuid.ToByteArray();
Byte[] secondAsBytes = secondGuid.ToByteArray();

Byte[] newBytes = new Byte[16];

for (Int32 i = 0; i < 16; i++)
{
// Add and truncate any overflow
newBytes[i] = (Byte)(firstAsBytes[i] + secondAsBytes[i] + offset);
}

return new Guid(newBytes);
}

/// <summary>
/// Combines the specified GUIDs into a new GUID.
/// </summary>
/// <param name="firstGuid">The first unique identifier.</param>
/// <param name="secondGuid">The second unique identifier.</param>
/// <returns>Guid.</returns>
public static Guid Combine(Guid firstGuid,
Guid secondGuid)
{
return GuidCalculator.Combine(firstGuid,
secondGuid,
0);
}

/// <summary>
/// Combines the specified first unique identifier.
/// </summary>
/// <param name="firstGuid">The first unique identifier.</param>
/// <param name="secondGuid">The second unique identifier.</param>
/// <param name="thirdGuid">The third unique identifier.</param>
/// <param name="offset">The offset.</param>
/// <returns>Guid.</returns>
public static Guid Combine(Guid firstGuid,
Guid secondGuid,
Guid thirdGuid,
Byte offset)
{
Byte[] firstAsBytes = firstGuid.ToByteArray();
Byte[] secondAsBytes = secondGuid.ToByteArray();
Byte[] thirdAsBytes = thirdGuid.ToByteArray();

Byte[] newBytes = new Byte[16];

for (Int32 i = 0; i < 16; i++)
{
// Add and truncate any overflow
newBytes[i] = (Byte)(firstAsBytes[i] + secondAsBytes[i] + thirdAsBytes[i] + offset);
}

return new Guid(newBytes);
}

/// <summary>
/// Combines the specified first unique identifier.
/// </summary>
/// <param name="firstGuid">The first unique identifier.</param>
/// <param name="secondGuid">The second unique identifier.</param>
/// <param name="thirdGuid">The third unique identifier.</param>
/// <returns>Guid.</returns>
public static Guid Combine(Guid firstGuid,
Guid secondGuid,
Guid thirdGuid)
{
return GuidCalculator.Combine(firstGuid,
secondGuid,
thirdGuid,
0);
}

#endregion
}
public static class Extensions
{
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ private async Task HandleSpecificDomainEvent(TransactionHasBeenCompletedEvent do
Logger.LogInformation($"SettlementSchedule {merchant.SettlementSchedule}");
DateTime settlementDate = CalculateSettlementDate(merchant.SettlementSchedule, domainEvent.CompletedDateTime);
Logger.LogInformation($"Settlement Date {settlementDate}");
Guid aggregateId = settlementDate.ToGuid();
Guid aggregateId = Helpers.CalculateSettlementAggregateId(settlementDate, domainEvent.EstateId);

// We need to add the fees to a pending settlement stream (for today)
SettlementAggregate aggregate = await this.SettlementAggregateRepository.GetLatestVersion(aggregateId, cancellationToken);
Expand All @@ -259,6 +259,8 @@ private async Task HandleSpecificDomainEvent(TransactionHasBeenCompletedEvent do
}
}



private DateTime CalculateSettlementDate(SettlementSchedule merchantSettlementSchedule, DateTime completeDateTime)
{
if (merchantSettlementSchedule == SettlementSchedule.Weekly)
Expand Down Expand Up @@ -305,7 +307,7 @@ private async Task HandleSpecificDomainEvent(MerchantFeeAddedToTransactionEvent
return;
}

Guid aggregateId = domainEvent.SettlementDueDate.ToGuid();
Guid aggregateId = Helpers.CalculateSettlementAggregateId(domainEvent.SettlementDueDate, domainEvent.EstateId);

SettlementAggregate pendingSettlementAggregate = await this.SettlementAggregateRepository.GetLatestVersion(aggregateId, cancellationToken);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public async Task<ProcessSettlementResponse> ProcessSettlement(DateTime settleme
{
ProcessSettlementResponse response = new ProcessSettlementResponse();

Guid aggregateId = settlementDate.ToGuid();
Guid aggregateId = Helpers.CalculateSettlementAggregateId(settlementDate,estateId);

SettlementAggregate settlementAggregate = await this.SettlementAggregateRepository.GetLatestVersion(aggregateId, cancellationToken);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -893,7 +893,7 @@ public async Task WhenIGetThePendingSettlementsTheFollowingInformationShouldBeRe
Int32 numberOfFees = SpecflowTableHelper.GetIntValue(tableRow, "NumberOfFees");
DateTime settlementDate = SpecflowTableHelper.GetDateForDateString(settlementDateString, DateTime.UtcNow.Date);

var aggregateid = settlementDate.ToGuid();
Guid aggregateid = Helpers.CalculateSettlementAggregateId(settlementDate, estateDetails.EstateId);
await Retry.For(async () =>
{
SettlementResponse settlements =
Expand Down
2 changes: 1 addition & 1 deletion TransactionProcessor/Controllers/SettlementController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public async Task<IActionResult> GetPendingSettlement([FromRoute] DateTime settl
{
// TODO: Convert to using a manager/model/factory
// Convert the date passed in to a guid
var aggregateId = settlementDate.Date.ToGuid();
Guid aggregateId = Helpers.CalculateSettlementAggregateId(settlementDate, estateId);

Logger.LogInformation($"Settlement Aggregate Id {aggregateId}");

Expand Down