diff --git a/packages/loot-core/src/server/accounts/sync.ts b/packages/loot-core/src/server/accounts/sync.ts index 3ac8b592d44..c7709c728de 100644 --- a/packages/loot-core/src/server/accounts/sync.ts +++ b/packages/loot-core/src/server/accounts/sync.ts @@ -482,6 +482,26 @@ export async function reconcileExternalTransactions(acctId, transactions) { acctId, ], ); + + // Sort the matched transactions according to the distance from the original + // transactions date. i.e. if the original transaction is in 21-02-2024 and + // the matched transactions are: 20-02-2024, 21-02-2024, 29-02-2024 then + // the resulting data-set should be: 21-02-2024, 20-02-2024, 29-02-2024. + fuzzyDataset = fuzzyDataset.sort((a, b) => { + const aDistance = Math.abs( + dateFns.differenceInMilliseconds( + dateFns.parseISO(trans.date), + dateFns.parseISO(db.fromDateRepr(a.date)), + ), + ); + const bDistance = Math.abs( + dateFns.differenceInMilliseconds( + dateFns.parseISO(trans.date), + dateFns.parseISO(db.fromDateRepr(b.date)), + ), + ); + return aDistance > bDistance ? 1 : -1; + }); } transactionsStep1.push({ @@ -645,6 +665,26 @@ export async function reconcileTransactions(acctId, transactions) { acctId, ], ); + + // Sort the matched transactions according to the distance from the original + // transactions date. i.e. if the original transaction is in 21-02-2024 and + // the matched transactions are: 20-02-2024, 21-02-2024, 29-02-2024 then + // the resulting data-set should be: 21-02-2024, 20-02-2024, 29-02-2024. + fuzzyDataset = fuzzyDataset.sort((a, b) => { + const aDistance = Math.abs( + dateFns.differenceInMilliseconds( + dateFns.parseISO(trans.date), + dateFns.parseISO(db.fromDateRepr(a.date)), + ), + ); + const bDistance = Math.abs( + dateFns.differenceInMilliseconds( + dateFns.parseISO(trans.date), + dateFns.parseISO(db.fromDateRepr(b.date)), + ), + ); + return aDistance > bDistance ? 1 : -1; + }); } transactionsStep1.push({ diff --git a/upcoming-release-notes/2464.md b/upcoming-release-notes/2464.md new file mode 100644 index 00000000000..e2b2de5a91f --- /dev/null +++ b/upcoming-release-notes/2464.md @@ -0,0 +1,6 @@ +--- +category: Bugfix +authors: [MatissJanis] +--- + +Fix csv/ofx import sometimes importing duplicate transactions