Skip to content

Commit

Permalink
Add ability to provide defaults for and skip updating cleared status …
Browse files Browse the repository at this point in the history
…in API (#4129)

* Cleared import changes
- Add ability to provide default for cleared field
- Add ability to skip updating cleared field

* Add release notes

* Make linter happy

* Fix optional param

* Use nullish coalescing operator
  • Loading branch information
NikxDa authored Jan 15, 2025
1 parent b92fa70 commit aa529a2
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 3 deletions.
13 changes: 12 additions & 1 deletion packages/api/methods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,21 @@ export function addTransactions(
});
}

export function importTransactions(accountId, transactions) {
export interface ImportTransactionsOpts {
defaultCleared?: boolean;
}

export function importTransactions(
accountId,
transactions,
opts: ImportTransactionsOpts = {
defaultCleared: true,
},
) {
return send('api/transactions-import', {
accountId,
transactions,
opts,
});
}

Expand Down
5 changes: 3 additions & 2 deletions packages/loot-core/src/server/accounts/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,7 @@ export async function reconcileTransactions(
isBankSyncAccount = false,
strictIdChecking = true,
isPreview = false,
defaultCleared = true,
) {
console.log('Performing transaction reconciliation');

Expand Down Expand Up @@ -436,7 +437,7 @@ export async function reconcileTransactions(
category: existing.category || trans.category || null,
imported_payee: trans.imported_payee || null,
notes: existing.notes || trans.notes || null,
cleared: trans.cleared != null ? trans.cleared : true,
cleared: trans.cleared ?? existing.cleared,
};

if (hasFieldsChanged(existing, updates, Object.keys(updates))) {
Expand Down Expand Up @@ -468,7 +469,7 @@ export async function reconcileTransactions(
...newTrans,
id: uuidv4(),
category: trans.category || null,
cleared: trans.cleared != null ? trans.cleared : true,
cleared: trans.cleared ?? defaultCleared,
};

if (subtransactions && subtransactions.length > 0) {
Expand Down
2 changes: 2 additions & 0 deletions packages/loot-core/src/server/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1242,6 +1242,7 @@ handlers['transactions-import'] = mutator(function ({
accountId,
transactions,
isPreview,
opts,
}) {
return withUndo(async () => {
if (typeof accountId !== 'string') {
Expand All @@ -1255,6 +1256,7 @@ handlers['transactions-import'] = mutator(function ({
false,
true,
isPreview,
opts?.defaultCleared,
);
} catch (err) {
if (err instanceof TransactionError) {
Expand Down
3 changes: 3 additions & 0 deletions packages/loot-core/src/types/api-handlers.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { ImportTransactionsOpts } from '@actual-app/api';

import { type batchUpdateTransactions } from '../server/accounts/transactions';
import type {
APIAccountEntity,
Expand Down Expand Up @@ -80,6 +82,7 @@ export interface ApiHandlers {
accountId;
transactions;
isPreview?;
opts?: ImportTransactionsOpts;
}) => Promise<{
errors?: { message: string }[];
added;
Expand Down
3 changes: 3 additions & 0 deletions packages/loot-core/src/types/server-handlers.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { ImportTransactionsOpts } from '@actual-app/api';

import { ParseFileResult } from '../server/accounts/parse-file';
import { batchUpdateTransactions } from '../server/accounts/transactions';
import { Backup } from '../server/backups';
Expand Down Expand Up @@ -239,6 +241,7 @@ export interface ServerHandlers {
accountId;
transactions;
isPreview;
opts?: ImportTransactionsOpts;
}) => Promise<{
errors?: { message: string }[];
added;
Expand Down
6 changes: 6 additions & 0 deletions upcoming-release-notes/4129.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
category: Enhancements
authors: [NikxDa]
---

Add ability to provide default cleared status in the API and skip updating the cleared status on subsequent imports.

0 comments on commit aa529a2

Please sign in to comment.