Added journal reversals.
This commit is contained in:
@@ -19,21 +19,16 @@ 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;
|
||||
return data ?? throw new InvalidOperationException($"Journal Entry {cmd.OriginalJournalId} does not exist");
|
||||
}
|
||||
|
||||
public static (
|
||||
IStorageAction<JournalEntry> ReversalWrite,
|
||||
IStorageAction<Account>[] AccountWrites,
|
||||
IStorageAction<JournalEntry> JournalWrite,
|
||||
UnitOfWork<Account> AccountWrites,
|
||||
JournalReversedEvent Event) Handle(ReverseJournalCommand cmd, ReversalData data)
|
||||
{
|
||||
var reversalJournal =
|
||||
BalancedJournal.CreateReversal(data.OriginalJournalEntry, cmd.Reason, DateTimeOffset.UtcNow);
|
||||
BalancedJournal.CreateReversal(data.OriginalJournalEntry, $"Reversal: {cmd.Reason}", DateTimeOffset.UtcNow);
|
||||
var accountState = data.AffectedAccounts.ToDictionary(a => a.Id);
|
||||
|
||||
reversalJournal.Entry.Lines.ForEach(l =>
|
||||
@@ -41,11 +36,14 @@ public static class ReverseJournalHandler
|
||||
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();
|
||||
var accountUow = new UnitOfWork<Account>();
|
||||
foreach (var account in accountState.Values)
|
||||
{
|
||||
accountUow.Update(account);
|
||||
}
|
||||
return (
|
||||
Storage.Insert(reversalJournal.Entry),
|
||||
accountWrites,
|
||||
accountUow,
|
||||
new JournalReversedEvent(cmd.OriginalJournalId, reversalJournal.Entry.Id)
|
||||
);
|
||||
}
|
||||
|
||||
@@ -10,23 +10,23 @@ public class DomainExceptionHandler : IExceptionHandler
|
||||
Exception exception,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
// Only intercept our specific domain/validation exceptions
|
||||
if (exception is InvalidOperationException or ArgumentException)
|
||||
if (exception is not (InvalidOperationException or ArgumentException))
|
||||
{
|
||||
var problemDetails = new ProblemDetails
|
||||
{
|
||||
Status = StatusCodes.Status400BadRequest,
|
||||
Title = "Domain Validation Error",
|
||||
Detail = exception.Message,
|
||||
Type = "https://datatracker.ietf.org/doc/html/rfc7231#section-6.5.1"
|
||||
};
|
||||
|
||||
httpContext.Response.StatusCode = problemDetails.Status.Value;
|
||||
await httpContext.Response.WriteAsJsonAsync(problemDetails, cancellationToken);
|
||||
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
return false;
|
||||
var problemDetails = new ProblemDetails
|
||||
{
|
||||
Status = StatusCodes.Status400BadRequest,
|
||||
Title = "Domain Validation Error",
|
||||
Detail = exception.Message,
|
||||
Type = "https://datatracker.ietf.org/doc/html/rfc7231#section-6.5.1"
|
||||
};
|
||||
|
||||
httpContext.Response.StatusCode = problemDetails.Status.Value;
|
||||
await httpContext.Response.WriteAsJsonAsync(problemDetails, cancellationToken);
|
||||
|
||||
return true;
|
||||
|
||||
}
|
||||
}
|
||||
@@ -22,7 +22,7 @@ Content-Type: application/json
|
||||
{
|
||||
"sourceAccountId": 1,
|
||||
"destinationAccountId": 2,
|
||||
"amount": 3000.00
|
||||
"amount": 1000.00
|
||||
}
|
||||
|
||||
### Reverse a Journal Entry
|
||||
@@ -31,6 +31,6 @@ POST {{FinancialApi_HostAddress}}/journal/reverse
|
||||
Content-Type: application/json
|
||||
|
||||
{
|
||||
"originalJournalId": "AD4FC011-9979-4364-A0D4-2CCD922ECB5E",
|
||||
"reason": "Incorrect Account"
|
||||
"originalJournalId": "3E829F92-E2FA-4FCD-9B7B-E1B5AAC96FF1",
|
||||
"reason": "Incorrect Account Fee"
|
||||
}
|
||||
|
||||
@@ -58,20 +58,20 @@ app.UseHttpsRedirection();
|
||||
|
||||
app.MapPost("/accounts/apply-fee", async (ApplyFeeCommand cmd, IMessageBus bus) =>
|
||||
{
|
||||
await bus.InvokeAsync(cmd);
|
||||
await bus.InvokeAsync(cmd);
|
||||
return Results.Accepted();
|
||||
});
|
||||
|
||||
app.MapPost("/accounts/transfer", async (TransferFundsCommand cmd, IMessageBus bus) =>
|
||||
{
|
||||
await bus.InvokeAsync(cmd);
|
||||
await bus.InvokeAsync(cmd);
|
||||
return Results.Accepted();
|
||||
});
|
||||
|
||||
app.MapPost("/journal/reverse", async (ReverseJournalCommand cmd, IMessageBus bus) =>
|
||||
{
|
||||
await bus.InvokeAsync(cmd);
|
||||
await bus.InvokeAsync(cmd);
|
||||
return Results.Accepted();
|
||||
});
|
||||
|
||||
app.Run();
|
||||
app.Run();
|
||||
Reference in New Issue
Block a user