Files
silver-octo-spoon/wolverine/a-frame-architecture/FinancialApi.Application/ApplyFeeHandler.cs
T

58 lines
1.8 KiB
C#

using FinancialApi.Domain;
using Wolverine.Attributes;
using Wolverine.Persistence;
namespace FinancialApi.Application;
public record ApplyFeeCommand(int AccountId, decimal Amount);
public record FeeAppliedEvent(int AccountId, decimal Amount);
public static class ApplyFeeHandler
{
public static async Task<(Account, Account)> LoadAsync(
ApplyFeeCommand cmd,
IAccountQuery query)
{
var source = await query.FindByIdAsync(cmd.AccountId);
var dest = await query.FindByIdAsync(99999);
if (source == null || dest == null)
{
throw new InvalidOperationException($"Cannot process transfer. Account(s) not found.");
}
return (source, dest);
}
public static (
IStorageAction<Account> CustomerWrite,
IStorageAction<Account> RevenueWrite,
IStorageAction<JournalEntry> JournalWrite,
FeeAppliedEvent Event
)
Handle(
ApplyFeeCommand cmd,
Account source,
Account dest
)
{
var updatedSource = source.ApplyPosting(cmd.Amount, EntryType.Debit);
var updatedRevenue = dest.ApplyPosting(cmd.Amount, EntryType.Credit);
var lines = new List<JournalLine>
{
new(updatedSource.Id, cmd.Amount, EntryType.Debit),
new(updatedRevenue.Id, cmd.Amount, EntryType.Credit)
};
var journalEntry = BalancedJournal.Create(Guid.NewGuid(),
$"Service Fee Applied: {cmd.Amount} to Account {cmd.AccountId}",
DateTimeOffset.UtcNow, lines);
var @event = new FeeAppliedEvent(cmd.AccountId, cmd.Amount);
return (
Storage.Update(updatedSource),
Storage.Update(updatedRevenue),
Storage.Insert(journalEntry.Entry),
@event
);
}
}