Reformat all code as per my preferred formatting.
This commit is contained in:
+12
-20
@@ -11,9 +11,7 @@ namespace FinancialApi.Application.Handlers;
|
||||
|
||||
public static class ApplyFeeHandler
|
||||
{
|
||||
public static async Task<FeeContext> LoadAsync(
|
||||
ApplyFeeCommand cmd,
|
||||
IAccountQuery query, TimeProvider timeProvider)
|
||||
public static async Task<FeeContext> LoadAsync(ApplyFeeCommand cmd, IAccountQuery query, TimeProvider timeProvider)
|
||||
{
|
||||
var source = await query.FindByIdAsync(cmd.AccountId);
|
||||
var dest = await query.FindByIdAsync(99999);
|
||||
@@ -25,13 +23,8 @@ public static class ApplyFeeHandler
|
||||
return new FeeContext(source, dest, timeProvider.GetUtcNow());
|
||||
}
|
||||
|
||||
public static (
|
||||
IStorageAction<Account> CustomerWrite,
|
||||
IStorageAction<Account> RevenueWrite,
|
||||
IStorageAction<JournalEntry> JournalWrite,
|
||||
FeeAppliedEvent Event
|
||||
)
|
||||
Handle(
|
||||
public static ( IStorageAction<Account> CustomerWrite, IStorageAction<Account> RevenueWrite,
|
||||
IStorageAction<JournalEntry> JournalWrite, FeeAppliedEvent Event ) Handle(
|
||||
ApplyFeeCommand cmd,
|
||||
FeeContext context
|
||||
)
|
||||
@@ -39,25 +32,24 @@ public static class ApplyFeeHandler
|
||||
var updatedSource = context.Source.ApplyPosting(cmd.Amount, EntryType.Debit);
|
||||
var updatedRevenue = context.Destination.ApplyPosting(cmd.Amount, EntryType.Credit);
|
||||
var serviceFee = 0.5m;
|
||||
|
||||
|
||||
var lines = new List<JournalLine>
|
||||
{
|
||||
new(updatedSource.Id, cmd.Amount, EntryType.Debit),
|
||||
new(updatedRevenue.Id, cmd.Amount - serviceFee, EntryType.Credit),
|
||||
new(updatedRevenue.Id, serviceFee, EntryType.Credit)
|
||||
};
|
||||
|
||||
var journalEntry = BalancedJournal.Create(Guid.NewGuid(),
|
||||
|
||||
var journalEntry = BalancedJournal.Create(
|
||||
Guid.NewGuid(),
|
||||
$"Service Fee Applied: {cmd.Amount} to Account {cmd.AccountId}",
|
||||
context.TimeStamp, lines);
|
||||
context.TimeStamp,
|
||||
lines
|
||||
);
|
||||
|
||||
var @event = new FeeAppliedEvent(cmd.AccountId, cmd.Amount);
|
||||
|
||||
return (
|
||||
Storage.Update(updatedSource),
|
||||
Storage.Update(updatedRevenue),
|
||||
Storage.Insert(journalEntry.Entry),
|
||||
@event
|
||||
);
|
||||
return (Storage.Update(updatedSource), Storage.Update(updatedRevenue), Storage.Insert(journalEntry.Entry),
|
||||
@event);
|
||||
}
|
||||
}
|
||||
+3
-12
@@ -5,20 +5,14 @@ namespace FinancialApi.Application.Handlers;
|
||||
|
||||
public static class FinancialEventHandler
|
||||
{
|
||||
public static void Handle(
|
||||
FeeAppliedEvent @event,
|
||||
IEventTracker store,
|
||||
ILogger logger)
|
||||
public static void Handle(FeeAppliedEvent @event, IEventTracker store, ILogger logger)
|
||||
{
|
||||
var message = $"FeeApplied: ${@event.Amount} moved from Account {@event.AccountId} to Account 99999.";
|
||||
logger.LogInformation("BACKGROUND EVENT FIRED: {Message}", message);
|
||||
store.Add(message);
|
||||
}
|
||||
|
||||
public static void Handle(
|
||||
FundsTransferredEvent @event,
|
||||
IEventTracker store,
|
||||
ILogger logger)
|
||||
public static void Handle(FundsTransferredEvent @event, IEventTracker store, ILogger logger)
|
||||
{
|
||||
var message =
|
||||
$"FundsTransferred: ${@event.Amount} moved from Account {@event.SourceAccountId} to Account {@event.DestinationAccountId}.";
|
||||
@@ -26,10 +20,7 @@ public static class FinancialEventHandler
|
||||
store.Add(message);
|
||||
}
|
||||
|
||||
public static void Handle(
|
||||
JournalReversedEvent @event,
|
||||
IEventTracker store,
|
||||
ILogger logger)
|
||||
public static void Handle(JournalReversedEvent @event, IEventTracker store, ILogger logger)
|
||||
{
|
||||
var message =
|
||||
$"JournalReversed: Original Entry {@event.OriginalJournalId} was reversed by Entry {@event.ReversalJournalId}.";
|
||||
|
||||
+19
-18
@@ -11,38 +11,39 @@ namespace FinancialApi.Application.Handlers;
|
||||
|
||||
public static class ReverseJournalHandler
|
||||
{
|
||||
public static async Task<JournalReversalContext?> LoadAsync(ReverseJournalCommand cmd, IReversalQuery query,
|
||||
TimeProvider timeProvider)
|
||||
public static async Task<JournalReversalContext?> LoadAsync(
|
||||
ReverseJournalCommand cmd,
|
||||
IReversalQuery query,
|
||||
TimeProvider timeProvider
|
||||
)
|
||||
{
|
||||
var data = await query.GetReversalDataAsync(cmd.OriginalJournalId);
|
||||
return data ?? throw new InvalidOperationException($"Journal Entry {cmd.OriginalJournalId} does not exist");
|
||||
}
|
||||
|
||||
public static (
|
||||
IStorageAction<JournalEntry> JournalWrite,
|
||||
UnitOfWork<Account> AccountWrites,
|
||||
JournalReversedEvent Event)
|
||||
Handle(ReverseJournalCommand cmd, JournalReversalContext context)
|
||||
public static ( IStorageAction<JournalEntry> JournalWrite, UnitOfWork<Account> AccountWrites, JournalReversedEvent
|
||||
Event) Handle(ReverseJournalCommand cmd, JournalReversalContext context)
|
||||
{
|
||||
var reversalJournal =
|
||||
BalancedJournal.CreateReversal(context.OriginalJournalEntry, $"Reversal: {cmd.Reason}", context.TimeStamp);
|
||||
var reversalJournal = BalancedJournal.CreateReversal(
|
||||
context.OriginalJournalEntry,
|
||||
$"Reversal: {cmd.Reason}",
|
||||
context.TimeStamp
|
||||
);
|
||||
var accountState = context.AffectedAccounts.ToDictionary(a => a.Id);
|
||||
|
||||
reversalJournal.Entry.Lines.ForEach(l =>
|
||||
{
|
||||
var currentAccount = accountState[l.AccountId];
|
||||
accountState[l.AccountId] = currentAccount.ApplyPosting(l.Amount, l.Type);
|
||||
});
|
||||
{
|
||||
var currentAccount = accountState[l.AccountId];
|
||||
accountState[l.AccountId] = currentAccount.ApplyPosting(l.Amount, l.Type);
|
||||
}
|
||||
);
|
||||
var accountUow = new UnitOfWork<Account>();
|
||||
foreach (var account in accountState.Values)
|
||||
{
|
||||
accountUow.Update(account);
|
||||
}
|
||||
|
||||
return (
|
||||
Storage.Insert(reversalJournal.Entry),
|
||||
accountUow,
|
||||
new JournalReversedEvent(cmd.OriginalJournalId, reversalJournal.Entry.Id)
|
||||
);
|
||||
return (Storage.Insert(reversalJournal.Entry), accountUow,
|
||||
new JournalReversedEvent(cmd.OriginalJournalId, reversalJournal.Entry.Id));
|
||||
}
|
||||
}
|
||||
+14
-18
@@ -11,8 +11,11 @@ namespace FinancialApi.Application.Handlers;
|
||||
|
||||
public static class TransferFundsHandler
|
||||
{
|
||||
public static async Task<TransferContext?> LoadAsync(TransferFundsCommand cmd, IAccountQuery query,
|
||||
TimeProvider timeProvider)
|
||||
public static async Task<TransferContext?> LoadAsync(
|
||||
TransferFundsCommand cmd,
|
||||
IAccountQuery query,
|
||||
TimeProvider timeProvider
|
||||
)
|
||||
{
|
||||
var source = await query.FindByIdAsync(cmd.SourceAccountId);
|
||||
var dest = await query.FindByIdAsync(cmd.DestinationAccountId);
|
||||
@@ -22,12 +25,8 @@ public static class TransferFundsHandler
|
||||
return new TransferContext(source, dest, timeProvider.GetUtcNow());
|
||||
}
|
||||
|
||||
public static (
|
||||
IStorageAction<Account> SourceWrite,
|
||||
IStorageAction<Account> DestWrite,
|
||||
IStorageAction<JournalEntry> JournalWrite,
|
||||
FundsTransferredEvent Event
|
||||
) Handle(TransferFundsCommand cmd, TransferContext context)
|
||||
public static ( IStorageAction<Account> SourceWrite, IStorageAction<Account> DestWrite, IStorageAction<JournalEntry>
|
||||
JournalWrite, FundsTransferredEvent Event ) Handle(TransferFundsCommand cmd, TransferContext context)
|
||||
{
|
||||
var updatedSource = context.Source.Debit(cmd.Amount);
|
||||
var updatedDest = context.Destination.Credit(cmd.Amount);
|
||||
@@ -38,18 +37,15 @@ public static class TransferFundsHandler
|
||||
new(context.Destination.Id, cmd.Amount, EntryType.Credit)
|
||||
};
|
||||
|
||||
var journalEntry =
|
||||
BalancedJournal.Create(Guid.NewGuid(),
|
||||
$"Transfer: {cmd.Amount} from {cmd.SourceAccountId} to {cmd.DestinationAccountId}",
|
||||
context.TimeStamp, lines);
|
||||
var journalEntry = BalancedJournal.Create(
|
||||
Guid.NewGuid(),
|
||||
$"Transfer: {cmd.Amount} from {cmd.SourceAccountId} to {cmd.DestinationAccountId}",
|
||||
context.TimeStamp,
|
||||
lines
|
||||
);
|
||||
|
||||
var @event = new FundsTransferredEvent(cmd.SourceAccountId, cmd.DestinationAccountId, cmd.Amount);
|
||||
|
||||
return (
|
||||
Storage.Update(updatedSource),
|
||||
Storage.Update(updatedDest),
|
||||
Storage.Insert(journalEntry.Entry),
|
||||
@event
|
||||
);
|
||||
return (Storage.Update(updatedSource), Storage.Update(updatedDest), Storage.Insert(journalEntry.Entry), @event);
|
||||
}
|
||||
}
|
||||
+2
-1
@@ -6,4 +6,5 @@ namespace FinancialApi.Application.Models;
|
||||
public record JournalReversalContext(
|
||||
JournalEntry OriginalJournalEntry,
|
||||
IReadOnlyList<Account> AffectedAccounts,
|
||||
DateTimeOffset TimeStamp);
|
||||
DateTimeOffset TimeStamp
|
||||
);
|
||||
Reference in New Issue
Block a user