Creating a Journal Reversal handler.
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user