Skip to content

Commit f083ed3

Browse files
authored
fix: verify that duplicate detection is only performed for the same budget (#688)
1 parent 1dc9f5f commit f083ed3

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

pkg/controllers/import.go

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ func (co Controller) ImportYnabImportPreview(c *gin.Context) {
143143
return
144144
}
145145

146-
transactions = duplicateTransactions(co, transactions)
146+
transactions = duplicateTransactions(co, transactions, account.BudgetID)
147147
transactions = findAccounts(co, transactions, account.BudgetID)
148148

149149
c.JSON(http.StatusOK, ImportPreviewList{Data: transactions})
@@ -242,21 +242,29 @@ func getUploadedFile(c *gin.Context, suffix string) (multipart.File, bool) {
242242
// duplicateTransactions finds duplicate transactions by their import hash. For all input resources,
243243
// existing resources with the same import hash are searched. If any exist, their IDs are set in the
244244
// DuplicateTransactionIDs field.
245-
func duplicateTransactions(co Controller, transactions []importer.TransactionPreview) []importer.TransactionPreview {
245+
func duplicateTransactions(co Controller, transactions []importer.TransactionPreview, budgetID uuid.UUID) []importer.TransactionPreview {
246246
for k, transaction := range transactions {
247247
var duplicates []models.Transaction
248-
co.DB.Find(&duplicates, models.Transaction{
249-
TransactionCreate: models.TransactionCreate{
250-
ImportHash: transaction.Transaction.ImportHash,
251-
},
252-
})
248+
co.DB.
249+
Preload("SourceAccount").
250+
Preload("DestinationAccount").
251+
Where(models.Transaction{
252+
TransactionCreate: models.TransactionCreate{
253+
ImportHash: transaction.Transaction.ImportHash,
254+
},
255+
}).
256+
Where(models.Transaction{SourceAccount: models.Account{AccountCreate: models.AccountCreate{BudgetID: budgetID}}}).
257+
Or(models.Transaction{DestinationAccount: models.Account{AccountCreate: models.AccountCreate{BudgetID: budgetID}}}).
258+
Find(&duplicates)
253259

254260
// When there are no resources, we want an empty list, not null
255261
// Therefore, we use make to create a slice with zero elements
256262
// which will be marshalled to an empty JSON array
257263
duplicateIDs := make([]uuid.UUID, 0)
258264
for _, duplicate := range duplicates {
259-
duplicateIDs = append(duplicateIDs, duplicate.ID)
265+
if duplicate.SourceAccount.BudgetID == budgetID || duplicate.DestinationAccount.BudgetID == budgetID {
266+
duplicateIDs = append(duplicateIDs, duplicate.ID)
267+
}
260268
}
261269
transaction.DuplicateTransactionIDs = duplicateIDs
262270
transactions[k] = transaction

pkg/controllers/import_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,12 @@ func (suite *TestSuiteStandard) TestYnabImportPreviewDuplicateDetection() {
163163
Amount: decimal.NewFromFloat(1.13),
164164
})
165165

166+
_ = suite.createTestTransaction(models.TransactionCreate{
167+
SourceAccountID: suite.createTestAccount(models.AccountCreate{Note: "This account is in a different Budget, but has the same ImportHash"}).Data.ID,
168+
ImportHash: preview.Data[0].Transaction.ImportHash,
169+
Amount: decimal.NewFromFloat(42.23),
170+
})
171+
166172
preview = parseCSV(suite, account.Data.ID, "comdirect-ynap.csv")
167173

168174
suite.Assert().Len(preview.Data[0].DuplicateTransactionIDs, 1, "Duplicate transaction IDs field does not have the correct number of IDs")

0 commit comments

Comments
 (0)