Skip to content

Commit c6cea8b

Browse files
Merge pull request #617 from TransactionProcessing/bug/#615_eventhander_not_getting_toke_before_remote_call
fix the duplicate txn for float issue
2 parents 7e31d37 + 3b2e5a4 commit c6cea8b

File tree

2 files changed

+18
-17
lines changed

2 files changed

+18
-17
lines changed

TransactionProcessor.FloatAggregate.Tests/FloatAggregateTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,14 +99,14 @@ public void FloatAggregate_RecordCreditPurchase_MultipleCreditPurchases_AllCredi
9999
}
100100

101101
[Fact]
102-
public void FloatAggregate_RecordCreditPurchase_DuplicateCreditPurchase_NoErrorThrown()
102+
public void FloatAggregate_RecordCreditPurchase_DuplicateCreditPurchase_ErrorThrown()
103103
{
104104
FloatAggregate aggregate = FloatAggregate.Create(TestData.FloatAggregateId);
105105
aggregate.CreateFloat(TestData.EstateId, TestData.ContractId, TestData.ProductId, TestData.FloatCreatedDateTime);
106106
DateTime purchaseDateTime = DateTime.Now;
107107
aggregate.RecordCreditPurchase(purchaseDateTime, 1000, 900);
108108

109-
Should.NotThrow(() => {
109+
Should.Throw<InvalidOperationException>(() => {
110110
aggregate.RecordCreditPurchase(purchaseDateTime, 1000, 900);
111111
});
112112
}
@@ -152,14 +152,14 @@ public void FloatAggregate_RecordTransactionAgainstFloat_FloatNotCreated_ErrorTh
152152
}
153153

154154
[Fact]
155-
public void FloatAggregate_RecordTransactionAgainstFloat_DuplicateTransaction_ErrorThrown()
155+
public void FloatAggregate_RecordTransactionAgainstFloat_DuplicateTransaction_NoErrorThrown()
156156
{
157157
FloatAggregate aggregate = FloatAggregate.Create(TestData.FloatAggregateId);
158158
aggregate.CreateFloat(TestData.EstateId, TestData.ContractId, TestData.ProductId, TestData.FloatCreatedDateTime);
159159
DateTime purchaseDateTime = DateTime.Now;
160160
aggregate.RecordCreditPurchase(purchaseDateTime, 1000, 900);
161161
aggregate.RecordTransactionAgainstFloat(TestData.TransactionId, 100);
162-
Should.Throw<InvalidOperationException>(() => {
162+
Should.NotThrow(() => {
163163
aggregate.RecordTransactionAgainstFloat(TestData.TransactionId, 100);
164164
});
165165
}

TransactionProcessor.FloatAggregate/FloatAggregate.cs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,8 @@ public static void CreateFloat(this FloatAggregate aggregate,
5151
public static void RecordCreditPurchase(this FloatAggregate aggregate, DateTime creditPurchasedDate, Decimal amount, Decimal costPrice)
5252
{
5353
aggregate.ValidateFloatIsAlreadyCreated();
54-
55-
Boolean isCreditADuplicate = aggregate.IsCreditADuplicate(creditPurchasedDate,amount,costPrice);
56-
if (isCreditADuplicate)
57-
return;
58-
54+
aggregate.ValidateCreditIsNotADuplicate(creditPurchasedDate, amount, costPrice);
55+
5956
FloatCreditPurchasedEvent floatCreditPurchasedEvent = new FloatCreditPurchasedEvent(aggregate.AggregateId, aggregate.EstateId,
6057
creditPurchasedDate, amount, costPrice);
6158

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

6562
public static void RecordTransactionAgainstFloat(this FloatAggregate aggregate, Guid transactionId, Decimal transactionAmount){
6663
aggregate.ValidateFloatIsAlreadyCreated();
67-
aggregate.ValidateTransactionIsNotADuplicate(transactionId);
64+
65+
Boolean isTransactionADuplicate = aggregate.IsTransactionADuplicate(transactionId);
66+
if (isTransactionADuplicate)
67+
return;
6868

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

@@ -88,18 +88,19 @@ public static void ValidateFloatIsNotAlreadyCreated(this FloatAggregate aggregat
8888
}
8989
}
9090

91-
public static Boolean IsCreditADuplicate(this FloatAggregate aggregate, DateTime creditPurchasedDate, Decimal amount, Decimal costPrice){
91+
public static void ValidateCreditIsNotADuplicate(this FloatAggregate aggregate, DateTime creditPurchasedDate, Decimal amount, Decimal costPrice)
92+
{
9293
Boolean isDuplicate = aggregate.Credits.Any(c => c.costPrice == costPrice && c.amount == amount && c.creditPurchasedDate == creditPurchasedDate);
93-
return isDuplicate;
94+
if (isDuplicate == true)
95+
{
96+
throw new InvalidOperationException($"Float Aggregate Id {aggregate.AggregateId} already has a credit with this information recorded");
97+
}
9498
}
9599

96-
public static void ValidateTransactionIsNotADuplicate(this FloatAggregate aggregate, Guid transactionId)
100+
public static Boolean IsTransactionADuplicate(this FloatAggregate aggregate, Guid transactionId)
97101
{
98102
Boolean isDuplicate = aggregate.Transactions.Any(c => c.transactionId == transactionId);
99-
if (isDuplicate == true)
100-
{
101-
throw new InvalidOperationException($"Float Aggregate Id {aggregate.AggregateId} already has a transaction with this Id {transactionId} recorded");
102-
}
103+
return isDuplicate;
103104
}
104105
}
105106

0 commit comments

Comments
 (0)