Skip to content

Commit 51ba679

Browse files
authored
feat!: set default available from to next month (#1100)
1 parent f93af87 commit 51ba679

File tree

4 files changed

+8
-7
lines changed

4 files changed

+8
-7
lines changed

pkg/controllers/v4/month_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,7 @@ func (suite *TestSuiteStandard) TestMonths() {
396396

397397
_ = createTestTransaction(suite.T(), v4.TransactionEditable{
398398
Date: time.Date(2022, 3, 1, 7, 38, 17, 0, time.UTC),
399+
AvailableFrom: types.NewMonth(2022, 3),
399400
Amount: decimal.NewFromFloat(1500),
400401
Note: "Income for march",
401402
SourceAccountID: externalAccount.Data.ID,

pkg/controllers/v4/transaction_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"testing"
77
"time"
88

9+
"github.com/envelope-zero/backend/v5/internal/types"
910
v4 "github.com/envelope-zero/backend/v5/pkg/controllers/v4"
1011
"github.com/envelope-zero/backend/v5/pkg/httputil"
1112
"github.com/envelope-zero/backend/v5/pkg/models"
@@ -192,10 +193,9 @@ func (suite *TestSuiteStandard) TestTransactionsGetFilter() {
192193
ReconciledDestination: false,
193194
})
194195

195-
// Note that this transaction will be available from 2016-05-01 because it's also at
196-
// that date, but by default, "available from" is set to the first of the month
197196
_ = createTestTransaction(suite.T(), v4.TransactionEditable{
198197
Date: time.Date(2016, 5, 1, 14, 13, 25, 584575, time.UTC),
198+
AvailableFrom: types.NewMonth(2016, 5),
199199
Amount: decimal.NewFromFloat(11235.813),
200200
Note: "Not important",
201201
EnvelopeID: e2ID,

pkg/models/budget_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -169,14 +169,14 @@ func (suite *TestSuiteStandard) TestBudgetCalculations() {
169169
}
170170
assert.True(suite.T(), isBalance.Equal(shouldBalance), "Balance for budget is not correct. Should be %s, is %s", shouldBalance, isBalance)
171171

172-
// Verify income for used budget in March
172+
// Verify income for used budget in March. AvailableFrom defaults to next month, so we check for April
173173
shouldIncome := decimal.NewFromFloat(4620) // Income transaction from employer + income from off budget account
174-
income, err := budget.Income(models.DB, marchTwentyTwentyTwo)
174+
income, err := budget.Income(models.DB, marchTwentyTwentyTwo.AddDate(0, 1))
175175
assert.Nil(suite.T(), err)
176176
assert.True(suite.T(), income.Equal(shouldIncome), "Income is %s, should be %s", income, shouldIncome)
177177

178-
// Verify income for empty budget in March
179-
income, err = emptyBudget.Income(models.DB, marchTwentyTwentyTwo)
178+
// Verify income for empty budget in March. AvailableFrom defaults to next month, so we check for April
179+
income, err = emptyBudget.Income(models.DB, marchTwentyTwentyTwo.AddDate(0, 1))
180180
assert.Nil(suite.T(), err)
181181
assert.True(suite.T(), income.IsZero(), "Income is %s, should be 0", income)
182182

pkg/models/transaction.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ func (t *Transaction) BeforeSave(tx *gorm.DB) (err error) {
135135

136136
// Default the AvailableForBudget date to the transaction date
137137
if t.AvailableFrom.IsZero() {
138-
t.AvailableFrom = types.MonthOf(t.Date)
138+
t.AvailableFrom = types.MonthOf(t.Date).AddDate(0, 1)
139139
} else if t.AvailableFrom.Before(types.MonthOf(t.Date)) {
140140
return fmt.Errorf("%w, transaction date: %s, available month %s", ErrAvailabilityMonthTooEarly, t.Date.Format("2006-01-02"), t.AvailableFrom)
141141
}

0 commit comments

Comments
 (0)