Skip to content

Commit

Permalink
fix: availability date is now enforced to not be earlier than the mon…
Browse files Browse the repository at this point in the history
…th of the transaction

Resolves #768.
  • Loading branch information
morremeyer committed Oct 8, 2023
1 parent 8f108f1 commit 9408714
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
1 change: 1 addition & 0 deletions pkg/models/account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ func (suite *TestSuiteStandard) TestAccountCalculations() {
SourceAccountID: externalAccount.ID,
DestinationAccountID: account.ID,
Amount: decimal.NewFromFloat(100),
Date: time.Now(),
AvailableFrom: types.MonthOf(time.Now()).AddDate(0, 1),
Note: "Future Income Transaction",
})
Expand Down
2 changes: 2 additions & 0 deletions pkg/models/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ func (t *Transaction) BeforeSave(tx *gorm.DB) (err error) {
// Default the AvailableForBudget date to the transaction date
if t.AvailableFrom.IsZero() {
t.AvailableFrom = types.MonthOf(t.Date)
} else if t.AvailableFrom.Before(types.MonthOf(t.Date)) {
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)
}

// Enforce ReconciledSource = false when source account is external
Expand Down
22 changes: 22 additions & 0 deletions pkg/models/transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"
"time"

"github.com/envelope-zero/backend/v3/internal/types"
"github.com/envelope-zero/backend/v3/pkg/models"
"github.com/google/uuid"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -120,3 +121,24 @@ func (suite *TestSuiteStandard) TestTransactionReconciled() {
func (suite *TestSuiteStandard) TestTransactionSelf() {
assert.Equal(suite.T(), "Transaction", models.Transaction{}.Self())
}

// Regression test for https://github.com/envelope-zero/backend/issues/768
func (suite *TestSuiteStandard) TestTransactionAvailableFromDate() {
budget := suite.createTestBudget(models.BudgetCreate{})
internalAccount := suite.createTestAccount(models.AccountCreate{External: false, BudgetID: budget.ID})
externalAccount := suite.createTestAccount(models.AccountCreate{External: true, BudgetID: budget.ID})

transaction := models.Transaction{
TransactionCreate: models.TransactionCreate{
SourceAccountID: externalAccount.ID,
DestinationAccountID: internalAccount.ID,
Note: "TestTransactionAvailableFromDate",
AvailableFrom: types.NewMonth(2023, 9),
Date: time.Date(2023, 10, 7, 0, 0, 0, 0, time.UTC),
},
}

err := suite.db.Save(&transaction).Error
suite.Assert().NotNil(err, "Saving a transaction with an AvailableFrom date in a month before the transaction date should not be possible")
suite.Assert().Contains(err.Error(), "availability month must not be earlier than the month of the transaction")
}

0 comments on commit 9408714

Please sign in to comment.