Skip to content

A local-first personal finance app

Create category rules for ynab imports (#1944)

DJ Mountneyb4fd6e86ed53
diff --git a/packages/api/methods.js b/packages/api/methods.js
index dad30f1ef5..7d6ef8a29f 100644
--- a/packages/api/methods.js
+++ b/packages/api/methods.js
@@ -58,8 +58,17 @@ export function setBudgetCarryover(month, categoryId, flag) {
return send('api/budget-set-carryover', { month, categoryId, flag });
}
-export function addTransactions(accountId, transactions) {
- return send('api/transactions-add', { accountId, transactions });
+export function addTransactions(
+ accountId,
+ transactions,
+ { learnCategories = false, runTransfers = false } = {},
+) {
+ return send('api/transactions-add', {
+ accountId,
+ transactions,
+ learnCategories,
+ runTransfers,
+ });
}
export function importTransactions(accountId, transactions) {
diff --git a/packages/loot-core/src/server/accounts/sync.ts b/packages/loot-core/src/server/accounts/sync.ts
index d602fee0ac..112e9182a3 100644
--- a/packages/loot-core/src/server/accounts/sync.ts
+++ b/packages/loot-core/src/server/accounts/sync.ts
@@ -705,7 +705,7 @@ export async function reconcileTransactions(acctId, transactions) {
export async function addTransactions(
acctId,
transactions,
- { runTransfers = true } = {},
+ { runTransfers = true, learnCategories = false } = {},
) {
const added = [];
@@ -737,8 +737,12 @@ export async function addTransactions(
await createNewPayees(payeesToCreate, added);
let newTransactions;
- if (runTransfers) {
- let res = await batchUpdateTransactions({ added });
+ if (runTransfers || learnCategories) {
+ let res = await batchUpdateTransactions({
+ added,
+ learnCategories,
+ runTransfers,
+ });
newTransactions = res.added.map(t => t.id);
} else {
await batchMessages(async () => {
diff --git a/packages/loot-core/src/server/accounts/transactions.ts b/packages/loot-core/src/server/accounts/transactions.ts
index ed1591290e..30f4ea94ae 100644
--- a/packages/loot-core/src/server/accounts/transactions.ts
+++ b/packages/loot-core/src/server/accounts/transactions.ts
@@ -40,6 +40,7 @@ export async function batchUpdateTransactions({
updated,
learnCategories = false,
detectOrphanPayees = true,
+ runTransfers = true,
}: {
added?: Array<{ id: string; payee: unknown; category: unknown }>;
deleted?: Array<{ id: string; payee: unknown }>;
@@ -51,6 +52,7 @@ export async function batchUpdateTransactions({
}>;
learnCategories?: boolean;
detectOrphanPayees?: boolean;
+ runTransfers?: boolean;
}) {
// Track the ids of each type of transaction change (see below for why)
let addedIds = [];
@@ -126,18 +128,21 @@ export async function batchUpdateTransactions({
// to the client so that can apply them. Note that added
// transactions just return the full transaction.
let resultAdded = allAdded;
- let resultUpdated: Awaited<ReturnType<typeof transfer.onUpdate>>[];
+ let resultUpdated = allUpdated;
+ let transfersUpdated: Awaited<ReturnType<typeof transfer.onUpdate>>[];
- await batchMessages(async () => {
- await Promise.all(allAdded.map(t => transfer.onInsert(t)));
+ if (runTransfers) {
+ await batchMessages(async () => {
+ await Promise.all(allAdded.map(t => transfer.onInsert(t)));
- // Return any updates from here
- resultUpdated = (
- await Promise.all(allUpdated.map(t => transfer.onUpdate(t)))
- ).filter(Boolean);
+ // Return any updates from here
+ transfersUpdated = (
+ await Promise.all(allUpdated.map(t => transfer.onUpdate(t)))
+ ).filter(Boolean);
- await Promise.all(allDeleted.map(t => transfer.onDelete(t)));
- });
+ await Promise.all(allDeleted.map(t => transfer.onDelete(t)));
+ });
+ }
if (learnCategories) {
// Analyze any updated categories and update rules to learn from
@@ -176,6 +181,6 @@ export async function batchUpdateTransactions({
return {
added: resultAdded,
- updated: resultUpdated,
+ updated: runTransfers ? transfersUpdated : resultUpdated,
};
}
diff --git a/packages/loot-core/src/server/api.ts b/packages/loot-core/src/server/api.ts
index 01190365df..eee5f28c14 100644
--- a/packages/loot-core/src/server/api.ts
+++ b/packages/loot-core/src/server/api.ts
@@ -393,9 +393,14 @@ handlers['api/transactions-import'] = withMutation(async function ({
handlers['api/transactions-add'] = withMutation(async function ({
accountId,
transactions,
+ runTransfers = false,
+ learnCategories = false,
}) {
checkFileOpen();
- await addTransactions(accountId, transactions, { runTransfers: false });
+ await addTransactions(accountId, transactions, {
+ runTransfers,
+ learnCategories,
+ });
return 'ok';
});
diff --git a/packages/loot-core/src/server/importers/ynab4.ts b/packages/loot-core/src/server/importers/ynab4.ts
index c127c6e408..9a6084c510 100644
--- a/packages/loot-core/src/server/importers/ynab4.ts
+++ b/packages/loot-core/src/server/importers/ynab4.ts
@@ -213,7 +213,9 @@ async function importTransactions(
})
.filter(x => x);
- await actual.addTransactions(entityIdMap.get(accountId), toImport);
+ await actual.addTransactions(entityIdMap.get(accountId), toImport, {
+ learnCategories: true,
+ });
}),
);
}
diff --git a/packages/loot-core/src/server/importers/ynab5.ts b/packages/loot-core/src/server/importers/ynab5.ts
index 8641d82cb2..6268ffba4a 100644
--- a/packages/loot-core/src/server/importers/ynab5.ts
+++ b/packages/loot-core/src/server/importers/ynab5.ts
@@ -234,7 +234,9 @@ async function importTransactions(
})
.filter(x => x);
- await actual.addTransactions(entityIdMap.get(accountId), toImport);
+ await actual.addTransactions(entityIdMap.get(accountId), toImport, {
+ learnCategories: true,
+ });
}),
);
}
diff --git a/upcoming-release-notes/1944.md b/upcoming-release-notes/1944.md
new file mode 100644
index 0000000000..06616941ed
--- /dev/null
+++ b/upcoming-release-notes/1944.md
@@ -0,0 +1,6 @@
+---
+category: Enhancements
+authors: [twk3]
+---
+
+Create category rules during ynab imports
\ No newline at end of file