Skip to content

Commit

Permalink
fix: do not match archived accounts
Browse files Browse the repository at this point in the history
This fixes a bug where match rules targeting archived accounts were still applied, which is counterintuitive.
  • Loading branch information
morremeyer committed Feb 12, 2025
1 parent 3bb89b0 commit 7e5b798
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
4 changes: 2 additions & 2 deletions internal/controllers/v4/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,10 +289,10 @@ func ImportYnabImportPreview(c *gin.Context) {
return
}

// Get all match rules for the budget that the import target account is part of
// Get all match rules for the budget for which the account is not archived
var matchRules []models.MatchRule
err = models.DB.
Joins("JOIN accounts ON accounts.budget_id = ?", account.BudgetID).
Joins("JOIN accounts ON accounts.budget_id = ? AND NOT accounts.archived", account.BudgetID).
Joins("JOIN match_rules rr ON rr.account_id = accounts.id").
Order("rr.priority asc").
Find(&matchRules).Error
Expand Down
20 changes: 20 additions & 0 deletions internal/controllers/v4/import_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,26 @@ func (suite *TestSuiteStandard) TestImportYnabImportPreviewFindAccounts() {
}
}

// TestImportMatchingNoArchivedAccounts verifies that during an import, match rules for archived accounts
// are not used
func (suite *TestSuiteStandard) TestImportMatchingNoArchivedAccounts() {
// Create a budget and two existing accounts to use
budget := createTestBudget(suite.T(), v4.BudgetEditable{})

// Account we import to
internalAccount := createTestAccount(suite.T(), v4.AccountEditable{BudgetID: budget.Data.ID, Name: "Envelope Zero Account"})

edeka := createTestAccount(suite.T(), v4.AccountEditable{BudgetID: budget.Data.ID, Name: "Edeka", External: true, Archived: true})
_ = createTestMatchRule(suite.T(), v4.MatchRuleEditable{
Match: "Supermarket*",
AccountID: edeka.Data.ID,
})

preview := suite.parseCSV(suite.T(), internalAccount.Data.ID, "match-rule-archived-account.csv")

assert.Equal(suite.T(), uuid.Nil, preview.Data[0].Transaction.DestinationAccountID)
}

func (suite *TestSuiteStandard) TestImportYnabImportPreviewMatch() {
// Create a budget and two existing accounts to use
budget := createTestBudget(suite.T(), v4.BudgetEditable{})
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Date,Payee,Memo,Outflow,Inflow
04/01/2019,Supermarket,,59.97,

0 comments on commit 7e5b798

Please sign in to comment.