Skip to content

A local-first personal finance app

:bug: (import) patch phantom transactions getting created (#2464)

Matiss Janis Aboltins5343030800e8
diff --git a/packages/loot-core/src/server/accounts/sync.ts b/packages/loot-core/src/server/accounts/sync.ts
index 3ac8b592d4..c7709c728d 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 0000000000..e2b2de5a91
--- /dev/null
+++ b/upcoming-release-notes/2464.md
@@ -0,0 +1,6 @@
+---
+category: Bugfix
+authors: [MatissJanis]
+---
+
+Fix csv/ofx import sometimes importing duplicate transactions