Creating a Journal Reversal handler.
This commit is contained in:
@@ -8,9 +8,11 @@ public record ApplyFeeCommand(int AccountId, decimal Amount);
|
||||
|
||||
public record FeeAppliedEvent(int AccountId, decimal Amount);
|
||||
|
||||
public record FeePair(Account Source, Account Destination);
|
||||
|
||||
public static class ApplyFeeHandler
|
||||
{
|
||||
public static async Task<(Account, Account)> LoadAsync(
|
||||
public static async Task<FeePair> LoadAsync(
|
||||
ApplyFeeCommand cmd,
|
||||
IAccountQuery query)
|
||||
{
|
||||
@@ -20,7 +22,7 @@ public static class ApplyFeeHandler
|
||||
{
|
||||
throw new InvalidOperationException($"Cannot process transfer. Account(s) not found.");
|
||||
}
|
||||
return (source, dest);
|
||||
return new FeePair(source, dest);
|
||||
}
|
||||
|
||||
public static (
|
||||
@@ -31,12 +33,11 @@ public static class ApplyFeeHandler
|
||||
)
|
||||
Handle(
|
||||
ApplyFeeCommand cmd,
|
||||
Account source,
|
||||
Account dest
|
||||
FeePair pair
|
||||
)
|
||||
{
|
||||
var updatedSource = source.ApplyPosting(cmd.Amount, EntryType.Debit);
|
||||
var updatedRevenue = dest.ApplyPosting(cmd.Amount, EntryType.Credit);
|
||||
var updatedSource = pair.Source.ApplyPosting(cmd.Amount, EntryType.Debit);
|
||||
var updatedRevenue = pair.Destination.ApplyPosting(cmd.Amount, EntryType.Credit);
|
||||
var lines = new List<JournalLine>
|
||||
{
|
||||
new(updatedSource.Id, cmd.Amount, EntryType.Debit),
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
using FinancialApi.Domain;
|
||||
using Wolverine.Persistence;
|
||||
|
||||
namespace FinancialApi.Application;
|
||||
|
||||
public record ReverseJournalCommand(Guid OriginalJournalId, string Reason);
|
||||
|
||||
public record JournalReversedEvent(Guid OriginalJournalId, Guid ReversalJournalId);
|
||||
|
||||
public record ReversalData(JournalEntry OriginalJournalEntry, IReadOnlyList<Account> AffectedAccounts);
|
||||
|
||||
public interface IReversalQuery
|
||||
{
|
||||
Task<ReversalData?> GetReversalDataAsync(Guid journalEntryId);
|
||||
}
|
||||
|
||||
public static class ReverseJournalHandler
|
||||
{
|
||||
public static async Task<ReversalData?> LoadAsync(ReverseJournalCommand cmd, IReversalQuery query)
|
||||
{
|
||||
var data = await query.GetReversalDataAsync(cmd.OriginalJournalId);
|
||||
if (data == null)
|
||||
{
|
||||
throw new InvalidOperationException($"Journal Entry {cmd.OriginalJournalId} does not exist");
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
public static (
|
||||
IStorageAction<JournalEntry> ReversalWrite,
|
||||
IStorageAction<Account>[] AccountWrites,
|
||||
JournalReversedEvent Event) Handle(ReverseJournalCommand cmd, ReversalData data)
|
||||
{
|
||||
var reversalJournal =
|
||||
BalancedJournal.CreateReversal(data.OriginalJournalEntry, cmd.Reason, DateTimeOffset.UtcNow);
|
||||
var accountState = data.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 accountWrites = accountState.Values.Select(a => (IStorageAction<Account>)Storage.Update(a)).ToArray();
|
||||
return (
|
||||
Storage.Insert(reversalJournal.Entry),
|
||||
accountWrites,
|
||||
new JournalReversedEvent(cmd.OriginalJournalId, reversalJournal.Entry.Id)
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user