Skip to content

A local-first personal finance app

[AI] Fix CSV preview category normalization (#7878)

mehmet turac44f1aac59ad7
diff --git a/packages/desktop-client/src/components/modals/ImportTransactionsModal/ImportTransactionsModal.tsx b/packages/desktop-client/src/components/modals/ImportTransactionsModal/ImportTransactionsModal.tsx
index fafa2e4d63..4ed9b45b16 100644
--- a/packages/desktop-client/src/components/modals/ImportTransactionsModal/ImportTransactionsModal.tsx
+++ b/packages/desktop-client/src/components/modals/ImportTransactionsModal/ImportTransactionsModal.tsx
@@ -46,6 +46,7 @@ import {
filterByStartDate,
isDateFormat,
parseAmountFields,
+ parseCategoryFields,
parseDate,
stripCsvImportTransaction,
} from './utils';
@@ -160,19 +161,6 @@ function getInitialMappings(transactions) {
};
}
-function parseCategoryFields(trans, categories) {
- let match = null;
- categories.forEach(category => {
- if (category.id === trans.category) {
- return null;
- }
- if (category.name === trans.category) {
- match = category.id;
- }
- });
- return match;
-}
-
export function ImportTransactionsModal({
filename: originalFileName,
accountId,
@@ -317,9 +305,7 @@ export function ImportTransactionsModal({
}
const category_id = parseCategoryFields(trans, categories);
- if (category_id != null) {
- trans.category = category_id;
- }
+ trans.category = category_id;
const {
inflow: _inflow,
diff --git a/packages/desktop-client/src/components/modals/ImportTransactionsModal/utils.test.ts b/packages/desktop-client/src/components/modals/ImportTransactionsModal/utils.test.ts
index 1df9862841..3c30c94535 100644
--- a/packages/desktop-client/src/components/modals/ImportTransactionsModal/utils.test.ts
+++ b/packages/desktop-client/src/components/modals/ImportTransactionsModal/utils.test.ts
@@ -1,4 +1,4 @@
-import { filterByStartDate, parseDate } from './utils';
+import { filterByStartDate, parseCategoryFields, parseDate } from './utils';
import type { ImportTransaction } from './utils';
describe('Import transactions', () => {
@@ -282,4 +282,23 @@ describe('Import transactions', () => {
expect(result[0].trx_id).toBe('1');
});
});
+
+ describe('parseCategoryFields', () => {
+ const categories = [
+ { id: 'cat-income', name: 'Income' },
+ { id: 'cat-deposits', name: 'Deposits' },
+ ];
+
+ it('returns the category id when CSV category text matches a category name', () => {
+ expect(parseCategoryFields({ category: 'Deposits' }, categories)).toBe(
+ 'cat-deposits',
+ );
+ });
+
+ it('returns null for unresolved CSV category text', () => {
+ expect(
+ parseCategoryFields({ category: 'Missing category' }, categories),
+ ).toBeNull();
+ });
+ });
});
diff --git a/packages/desktop-client/src/components/modals/ImportTransactionsModal/utils.ts b/packages/desktop-client/src/components/modals/ImportTransactionsModal/utils.ts
index 451c1a1faf..6f7ca7db9f 100644
--- a/packages/desktop-client/src/components/modals/ImportTransactionsModal/utils.ts
+++ b/packages/desktop-client/src/components/modals/ImportTransactionsModal/utils.ts
@@ -139,6 +139,22 @@ export type ImportTransaction = {
date?: string;
} & Record<string, string | number | boolean>;
+type ImportCategory = {
+ id: string;
+ name: string;
+};
+
+export function parseCategoryFields(
+ trans: Pick<ImportTransaction, 'category'>,
+ categories: ImportCategory[],
+) {
+ const match = categories.find(
+ category =>
+ category.id !== trans.category && category.name === trans.category,
+ );
+ return match?.id ?? null;
+}
+
export type FieldMapping = {
date: string | null;
amount: string | null;
diff --git a/upcoming-release-notes/7878.md b/upcoming-release-notes/7878.md
new file mode 100644
index 0000000000..c6677f4033
--- /dev/null
+++ b/upcoming-release-notes/7878.md
@@ -0,0 +1,6 @@
+---
+category: Bugfixes
+authors: [mturac]
+---
+
+Fix CSV import previews showing false updates when a mapped category does not exist.