Skip to content

Commit 34d3180

Browse files
authored
fix: availability date is now enforced to not be earlier than the month of the transaction (#789)
Resolves #768.
1 parent 8f108f1 commit 34d3180

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed

pkg/models/account_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ func (suite *TestSuiteStandard) TestAccountCalculations() {
5959
SourceAccountID: externalAccount.ID,
6060
DestinationAccountID: account.ID,
6161
Amount: decimal.NewFromFloat(100),
62+
Date: time.Now(),
6263
AvailableFrom: types.MonthOf(time.Now()).AddDate(0, 1),
6364
Note: "Future Income Transaction",
6465
})

pkg/models/transaction.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ func (t *Transaction) BeforeSave(tx *gorm.DB) (err error) {
9090
// Default the AvailableForBudget date to the transaction date
9191
if t.AvailableFrom.IsZero() {
9292
t.AvailableFrom = types.MonthOf(t.Date)
93+
} else if t.AvailableFrom.Before(types.MonthOf(t.Date)) {
94+
return fmt.Errorf("availability month must not be earlier than the month of the transaction, transaction date: %s, available month %s", t.Date, t.AvailableFrom)
9395
}
9496

9597
// Enforce ReconciledSource = false when source account is external

pkg/models/transaction_test.go

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

8+
"github.com/envelope-zero/backend/v3/internal/types"
89
"github.com/envelope-zero/backend/v3/pkg/models"
910
"github.com/google/uuid"
1011
"github.com/stretchr/testify/assert"
@@ -120,3 +121,24 @@ func (suite *TestSuiteStandard) TestTransactionReconciled() {
120121
func (suite *TestSuiteStandard) TestTransactionSelf() {
121122
assert.Equal(suite.T(), "Transaction", models.Transaction{}.Self())
122123
}
124+
125+
// Regression test for https://github.com/envelope-zero/backend/issues/768
126+
func (suite *TestSuiteStandard) TestTransactionAvailableFromDate() {
127+
budget := suite.createTestBudget(models.BudgetCreate{})
128+
internalAccount := suite.createTestAccount(models.AccountCreate{External: false, BudgetID: budget.ID})
129+
externalAccount := suite.createTestAccount(models.AccountCreate{External: true, BudgetID: budget.ID})
130+
131+
transaction := models.Transaction{
132+
TransactionCreate: models.TransactionCreate{
133+
SourceAccountID: externalAccount.ID,
134+
DestinationAccountID: internalAccount.ID,
135+
Note: "TestTransactionAvailableFromDate",
136+
AvailableFrom: types.NewMonth(2023, 9),
137+
Date: time.Date(2023, 10, 7, 0, 0, 0, 0, time.UTC),
138+
},
139+
}
140+
141+
err := suite.db.Save(&transaction).Error
142+
suite.Assert().NotNil(err, "Saving a transaction with an AvailableFrom date in a month before the transaction date should not be possible")
143+
suite.Assert().Contains(err.Error(), "availability month must not be earlier than the month of the transaction")
144+
}

0 commit comments

Comments
 (0)