Skip to content

Commit bcf72b4

Browse files
authored
fix: propose envelopes no matter how opposing account was found (#743)
This fixes the proposed envelope to work with rename rules. Fixes #735.
1 parent 97789c7 commit bcf72b4

File tree

2 files changed

+46
-10
lines changed

2 files changed

+46
-10
lines changed

pkg/controllers/import.go

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,20 @@ func (co Controller) ImportYnabImportPreview(c *gin.Context) {
171171
}
172172

173173
duplicateTransactions(co, &transaction, account.BudgetID)
174+
175+
// Recommend an envelope
176+
if transaction.Transaction.SourceAccountID != account.ID && transaction.Transaction.SourceAccountID != uuid.Nil {
177+
err = recommendEnvelope(co, &transaction, transaction.Transaction.SourceAccountID)
178+
if err != nil {
179+
httperrors.Handler(c, err)
180+
}
181+
} else if transaction.Transaction.DestinationAccountID != account.ID && transaction.Transaction.DestinationAccountID != uuid.Nil {
182+
err = recommendEnvelope(co, &transaction, transaction.Transaction.DestinationAccountID)
183+
if err != nil {
184+
httperrors.Handler(c, err)
185+
}
186+
}
187+
174188
transactions[i] = transaction
175189
}
176190

@@ -331,16 +345,6 @@ func findAccounts(co Controller, transaction *importer.TransactionPreview, budge
331345
} else {
332346
transaction.Transaction.DestinationAccountID = accounts[0].ID
333347
}
334-
335-
// Preset the most popular recent envelope
336-
recentEnvelopes, err := accounts[0].RecentEnvelopes(co.DB)
337-
if err != nil {
338-
return err
339-
}
340-
341-
if len(recentEnvelopes) > 0 && recentEnvelopes[0].ID != uuid.Nil {
342-
transaction.Transaction.EnvelopeID = &recentEnvelopes[0].ID
343-
}
344348
}
345349

346350
return nil
@@ -368,3 +372,25 @@ func rename(transaction *importer.TransactionPreview, rules []models.RenameRule)
368372
transaction.Transaction.DestinationAccountID, transaction.RenameRuleID = replace(transaction.DestinationAccountName)
369373
}
370374
}
375+
376+
// recommendEnvelope sets the first of the recommended envelopes for the opposing account.
377+
func recommendEnvelope(co Controller, transaction *importer.TransactionPreview, id uuid.UUID) error {
378+
// Load the account
379+
var opposingAccount models.Account
380+
err := co.DB.First(&opposingAccount, models.Account{DefaultModel: models.DefaultModel{ID: id}}).Error
381+
if err != nil {
382+
return err
383+
}
384+
385+
// Preset the most popular recent envelope
386+
recentEnvelopes, err := opposingAccount.RecentEnvelopes(co.DB)
387+
if err != nil {
388+
return err
389+
}
390+
391+
if len(recentEnvelopes) > 0 {
392+
transaction.Transaction.EnvelopeID = &recentEnvelopes[0].ID
393+
}
394+
395+
return nil
396+
}

pkg/controllers/import_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,16 +259,23 @@ func (suite *TestSuiteStandard) TestRename() {
259259
// Account we import to
260260
internalAccount := suite.createTestAccount(models.AccountCreate{BudgetID: budget.Data.ID, Name: "Envelope Zero Account"})
261261

262+
// Test envelope and test transaction to the Edeka account with an envelope to test the envelope prefill
263+
envelope := suite.createTestEnvelope(models.EnvelopeCreate{CategoryID: suite.createTestCategory(models.CategoryCreate{BudgetID: budget.Data.ID}).Data.ID})
264+
envelopeID := envelope.Data.ID
265+
_ = suite.createTestTransaction(models.TransactionCreate{BudgetID: budget.Data.ID, SourceAccountID: internalAccount.Data.ID, DestinationAccountID: edeka.Data.ID, EnvelopeID: &envelopeID, Amount: decimal.NewFromFloat(12.00)})
266+
262267
tests := []struct {
263268
name string // Name of the test
264269
sourceAccountIDs []uuid.UUID // The IDs of the source accounts
265270
destinationAccountIDs []uuid.UUID // The IDs of the destination accounts
271+
envelopeIDs []*uuid.UUID // expected IDs of envelopes
266272
preTest func(*testing.T) [3]uuid.UUID // Function to execute before running tests
267273
}{
268274
{
269275
"Rule for Edeka",
270276
[]uuid.UUID{internalAccount.Data.ID, internalAccount.Data.ID, uuid.Nil},
271277
[]uuid.UUID{edeka.Data.ID, uuid.Nil, internalAccount.Data.ID},
278+
[]*uuid.UUID{&envelopeID, nil, nil},
272279
func(t *testing.T) [3]uuid.UUID {
273280
edeka := suite.createTestRenameRule(t, models.RenameRuleCreate{
274281
Match: "EDEKA*",
@@ -282,6 +289,7 @@ func (suite *TestSuiteStandard) TestRename() {
282289
"Rule for Edeka and DB",
283290
[]uuid.UUID{internalAccount.Data.ID, internalAccount.Data.ID, uuid.Nil},
284291
[]uuid.UUID{edeka.Data.ID, bahn.Data.ID, internalAccount.Data.ID},
292+
[]*uuid.UUID{&envelopeID, nil, nil},
285293
func(t *testing.T) [3]uuid.UUID {
286294
edeka := suite.createTestRenameRule(t, models.RenameRuleCreate{
287295
Match: "EDEKA*",
@@ -314,6 +322,8 @@ func (suite *TestSuiteStandard) TestRename() {
314322
}
315323

316324
assert.Equal(t, renameRuleIDs[i], transaction.RenameRuleID, "Expected rename rule has match '%s', actual rename rule has match '%s'", renameRuleIDs[i])
325+
326+
assert.Equal(t, tt.envelopeIDs[i], transaction.Transaction.EnvelopeID, "proposed envelope ID does not match in line %d", line)
317327
}
318328

319329
// Delete rename rules

0 commit comments

Comments
 (0)