Skip to content

A local-first personal finance app

Handle duplicate categories/groups in nYNAB importer. (#4293)

youngcwe8b4a750ed52
diff --git a/packages/loot-core/src/server/importers/ynab5.ts b/packages/loot-core/src/server/importers/ynab5.ts
index 2595d5957b..acec98bd2d 100644
--- a/packages/loot-core/src/server/importers/ynab5.ts
+++ b/packages/loot-core/src/server/importers/ynab5.ts
@@ -79,13 +79,30 @@ async function importCategories(
if (
!equalsIgnoreCase(group.name, 'Internal Master Category') &&
!equalsIgnoreCase(group.name, 'Credit Card Payments') &&
+ !equalsIgnoreCase(group.name, 'Hidden Categories') &&
!equalsIgnoreCase(group.name, 'Income')
) {
- groupId = await actual.createCategoryGroup({
- name: group.name,
- is_income: false,
- });
- entityIdMap.set(group.id, groupId);
+ let run = true;
+ const MAX_RETRY = 10;
+ let count = 1;
+ const origName = group.name;
+ while (run) {
+ try {
+ groupId = await actual.createCategoryGroup({
+ name: group.name,
+ is_income: false,
+ });
+ entityIdMap.set(group.id, groupId);
+ run = false;
+ } catch (e) {
+ group.name = origName + '-' + count.toString();
+ count += 1;
+ if (count >= MAX_RETRY) {
+ run = false;
+ throw Error(e.message);
+ }
+ }
+ }
}
if (equalsIgnoreCase(group.name, 'Income')) {
@@ -112,12 +129,27 @@ async function importCategories(
case 'internal': // uncategorized is ignored too, handled by actual
break;
default: {
- const id = await actual.createCategory({
- name: cat.name,
- group_id: groupId,
- });
- entityIdMap.set(cat.id, id);
- break;
+ let run = true;
+ const MAX_RETRY = 10;
+ let count = 1;
+ const origName = cat.name;
+ while (run) {
+ try {
+ const id = await actual.createCategory({
+ name: cat.name,
+ group_id: groupId,
+ });
+ entityIdMap.set(cat.id, id);
+ run = false;
+ } catch (e) {
+ cat.name = origName + '-' + count.toString();
+ count += 1;
+ if (count >= MAX_RETRY) {
+ run = false;
+ throw Error(e.message);
+ }
+ }
+ }
}
}
}
diff --git a/upcoming-release-notes/4293.md b/upcoming-release-notes/4293.md
new file mode 100644
index 0000000000..1d11fc0747
--- /dev/null
+++ b/upcoming-release-notes/4293.md
@@ -0,0 +1,6 @@
+---
+category: Bugfix
+authors: [youngcw]
+---
+
+Fix nynab importer failing on duplicate categories