Skip to content

Commit 9946536

Browse files
authored
fix: ignore deleted resources in calculation of balance for envelope (#661)
This fixes a bug where deleted resources were included in the balance calculation for an envelope, yielding wrong results.
1 parent 51f1966 commit 9946536

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

pkg/models/envelope.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ func (e Envelope) Balance(db *gorm.DB, month types.Month) (decimal.Decimal, erro
114114
Joins("JOIN accounts destination_account ON transactions.destination_account_id = destination_account.id AND destination_account.deleted_at IS NULL").
115115
Where("transactions.date < date(?)", month.AddDate(0, 1)).
116116
Where("transactions.envelope_id = ?", e.ID).
117+
Where("transactions.deleted_at IS NULL").
117118
Select("transactions.amount AS Amount, transactions.date AS Date, source_account.on_budget AS SourceAccountOnBudget, destination_account.on_budget AS DestinationAccountOnBudget").
118119
Find(&rawTransactions).Error
119120
if err != nil {
@@ -133,6 +134,7 @@ func (e Envelope) Balance(db *gorm.DB, month types.Month) (decimal.Decimal, erro
133134
Table("allocations").
134135
Where("allocations.month < date(?)", month.AddDate(0, 1)).
135136
Where("allocations.envelope_id = ?", e.ID).
137+
Where("allocations.deleted_at IS NULL").
136138
Find(&rawAllocations).Error
137139
if err != nil {
138140
return decimal.Zero, nil
@@ -150,6 +152,7 @@ func (e Envelope) Balance(db *gorm.DB, month types.Month) (decimal.Decimal, erro
150152
Table("month_configs").
151153
Where("month_configs.month < date(?)", month.AddDate(0, 1)).
152154
Where("month_configs.envelope_id = ?", e.ID).
155+
Where("month_configs.deleted_at IS NULL").
153156
Find(&rawConfigs).Error
154157
if err != nil {
155158
return decimal.Zero, nil

pkg/models/envelope_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,17 @@ func (suite *TestSuiteStandard) TestEnvelopeMonthBalance() {
236236
Date: time.Time(january.AddDate(0, 1)),
237237
})
238238

239+
// Deleted transaction to verify that deleted transactions are not used in the calculation
240+
deletedTransaction := suite.createTestTransaction(models.TransactionCreate{
241+
BudgetID: budget.ID,
242+
EnvelopeID: &envelope.ID,
243+
Amount: decimal.NewFromFloat(30),
244+
SourceAccountID: internalAccount.ID,
245+
DestinationAccountID: externalAccount.ID,
246+
Date: time.Time(january.AddDate(0, 1)),
247+
})
248+
suite.db.Delete(&deletedTransaction)
249+
239250
tests := []struct {
240251
month types.Month
241252
envelope models.Envelope

0 commit comments

Comments
 (0)