Skip to content

Commit 1ecdf90

Browse files
authored
feat: pre-fill preview with most common recent envelope (#691)
1 parent 853a7b3 commit 1ecdf90

File tree

2 files changed

+33
-11
lines changed

2 files changed

+33
-11
lines changed

pkg/controllers/import.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,11 @@ func (co Controller) ImportYnabImportPreview(c *gin.Context) {
144144
}
145145

146146
transactions = duplicateTransactions(co, transactions, account.BudgetID)
147-
transactions = findAccounts(co, transactions, account.BudgetID)
147+
transactions, err = findAccounts(co, transactions, account.BudgetID)
148+
if err != nil {
149+
httperrors.Handler(c, err)
150+
return
151+
}
148152

149153
c.JSON(http.StatusOK, ImportPreviewList{Data: transactions})
150154
}
@@ -275,7 +279,7 @@ func duplicateTransactions(co Controller, transactions []importer.TransactionPre
275279

276280
// findAccounts sets the source or destination account ID for a TransactionPreview resource
277281
// if there is exactly one account with a matching name.
278-
func findAccounts(co Controller, transactions []importer.TransactionPreview, budgetID uuid.UUID) []importer.TransactionPreview {
282+
func findAccounts(co Controller, transactions []importer.TransactionPreview, budgetID uuid.UUID) ([]importer.TransactionPreview, error) {
279283
for k, transaction := range transactions {
280284
// Find the right account name
281285
name := transaction.DestinationAccountName
@@ -311,10 +315,20 @@ func findAccounts(co Controller, transactions []importer.TransactionPreview, bud
311315
transaction.Transaction.DestinationAccountID = accounts[0].ID
312316
transaction.DestinationAccountName = ""
313317
}
318+
319+
// Preset the most popular recent envelope
320+
recentEnvelopes, err := accounts[0].RecentEnvelopes(co.DB)
321+
if err != nil {
322+
return []importer.TransactionPreview{}, err
323+
}
324+
325+
if len(recentEnvelopes) > 0 && recentEnvelopes[0].ID != uuid.Nil {
326+
transaction.Transaction.EnvelopeID = &recentEnvelopes[0].ID
327+
}
314328
}
315329

316330
transactions[k] = transaction
317331
}
318332

319-
return transactions
333+
return transactions, nil
320334
}

pkg/controllers/import_test.go

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -206,16 +206,22 @@ func (suite *TestSuiteStandard) TestYnabImportFindAccounts() {
206206
// Account we import to
207207
internalAccount := suite.createTestAccount(models.AccountCreate{BudgetID: budget.Data.ID, Name: "Envelope Zero Account"})
208208

209+
// Test envelope and test transaction to the Edeka account with an envelope to test the envelope prefill
210+
envelope := suite.createTestEnvelope(models.EnvelopeCreate{CategoryID: suite.createTestCategory(models.CategoryCreate{BudgetID: budget.Data.ID}).Data.ID})
211+
envelopeID := envelope.Data.ID
212+
_ = suite.createTestTransaction(models.TransactionCreate{BudgetID: budget.Data.ID, SourceAccountID: internalAccount.Data.ID, DestinationAccountID: edeka.Data.ID, EnvelopeID: &envelopeID, Amount: decimal.NewFromFloat(12.00)})
213+
209214
tests := []struct {
210-
name string // Name of the test
211-
sourceAccountIDs []uuid.UUID // The IDs of the source accounts
212-
sourceAccountNames []string // The sourceAccountName attribute after the find has been performed
213-
destinationAccountIDs []uuid.UUID // The IDs of the destination accounts
214-
destinationAccountNames []string // The destinationAccountName attribute after the find has been performed
215-
preTest func() // Function to execute before running tests
215+
name string // Name of the test
216+
sourceAccountIDs []uuid.UUID // The IDs of the source accounts
217+
sourceAccountNames []string // The sourceAccountName attribute after the find has been performed
218+
destinationAccountIDs []uuid.UUID // The IDs of the destination accounts
219+
destinationAccountNames []string // The destinationAccountName attribute after the find has been performed
220+
envelopeIDs []*uuid.UUID // expected IDs of envelopes
221+
preTest func() // Function to execute before running tests
216222
}{
217-
{"No matching (Some Company) & 1 Matching (Edeka) accounts", []uuid.UUID{internalAccount.Data.ID, internalAccount.Data.ID, uuid.Nil}, []string{"", "", "Some Company"}, []uuid.UUID{edeka.Data.ID, uuid.Nil, internalAccount.Data.ID}, []string{"", "Deutsche Bahn", ""}, func() {}},
218-
{"Two matching non-archived accounts", []uuid.UUID{internalAccount.Data.ID, internalAccount.Data.ID, uuid.Nil}, []string{"", "", "Some Company"}, []uuid.UUID{uuid.Nil, uuid.Nil, internalAccount.Data.ID}, []string{"Edeka", "Deutsche Bahn", ""}, func() {
223+
{"No matching (Some Company) & 1 Matching (Edeka) accounts", []uuid.UUID{internalAccount.Data.ID, internalAccount.Data.ID, uuid.Nil}, []string{"", "", "Some Company"}, []uuid.UUID{edeka.Data.ID, uuid.Nil, internalAccount.Data.ID}, []string{"", "Deutsche Bahn", ""}, []*uuid.UUID{&envelopeID, nil, nil}, func() {}},
224+
{"Two matching non-archived accounts", []uuid.UUID{internalAccount.Data.ID, internalAccount.Data.ID, uuid.Nil}, []string{"", "", "Some Company"}, []uuid.UUID{uuid.Nil, uuid.Nil, internalAccount.Data.ID}, []string{"Edeka", "Deutsche Bahn", ""}, []*uuid.UUID{nil, nil, nil}, func() {
219225
_ = suite.createTestAccount(models.AccountCreate{BudgetID: budget.Data.ID, Name: "Edeka"})
220226
}},
221227
}
@@ -230,6 +236,8 @@ func (suite *TestSuiteStandard) TestYnabImportFindAccounts() {
230236
assert.Equal(t, tt.sourceAccountNames[i], transaction.SourceAccountName, "sourceAccountName does not match in line %d", line)
231237
assert.Equal(t, tt.destinationAccountNames[i], transaction.DestinationAccountName, "destinationAccountName does not match in line %d", line)
232238

239+
assert.Equal(t, tt.envelopeIDs[i], transaction.Transaction.EnvelopeID, "proposed envelope ID does not match in line %d", line)
240+
233241
if tt.sourceAccountIDs[i] != uuid.Nil {
234242
assert.Equal(t, tt.sourceAccountIDs[i], transaction.Transaction.SourceAccountID, "sourceAccountID does not match in line %d", line)
235243
}

0 commit comments

Comments
 (0)