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
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,14 @@ public void FloatAggregate_RecordCreditPurchase_MultipleCreditPurchases_AllCredi
}

[Fact]
public void FloatAggregate_RecordCreditPurchase_DuplicateCreditPurchase_NoErrorThrown()
public void FloatAggregate_RecordCreditPurchase_DuplicateCreditPurchase_ErrorThrown()
{
FloatAggregate aggregate = FloatAggregate.Create(TestData.FloatAggregateId);
aggregate.CreateFloat(TestData.EstateId, TestData.ContractId, TestData.ProductId, TestData.FloatCreatedDateTime);
DateTime purchaseDateTime = DateTime.Now;
aggregate.RecordCreditPurchase(purchaseDateTime, 1000, 900);

Should.NotThrow(() => {
Should.Throw<InvalidOperationException>(() => {
aggregate.RecordCreditPurchase(purchaseDateTime, 1000, 900);
});
}
Expand Down Expand Up @@ -152,14 +152,14 @@ public void FloatAggregate_RecordTransactionAgainstFloat_FloatNotCreated_ErrorTh
}

[Fact]
public void FloatAggregate_RecordTransactionAgainstFloat_DuplicateTransaction_ErrorThrown()
public void FloatAggregate_RecordTransactionAgainstFloat_DuplicateTransaction_NoErrorThrown()
{
FloatAggregate aggregate = FloatAggregate.Create(TestData.FloatAggregateId);
aggregate.CreateFloat(TestData.EstateId, TestData.ContractId, TestData.ProductId, TestData.FloatCreatedDateTime);
DateTime purchaseDateTime = DateTime.Now;
aggregate.RecordCreditPurchase(purchaseDateTime, 1000, 900);
aggregate.RecordTransactionAgainstFloat(TestData.TransactionId, 100);
Should.Throw<InvalidOperationException>(() => {
Should.NotThrow(() => {
aggregate.RecordTransactionAgainstFloat(TestData.TransactionId, 100);
});
}
Expand Down
27 changes: 14 additions & 13 deletions TransactionProcessor.FloatAggregate/FloatAggregate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,8 @@ public static void CreateFloat(this FloatAggregate aggregate,
public static void RecordCreditPurchase(this FloatAggregate aggregate, DateTime creditPurchasedDate, Decimal amount, Decimal costPrice)
{
aggregate.ValidateFloatIsAlreadyCreated();

Boolean isCreditADuplicate = aggregate.IsCreditADuplicate(creditPurchasedDate,amount,costPrice);
if (isCreditADuplicate)
return;

aggregate.ValidateCreditIsNotADuplicate(creditPurchasedDate, amount, costPrice);

FloatCreditPurchasedEvent floatCreditPurchasedEvent = new FloatCreditPurchasedEvent(aggregate.AggregateId, aggregate.EstateId,
creditPurchasedDate, amount, costPrice);

Expand All @@ -64,7 +61,10 @@ public static void RecordCreditPurchase(this FloatAggregate aggregate, DateTime

public static void RecordTransactionAgainstFloat(this FloatAggregate aggregate, Guid transactionId, Decimal transactionAmount){
aggregate.ValidateFloatIsAlreadyCreated();
aggregate.ValidateTransactionIsNotADuplicate(transactionId);

Boolean isTransactionADuplicate = aggregate.IsTransactionADuplicate(transactionId);
if (isTransactionADuplicate)
return;

FloatDecreasedByTransactionEvent floatDecreasedByTransactionEvent = new FloatDecreasedByTransactionEvent(aggregate.AggregateId, aggregate.EstateId, transactionId, transactionAmount);

Expand All @@ -88,18 +88,19 @@ public static void ValidateFloatIsNotAlreadyCreated(this FloatAggregate aggregat
}
}

public static Boolean IsCreditADuplicate(this FloatAggregate aggregate, DateTime creditPurchasedDate, Decimal amount, Decimal costPrice){
public static void ValidateCreditIsNotADuplicate(this FloatAggregate aggregate, DateTime creditPurchasedDate, Decimal amount, Decimal costPrice)
{
Boolean isDuplicate = aggregate.Credits.Any(c => c.costPrice == costPrice && c.amount == amount && c.creditPurchasedDate == creditPurchasedDate);
return isDuplicate;
if (isDuplicate == true)
{
throw new InvalidOperationException($"Float Aggregate Id {aggregate.AggregateId} already has a credit with this information recorded");
}
}

public static void ValidateTransactionIsNotADuplicate(this FloatAggregate aggregate, Guid transactionId)
public static Boolean IsTransactionADuplicate(this FloatAggregate aggregate, Guid transactionId)
{
Boolean isDuplicate = aggregate.Transactions.Any(c => c.transactionId == transactionId);
if (isDuplicate == true)
{
throw new InvalidOperationException($"Float Aggregate Id {aggregate.AggregateId} already has a transaction with this Id {transactionId} recorded");
}
return isDuplicate;
}
}

Expand Down