Skip to content

A local-first personal finance app

Apply tag colors to YNAB flag tags (#6866)

Stephen Brown IIdeadd9aefc47
diff --git a/packages/desktop-client/e2e/data/ynab5-demo-budget.json b/packages/desktop-client/e2e/data/ynab5-demo-budget.json
index fe236e8bb9..432d240deb 100644
--- a/packages/desktop-client/e2e/data/ynab5-demo-budget.json
+++ b/packages/desktop-client/e2e/data/ynab5-demo-budget.json
@@ -1710,7 +1710,8 @@
"memo": "sending to savings",
"cleared": "cleared",
"approved": true,
- "flag_color": null,
+ "flag_color": "blue",
+ "flag_name": "Savings",
"account_id": "bc1d862f-bab0-41c3-bd1e-6cee8c688e32",
"payee_id": "8d3017e0-2aa6-4fe2-b011-c53c9f147eb6",
"category_id": null,
@@ -1730,7 +1731,8 @@
"memo": null,
"cleared": "cleared",
"approved": true,
- "flag_color": null,
+ "flag_color": "red",
+ "flag_name": "One-off",
"account_id": "bc1d862f-bab0-41c3-bd1e-6cee8c688e32",
"payee_id": "2a20470a-634f-4efa-a7f6-f1c0b0bdda41",
"category_id": "225be370-37da-4cf8-8b6b-4c6d61a0dd95",
@@ -1750,7 +1752,8 @@
"memo": "getting paid",
"cleared": "reconciled",
"approved": true,
- "flag_color": null,
+ "flag_color": "green",
+ "flag_name": "JOB",
"account_id": "bc1d862f-bab0-41c3-bd1e-6cee8c688e32",
"payee_id": "c843e030-5a77-4dc5-9b93-f8acc64b74f8",
"category_id": "36120d44-6c61-4402-985a-891a8d267858",
@@ -1770,7 +1773,8 @@
"memo": null,
"cleared": "cleared",
"approved": true,
- "flag_color": null,
+ "flag_color": "purple",
+ "flag_name": "Savings",
"account_id": "125f339b-2a63-481e-84c0-f04d898905d2",
"payee_id": "c843e030-5a77-4dc5-9b93-f8acc64b74f8",
"category_id": "36120d44-6c61-4402-985a-891a8d267858",
@@ -1984,7 +1988,8 @@
"frequency": "monthly",
"amount": -100000,
"memo": "Scheduled - repeated monthly",
- "flag_color": null,
+ "flag_color": "purple",
+ "flag_name": "Savings",
"account_id": "bc1d862f-bab0-41c3-bd1e-6cee8c688e32",
"payee_id": "0f0899e3-242f-42e6-aae9-a751060d878e",
"category_id": "419ae801-27c8-424b-8f39-9611825803db",
@@ -1999,6 +2004,7 @@
"amount": -100000,
"memo": "Scheduled - repeated weekly",
"flag_color": "blue",
+ "flag_name": "Savings",
"account_id": "bc1d862f-bab0-41c3-bd1e-6cee8c688e32",
"payee_id": "0f0899e3-242f-42e6-aae9-a751060d878e",
"category_id": "419ae801-27c8-424b-8f39-9611825803db",
@@ -2083,7 +2089,7 @@
"frequency": "daily",
"amount": -100000,
"memo": "Scheduled - repeated daily",
- "flag_color": "purple",
+ "flag_color": null,
"account_id": "bc1d862f-bab0-41c3-bd1e-6cee8c688e32",
"payee_id": "0f0899e3-242f-42e6-aae9-a751060d878e",
"category_id": "419ae801-27c8-424b-8f39-9611825803db",
@@ -2097,7 +2103,7 @@
"frequency": "monthly",
"amount": -100000,
"memo": "Scheduled - split categories monthly",
- "flag_color": "green",
+ "flag_color": "yellow",
"flag_name": "Split",
"account_id": "bc1d862f-bab0-41c3-bd1e-6cee8c688e32",
"payee_id": "0f0899e3-242f-42e6-aae9-a751060d878e",
diff --git a/packages/loot-core/src/server/importers/ynab5-types.ts b/packages/loot-core/src/server/importers/ynab5-types.ts
index f4e35fe48c..6d05b455c4 100644
--- a/packages/loot-core/src/server/importers/ynab5-types.ts
+++ b/packages/loot-core/src/server/importers/ynab5-types.ts
@@ -17,9 +17,9 @@ export type Budget = {
category_groups: CategoryGroup[];
categories: Category[];
months: MonthDetail[];
- transactions: TransactionSummary[];
+ transactions: Transaction[];
subtransactions: Subtransaction[];
- scheduled_transactions: ScheduledTransactionSummary[];
+ scheduled_transactions: ScheduledTransaction[];
scheduled_subtransactions: ScheduledSubtransaction[];
};
@@ -155,7 +155,7 @@ export type Category = {
export type GoalType = 'TB' | 'TBD' | 'MF' | 'NEED' | 'DEBT';
// Source: https://api.ynab.com/papi/open_api_spec.yaml#/components/schemas/TransactionSummary
-export type TransactionSummary = {
+export type Transaction = {
id: string;
date: string;
amount: number;
@@ -225,7 +225,7 @@ export type Subtransaction = {
};
// Source: https://api.ynab.com/papi/open_api_spec.yaml#/components/schemas/ScheduledTransactionSummary
-export type ScheduledTransactionSummary = {
+export type ScheduledTransaction = {
id: string;
date_first: string;
date_next: string;
diff --git a/packages/loot-core/src/server/importers/ynab5.ts b/packages/loot-core/src/server/importers/ynab5.ts
index 6242c32d20..12cb8d60f2 100644
--- a/packages/loot-core/src/server/importers/ynab5.ts
+++ b/packages/loot-core/src/server/importers/ynab5.ts
@@ -18,15 +18,53 @@ import {
} from '../../types/models';
import { ruleModel } from '../transactions/transaction-rules';
-import type {
- Budget,
- Payee,
- ScheduledSubtransaction,
- ScheduledTransactionSummary,
- Subtransaction,
- TransactionSummary,
+import {
+ type Budget,
+ type Payee,
+ type ScheduledSubtransaction,
+ type ScheduledTransaction,
+ type Subtransaction,
+ type Transaction,
} from './ynab5-types';
+type FlaggedTransaction = Pick<
+ Transaction | ScheduledTransaction,
+ 'flag_name' | 'flag_color' | 'deleted'
+>;
+
+const flagColorMap: Record<string, string | null> = {
+ red: '#FF6666',
+ orange: '#F57C00',
+ yellow: '#FBC02D',
+ green: '#689F38',
+ blue: '#1976D2',
+ purple: '#512DA8',
+ null: null,
+ '': null,
+};
+
+function equalsIgnoreCase(stringa: string, stringb: string): boolean {
+ return (
+ stringa.localeCompare(stringb, undefined, {
+ sensitivity: 'base',
+ }) === 0
+ );
+}
+
+function findByNameIgnoreCase<T extends { name: string }>(
+ categories: T[],
+ name: string,
+) {
+ return categories.find(cat => equalsIgnoreCase(cat.name, name));
+}
+
+function findIdByName<T extends { id: string; name: string }>(
+ categories: Array<T>,
+ name: string,
+) {
+ return findByNameIgnoreCase<T>(categories, name)?.id;
+}
+
function amountFromYnab(amount: number) {
// YNAB multiplies amount by 1000 and Actual by 100
// so, this function divides by 10
@@ -127,7 +165,7 @@ function mapYnabFrequency(
}
function getScheduleDateValue(
- scheduled: ScheduledTransactionSummary,
+ scheduled: ScheduledTransaction,
): RecurConfig | string {
const dateFirst = scheduled.date_first;
const frequency = scheduled.frequency;
@@ -148,6 +186,84 @@ function getScheduleDateValue(
};
}
+function getFlaggedTransactions(data: Budget): FlaggedTransaction[] {
+ return [...data.transactions, ...data.scheduled_transactions];
+}
+
+function getFlagTag(
+ transaction: FlaggedTransaction,
+ flagNameConflicts: Set<string>,
+): string {
+ const tagName = transaction.flag_name?.trim() ?? '';
+ const colorKey = transaction.flag_color?.trim() ?? '';
+
+ if (tagName.length === 0) {
+ return colorKey.length > 0 ? `#${colorKey}` : '';
+ }
+
+ if (flagNameConflicts.has(tagName)) {
+ return `#${tagName}-${colorKey}`;
+ }
+
+ return `#${tagName}`;
+}
+
+function getFlagNameConflicts(data: Budget): Set<string> {
+ const colorsByName = new Map<string, Set<string>>();
+ const flaggedTransactions = getFlaggedTransactions(data);
+
+ for (const transaction of flaggedTransactions) {
+ if (transaction.deleted) {
+ continue;
+ }
+
+ const tagName = transaction.flag_name?.trim() ?? '';
+ const colorKey = transaction.flag_color?.trim() ?? '';
+ if (tagName.length === 0 || !flagColorMap[colorKey]) {
+ continue;
+ }
+
+ let colors = colorsByName.get(tagName);
+ if (!colors) {
+ colors = new Set();
+ colorsByName.set(tagName, colors);
+ }
+ colors.add(colorKey);
+ }
+
+ const conflicts = new Set<string>();
+ colorsByName.forEach((colors, name) => {
+ if (colors.size > 1) {
+ conflicts.add(name);
+ }
+ });
+
+ return conflicts;
+}
+
+function buildTransactionNotes(
+ transaction: Transaction | ScheduledTransaction,
+ flagNameConflicts: Set<string>,
+): string | null {
+ const normalizedMemo = transaction.memo?.trim() ?? '';
+ const tagText = getFlagTag(transaction, flagNameConflicts);
+ const notes = `${normalizedMemo} ${tagText}`.trim();
+ return notes.length > 0 ? notes : null;
+}
+
+function buildRuleUpdate(
+ rule: RuleEntity,
+ actions: RuleEntity['actions'],
+): RuleEntity {
+ return {
+ id: rule.id,
+ stage: rule.stage ?? null,
+ conditionsOp: rule.conditionsOp ?? 'and',
+ conditions: rule.conditions,
+ actions,
+ };
+}
+
function importAccounts(data: Budget, entityIdMap: Map<string, string>) {
return Promise.all(
data.accounts.map(async account => {
@@ -309,294 +425,112 @@ function importPayees(data: Budget, entityIdMap: Map<string, string>) {
);
}
-async function importScheduledTransactions(
+async function importFlagsAsTags(
data: Budget,
- entityIdMap: Map<string, string>,
-) {
- const scheduledTransactions = data.scheduled_transactions;
- const scheduledSubtransactionsGrouped = groupBy(
- data.scheduled_subtransactions,
- 'scheduled_transaction_id',
- );
- if (scheduledTransactions.length === 0) {
- return;
- }
-
- const payees = await actual.getPayees();
- const payeesByTransferAcct = payees
- .filter(payee => payee?.transfer_acct)
- .map(payee => [payee.transfer_acct, payee] as [string, Payee]);
- const payeeTransferAcctHashMap = new Map<string, Payee>(payeesByTransferAcct);
- const scheduleCategoryMap = new Map<string, string>();
- const scheduleSplitsMap = new Map<string, ScheduledSubtransaction[]>();
- const schedulePayeeMap = new Map<string, string>();
-
- async function createScheduleWithUniqueName(params: {
- name: string;
- posts_transaction: boolean;
- payee: string;
- account: string;
- amount: number;
- amountOp: 'is';
- date: RecurConfig | string;
- }) {
- const baseName = params.name;
- const MAX_RETRY = 50;
- let count = 1;
-
- while (true) {
- try {
- return await actual.createSchedule({ ...params, name: params.name });
- } catch (e) {
- if (count >= MAX_RETRY) {
- throw Error(e.message);
- }
- params.name = `${baseName} (${count})`;
- count += 1;
- }
- }
- }
+ flagNameConflicts: Set<string>,
+): Promise<void> {
+ const tagsToCreate = new Map<string, string | null>();
+ const flaggedTransactions = getFlaggedTransactions(data);
- async function getRuleForSchedule(
- scheduleId: string,
- ): Promise<RuleEntity | null> {
- const { data: ruleId } = (await actual.aqlQuery(
- q('schedules').filter({ id: scheduleId }).calculate('rule'),
- )) as { data: string | null };
- if (!ruleId) {
- return null;
- }
-
- const { data: ruleData } = (await actual.aqlQuery(
- q('rules').filter({ id: ruleId }).select('*'),
- )) as { data: Array<Record<string, unknown>> };
- const ruleRow = ruleData?.[0];
- if (!ruleRow) {
- return null;
+ for (const transaction of flaggedTransactions) {
+ if (transaction.deleted) {
+ continue;
}
- return ruleModel.toJS(ruleRow);
- }
+ const tagName = transaction.flag_name?.trim() ?? '';
+ const colorKey = transaction.flag_color?.trim() ?? '';
+ const tagColor = flagColorMap[colorKey] ?? null;
- for (const scheduled of scheduledTransactions) {
- if (scheduled.deleted) {
+ if (!tagColor) {
continue;
}
- const mappedAccountId = entityIdMap.get(scheduled.account_id);
- if (!mappedAccountId) {
+ if (tagName.length === 0) {
+ if (!tagsToCreate.has(colorKey)) {
+ tagsToCreate.set(colorKey, tagColor);
+ }
continue;
}
- const scheduleDate = getScheduleDateValue(scheduled);
-
- let mappedPayeeId: string | undefined;
- if (scheduled.transfer_account_id) {
- const mappedTransferAccountId = entityIdMap.get(
- scheduled.transfer_account_id,
- );
- mappedPayeeId = mappedTransferAccountId
- ? payeeTransferAcctHashMap.get(mappedTransferAccountId)?.id
- : undefined;
- } else if (scheduled.payee_id) {
- mappedPayeeId = entityIdMap.get(scheduled.payee_id);
- }
+ const mappedName = flagNameConflicts.has(tagName)
+ ? `${tagName}-${colorKey}`
+ : tagName;
- if (!mappedPayeeId) {
- continue;
+ if (!tagsToCreate.has(mappedName)) {
+ tagsToCreate.set(mappedName, tagColor);
}
+ }
- const scheduleId = await createScheduleWithUniqueName({
- name: scheduled.memo,
- posts_transaction: false,
- payee: mappedPayeeId,
- account: mappedAccountId,
- amount: amountFromYnab(scheduled.amount),
- amountOp: 'is',
- date: scheduleDate,
- });
- schedulePayeeMap.set(scheduleId, mappedPayeeId);
+ if (tagsToCreate.size === 0) {
+ return;
+ }
- const scheduleNotes = buildTransactionNotes(scheduled);
- if (scheduleNotes) {
- const rule = await getRuleForSchedule(scheduleId);
- if (rule) {
- const actions = rule.actions ? [...rule.actions] : [];
- actions.push({
- op: 'set',
- field: 'notes',
- value: scheduleNotes,
- });
+ await Promise.all(
+ [...tagsToCreate.entries()].map(async ([tag, color]) => {
+ await send('tags-create', {
+ tag,
+ color,
+ description: 'Imported from YNAB',
+ });
+ }),
+ );
+}
- await actual.updateRule(buildRuleUpdate(rule, actions));
- }
- }
+async function importTransactions(
+ data: Budget,
+ entityIdMap: Map<string, string>,
+ flagNameConflicts: Set<string>,
+) {
+ const payees = await actual.getPayees();
+ const categories = await actual.getCategories();
+ const incomeCatId = findIdByName(categories, 'Income');
+ const startingBalanceCatId = findIdByName(categories, 'Starting Balances'); //better way to do it?
- const scheduledSubtransactions =
- scheduledSubtransactionsGrouped
- .get(scheduled.id)
- ?.filter(subtransaction => !subtransaction.deleted) || [];
+ const startingPayeeYNAB = findIdByName(data.payees, 'Starting Balance');
- if (scheduledSubtransactions.length > 0) {
- scheduleSplitsMap.set(scheduleId, scheduledSubtransactions);
- } else if (!scheduled.transfer_account_id && scheduled.category_id) {
- const mappedCategoryId = entityIdMap.get(scheduled.category_id);
- if (mappedCategoryId) {
- scheduleCategoryMap.set(scheduleId, mappedCategoryId);
- }
- }
- }
+ const transactionsGrouped = groupBy(data.transactions, 'account_id');
+ const subtransactionsGrouped = groupBy(
+ data.subtransactions,
+ 'transaction_id',
+ );
- if (scheduleCategoryMap.size > 0 || scheduleSplitsMap.size > 0) {
- for (const [scheduleId, categoryId] of scheduleCategoryMap.entries()) {
- const rule = await getRuleForSchedule(scheduleId);
- if (!rule) {
- continue;
- }
+ const payeesByTransferAcct = payees
+ .filter(payee => payee?.transfer_acct)
+ .map(payee => [payee.transfer_acct, payee] as [string, Payee]);
+ const payeeTransferAcctHashMap = new Map<string, Payee>(payeesByTransferAcct);
+ const orphanTransferMap = new Map<string, Transaction[]>();
+ const orphanSubtransfer = [] as Subtransaction[];
+ const orphanSubtransferTrxId = [] as string[];
+ const orphanSubtransferAcctIdByTrxIdMap = new Map<string, string>();
+ const orphanSubtransferDateByTrxIdMap = new Map<string, string>();
- const actions = rule.actions ? [...rule.actions] : [];
- actions.push({
- op: 'set',
- field: 'category',
- value: categoryId,
- });
+ // Go ahead and generate ids for all of the transactions so we can
+ // reliably resolve transfers
+ // Also identify orphan transfer transactions and subtransactions.
+ for (const transaction of data.subtransactions) {
+ entityIdMap.set(transaction.id, uuidv4());
- await actual.updateRule(buildRuleUpdate(rule, actions));
+ if (transaction.transfer_account_id) {
+ orphanSubtransfer.push(transaction);
+ orphanSubtransferTrxId.push(transaction.transaction_id);
}
+ }
- for (const [scheduleId, subtransactions] of scheduleSplitsMap.entries()) {
- const rule = await getRuleForSchedule(scheduleId);
- if (!rule) {
- continue;
+ for (const transaction of data.transactions) {
+ entityIdMap.set(transaction.id, uuidv4());
+
+ if (
+ transaction.transfer_account_id &&
+ !transaction.transfer_transaction_id
+ ) {
+ const key =
+ transaction.account_id + '#' + transaction.transfer_account_id;
+ if (!orphanTransferMap.has(key)) {
+ orphanTransferMap.set(key, [transaction]);
+ } else {
+ orphanTransferMap.get(key).push(transaction);
}
-
- const actions = rule.actions ? [...rule.actions] : [];
- const parentPayeeId = schedulePayeeMap.get(scheduleId);
-
- subtransactions.forEach((subtransaction, index) => {
- const splitIndex = index + 1;
-
- actions.push({
- op: 'set-split-amount',
- value: amountFromYnab(subtransaction.amount),
- options: { splitIndex, method: 'fixed-amount' },
- });
-
- if (subtransaction.memo) {
- actions.push({
- op: 'set',
- field: 'notes',
- value: subtransaction.memo,
- options: { splitIndex },
- });
- }
-
- if (subtransaction.transfer_account_id) {
- const mappedTransferAccountId = entityIdMap.get(
- subtransaction.transfer_account_id,
- );
- const transferPayeeId = mappedTransferAccountId
- ? payeeTransferAcctHashMap.get(mappedTransferAccountId)?.id
- : undefined;
- if (transferPayeeId) {
- actions.push({
- op: 'set',
- field: 'payee',
- value: transferPayeeId,
- options: { splitIndex },
- });
- }
- } else if (subtransaction.payee_id) {
- const mappedPayeeId = entityIdMap.get(subtransaction.payee_id);
- if (mappedPayeeId) {
- actions.push({
- op: 'set',
- field: 'payee',
- value: mappedPayeeId,
- options: { splitIndex },
- });
- }
- } else if (parentPayeeId) {
- actions.push({
- op: 'set',
- field: 'payee',
- value: parentPayeeId,
- options: { splitIndex },
- });
- }
-
- if (!subtransaction.transfer_account_id && subtransaction.category_id) {
- const mappedCategoryId = entityIdMap.get(subtransaction.category_id);
- if (mappedCategoryId) {
- actions.push({
- op: 'set',
- field: 'category',
- value: mappedCategoryId,
- options: { splitIndex },
- });
- }
- }
- });
-
- await actual.updateRule(buildRuleUpdate(rule, actions));
- }
- }
-}
-
-async function importTransactions(
- data: Budget,
- entityIdMap: Map<string, string>,
-) {
- const payees = await actual.getPayees();
- const categories = await actual.getCategories();
- const incomeCatId = findIdByName(categories, 'Income');
- const startingBalanceCatId = findIdByName(categories, 'Starting Balances'); //better way to do it?
-
- const startingPayeeYNAB = findIdByName(data.payees, 'Starting Balance');
-
- const transactionsGrouped = groupBy(data.transactions, 'account_id');
- const subtransactionsGrouped = groupBy(
- data.subtransactions,
- 'transaction_id',
- );
-
- const payeesByTransferAcct = payees
- .filter(payee => payee?.transfer_acct)
- .map(payee => [payee.transfer_acct, payee] as [string, Payee]);
- const payeeTransferAcctHashMap = new Map<string, Payee>(payeesByTransferAcct);
- const orphanTransferMap = new Map<string, TransactionSummary[]>();
- const orphanSubtransfer = [] as Subtransaction[];
- const orphanSubtransferTrxId = [] as string[];
- const orphanSubtransferAcctIdByTrxIdMap = new Map<string, string>();
- const orphanSubtransferDateByTrxIdMap = new Map<string, string>();
-
- // Go ahead and generate ids for all of the transactions so we can
- // reliably resolve transfers
- // Also identify orphan transfer transactions and subtransactions.
- for (const transaction of data.subtransactions) {
- entityIdMap.set(transaction.id, uuidv4());
-
- if (transaction.transfer_account_id) {
- orphanSubtransfer.push(transaction);
- orphanSubtransferTrxId.push(transaction.transaction_id);
- }
- }
-
- for (const transaction of data.transactions) {
- entityIdMap.set(transaction.id, uuidv4());
-
- if (
- transaction.transfer_account_id &&
- !transaction.transfer_transaction_id
- ) {
- const key =
- transaction.account_id + '#' + transaction.transfer_account_id;
- if (!orphanTransferMap.has(key)) {
- orphanTransferMap.set(key, [transaction]);
- } else {
- orphanTransferMap.get(key).push(transaction);
- }
- }
+ }
if (orphanSubtransferTrxId.includes(transaction.id)) {
orphanSubtransferAcctIdByTrxIdMap.set(
@@ -630,11 +564,11 @@ async function importTransactions(
// corresponding tranfer subtransaction in two aligned list. Hopefully
// for every list index in the transactions list, the related subtransaction
// will be at the same index.
- const orphanTransferComparator = (
- a: TransactionSummary | Subtransaction,
- b: TransactionSummary | Subtransaction,
- ) => {
- // a and b can be a TransactionSummary (having a date attribute) or a
+ function orphanTransferComparator(
+ a: Transaction | Subtransaction,
+ b: Transaction | Subtransaction,
+ ) {
+ // a and b can be a Transaction (having a date attribute) or a
// Subtransaction (missing that date attribute)
const date_a =
@@ -659,7 +593,7 @@ async function importTransactions(
if (a.memo > b.memo) return 1;
if (a.memo < b.memo) return -1;
return 0;
- };
+ }
const orphanTrxIdSubtrxIdMap = new Map<string, string>();
orphanTransferMap.forEach((transactions, key) => {
@@ -738,7 +672,7 @@ async function importTransactions(
category: entityIdMap.get(transaction.category_id) || null,
cleared: ['cleared', 'reconciled'].includes(transaction.cleared),
reconciled: transaction.cleared === 'reconciled',
- notes: buildTransactionNotes(transaction),
+ notes: buildTransactionNotes(transaction, flagNameConflicts),
imported_id: transaction.import_id || null,
transfer_id:
entityIdMap.get(transaction.transfer_transaction_id) ||
@@ -764,7 +698,7 @@ async function importTransactions(
// Handle transactions and subtransactions payee
function transactionPayeeUpdate(
- trx: TransactionSummary | Subtransaction,
+ trx: Transaction | Subtransaction,
newTrx,
fallbackPayeeId?: string | null,
) {
@@ -818,6 +752,241 @@ async function importTransactions(
);
}
+async function importScheduledTransactions(
+ data: Budget,
+ entityIdMap: Map<string, string>,
+ flagNameConflicts: Set<string>,
+) {
+ const scheduledTransactions = data.scheduled_transactions;
+ const scheduledSubtransactionsGrouped = groupBy(
+ data.scheduled_subtransactions,
+ 'scheduled_transaction_id',
+ );
+ if (scheduledTransactions.length === 0) {
+ return;
+ }
+
+ const payees = await actual.getPayees();
+ const payeesByTransferAcct = payees
+ .filter(payee => payee?.transfer_acct)
+ .map(payee => [payee.transfer_acct, payee] as [string, Payee]);
+ const payeeTransferAcctHashMap = new Map<string, Payee>(payeesByTransferAcct);
+ const scheduleCategoryMap = new Map<string, string>();
+ const scheduleSplitsMap = new Map<string, ScheduledSubtransaction[]>();
+ const schedulePayeeMap = new Map<string, string>();
+
+ async function createScheduleWithUniqueName(params: {
+ name: string;
+ posts_transaction: boolean;
+ payee: string;
+ account: string;
+ amount: number;
+ amountOp: 'is';
+ date: RecurConfig | string;
+ }) {
+ const baseName = params.name;
+ const MAX_RETRY = 50;
+ let count = 1;
+
+ while (true) {
+ try {
+ return await actual.createSchedule({ ...params, name: params.name });
+ } catch (e) {
+ if (count >= MAX_RETRY) {
+ throw Error(e.message);
+ }
+ params.name = `${baseName} (${count})`;
+ count += 1;
+ }
+ }
+ }
+
+ async function getRuleForSchedule(
+ scheduleId: string,
+ ): Promise<RuleEntity | null> {
+ const { data: ruleId } = (await actual.aqlQuery(
+ q('schedules').filter({ id: scheduleId }).calculate('rule'),
+ )) as { data: string | null };
+ if (!ruleId) {
+ return null;
+ }
+
+ const { data: ruleData } = (await actual.aqlQuery(
+ q('rules').filter({ id: ruleId }).select('*'),
+ )) as { data: Array<Record<string, unknown>> };
+ const ruleRow = ruleData?.[0];
+ if (!ruleRow) {
+ return null;
+ }
+
+ return ruleModel.toJS(ruleRow);
+ }
+
+ for (const scheduled of scheduledTransactions) {
+ if (scheduled.deleted) {
+ continue;
+ }
+
+ const mappedAccountId = entityIdMap.get(scheduled.account_id);
+ if (!mappedAccountId) {
+ continue;
+ }
+
+ const scheduleDate = getScheduleDateValue(scheduled);
+
+ let mappedPayeeId: string | undefined;
+ if (scheduled.transfer_account_id) {
+ const mappedTransferAccountId = entityIdMap.get(
+ scheduled.transfer_account_id,
+ );
+ mappedPayeeId = mappedTransferAccountId
+ ? payeeTransferAcctHashMap.get(mappedTransferAccountId)?.id
+ : undefined;
+ } else if (scheduled.payee_id) {
+ mappedPayeeId = entityIdMap.get(scheduled.payee_id);
+ }
+
+ if (!mappedPayeeId) {
+ continue;
+ }
+
+ const scheduleId = await createScheduleWithUniqueName({
+ name: scheduled.memo,
+ posts_transaction: false,
+ payee: mappedPayeeId,
+ account: mappedAccountId,
+ amount: amountFromYnab(scheduled.amount),
+ amountOp: 'is',
+ date: scheduleDate,
+ });
+ schedulePayeeMap.set(scheduleId, mappedPayeeId);
+
+ const scheduleNotes = buildTransactionNotes(scheduled, flagNameConflicts);
+ if (scheduleNotes) {
+ const rule = await getRuleForSchedule(scheduleId);
+ if (rule) {
+ const actions = rule.actions ? [...rule.actions] : [];
+ actions.push({
+ op: 'set',
+ field: 'notes',
+ value: scheduleNotes,
+ });
+
+ await actual.updateRule(buildRuleUpdate(rule, actions));
+ }
+ }
+
+ const scheduledSubtransactions =
+ scheduledSubtransactionsGrouped
+ .get(scheduled.id)
+ ?.filter(subtransaction => !subtransaction.deleted) || [];
+
+ if (scheduledSubtransactions.length > 0) {
+ scheduleSplitsMap.set(scheduleId, scheduledSubtransactions);
+ } else if (!scheduled.transfer_account_id && scheduled.category_id) {
+ const mappedCategoryId = entityIdMap.get(scheduled.category_id);
+ if (mappedCategoryId) {
+ scheduleCategoryMap.set(scheduleId, mappedCategoryId);
+ }
+ }
+ }
+
+ if (scheduleCategoryMap.size > 0 || scheduleSplitsMap.size > 0) {
+ for (const [scheduleId, categoryId] of scheduleCategoryMap.entries()) {
+ const rule = await getRuleForSchedule(scheduleId);
+ if (!rule) {
+ continue;
+ }
+
+ const actions = rule.actions ? [...rule.actions] : [];
+ actions.push({
+ op: 'set',
+ field: 'category',
+ value: categoryId,
+ });
+
+ await actual.updateRule(buildRuleUpdate(rule, actions));
+ }
+
+ for (const [scheduleId, subtransactions] of scheduleSplitsMap.entries()) {
+ const rule = await getRuleForSchedule(scheduleId);
+ if (!rule) {
+ continue;
+ }
+
+ const actions = rule.actions ? [...rule.actions] : [];
+ const parentPayeeId = schedulePayeeMap.get(scheduleId);
+
+ subtransactions.forEach((subtransaction, index) => {
+ const splitIndex = index + 1;
+
+ actions.push({
+ op: 'set-split-amount',
+ value: amountFromYnab(subtransaction.amount),
+ options: { splitIndex, method: 'fixed-amount' },
+ });
+
+ if (subtransaction.memo) {
+ actions.push({
+ op: 'set',
+ field: 'notes',
+ value: subtransaction.memo,
+ options: { splitIndex },
+ });
+ }
+
+ if (subtransaction.transfer_account_id) {
+ const mappedTransferAccountId = entityIdMap.get(
+ subtransaction.transfer_account_id,
+ );
+ const transferPayeeId = mappedTransferAccountId
+ ? payeeTransferAcctHashMap.get(mappedTransferAccountId)?.id
+ : undefined;
+ if (transferPayeeId) {
+ actions.push({
+ op: 'set',
+ field: 'payee',
+ value: transferPayeeId,
+ options: { splitIndex },
+ });
+ }
+ } else if (subtransaction.payee_id) {
+ const mappedPayeeId = entityIdMap.get(subtransaction.payee_id);
+ if (mappedPayeeId) {
+ actions.push({
+ op: 'set',
+ field: 'payee',
+ value: mappedPayeeId,
+ options: { splitIndex },
+ });
+ }
+ } else if (parentPayeeId) {
+ actions.push({
+ op: 'set',
+ field: 'payee',
+ value: parentPayeeId,
+ options: { splitIndex },
+ });
+ }
+
+ if (!subtransaction.transfer_account_id && subtransaction.category_id) {
+ const mappedCategoryId = entityIdMap.get(subtransaction.category_id);
+ if (mappedCategoryId) {
+ actions.push({
+ op: 'set',
+ field: 'category',
+ value: mappedCategoryId,
+ options: { splitIndex },
+ });
+ }
+ }
+ });
+
+ await actual.updateRule(buildRuleUpdate(rule, actions));
+ }
+ }
+}
+
async function importBudgets(data: Budget, entityIdMap: Map<string, string>) {
// There should be info in the docs to deal with
// no credit card category and how YNAB and Actual
@@ -862,32 +1031,6 @@ async function importBudgets(data: Budget, entityIdMap: Map<string, string>) {
});
}
-// Utils
-
-export async function doImport(data: Budget) {
- const entityIdMap = new Map<string, string>();
-
- logger.log('Importing Accounts...');
- await importAccounts(data, entityIdMap);
-
- logger.log('Importing Categories...');
- await importCategories(data, entityIdMap);
-
- logger.log('Importing Payees...');
- await importPayees(data, entityIdMap);
-
- logger.log('Importing Transactions...');
- await importTransactions(data, entityIdMap);
-
- logger.log('Importing Scheduled Transactions...');
- await importScheduledTransactions(data, entityIdMap);
-
- logger.log('Importing Budgets...');
- await importBudgets(data, entityIdMap);
-
- logger.log('Setting up...');
-}
-
export function parseFile(buffer: Buffer): Budget {
let data = JSON.parse(buffer.toString());
if (data.data) {
@@ -904,53 +1047,30 @@ export function getBudgetName(_filepath: string, data: Budget) {
return data.budget_name || data.name;
}
-function equalsIgnoreCase(stringa: string, stringb: string): boolean {
- return (
- stringa.localeCompare(stringb, undefined, {
- sensitivity: 'base',
- }) === 0
- );
-}
+export async function doImport(data: Budget) {
+ const entityIdMap = new Map<string, string>();
+ const flagNameConflicts = getFlagNameConflicts(data);
-function findByNameIgnoreCase<T extends { name: string }>(
- categories: T[],
- name: string,
-) {
- return categories.find(cat => equalsIgnoreCase(cat.name, name));
-}
+ logger.log('Importing Accounts...');
+ await importAccounts(data, entityIdMap);
-function findIdByName<T extends { id: string; name: string }>(
- categories: Array<T>,
- name: string,
-) {
- return findByNameIgnoreCase<T>(categories, name)?.id;
-}
+ logger.log('Importing Categories...');
+ await importCategories(data, entityIdMap);
-type FlaggedMemoTransaction = {
- memo?: string | null;
- flag_name?: string | null;
- flag_color?: string | null;
-};
+ logger.log('Importing Payees...');
+ await importPayees(data, entityIdMap);
-function buildTransactionNotes(transaction: FlaggedMemoTransaction) {
- const normalizedMemo = transaction.memo?.trim() ?? '';
- const normalizedFlag =
- transaction.flag_name?.trim() ?? transaction.flag_color?.trim() ?? '';
- const notes = `${normalizedMemo} ${
- normalizedFlag ? `#${normalizedFlag}` : ''
- }`.trim();
- return notes.length > 0 ? notes : null;
-}
+ logger.log('Importing Tags...');
+ await importFlagsAsTags(data, flagNameConflicts);
-function buildRuleUpdate(
- rule: RuleEntity,
- actions: RuleEntity['actions'],
-): RuleEntity {
- return {
- id: rule.id,
- stage: rule.stage ?? null,
- conditionsOp: rule.conditionsOp ?? 'and',
- conditions: rule.conditions,
- actions,
- };
+ logger.log('Importing Transactions...');
+ await importTransactions(data, entityIdMap, flagNameConflicts);
+
+ logger.log('Importing Scheduled Transactions...');
+ await importScheduledTransactions(data, entityIdMap, flagNameConflicts);
+
+ logger.log('Importing Budgets...');
+ await importBudgets(data, entityIdMap);
+
+ logger.log('Setting up...');
}
diff --git a/upcoming-release-notes/6866.md b/upcoming-release-notes/6866.md
new file mode 100644
index 0000000000..3eb5e96b99
--- /dev/null
+++ b/upcoming-release-notes/6866.md
@@ -0,0 +1,6 @@
+---
+category: Enhancements
+authors: [StephenBrown2]
+---
+
+Apply tag colors to match YNAB flags when importing from nYNAB