Skip to content

Commit 2e14b83

Browse files
committed
fix: budget balance not double the actual balance anymore
1 parent 0208596 commit 2e14b83

File tree

3 files changed

+39
-2
lines changed

3 files changed

+39
-2
lines changed

pkg/controllers/budget.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -616,7 +616,7 @@ func getBudgetObject(c *gin.Context, id uuid.UUID) (Budget, error) {
616616
}
617617

618618
return Budget{
619-
resource.WithCalculations(),
619+
resource,
620620
getBudgetLinks(c, resource.ID),
621621
}, nil
622622
}

pkg/controllers/budget_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -637,3 +637,39 @@ func (suite *TestSuiteEnv) TestSetAllocationsMonthFailures() {
637637
recorder = test.Request(suite.T(), http.MethodPost, strings.Replace(budgetAllocationsLink, "YYYY-MM", "2022-01", 1), `{ "mode": "UNKNOWN_MODE" }`)
638638
test.AssertHTTPStatus(suite.T(), http.StatusBadRequest, &recorder)
639639
}
640+
641+
// TestBudgetBalanceDoubleRegression verifies that the Budget balance is only added once.
642+
func (suite *TestSuiteEnv) TestBudgetBalanceDoubleRegression() {
643+
shouldBalance := decimal.NewFromFloat(1000)
644+
645+
budget := createTestBudget(suite.T(), models.BudgetCreate{Name: "TestBudgetBalanceDoubleRegression"})
646+
647+
internalAccount := createTestAccount(suite.T(), models.AccountCreate{
648+
BudgetID: budget.Data.ID,
649+
OnBudget: true,
650+
External: false,
651+
})
652+
653+
externalAccount := createTestAccount(suite.T(), models.AccountCreate{
654+
BudgetID: budget.Data.ID,
655+
OnBudget: true,
656+
External: true,
657+
})
658+
659+
category := createTestCategory(suite.T(), models.CategoryCreate{BudgetID: budget.Data.ID})
660+
envelope := createTestEnvelope(suite.T(), models.EnvelopeCreate{CategoryID: category.Data.ID})
661+
662+
_ = createTestTransaction(suite.T(), models.TransactionCreate{
663+
BudgetID: budget.Data.ID,
664+
Amount: shouldBalance,
665+
SourceAccountID: externalAccount.Data.ID,
666+
DestinationAccountID: internalAccount.Data.ID,
667+
EnvelopeID: &envelope.Data.ID,
668+
})
669+
670+
var budgetResponse controllers.BudgetResponse
671+
recorder := test.Request(suite.T(), http.MethodGet, budget.Data.Links.Self, "")
672+
test.DecodeResponse(suite.T(), &recorder, &budgetResponse)
673+
674+
assert.True(suite.T(), budgetResponse.Data.Balance.Equal(shouldBalance), "Balance is %s, should be %s", budgetResponse.Data.Balance, shouldBalance)
675+
}

pkg/models/budget.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ type BudgetMonth struct {
3636

3737
// WithCalculations computes all the calculated values.
3838
func (b Budget) WithCalculations() Budget {
39+
b.Balance = decimal.Zero
40+
3941
// Get all OnBudget accounts for the budget
4042
var accounts []Account
4143
_ = database.DB.Where(&Account{
@@ -47,7 +49,6 @@ func (b Budget) WithCalculations() Budget {
4749

4850
// Add all their balances to the budget's balance
4951
for _, account := range accounts {
50-
fmt.Println(account.WithCalculations().Balance)
5152
b.Balance = b.Balance.Add(account.WithCalculations().Balance)
5253
}
5354

0 commit comments

Comments
 (0)