From 9408714f0a8ae3d0a8c9d76b9d26cac31ffe9237 Mon Sep 17 00:00:00 2001 From: Morre Date: Sun, 8 Oct 2023 15:26:09 +0200 Subject: [PATCH] fix: availability date is now enforced to not be earlier than the month of the transaction Resolves #768. --- pkg/models/account_test.go | 1 + pkg/models/transaction.go | 2 ++ pkg/models/transaction_test.go | 22 ++++++++++++++++++++++ 3 files changed, 25 insertions(+) diff --git a/pkg/models/account_test.go b/pkg/models/account_test.go index 11735290..9b4a504c 100644 --- a/pkg/models/account_test.go +++ b/pkg/models/account_test.go @@ -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", }) diff --git a/pkg/models/transaction.go b/pkg/models/transaction.go index a68ca0aa..2c4fdcdc 100644 --- a/pkg/models/transaction.go +++ b/pkg/models/transaction.go @@ -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 diff --git a/pkg/models/transaction_test.go b/pkg/models/transaction_test.go index a68f3641..ac8d399a 100644 --- a/pkg/models/transaction_test.go +++ b/pkg/models/transaction_test.go @@ -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" @@ -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") +}