Skip to content

Commit 65ac62a

Browse files
committed
fix: manual adjustments did not work
- latest balance was not fetched properly
1 parent bfce767 commit 65ac62a

File tree

4 files changed

+30
-35
lines changed

4 files changed

+30
-35
lines changed

internal/repositories/account_repository.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -846,3 +846,32 @@ func (r *AccountRepository) DeleteAccountSnapshots(tx *gorm.DB, accountID int64)
846846
func (r *AccountRepository) DeleteAllBalancesForAccount(tx *gorm.DB, accountID int64) error {
847847
return tx.Where("account_id = ?", accountID).Delete(&models.Balance{}).Error
848848
}
849+
850+
func (r *AccountRepository) GetLatestBalance(tx *gorm.DB, accountID int64) (decimal.Decimal, error) {
851+
var endBalance sql.NullString
852+
853+
err := tx.
854+
Raw(`
855+
SELECT end_balance::text
856+
FROM balances
857+
WHERE account_id = ?
858+
ORDER BY as_of DESC
859+
LIMIT 1
860+
`, accountID).
861+
Scan(&endBalance).Error
862+
if err != nil {
863+
return decimal.Zero, err
864+
}
865+
866+
if !endBalance.Valid {
867+
// no row found - treat as 0
868+
return decimal.Zero, nil
869+
}
870+
871+
d, err := decimal.NewFromString(endBalance.String)
872+
if err != nil {
873+
return decimal.Zero, err
874+
}
875+
876+
return d, nil
877+
}

internal/services/account_service.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -423,9 +423,7 @@ func (s *AccountService) UpdateAccount(userID int64, id int64, req *models.Accou
423423
return fmt.Errorf("invalid balance value: %w", err)
424424
}
425425

426-
// Current end balance from snapshot
427-
asOf := time.Now().UTC()
428-
current, err := utils.GetEndBalanceAsOf(tx, exAcc.ID, asOf)
426+
current, err := s.Repo.GetLatestBalance(tx, exAcc.ID)
429427
if err != nil {
430428
tx.Rollback()
431429
return err

internal/services/statistics_service.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,6 @@ func (s *StatisticsService) GetCurrentMonthStats(userID int64, accountID *int64)
274274
if tr.TransactionInflow.Account.AccountType.Classification == "liability" {
275275
debtRepaymentTotal = debtRepaymentTotal.Add(tr.Amount)
276276
}
277-
fmt.Println(debtRepaymentTotal)
278277

279278
takeHome = takeHome.Sub(tr.Amount)
280279
}

pkg/utils/ledger_helper.go

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,11 @@
11
package utils
22

33
import (
4-
"database/sql"
54
"fmt"
65
"time"
76
"wealth-warden/internal/models"
8-
9-
"github.com/shopspring/decimal"
10-
"gorm.io/gorm"
117
)
128

13-
func GetEndBalanceAsOf(tx *gorm.DB, accountID int64, t time.Time) (decimal.Decimal, error) {
14-
var endBalance sql.NullString
15-
16-
err := tx.
17-
Raw(`
18-
SELECT end_balance::text
19-
FROM balances
20-
WHERE account_id = ? AND as_of = ?
21-
`, accountID, t.UTC().Format("2006-01-02")).
22-
Scan(&endBalance).Error
23-
if err != nil {
24-
return decimal.Zero, err
25-
}
26-
27-
if !endBalance.Valid {
28-
// no row found → treat as 0
29-
return decimal.Zero, nil
30-
}
31-
32-
d, err := decimal.NewFromString(endBalance.String)
33-
if err != nil {
34-
return decimal.Zero, err
35-
}
36-
37-
return d, nil
38-
}
39-
409
func ValidateAccount(acc *models.Account, role string) error {
4110
if acc.ClosedAt != nil {
4211
return fmt.Errorf("%s account (ID=%d) is closed and cannot be used", role, acc.ID)

0 commit comments

Comments
 (0)