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 FeeAppliedEvent(int AccountId, decimal Amount);
|
||||||
|
|
||||||
|
public record FeePair(Account Source, Account Destination);
|
||||||
|
|
||||||
public static class ApplyFeeHandler
|
public static class ApplyFeeHandler
|
||||||
{
|
{
|
||||||
public static async Task<(Account, Account)> LoadAsync(
|
public static async Task<FeePair> LoadAsync(
|
||||||
ApplyFeeCommand cmd,
|
ApplyFeeCommand cmd,
|
||||||
IAccountQuery query)
|
IAccountQuery query)
|
||||||
{
|
{
|
||||||
@@ -20,7 +22,7 @@ public static class ApplyFeeHandler
|
|||||||
{
|
{
|
||||||
throw new InvalidOperationException($"Cannot process transfer. Account(s) not found.");
|
throw new InvalidOperationException($"Cannot process transfer. Account(s) not found.");
|
||||||
}
|
}
|
||||||
return (source, dest);
|
return new FeePair(source, dest);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static (
|
public static (
|
||||||
@@ -31,12 +33,11 @@ public static class ApplyFeeHandler
|
|||||||
)
|
)
|
||||||
Handle(
|
Handle(
|
||||||
ApplyFeeCommand cmd,
|
ApplyFeeCommand cmd,
|
||||||
Account source,
|
FeePair pair
|
||||||
Account dest
|
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
var updatedSource = source.ApplyPosting(cmd.Amount, EntryType.Debit);
|
var updatedSource = pair.Source.ApplyPosting(cmd.Amount, EntryType.Debit);
|
||||||
var updatedRevenue = dest.ApplyPosting(cmd.Amount, EntryType.Credit);
|
var updatedRevenue = pair.Destination.ApplyPosting(cmd.Amount, EntryType.Credit);
|
||||||
var lines = new List<JournalLine>
|
var lines = new List<JournalLine>
|
||||||
{
|
{
|
||||||
new(updatedSource.Id, cmd.Amount, EntryType.Debit),
|
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)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -20,4 +20,13 @@ public record BalancedJournal
|
|||||||
|
|
||||||
return new(new JournalEntry(id, description, createdAt, lines));
|
return new(new JournalEntry(id, description, createdAt, lines));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static BalancedJournal CreateReversal(JournalEntry original, string reason, DateTimeOffset now)
|
||||||
|
{
|
||||||
|
var reversedLines = original.Lines.Select(l =>
|
||||||
|
l with { Type = l.Type == EntryType.Debit ? EntryType.Credit : EntryType.Debit })
|
||||||
|
.ToList();
|
||||||
|
|
||||||
|
return Create(Guid.NewGuid(), reason, now, reversedLines);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -7,6 +7,10 @@ public record JournalEntry
|
|||||||
public DateTimeOffset CreatedAt { get; init; }
|
public DateTimeOffset CreatedAt { get; init; }
|
||||||
public List<JournalLine> Lines { get; init; } = [];
|
public List<JournalLine> Lines { get; init; } = [];
|
||||||
|
|
||||||
|
internal JournalEntry()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
internal JournalEntry(Guid id, string description, DateTimeOffset createdAt, List<JournalLine> lines)
|
internal JournalEntry(Guid id, string description, DateTimeOffset createdAt, List<JournalLine> lines)
|
||||||
{
|
{
|
||||||
Lines = lines;
|
Lines = lines;
|
||||||
|
|||||||
@@ -0,0 +1,33 @@
|
|||||||
|
using FinancialApi.Application;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
|
||||||
|
namespace FinancialApi.Infrastructure;
|
||||||
|
|
||||||
|
public class ReversalQuery : IReversalQuery
|
||||||
|
{
|
||||||
|
private readonly AccountDbContext _db;
|
||||||
|
|
||||||
|
public ReversalQuery(AccountDbContext db) => _db = db;
|
||||||
|
|
||||||
|
public async Task<ReversalData?> GetReversalDataAsync(Guid journalEntryId)
|
||||||
|
{
|
||||||
|
var entry = await _db.JournalEntries
|
||||||
|
.Include(x => x.Lines)
|
||||||
|
.AsNoTracking()
|
||||||
|
.FirstOrDefaultAsync(x => x.Id == journalEntryId);
|
||||||
|
|
||||||
|
if (entry == null)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
var accountIds = entry.Lines.Select(l => l.AccountId).Distinct().ToList();
|
||||||
|
|
||||||
|
var accounts = await _db.Accounts
|
||||||
|
.AsNoTracking()
|
||||||
|
.Where(a => accountIds.Contains(a.Id))
|
||||||
|
.ToListAsync();
|
||||||
|
|
||||||
|
return new ReversalData(entry, accounts);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -24,3 +24,13 @@ Content-Type: application/json
|
|||||||
"destinationAccountId": 2,
|
"destinationAccountId": 2,
|
||||||
"amount": 3000.00
|
"amount": 3000.00
|
||||||
}
|
}
|
||||||
|
|
||||||
|
### Reverse a Journal Entry
|
||||||
|
|
||||||
|
POST {{FinancialApi_HostAddress}}/journal/reverse
|
||||||
|
Content-Type: application/json
|
||||||
|
|
||||||
|
{
|
||||||
|
"originalJournalId": "AD4FC011-9979-4364-A0D4-2CCD922ECB5E",
|
||||||
|
"reason": "Incorrect Account"
|
||||||
|
}
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ builder.Services.AddDbContext<AccountDbContext>(options =>
|
|||||||
options.UseSqlite(connectionString));
|
options.UseSqlite(connectionString));
|
||||||
|
|
||||||
builder.Services.AddScoped<IAccountQuery, AccountQuery>();
|
builder.Services.AddScoped<IAccountQuery, AccountQuery>();
|
||||||
|
builder.Services.AddScoped<IReversalQuery, ReversalQuery>();
|
||||||
|
|
||||||
// Add services to the container.
|
// Add services to the container.
|
||||||
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
|
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
|
||||||
@@ -67,4 +68,10 @@ app.MapPost("/accounts/transfer", async (TransferFundsCommand cmd, IMessageBus b
|
|||||||
return Results.Accepted();
|
return Results.Accepted();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
app.MapPost("/journal/reverse", async (ReverseJournalCommand cmd, IMessageBus bus) =>
|
||||||
|
{
|
||||||
|
await bus.InvokeAsync(cmd);
|
||||||
|
return Results.Accepted();
|
||||||
|
});
|
||||||
|
|
||||||
app.Run();
|
app.Run();
|
||||||
|
|||||||
Reference in New Issue
Block a user