Skip to content

Commit 68ebbb9

Browse files
authored
fix: match rules are now called match rules and follow API v2 specification (#787)
This deprecates the `/v2/rename-rules` endpoint and introduces `/v2/match-rules` with the correct API semantics.
1 parent e823490 commit 68ebbb9

File tree

17 files changed

+1436
-299
lines changed

17 files changed

+1436
-299
lines changed

api/docs.go

Lines changed: 378 additions & 80 deletions
Large diffs are not rendered by default.

api/swagger.json

Lines changed: 378 additions & 80 deletions
Large diffs are not rendered by default.

api/swagger.yaml

Lines changed: 273 additions & 65 deletions
Large diffs are not rendered by default.

pkg/controllers/budget.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,7 @@ func (co Controller) DeleteBudget(c *gin.Context) {
470470
// @Param month path string true "The month in YYYY-MM format"
471471
// @Param budgetId path string true "Budget ID formatted as string"
472472
// @Router /v1/budgets/{budgetId}/{month}/allocations [delete]
473-
// @Deprecated true.
473+
// @Deprecated true
474474
func (co Controller) DeleteAllocationsMonth(c *gin.Context) {
475475
id, err := uuid.Parse(c.Param("budgetId"))
476476
if err != nil {
@@ -524,7 +524,7 @@ func (co Controller) DeleteAllocationsMonth(c *gin.Context) {
524524
// @Param budgetId path string true "Budget ID formatted as string"
525525
// @Param mode body BudgetAllocationMode true "Budget"
526526
// @Router /v1/budgets/{budgetId}/{month}/allocations [post]
527-
// @Deprecated true.
527+
// @Deprecated true
528528
func (co Controller) SetAllocationsMonth(c *gin.Context) {
529529
id, err := uuid.Parse(c.Param("budgetId"))
530530
if err != nil {

pkg/controllers/import.go

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -144,21 +144,21 @@ func (co Controller) ImportYnabImportPreview(c *gin.Context) {
144144
return
145145
}
146146

147-
// Get all rename rules for the budget that the import target account is part of
148-
var renameRules []models.RenameRule
147+
// Get all match rules for the budget that the import target account is part of
148+
var matchRules []models.MatchRule
149149
err = co.DB.
150150
Joins("JOIN accounts ON accounts.budget_id = ?", account.BudgetID).
151-
Joins("JOIN rename_rules rr ON rr.account_id = accounts.id").
151+
Joins("JOIN match_rules rr ON rr.account_id = accounts.id").
152152
Order("rr.priority asc").
153-
Find(&renameRules).Error
153+
Find(&matchRules).Error
154154
if err != nil {
155155
httperrors.Handler(c, err)
156156
return
157157
}
158158

159159
for i, transaction := range transactions {
160-
if len(renameRules) > 0 {
161-
rename(&transaction, renameRules)
160+
if len(matchRules) > 0 {
161+
match(&transaction, matchRules)
162162
}
163163

164164
// Only find accounts when they are not yet both set
@@ -349,8 +349,8 @@ func findAccounts(co Controller, transaction *importer.TransactionPreview, budge
349349
return nil
350350
}
351351

352-
// rename applies the renaming rules to a transaction.
353-
func rename(transaction *importer.TransactionPreview, rules []models.RenameRule) {
352+
// match applies the match rules to a transaction.
353+
func match(transaction *importer.TransactionPreview, rules []models.MatchRule) {
354354
replace := func(name string) (uuid.UUID, uuid.UUID) {
355355
// Iterate over all rules
356356
for _, rule := range rules {
@@ -364,11 +364,19 @@ func rename(transaction *importer.TransactionPreview, rules []models.RenameRule)
364364
}
365365

366366
if transaction.SourceAccountName != "" {
367-
transaction.Transaction.SourceAccountID, transaction.RenameRuleID = replace(transaction.SourceAccountName)
367+
transaction.Transaction.SourceAccountID, transaction.MatchRuleID = replace(transaction.SourceAccountName)
368+
369+
// This is kept for backwards compatibility and will be removed with API version 3
370+
// https://github.com/envelope-zero/backend/issues/763
371+
transaction.RenameRuleID = transaction.MatchRuleID
368372
}
369373

370374
if transaction.DestinationAccountName != "" {
371-
transaction.Transaction.DestinationAccountID, transaction.RenameRuleID = replace(transaction.DestinationAccountName)
375+
transaction.Transaction.DestinationAccountID, transaction.MatchRuleID = replace(transaction.DestinationAccountName)
376+
377+
// This is kept for backwards compatibility and will be removed with API version 3
378+
// https://github.com/envelope-zero/backend/issues/763
379+
transaction.RenameRuleID = transaction.MatchRuleID
372380
}
373381
}
374382

pkg/controllers/import_test.go

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ func (suite *TestSuiteStandard) TestYnabImportFindAccounts() {
250250
}
251251
}
252252

253-
func (suite *TestSuiteStandard) TestRename() {
253+
func (suite *TestSuiteStandard) TestMatch() {
254254
// Create a budget and two existing accounts to use
255255
budget := suite.createTestBudget(models.BudgetCreate{})
256256
edeka := suite.createTestAccount(models.AccountCreate{BudgetID: budget.Data.ID, Name: "Edeka", External: true})
@@ -277,12 +277,12 @@ func (suite *TestSuiteStandard) TestRename() {
277277
[]uuid.UUID{edeka.Data.ID, uuid.Nil, internalAccount.Data.ID},
278278
[]*uuid.UUID{&envelopeID, nil, nil},
279279
func(t *testing.T) [3]uuid.UUID {
280-
edeka := suite.createTestRenameRule(t, models.RenameRuleCreate{
280+
edeka := suite.createTestMatchRule(t, models.MatchRuleCreate{
281281
Match: "EDEKA*",
282282
AccountID: edeka.Data.ID,
283283
})
284284

285-
return [3]uuid.UUID{edeka.Data.ID}
285+
return [3]uuid.UUID{edeka.ID}
286286
},
287287
},
288288
{
@@ -291,25 +291,25 @@ func (suite *TestSuiteStandard) TestRename() {
291291
[]uuid.UUID{edeka.Data.ID, bahn.Data.ID, internalAccount.Data.ID},
292292
[]*uuid.UUID{&envelopeID, nil, nil},
293293
func(t *testing.T) [3]uuid.UUID {
294-
edeka := suite.createTestRenameRule(t, models.RenameRuleCreate{
294+
edeka := suite.createTestMatchRule(t, models.MatchRuleCreate{
295295
Match: "EDEKA*",
296296
AccountID: edeka.Data.ID,
297297
})
298298

299-
db := suite.createTestRenameRule(t, models.RenameRuleCreate{
299+
db := suite.createTestMatchRule(t, models.MatchRuleCreate{
300300
Match: "DB Vertrieb GmbH",
301301
AccountID: bahn.Data.ID,
302302
})
303303

304-
return [3]uuid.UUID{edeka.Data.ID, db.Data.ID}
304+
return [3]uuid.UUID{edeka.ID, db.ID}
305305
},
306306
},
307307
}
308308

309309
for _, tt := range tests {
310310
suite.T().Run(tt.name, func(t *testing.T) {
311-
renameRuleIDs := tt.preTest(t)
312-
preview := parseCSV(suite, internalAccount.Data.ID, "rename-rule-test.csv")
311+
matchRuleIDs := tt.preTest(t)
312+
preview := parseCSV(suite, internalAccount.Data.ID, "match-rule-test.csv")
313313

314314
for i, transaction := range preview.Data {
315315
line := i + 1
@@ -321,15 +321,19 @@ func (suite *TestSuiteStandard) TestRename() {
321321
assert.Equal(t, tt.destinationAccountIDs[i], transaction.Transaction.DestinationAccountID, "destinationAccountID does not match in line %d", line)
322322
}
323323

324-
assert.Equal(t, renameRuleIDs[i], transaction.RenameRuleID, "Expected rename rule has match '%s', actual rename rule has match '%s'", renameRuleIDs[i])
324+
assert.Equal(t, matchRuleIDs[i], transaction.MatchRuleID, "Expected match rule has match '%s', actual match rule has match '%s'", matchRuleIDs[i], transaction.MatchRuleID)
325+
326+
// This is kept for backwards compatibility and will be removed with API version 3
327+
// https://github.com/envelope-zero/backend/issues/763
328+
assert.Equal(t, matchRuleIDs[i], transaction.RenameRuleID, "Expected rename rule has match '%s', actual rename rule has match '%s'", matchRuleIDs[i], transaction.MatchRuleID)
325329

326330
assert.Equal(t, tt.envelopeIDs[i], transaction.Transaction.EnvelopeID, "proposed envelope ID does not match in line %d", line)
327331
}
328332

329-
// Delete rename rules
330-
for _, id := range renameRuleIDs {
333+
// Delete match rules
334+
for _, id := range matchRuleIDs {
331335
if id != uuid.Nil {
332-
suite.controller.DB.Delete(&models.RenameRule{}, id)
336+
suite.controller.DB.Delete(&models.MatchRule{}, id)
333337
}
334338
}
335339
})

0 commit comments

Comments
 (0)