Improve testing and some cleanup refactoring.

This commit is contained in:
2026-07-29 11:07:12 +02:00
parent 249a166d07
commit 44846eaa13
15 changed files with 253 additions and 96 deletions
@@ -6,7 +6,10 @@ public record BalancedJournal
{
public JournalEntry Entry { get; }
private BalancedJournal(JournalEntry entry) => Entry = entry;
private BalancedJournal(JournalEntry entry)
{
Entry = entry;
}
public static BalancedJournal Create(Guid id, string description, DateTimeOffset createdAt, List<JournalLine> lines)
{
@@ -22,7 +25,7 @@ public record BalancedJournal
);
}
return new(new JournalEntry(id, description, createdAt, lines));
return new BalancedJournal(new JournalEntry(id, description, createdAt, lines));
}
public static BalancedJournal CreateReversal(JournalEntry original, string reason, DateTimeOffset now)
@@ -1,54 +1,32 @@
namespace FinancialApi.Domain.Entities;
public record Account(int Id, decimal Balance, string AccountType)
public enum AccountType
{
Liability,
Debit,
Credit,
Revenue
}
public record Account(int Id, decimal Balance, AccountType Type)
{
public Account ApplyPosting(decimal amount, EntryType entryType)
{
if (amount <= 0)
{
throw new ArgumentException("Credit amount must be positive.");
throw new ArgumentException("Amount must be positive.");
}
var newBalance = Balance;
if (AccountType == "Revenue")
{
newBalance = entryType == EntryType.Credit ? newBalance + amount : newBalance - amount;
}
else
{
newBalance = entryType == EntryType.Debit ? newBalance - amount : newBalance + amount;
}
var newBalance = entryType == EntryType.Credit ? Balance + amount : Balance - amount;
;
if (newBalance < 0 && AccountType == "Liability")
if (newBalance < 0 && Type is AccountType.Liability or AccountType.Debit or AccountType.Revenue)
{
throw new InvalidOperationException("Insufficient funds.");
throw new InsufficientFundsException("Insufficient funds.");
}
return this with { Balance = newBalance };
}
}
public Account Debit(decimal amount)
{
if (amount <= 0)
{
throw new ArgumentException("Debit amount must be positive.");
}
if (Balance - amount < 0)
{
throw new InvalidOperationException("Insufficient funds.");
}
return this with { Balance = Balance - amount };
}
public Account Credit(decimal amount)
{
if (amount <= 0)
{
throw new ArgumentException("Credit amount must be positive.");
}
return this with { Balance = Balance + amount };
}
}
public class InsufficientFundsException(string insufficientFunds) : Exception(insufficientFunds);