A local-first personal finance app
[AI] Import YNAB4 month notes and category budget notes (#9198)
Stephen Brown II991c2f7d092a
diff --git a/packages/loot-core/src/server/importers/ynab4-types.ts b/packages/loot-core/src/server/importers/ynab4-types.tsindex 15b45e9cca..b8614f2080 100644--- a/packages/loot-core/src/server/importers/ynab4-types.ts+++ b/packages/loot-core/src/server/importers/ynab4-types.ts@@ -69,6 +69,7 @@ export type RenameCondition = {export type MonthlyBudget = {entityType: string;+ note?: string;monthlySubCategoryBudgets: MonthlySubCategoryBudget[];month: string;entityVersion: string;@@ -77,6 +78,7 @@ export type MonthlyBudget = {export type MonthlySubCategoryBudget = {entityType: string;+ note?: string;categoryId: string;budgeted: number;overspendingHandling?: string | undefined | null;diff --git a/packages/loot-core/src/server/importers/ynab4.test.ts b/packages/loot-core/src/server/importers/ynab4.test.tsindex a55fe9c5b3..35c3b13927 100644--- a/packages/loot-core/src/server/importers/ynab4.test.ts+++ b/packages/loot-core/src/server/importers/ynab4.test.ts@@ -3,7 +3,8 @@ import { beforeEach, describe, expect, it, vi } from 'vitest';import { send } from '#server/main-app';import { makeChild } from '#shared/transactions';-import { importTransactions } from './ynab4';+import { doImport, getBudgetNotes, importTransactions } from './ynab4';+import type { MonthlyBudget, MonthlySubCategoryBudget } from './ynab4-types';import type * as YNAB4 from './ynab4-types';vi.mock('#server/main-app', () => ({@@ -108,3 +109,157 @@ describe('importTransactions', () => {expect(transferChild.transfer_id).toBe(entityIdMap.get('txn-transfer-in'));});});++function categoryBudget(+ overrides: Partial<MonthlySubCategoryBudget> = {},+): MonthlySubCategoryBudget {+ return {+ entityType: 'monthlyCategoryBudget',+ categoryId: 'A5',+ budgeted: 100,+ entityVersion: 'A-1',+ entityId: 'category-budget',+ parentMonthlyBudgetId: 'october',+ ...overrides,+ };+}++const entityIdMap = new Map([+ ['A5', 'actual-category'],+ ['deleted', 'actual-deleted'],+ ['empty', 'actual-empty'],+ ['missing-note', 'actual-missing-note'],+]);++describe('doImport', () => {+ it('preserves group notes alongside month and category budget notes', async () => {+ const data = {+ accounts: [],+ payees: [],+ transactions: [],+ scheduledTransactions: [],+ fileMetaData: {+ entityType: 'fileMetaData',+ budgetDataVersion: '4.2',+ currentKnowledge: 'A-1',+ },+ budgetMetaData: {+ entityType: 'budgetMetaData',+ strictBudget: 'true',+ entityVersion: 'A-1',+ currencyLocale: 'en_US',+ budgetType: 'Personal',+ dateLocale: 'en_US',+ entityId: 'budget',+ },+ masterCategories: [+ {+ entityType: 'masterCategory',+ entityId: 'A4',+ entityVersion: 'A-1',+ name: 'Sample Category',+ note: 'SAMPLE GROUP NOTE',+ type: 'OUTFLOW',+ expanded: true,+ deleteable: true,+ sortableIndex: 0,+ subCategories: [+ {+ entityType: 'subCategory',+ entityId: 'A5',+ entityVersion: 'A-1',+ masterCategoryId: 'A4',+ name: 'Sample Subcategory',+ type: 'OUTFLOW',+ sortableIndex: 0,+ },+ ],+ },+ ],+ monthlyBudgets: [+ {+ entityType: 'monthlyBudget',+ entityId: 'october',+ entityVersion: 'A-1',+ month: '2026-10-01',+ note: 'SAMPLE NOTE 1',+ monthlySubCategoryBudgets: [+ categoryBudget({ note: 'SAMPLE NOTE 2' }),+ ],+ },+ ],+ } satisfies YNAB4.YFull;++ vi.mocked(send).mockImplementation(async name => {+ switch (name) {+ case 'api/category-group-create':+ return 'actual-group';+ case 'api/category-create':+ return 'actual-category';+ case 'api/categories-get':+ return [{ id: 'income', name: 'Income' }];+ case 'api/accounts-get':+ case 'api/payees-get':+ return [];+ default:+ return null;+ }+ });++ await doImport(data);++ expect(+ vi.mocked(send).mock.calls.filter(([name]) => name === 'notes-save'),+ ).toEqual([+ ['notes-save', { id: 'actual-group', note: 'SAMPLE GROUP NOTE' }],+ ['notes-save', { id: 'budget-2026-10', note: 'SAMPLE NOTE 1' }],+ ['notes-save', { id: 'actual-category-2026-10', note: 'SAMPLE NOTE 2' }],+ ]);+ expect(send).toHaveBeenCalledWith('api/budget-set-amount', {+ month: '2026-10',+ categoryId: 'actual-category',+ amount: 10000,+ });+ });+});++describe('getBudgetNotes', () => {+ it('imports month and mapped category notes, skipping tombstones and empty or unmapped notes', () => {+ const budget = {+ entityType: 'monthlyBudget',+ month: '2026-10-01',+ note: 'SAMPLE NOTE 1',+ entityVersion: 'A-1',+ entityId: 'october',+ monthlySubCategoryBudgets: [+ categoryBudget({ note: 'SAMPLE NOTE 2' }),+ categoryBudget({+ categoryId: 'deleted',+ note: 'Deleted note',+ isTombstone: true,+ }),+ categoryBudget({ categoryId: 'empty', note: '' }),+ categoryBudget({ categoryId: 'missing-note', budgeted: 0 }),+ categoryBudget({ categoryId: 'unmapped', note: 'Unmapped note' }),+ ],+ } satisfies MonthlyBudget;++ expect(getBudgetNotes(budget, entityIdMap)).toEqual([+ { id: 'budget-2026-10', note: 'SAMPLE NOTE 1' },+ { id: 'actual-category-2026-10', note: 'SAMPLE NOTE 2' },+ ]);+ });++ it.each([undefined, ''])('skips a month note of %s', note => {+ const budget = {+ entityType: 'monthlyBudget',+ month: '2026-10-01',+ note,+ entityVersion: 'A-1',+ entityId: 'october',+ monthlySubCategoryBudgets: [],+ } satisfies MonthlyBudget;++ expect(getBudgetNotes(budget, entityIdMap)).toEqual([]);+ });+});diff --git a/packages/loot-core/src/server/importers/ynab4.ts b/packages/loot-core/src/server/importers/ynab4.tsindex a2ba6938ee..40f07bd750 100644--- a/packages/loot-core/src/server/importers/ynab4.ts+++ b/packages/loot-core/src/server/importers/ynab4.ts@@ -297,6 +297,32 @@ function fillInBudgets(return budgets;}+export function getBudgetNotes(+ budget: YNAB4.MonthlyBudget,+ entityIdMap: Map<string, string>,+): { id: string; note: string }[] {+ const month = monthUtils.monthFromDate(budget.month);+ const notes: { id: string; note: string }[] = [];++ if (typeof budget.note === 'string' && budget.note.length > 0) {+ notes.push({ id: `budget-${month}`, note: budget.note });+ }++ for (const categoryBudget of budget.monthlySubCategoryBudgets) {+ const categoryId = entityIdMap.get(categoryBudget.categoryId);+ if (+ !categoryBudget.isTombstone &&+ categoryId &&+ typeof categoryBudget.note === 'string' &&+ categoryBudget.note.length > 0+ ) {+ notes.push({ id: `${categoryId}-${month}`, note: categoryBudget.note });+ }+ }++ return notes;+}+async function importBudgets(data: YNAB4.YFull,entityIdMap: Map<string, string>,@@ -307,6 +333,10 @@ async function importBudgets(await send('api/batch-budget-start');try {for (const budget of budgets) {+ for (const note of getBudgetNotes(budget, entityIdMap)) {+ await send('notes-save', note);+ }+const filled = fillInBudgets(data,budget.monthlySubCategoryBudgets.filter(b => !b.isTombstone),diff --git a/upcoming-release-notes/ynab4-budget-notes.md b/upcoming-release-notes/ynab4-budget-notes.mdnew file mode 100644index 0000000000..b4bd259c90--- /dev/null+++ b/upcoming-release-notes/ynab4-budget-notes.md@@ -0,0 +1,6 @@+---+category: Bugfix+authors: [StephenBrown2]+---++Import YNAB4 month notes and category budget notes