refactor(app): implement year filter for account entries and update related services
This commit is contained in:
@@ -67,6 +67,27 @@ public class AccountService : IAccountService
|
||||
await db.SaveChangesAsync();
|
||||
}
|
||||
|
||||
public async Task UpdateCarryoverAsync(int accountId, decimal carryoverBalance, int? year)
|
||||
{
|
||||
if (!year.HasValue)
|
||||
{
|
||||
await UpdateCarryoverAsync(accountId, carryoverBalance);
|
||||
return;
|
||||
}
|
||||
|
||||
await using var db = await _dbFactory.CreateDbContextAsync();
|
||||
|
||||
var account = await db.Accounts.FindAsync(accountId)
|
||||
?? throw new InvalidOperationException($"Account {accountId} not found.");
|
||||
|
||||
var movementBeforeYear = await db.Entries
|
||||
.Where(e => e.AccountId == accountId && !e.IsDeleted && e.Date.Year < year.Value)
|
||||
.SumAsync(e => e.Type == EntryType.Income ? e.Amount : -e.Amount);
|
||||
|
||||
account.CarryoverBalance = carryoverBalance - movementBeforeYear;
|
||||
await db.SaveChangesAsync();
|
||||
}
|
||||
|
||||
public async Task DeleteAccountAsync(int accountId)
|
||||
{
|
||||
await using var db = await _dbFactory.CreateDbContextAsync();
|
||||
|
||||
Reference in New Issue
Block a user