Skip to content

A local-first personal finance app

Extract accounts related server handlers from `main.ts` to `server/accounts/app.ts` (#4227)

Joel Jeremy Marquez6e934e46a862
diff --git a/packages/desktop-client/src/components/accounts/AccountSyncCheck.tsx b/packages/desktop-client/src/components/accounts/AccountSyncCheck.tsx
index cf1143ccd2..4d3b07b3fc 100644
--- a/packages/desktop-client/src/components/accounts/AccountSyncCheck.tsx
+++ b/packages/desktop-client/src/components/accounts/AccountSyncCheck.tsx
@@ -96,7 +96,7 @@ export function AccountSyncCheck() {
setOpen(false);
if (acc.account_id) {
- authorizeBank(dispatch, { upgradingAccountId: acc.account_id });
+ authorizeBank(dispatch);
}
},
[dispatch],
diff --git a/packages/desktop-client/src/components/modals/CreateAccountModal.tsx b/packages/desktop-client/src/components/modals/CreateAccountModal.tsx
index 5b554f45bf..a753458409 100644
--- a/packages/desktop-client/src/components/modals/CreateAccountModal.tsx
+++ b/packages/desktop-client/src/components/modals/CreateAccountModal.tsx
@@ -54,9 +54,7 @@ export function CreateAccountModal({ upgradingAccountId }: CreateAccountProps) {
if (upgradingAccountId == null) {
authorizeBank(dispatch);
} else {
- authorizeBank(dispatch, {
- upgradingAccountId,
- });
+ authorizeBank(dispatch);
}
};
diff --git a/packages/desktop-client/src/gocardless.ts b/packages/desktop-client/src/gocardless.ts
index 8d868ebb92..6cbb6b905d 100644
--- a/packages/desktop-client/src/gocardless.ts
+++ b/packages/desktop-client/src/gocardless.ts
@@ -5,7 +5,6 @@ import { type GoCardlessToken } from 'loot-core/types/models';
function _authorize(
dispatch: AppDispatch,
- upgradingAccountId: string | undefined,
{
onSuccess,
onClose,
@@ -18,7 +17,6 @@ function _authorize(
pushModal('gocardless-external-msg', {
onMoveExternal: async ({ institutionId }) => {
const resp = await send('gocardless-create-web-token', {
- upgradingAccountId,
institutionId,
accessValidForDays: 90,
});
@@ -28,7 +26,6 @@ function _authorize(
window.Actual.openURLInBrowser(link);
return send('gocardless-poll-web-token', {
- upgradingAccountId,
requisitionId,
});
},
@@ -39,17 +36,13 @@ function _authorize(
);
}
-export async function authorizeBank(
- dispatch: AppDispatch,
- { upgradingAccountId }: { upgradingAccountId?: string } = {},
-) {
- _authorize(dispatch, upgradingAccountId, {
+export async function authorizeBank(dispatch: AppDispatch) {
+ _authorize(dispatch, {
onSuccess: async data => {
dispatch(
pushModal('select-linked-accounts', {
accounts: data.accounts,
requisitionId: data.id,
- upgradingAccountId,
syncSource: 'goCardless',
}),
);
diff --git a/packages/loot-core/src/client/accounts/accountsSlice.ts b/packages/loot-core/src/client/accounts/accountsSlice.ts
index f1ff845f4f..0f032115b1 100644
--- a/packages/loot-core/src/client/accounts/accountsSlice.ts
+++ b/packages/loot-core/src/client/accounts/accountsSlice.ts
@@ -1,7 +1,13 @@
import { createSlice, type PayloadAction } from '@reduxjs/toolkit';
import { send } from '../../platform/client/fetch';
-import { type AccountEntity, type TransactionEntity } from '../../types/models';
+import { type SyncResponseWithErrors } from '../../server/accounts/app';
+import {
+ type SyncServerGoCardlessAccount,
+ type AccountEntity,
+ type TransactionEntity,
+ type SyncServerSimpleFinAccount,
+} from '../../types/models';
import { addNotification } from '../actions';
import {
getAccounts,
@@ -80,9 +86,9 @@ export const unlinkAccount = createAppAsyncThunk(
type LinkAccountPayload = {
requisitionId: string;
- account: unknown;
- upgradingId?: AccountEntity['id'];
- offBudget?: boolean;
+ account: SyncServerGoCardlessAccount;
+ upgradingId?: AccountEntity['id'] | undefined;
+ offBudget?: boolean | undefined;
};
export const linkAccount = createAppAsyncThunk(
@@ -103,9 +109,9 @@ export const linkAccount = createAppAsyncThunk(
);
type LinkAccountSimpleFinPayload = {
- externalAccount: unknown;
- upgradingId?: AccountEntity['id'];
- offBudget?: boolean;
+ externalAccount: SyncServerSimpleFinAccount;
+ upgradingId?: AccountEntity['id'] | undefined;
+ offBudget?: boolean | undefined;
};
export const linkAccountSimpleFin = createAppAsyncThunk(
@@ -124,22 +130,9 @@ export const linkAccountSimpleFin = createAppAsyncThunk(
},
);
-type SyncResponse = {
- errors: Array<{
- type: string;
- category: string;
- code: string;
- message: string;
- internal?: string;
- }>;
- newTransactions: Array<TransactionEntity['id']>;
- matchedTransactions: Array<TransactionEntity['id']>;
- updatedAccounts: Array<AccountEntity['id']>;
-};
-
function handleSyncResponse(
accountId: AccountEntity['id'],
- res: SyncResponse,
+ res: SyncResponseWithErrors,
dispatch: AppDispatch,
resNewTransactions: Array<TransactionEntity['id']>,
resMatchedTransactions: Array<TransactionEntity['id']>,
@@ -153,7 +146,7 @@ function handleSyncResponse(
if (error) {
// We only want to mark the account as having problem if it
// was a real syncing error.
- if (error.type === 'SyncError') {
+ if ('type' in error && error.type === 'SyncError') {
dispatch(
markAccountFailed({
id: accountId,
@@ -168,7 +161,7 @@ function handleSyncResponse(
// Dispatch errors (if any)
errors.forEach(error => {
- if (error.type === 'SyncError') {
+ if ('type' in error && error.type === 'SyncError') {
dispatch(
addNotification({
type: 'error',
@@ -180,7 +173,7 @@ function handleSyncResponse(
addNotification({
type: 'error',
message: error.message,
- internal: error.internal,
+ internal: 'internal' in error ? error.internal : undefined,
}),
);
}
diff --git a/packages/loot-core/src/server/accounts/app.ts b/packages/loot-core/src/server/accounts/app.ts
new file mode 100644
index 0000000000..c9a8ca3c37
--- /dev/null
+++ b/packages/loot-core/src/server/accounts/app.ts
@@ -0,0 +1,1038 @@
+import { v4 as uuidv4 } from 'uuid';
+
+import { captureException } from '../../platform/exceptions';
+import * as asyncStorage from '../../platform/server/asyncStorage';
+import * as connection from '../../platform/server/connection';
+import { isNonProductionEnvironment } from '../../shared/environment';
+import { dayFromDate } from '../../shared/months';
+import * as monthUtils from '../../shared/months';
+import { amountToInteger } from '../../shared/util';
+import {
+ AccountEntity,
+ CategoryEntity,
+ SyncServerGoCardlessAccount,
+ PayeeEntity,
+ TransactionEntity,
+ SyncServerSimpleFinAccount,
+} from '../../types/models';
+import { BankEntity } from '../../types/models/bank';
+import { createApp } from '../app';
+import * as db from '../db';
+import {
+ APIError,
+ BankSyncError,
+ PostError,
+ TransactionError,
+} from '../errors';
+import { app as mainApp } from '../main-app';
+import { mutator } from '../mutators';
+import { get, post } from '../post';
+import { getServer } from '../server-config';
+import { batchMessages } from '../sync';
+import { undoable, withUndo } from '../undo';
+
+import * as link from './link';
+import { getStartingBalancePayee } from './payees';
+import * as bankSync from './sync';
+
+export type AccountHandlers = {
+ 'account-update': typeof updateAccount;
+ 'accounts-get': typeof getAccounts;
+ 'account-balance': typeof getAccountBalance;
+ 'account-properties': typeof getAccountProperties;
+ 'gocardless-accounts-link': typeof linkGoCardlessAccount;
+ 'simplefin-accounts-link': typeof linkSimpleFinAccount;
+ 'account-create': typeof createAccount;
+ 'account-close': typeof closeAccount;
+ 'account-reopen': typeof reopenAccount;
+ 'account-move': typeof moveAccount;
+ 'secret-set': typeof setSecret;
+ 'secret-check': typeof checkSecret;
+ 'gocardless-poll-web-token': typeof pollGoCardlessWebToken;
+ 'gocardless-poll-web-token-stop': typeof stopGoCardlessWebTokenPolling;
+ 'gocardless-status': typeof goCardlessStatus;
+ 'simplefin-status': typeof simpleFinStatus;
+ 'simplefin-accounts': typeof simpleFinAccounts;
+ 'gocardless-get-banks': typeof getGoCardlessBanks;
+ 'gocardless-create-web-token': typeof createGoCardlessWebToken;
+ 'accounts-bank-sync': typeof accountsBankSync;
+ 'simplefin-batch-sync': typeof simpleFinBatchSync;
+ 'transactions-import': typeof importTransactions;
+ 'account-unlink': typeof unlinkAccount;
+};
+
+async function updateAccount({ id, name }: Pick<AccountEntity, 'id' | 'name'>) {
+ await db.update('accounts', { id, name });
+ return {};
+}
+
+async function getAccounts() {
+ return db.getAccounts();
+}
+
+async function getAccountBalance({
+ id,
+ cutoff,
+}: {
+ id: string;
+ cutoff: string | Date;
+}) {
+ const { balance }: { balance: number } = await db.first(
+ 'SELECT sum(amount) as balance FROM transactions WHERE acct = ? AND isParent = 0 AND tombstone = 0 AND date <= ?',
+ [id, db.toDateRepr(dayFromDate(cutoff))],
+ );
+ return balance ? balance : 0;
+}
+
+async function getAccountProperties({ id }: { id: AccountEntity['id'] }) {
+ const { balance }: { balance: number } = await db.first(
+ 'SELECT sum(amount) as balance FROM transactions WHERE acct = ? AND isParent = 0 AND tombstone = 0',
+ [id],
+ );
+ const { count }: { count: number } = await db.first(
+ 'SELECT count(id) as count FROM transactions WHERE acct = ? AND tombstone = 0',
+ [id],
+ );
+
+ return { balance: balance || 0, numTransactions: count };
+}
+
+async function linkGoCardlessAccount({
+ requisitionId,
+ account,
+ upgradingId,
+ offBudget = false,
+}: {
+ requisitionId: string;
+ account: SyncServerGoCardlessAccount;
+ upgradingId?: AccountEntity['id'] | undefined;
+ offBudget?: boolean | undefined;
+}) {
+ let id;
+ const bank = await link.findOrCreateBank(account.institution, requisitionId);
+
+ if (upgradingId) {
+ const accRow: AccountEntity = await db.first(
+ 'SELECT * FROM accounts WHERE id = ?',
+ [upgradingId],
+ );
+ id = accRow.id;
+ await db.update('accounts', {
+ id,
+ account_id: account.account_id,
+ bank: bank.id,
+ account_sync_source: 'goCardless',
+ });
+ } else {
+ id = uuidv4();
+ await db.insertWithUUID('accounts', {
+ id,
+ account_id: account.account_id,
+ mask: account.mask,
+ name: account.name,
+ official_name: account.official_name,
+ bank: bank.id,
+ offbudget: offBudget ? 1 : 0,
+ account_sync_source: 'goCardless',
+ });
+ await db.insertPayee({
+ name: '',
+ transfer_acct: id,
+ });
+ }
+
+ await bankSync.syncAccount(
+ undefined,
+ undefined,
+ id,
+ account.account_id,
+ bank.bank_id,
+ );
+
+ connection.send('sync-event', {
+ type: 'success',
+ tables: ['transactions'],
+ });
+
+ return 'ok';
+}
+
+async function linkSimpleFinAccount({
+ externalAccount,
+ upgradingId,
+ offBudget = false,
+}: {
+ externalAccount: SyncServerSimpleFinAccount;
+ upgradingId?: AccountEntity['id'] | undefined;
+ offBudget?: boolean | undefined;
+}) {
+ let id;
+
+ const institution = {
+ name: externalAccount.institution ?? 'Unknown',
+ };
+
+ const bank = await link.findOrCreateBank(
+ institution,
+ externalAccount.orgDomain ?? externalAccount.orgId,
+ );
+
+ if (upgradingId) {
+ const accRow: AccountEntity = await db.first(
+ 'SELECT * FROM accounts WHERE id = ?',
+ [upgradingId],
+ );
+ id = accRow.id;
+ await db.update('accounts', {
+ id,
+ account_id: externalAccount.account_id,
+ bank: bank.id,
+ account_sync_source: 'simpleFin',
+ });
+ } else {
+ id = uuidv4();
+ await db.insertWithUUID('accounts', {
+ id,
+ account_id: externalAccount.account_id,
+ name: externalAccount.name,
+ official_name: externalAccount.name,
+ bank: bank.id,
+ offbudget: offBudget ? 1 : 0,
+ account_sync_source: 'simpleFin',
+ });
+ await db.insertPayee({
+ name: '',
+ transfer_acct: id,
+ });
+ }
+
+ await bankSync.syncAccount(
+ undefined,
+ undefined,
+ id,
+ externalAccount.account_id,
+ bank.bank_id,
+ );
+
+ await connection.send('sync-event', {
+ type: 'success',
+ tables: ['transactions'],
+ });
+
+ return 'ok';
+}
+
+async function createAccount({
+ name,
+ balance = 0,
+ offBudget = false,
+ closed = false,
+}: {
+ name: string;
+ balance?: number | undefined;
+ offBudget?: boolean | undefined;
+ closed?: boolean | undefined;
+}) {
+ const id: AccountEntity['id'] = await db.insertAccount({
+ name,
+ offbudget: offBudget ? 1 : 0,
+ closed: closed ? 1 : 0,
+ });
+
+ await db.insertPayee({
+ name: '',
+ transfer_acct: id,
+ });
+
+ if (balance != null && balance !== 0) {
+ const payee = await getStartingBalancePayee();
+
+ await db.insertTransaction({
+ account: id,
+ amount: amountToInteger(balance),
+ category: offBudget ? null : payee.category,
+ payee: payee.id,
+ date: monthUtils.currentDay(),
+ cleared: true,
+ starting_balance_flag: true,
+ });
+ }
+
+ return id;
+}
+
+async function closeAccount({
+ id,
+ transferAccountId,
+ categoryId,
+ forced = false,
+}: {
+ id: AccountEntity['id'];
+ transferAccountId?: AccountEntity['id'] | undefined;
+ categoryId?: CategoryEntity['id'] | undefined;
+ forced?: boolean | undefined;
+}) {
+ // Unlink the account if it's linked. This makes sure to remove it from
+ // bank-sync providers. (This should not be undo-able, as it mutates the
+ // remote server and the user will have to link the account again)
+ await unlinkAccount({ id });
+
+ return withUndo(async () => {
+ const account: AccountEntity = await db.first(
+ 'SELECT * FROM accounts WHERE id = ? AND tombstone = 0',
+ [id],
+ );
+
+ // Do nothing if the account doesn't exist or it's already been
+ // closed
+ if (!account || account.closed === 1) {
+ return;
+ }
+
+ const { balance, numTransactions } = await getAccountProperties({ id });
+
+ // If there are no transactions, we can simply delete the account
+ if (numTransactions === 0) {
+ await db.deleteAccount({ id });
+ } else if (forced) {
+ const rows = await db.runQuery<
+ Pick<db.DbViewTransaction, 'id' | 'transfer_id'>
+ >(
+ 'SELECT id, transfer_id FROM v_transactions WHERE account = ?',
+ [id],
+ true,
+ );
+
+ const { id: payeeId }: Pick<PayeeEntity, 'id'> = await db.first(
+ 'SELECT id FROM payees WHERE transfer_acct = ?',
+ [id],
+ );
+
+ await batchMessages(async () => {
+ // TODO: what this should really do is send a special message that
+ // automatically marks the tombstone value for all transactions
+ // within an account... or something? This is problematic
+ // because another client could easily add new data that
+ // should be marked as deleted.
+
+ rows.forEach(row => {
+ if (row.transfer_id) {
+ db.updateTransaction({
+ id: row.transfer_id,
+ payee: null,
+ transfer_id: null,
+ });
+ }
+
+ db.deleteTransaction({ id: row.id });
+ });
+
+ db.deleteAccount({ id });
+ db.deleteTransferPayee({ id: payeeId });
+ });
+ } else {
+ if (balance !== 0 && transferAccountId == null) {
+ throw APIError('balance is non-zero: transferAccountId is required');
+ }
+
+ await db.update('accounts', { id, closed: 1 });
+
+ // If there is a balance we need to transfer it to the specified
+ // account (and possibly categorize it)
+ if (balance !== 0 && transferAccountId) {
+ const { id: payeeId }: Pick<PayeeEntity, 'id'> = await db.first(
+ 'SELECT id FROM payees WHERE transfer_acct = ?',
+ [transferAccountId],
+ );
+
+ await mainApp.handlers['transaction-add']({
+ id: uuidv4(),
+ payee: payeeId,
+ amount: -balance,
+ account: id,
+ date: monthUtils.currentDay(),
+ notes: 'Closing account',
+ category: categoryId,
+ });
+ }
+ }
+ });
+}
+
+async function reopenAccount({ id }: { id: AccountEntity['id'] }) {
+ await db.update('accounts', { id, closed: 0 });
+}
+
+async function moveAccount({
+ id,
+ targetId,
+}: {
+ id: AccountEntity['id'];
+ targetId: AccountEntity['id'];
+}) {
+ await db.moveAccount(id, targetId);
+}
+
+async function setSecret({
+ name,
+ value,
+}: {
+ name: string;
+ value: string | null;
+}) {
+ const userToken = await asyncStorage.getItem('user-token');
+
+ if (!userToken) {
+ return { error: 'unauthorized' };
+ }
+
+ const serverConfig = getServer();
+ if (!serverConfig) {
+ throw new Error('Failed to get server config.');
+ }
+
+ try {
+ return await post(
+ serverConfig.BASE_SERVER + '/secret',
+ {
+ name,
+ value,
+ },
+ {
+ 'X-ACTUAL-TOKEN': userToken,
+ },
+ );
+ } catch (error) {
+ return {
+ error: 'failed',
+ reason: error instanceof PostError ? error.reason : undefined,
+ };
+ }
+}
+async function checkSecret(name: string) {
+ const userToken = await asyncStorage.getItem('user-token');
+
+ if (!userToken) {
+ return { error: 'unauthorized' };
+ }
+
+ const serverConfig = getServer();
+ if (!serverConfig) {
+ throw new Error('Failed to get server config.');
+ }
+
+ try {
+ return await get(serverConfig.BASE_SERVER + '/secret/' + name, {
+ 'X-ACTUAL-TOKEN': userToken,
+ });
+ } catch (error) {
+ console.error(error);
+ return { error: 'failed' };
+ }
+}
+
+let stopPolling = false;
+
+async function pollGoCardlessWebToken({
+ requisitionId,
+}: {
+ requisitionId: string;
+}) {
+ const userToken = await asyncStorage.getItem('user-token');
+ if (!userToken) return { error: 'unknown' };
+
+ const startTime = Date.now();
+ stopPolling = false;
+
+ async function getData(
+ cb: (error: string | null, data?: unknown | undefined) => void,
+ ) {
+ if (stopPolling) {
+ return;
+ }
+
+ if (Date.now() - startTime >= 1000 * 60 * 10) {
+ cb('timeout');
+ return;
+ }
+
+ const serverConfig = getServer();
+ if (!serverConfig) {
+ throw new Error('Failed to get server config.');
+ }
+
+ const data = await post(
+ serverConfig.GOCARDLESS_SERVER + '/get-accounts',
+ {
+ requisitionId,
+ },
+ {
+ 'X-ACTUAL-TOKEN': userToken,
+ },
+ );
+
+ if (data) {
+ if (data.error) {
+ cb('unknown');
+ } else {
+ cb(null, data);
+ }
+ } else {
+ setTimeout(() => getData(cb), 3000);
+ }
+ }
+
+ return new Promise(resolve => {
+ getData((error, data) => {
+ if (error) {
+ resolve({ error });
+ } else {
+ resolve({ data });
+ }
+ });
+ });
+}
+
+async function stopGoCardlessWebTokenPolling() {
+ stopPolling = true;
+ return 'ok';
+}
+
+async function goCardlessStatus() {
+ const userToken = await asyncStorage.getItem('user-token');
+
+ if (!userToken) {
+ return { error: 'unauthorized' };
+ }
+
+ const serverConfig = getServer();
+ if (!serverConfig) {
+ throw new Error('Failed to get server config.');
+ }
+
+ return post(
+ serverConfig.GOCARDLESS_SERVER + '/status',
+ {},
+ {
+ 'X-ACTUAL-TOKEN': userToken,
+ },
+ );
+}
+
+async function simpleFinStatus() {
+ const userToken = await asyncStorage.getItem('user-token');
+
+ if (!userToken) {
+ return { error: 'unauthorized' };
+ }
+
+ const serverConfig = getServer();
+ if (!serverConfig) {
+ throw new Error('Failed to get server config.');
+ }
+
+ return post(
+ serverConfig.SIMPLEFIN_SERVER + '/status',
+ {},
+ {
+ 'X-ACTUAL-TOKEN': userToken,
+ },
+ );
+}
+
+async function simpleFinAccounts() {
+ const userToken = await asyncStorage.getItem('user-token');
+
+ if (!userToken) {
+ return { error: 'unauthorized' };
+ }
+
+ const serverConfig = getServer();
+ if (!serverConfig) {
+ throw new Error('Failed to get server config.');
+ }
+
+ try {
+ return await post(
+ serverConfig.SIMPLEFIN_SERVER + '/accounts',
+ {},
+ {
+ 'X-ACTUAL-TOKEN': userToken,
+ },
+ 60000,
+ );
+ } catch (error) {
+ return { error_code: 'TIMED_OUT' };
+ }
+}
+
+async function getGoCardlessBanks(country: string) {
+ const userToken = await asyncStorage.getItem('user-token');
+
+ if (!userToken) {
+ return { error: 'unauthorized' };
+ }
+
+ const serverConfig = getServer();
+ if (!serverConfig) {
+ throw new Error('Failed to get server config.');
+ }
+
+ return post(
+ serverConfig.GOCARDLESS_SERVER + '/get-banks',
+ { country, showDemo: isNonProductionEnvironment() },
+ {
+ 'X-ACTUAL-TOKEN': userToken,
+ },
+ );
+}
+
+async function createGoCardlessWebToken({
+ institutionId,
+ accessValidForDays,
+}: {
+ institutionId: string;
+ accessValidForDays: number;
+}) {
+ const userToken = await asyncStorage.getItem('user-token');
+
+ if (!userToken) {
+ return { error: 'unauthorized' };
+ }
+
+ const serverConfig = getServer();
+ if (!serverConfig) {
+ throw new Error('Failed to get server config.');
+ }
+
+ try {
+ return await post(
+ serverConfig.GOCARDLESS_SERVER + '/create-web-token',
+ {
+ institutionId,
+ accessValidForDays,
+ },
+ {
+ 'X-ACTUAL-TOKEN': userToken,
+ },
+ );
+ } catch (error) {
+ console.error(error);
+ return { error: 'failed' };
+ }
+}
+
+type SyncResponse = {
+ newTransactions: Array<TransactionEntity['id']>;
+ matchedTransactions: Array<TransactionEntity['id']>;
+ updatedAccounts: Array<AccountEntity['id']>;
+};
+
+function handleSyncResponse(
+ res: {
+ added: Array<TransactionEntity['id']>;
+ updated: Array<TransactionEntity['id']>;
+ },
+ acct: db.DbAccount,
+): SyncResponse {
+ const { added, updated } = res;
+ const newTransactions: Array<TransactionEntity['id']> = [];
+ const matchedTransactions: Array<TransactionEntity['id']> = [];
+ const updatedAccounts: Array<AccountEntity['id']> = [];
+
+ newTransactions.push(...added);
+ matchedTransactions.push(...updated);
+
+ if (added.length > 0) {
+ updatedAccounts.push(acct.id);
+ }
+
+ return {
+ newTransactions,
+ matchedTransactions,
+ updatedAccounts,
+ };
+}
+
+type SyncError =
+ | {
+ type: 'SyncError';
+ accountId: AccountEntity['id'];
+ message: string;
+ category: string;
+ code: string;
+ }
+ | {
+ accountId: AccountEntity['id'];
+ message: string;
+ internal?: string;
+ };
+
+function handleSyncError(
+ err: Error | PostError | BankSyncError,
+ acct: db.DbAccount,
+): SyncError {
+ if (err instanceof BankSyncError) {
+ return {
+ type: 'SyncError',
+ accountId: acct.id,
+ message: 'Failed syncing account “' + acct.name + '.”',
+ category: err.category,
+ code: err.code,
+ };
+ }
+
+ if (err instanceof PostError && err.reason !== 'internal') {
+ return {
+ accountId: acct.id,
+ message: err.reason
+ ? err.reason
+ : `Account “${acct.name}” is not linked properly. Please link it again.`,
+ };
+ }
+
+ return {
+ accountId: acct.id,
+ message:
+ 'There was an internal error. Please get in touch https://actualbudget.org/contact for support.',
+ internal: err.stack,
+ };
+}
+
+export type SyncResponseWithErrors = SyncResponse & {
+ errors: SyncError[];
+};
+
+async function accountsBankSync({
+ ids = [],
+}: {
+ ids: Array<AccountEntity['id']>;
+}): Promise<SyncResponseWithErrors> {
+ const [[, userId], [, userKey]] = await asyncStorage.multiGet([
+ 'user-id',
+ 'user-key',
+ ]);
+
+ const accounts = await db.runQuery<
+ db.DbAccount & { bankId: db.DbBank['bank_id'] }
+ >(
+ `
+ SELECT a.*, b.bank_id as bankId
+ FROM accounts a
+ LEFT JOIN banks b ON a.bank = b.id
+ WHERE a.tombstone = 0 AND a.closed = 0
+ ${ids.length ? `AND a.id IN (${ids.map(() => '?').join(', ')})` : ''}
+ ORDER BY a.offbudget, a.sort_order
+ `,
+ ids,
+ true,
+ );
+
+ const errors: ReturnType<typeof handleSyncError>[] = [];
+ const newTransactions: Array<TransactionEntity['id']> = [];
+ const matchedTransactions: Array<TransactionEntity['id']> = [];
+ const updatedAccounts: Array<AccountEntity['id']> = [];
+
+ for (const acct of accounts) {
+ if (acct.bankId && acct.account_id) {
+ try {
+ console.group('Bank Sync operation for account:', acct.name);
+ const syncResponse = await bankSync.syncAccount(
+ userId as string,
+ userKey as string,
+ acct.id,
+ acct.account_id,
+ acct.bankId,
+ );
+
+ const syncResponseData = handleSyncResponse(syncResponse, acct);
+
+ newTransactions.push(...syncResponseData.newTransactions);
+ matchedTransactions.push(...syncResponseData.matchedTransactions);
+ updatedAccounts.push(...syncResponseData.updatedAccounts);
+ } catch (err) {
+ const error = err as Error;
+ errors.push(handleSyncError(error, acct));
+ captureException({
+ ...error,
+ message: 'Failed syncing account “' + acct.name + '.”',
+ } as Error);
+ } finally {
+ console.groupEnd();
+ }
+ }
+ }
+
+ if (updatedAccounts.length > 0) {
+ connection.send('sync-event', {
+ type: 'success',
+ tables: ['transactions'],
+ });
+ }
+
+ return { errors, newTransactions, matchedTransactions, updatedAccounts };
+}
+
+async function simpleFinBatchSync({
+ ids = [],
+}: {
+ ids: Array<AccountEntity['id']>;
+}): Promise<
+ Array<{ accountId: AccountEntity['id']; res: SyncResponseWithErrors }>
+> {
+ const accounts = await db.runQuery<
+ db.DbAccount & { bankId: db.DbBank['bank_id'] }
+ >(
+ `SELECT a.*, b.bank_id as bankId FROM accounts a
+ LEFT JOIN banks b ON a.bank = b.id
+ WHERE
+ a.tombstone = 0
+ AND a.closed = 0
+ AND a.account_sync_source = 'simpleFin'
+ ${ids.length ? `AND a.id IN (${ids.map(() => '?').join(', ')})` : ''}
+ ORDER BY a.offbudget, a.sort_order`,
+ ids.length ? ids : [],
+ true,
+ );
+
+ const retVal: Array<{
+ accountId: AccountEntity['id'];
+ res: {
+ errors: ReturnType<typeof handleSyncError>[];
+ newTransactions: Array<TransactionEntity['id']>;
+ matchedTransactions: Array<TransactionEntity['id']>;
+ updatedAccounts: Array<AccountEntity['id']>;
+ };
+ }> = [];
+
+ console.group('Bank Sync operation for all SimpleFin accounts');
+ try {
+ const syncResponses: Array<{
+ accountId: AccountEntity['id'];
+ res: {
+ error_code: string;
+ error_type: string;
+ added: Array<TransactionEntity['id']>;
+ updated: Array<TransactionEntity['id']>;
+ };
+ }> = await bankSync.simpleFinBatchSync(
+ accounts.map(a => ({
+ id: a.id,
+ account_id: a.account_id || null,
+ })),
+ );
+ for (const syncResponse of syncResponses) {
+ const account = accounts.find(a => a.id === syncResponse.accountId);
+ if (!account) {
+ console.error(
+ `Invalid account ID found in response: ${syncResponse.accountId}. Proceeding to the next account...`,
+ );
+ continue;
+ }
+
+ const errors: ReturnType<typeof handleSyncError>[] = [];
+ const newTransactions: Array<TransactionEntity['id']> = [];
+ const matchedTransactions: Array<TransactionEntity['id']> = [];
+ const updatedAccounts: Array<AccountEntity['id']> = [];
+
+ if (syncResponse.res.error_code) {
+ errors.push(
+ handleSyncError(
+ {
+ type: 'BankSyncError',
+ reason: 'Failed syncing account “' + account.name + '.”',
+ category: syncResponse.res.error_type,
+ code: syncResponse.res.error_code,
+ } as BankSyncError,
+ account,
+ ),
+ );
+ } else {
+ const syncResponseData = handleSyncResponse(syncResponse.res, account);
+
+ newTransactions.push(...syncResponseData.newTransactions);
+ matchedTransactions.push(...syncResponseData.matchedTransactions);
+ updatedAccounts.push(...syncResponseData.updatedAccounts);
+ }
+
+ retVal.push({
+ accountId: syncResponse.accountId,
+ res: { errors, newTransactions, matchedTransactions, updatedAccounts },
+ });
+ }
+ } catch (err) {
+ const errors = [];
+ for (const account of accounts) {
+ retVal.push({
+ accountId: account.id,
+ res: {
+ errors,
+ newTransactions: [],
+ matchedTransactions: [],
+ updatedAccounts: [],
+ },
+ });
+ const error = err as Error;
+ errors.push(handleSyncError(error, account));
+ }
+ }
+
+ if (retVal.some(a => a.res.updatedAccounts.length > 0)) {
+ connection.send('sync-event', {
+ type: 'success',
+ tables: ['transactions'],
+ });
+ }
+
+ console.groupEnd();
+
+ return retVal;
+}
+
+type ImportTransactionsResult = bankSync.ReconcileTransactionsResult & {
+ errors: Array<{
+ message: string;
+ }>;
+};
+
+async function importTransactions({
+ accountId,
+ transactions,
+ isPreview,
+ opts,
+}: {
+ accountId: AccountEntity['id'];
+ transactions: TransactionEntity[];
+ isPreview: boolean;
+ opts?: {
+ defaultCleared: boolean;
+ };
+}): Promise<ImportTransactionsResult> {
+ if (typeof accountId !== 'string') {
+ throw APIError('transactions-import: accountId must be an id');
+ }
+
+ try {
+ const reconciled = await bankSync.reconcileTransactions(
+ accountId,
+ transactions,
+ false,
+ true,
+ isPreview,
+ opts?.defaultCleared,
+ );
+ return {
+ errors: [],
+ added: reconciled.added,
+ updated: reconciled.updated,
+ updatedPreview: reconciled.updatedPreview,
+ };
+ } catch (err) {
+ if (err instanceof TransactionError) {
+ return {
+ errors: [{ message: err.message }],
+ added: [],
+ updated: [],
+ updatedPreview: [],
+ };
+ }
+
+ throw err;
+ }
+}
+
+async function unlinkAccount({ id }: { id: AccountEntity['id'] }) {
+ const { bank: bankId }: Pick<AccountEntity, 'bank'> = await db.first(
+ 'SELECT bank FROM accounts WHERE id = ?',
+ [id],
+ );
+
+ if (!bankId) {
+ return 'ok';
+ }
+
+ const accRow: AccountEntity = await db.first(
+ 'SELECT * FROM accounts WHERE id = ?',
+ [id],
+ );
+
+ const isGoCardless = accRow.account_sync_source === 'goCardless';
+
+ await db.updateAccount({
+ id,
+ account_id: null,
+ bank: null,
+ balance_current: null,
+ balance_available: null,
+ balance_limit: null,
+ account_sync_source: null,
+ });
+
+ if (isGoCardless === false) {
+ return;
+ }
+
+ const { count }: { count: number } = await db.first(
+ 'SELECT COUNT(*) as count FROM accounts WHERE bank = ?',
+ [bankId],
+ );
+
+ // No more accounts are associated with this bank. We can remove
+ // it from GoCardless.
+ const userToken = await asyncStorage.getItem('user-token');
+ if (!userToken) {
+ return 'ok';
+ }
+
+ if (count === 0) {
+ const { bank_id: requisitionId }: Pick<BankEntity, 'bank_id'> =
+ await db.first('SELECT bank_id FROM banks WHERE id = ?', [bankId]);
+
+ const serverConfig = getServer();
+ if (!serverConfig) {
+ throw new Error('Failed to get server config.');
+ }
+
+ try {
+ await post(
+ serverConfig.GOCARDLESS_SERVER + '/remove-account',
+ {
+ requisitionId,
+ },
+ {
+ 'X-ACTUAL-TOKEN': userToken,
+ },
+ );
+ } catch (error) {
+ console.log({ error });
+ }
+ }
+
+ return 'ok';
+}
+
+export const app = createApp<AccountHandlers>();
+
+app.method('account-update', mutator(undoable(updateAccount)));
+app.method('accounts-get', getAccounts);
+app.method('account-balance', getAccountBalance);
+app.method('account-properties', getAccountProperties);
+app.method('gocardless-accounts-link', linkGoCardlessAccount);
+app.method('simplefin-accounts-link', linkSimpleFinAccount);
+app.method('account-create', mutator(undoable(createAccount)));
+app.method('account-close', mutator(closeAccount));
+app.method('account-reopen', mutator(undoable(reopenAccount)));
+app.method('account-move', mutator(undoable(moveAccount)));
+app.method('secret-set', setSecret);
+app.method('secret-check', checkSecret);
+app.method('gocardless-poll-web-token', pollGoCardlessWebToken);
+app.method('gocardless-poll-web-token-stop', stopGoCardlessWebTokenPolling);
+app.method('gocardless-status', goCardlessStatus);
+app.method('simplefin-status', simpleFinStatus);
+app.method('simplefin-accounts', simpleFinAccounts);
+app.method('gocardless-get-banks', getGoCardlessBanks);
+app.method('gocardless-create-web-token', createGoCardlessWebToken);
+app.method('accounts-bank-sync', accountsBankSync);
+app.method('simplefin-batch-sync', simpleFinBatchSync);
+app.method('transactions-import', mutator(undoable(importTransactions)));
+app.method('account-unlink', mutator(unlinkAccount));
diff --git a/packages/loot-core/src/server/accounts/payees.ts b/packages/loot-core/src/server/accounts/payees.ts
index 30eba38b5e..bb8183f4e9 100644
--- a/packages/loot-core/src/server/accounts/payees.ts
+++ b/packages/loot-core/src/server/accounts/payees.ts
@@ -1,10 +1,11 @@
// @ts-strict-ignore
+import { CategoryEntity, PayeeEntity } from '../../types/models';
import * as db from '../db';
export async function createPayee(description) {
// Check to make sure no payee already exists with exactly the same
// name
- const row = await db.first(
+ const row: Pick<PayeeEntity, 'id'> = await db.first(
`SELECT id FROM payees WHERE UNICODE_LOWER(name) = ? AND tombstone = 0`,
[description.toLowerCase()],
);
@@ -12,12 +13,12 @@ export async function createPayee(description) {
if (row) {
return row.id;
} else {
- return db.insertPayee({ name: description });
+ return (await db.insertPayee({ name: description })) as PayeeEntity['id'];
}
}
export async function getStartingBalancePayee() {
- let category = await db.first(`
+ let category: CategoryEntity = await db.first(`
SELECT * FROM categories
WHERE is_income = 1 AND
LOWER(name) = 'starting balances' AND
diff --git a/packages/loot-core/src/server/accounts/sync.ts b/packages/loot-core/src/server/accounts/sync.ts
index 168883ebb3..dfe4ab4f87 100644
--- a/packages/loot-core/src/server/accounts/sync.ts
+++ b/packages/loot-core/src/server/accounts/sync.ts
@@ -423,6 +423,16 @@ async function createNewPayees(payeesToCreate, addsAndUpdates) {
});
}
+export type ReconcileTransactionsResult = {
+ added: string[];
+ updated: string[];
+ updatedPreview: Array<{
+ transaction: TransactionEntity;
+ existing?: TransactionEntity;
+ ignored?: boolean;
+ }>;
+};
+
export async function reconcileTransactions(
acctId,
transactions,
@@ -430,7 +440,7 @@ export async function reconcileTransactions(
strictIdChecking = true,
isPreview = false,
defaultCleared = true,
-) {
+): Promise<ReconcileTransactionsResult> {
console.log('Performing transaction reconciliation');
const updated = [];
@@ -857,8 +867,8 @@ async function processBankSyncDownload(
}
export async function syncAccount(
- userId: string,
- userKey: string,
+ userId: string | undefined,
+ userKey: string | undefined,
id: string,
acctId: string,
bankId: string,
@@ -890,25 +900,22 @@ export async function syncAccount(
return processBankSyncDownload(download, id, acctRow, newAccount);
}
-export async function SimpleFinBatchSync(
- accounts: {
- id: AccountEntity['id'];
- accountId: AccountEntity['account_id'];
- }[],
+export async function simpleFinBatchSync(
+ accounts: Array<Pick<AccountEntity, 'id' | 'account_id'>>,
) {
const startDates = await Promise.all(
accounts.map(async a => getAccountSyncStartDate(a.id)),
);
const res = await downloadSimpleFinTransactions(
- accounts.map(a => a.accountId),
+ accounts.map(a => a.account_id),
startDates,
);
const promises = [];
for (let i = 0; i < accounts.length; i++) {
const account = accounts[i];
- const download = res[account.accountId];
+ const download = res[account.account_id];
const acctRow = await db.select('accounts', account.id);
const oldestTransaction = await getAccountOldestTransaction(account.id);
diff --git a/packages/loot-core/src/server/errors.ts b/packages/loot-core/src/server/errors.ts
index e218c13c15..a8da01d745 100644
--- a/packages/loot-core/src/server/errors.ts
+++ b/packages/loot-core/src/server/errors.ts
@@ -12,6 +12,21 @@ export class PostError extends Error {
}
}
+export class BankSyncError extends Error {
+ reason: string;
+ category: string;
+ code: string;
+ type: 'BankSyncError';
+
+ constructor(reason: string, category: string, code: string) {
+ super('BankSyncError: ' + reason);
+ this.type = 'BankSyncError';
+ this.reason = reason;
+ this.category = category;
+ this.code = code;
+ }
+}
+
export class HTTPError extends Error {
statusCode: number;
responseBody: string;
diff --git a/packages/loot-core/src/server/main.ts b/packages/loot-core/src/server/main.ts
index 328e83f373..3e25b3723c 100644
--- a/packages/loot-core/src/server/main.ts
+++ b/packages/loot-core/src/server/main.ts
@@ -12,18 +12,14 @@ import * as connection from '../platform/server/connection';
import * as fs from '../platform/server/fs';
import { logger } from '../platform/server/log';
import * as sqlite from '../platform/server/sqlite';
-import { isNonProductionEnvironment } from '../shared/environment';
import * as monthUtils from '../shared/months';
-import { dayFromDate } from '../shared/months';
import { q } from '../shared/query';
-import { amountToInteger, stringToInteger } from '../shared/util';
+import { stringToInteger } from '../shared/util';
import { type Budget } from '../types/budget';
import { Handlers } from '../types/handlers';
import { OpenIdConfig } from '../types/models/openid';
-import * as link from './accounts/link';
-import { getStartingBalancePayee } from './accounts/payees';
-import * as bankSync from './accounts/sync';
+import { app as accountsApp } from './accounts/app';
import { app as adminApp } from './admin/app';
import { installAPI } from './api';
import { runQuery as aqlQuery } from './aql';
@@ -41,7 +37,7 @@ import { app as dashboardApp } from './dashboard/app';
import * as db from './db';
import * as mappings from './db/mappings';
import * as encryption from './encryption';
-import { APIError, TransactionError, PostError } from './errors';
+import { APIError } from './errors';
import { app as filtersApp } from './filters/app';
import { handleBudgetImport } from './importers';
import { app } from './main-app';
@@ -505,792 +501,6 @@ handlers['query'] = async function (query) {
return aqlQuery(query);
};
-handlers['account-update'] = mutator(async function ({ id, name }) {
- return withUndo(async () => {
- await db.update('accounts', { id, name });
- return {};
- });
-});
-
-handlers['accounts-get'] = async function () {
- return db.getAccounts();
-};
-
-handlers['account-balance'] = async function ({ id, cutoff }) {
- const { balance } = await db.first(
- 'SELECT sum(amount) as balance FROM transactions WHERE acct = ? AND isParent = 0 AND tombstone = 0 AND date <= ?',
- [id, db.toDateRepr(dayFromDate(cutoff))],
- );
- return balance ? balance : 0;
-};
-
-handlers['account-properties'] = async function ({ id }) {
- const { balance } = await db.first(
- 'SELECT sum(amount) as balance FROM transactions WHERE acct = ? AND isParent = 0 AND tombstone = 0',
- [id],
- );
- const { count } = await db.first(
- 'SELECT count(id) as count FROM transactions WHERE acct = ? AND tombstone = 0',
- [id],
- );
-
- return { balance: balance || 0, numTransactions: count };
-};
-
-handlers['gocardless-accounts-link'] = async function ({
- requisitionId,
- account,
- upgradingId,
- offBudget,
-}) {
- let id;
- const bank = await link.findOrCreateBank(account.institution, requisitionId);
-
- if (upgradingId) {
- const accRow = await db.first('SELECT * FROM accounts WHERE id = ?', [
- upgradingId,
- ]);
- id = accRow.id;
- await db.update('accounts', {
- id,
- account_id: account.account_id,
- bank: bank.id,
- account_sync_source: 'goCardless',
- });
- } else {
- id = uuidv4();
- await db.insertWithUUID('accounts', {
- id,
- account_id: account.account_id,
- mask: account.mask,
- name: account.name,
- official_name: account.official_name,
- bank: bank.id,
- offbudget: offBudget ? 1 : 0,
- account_sync_source: 'goCardless',
- });
- await db.insertPayee({
- name: '',
- transfer_acct: id,
- });
- }
-
- await bankSync.syncAccount(
- undefined,
- undefined,
- id,
- account.account_id,
- bank.bank_id,
- );
-
- connection.send('sync-event', {
- type: 'success',
- tables: ['transactions'],
- });
-
- return 'ok';
-};
-
-handlers['simplefin-accounts-link'] = async function ({
- externalAccount,
- upgradingId,
- offBudget,
-}) {
- let id;
-
- const institution = {
- name: externalAccount.institution ?? 'Unknown',
- };
-
- const bank = await link.findOrCreateBank(
- institution,
- externalAccount.orgDomain ?? externalAccount.orgId,
- );
-
- if (upgradingId) {
- const accRow = await db.first('SELECT * FROM accounts WHERE id = ?', [
- upgradingId,
- ]);
- id = accRow.id;
- await db.update('accounts', {
- id,
- account_id: externalAccount.account_id,
- bank: bank.id,
- account_sync_source: 'simpleFin',
- });
- } else {
- id = uuidv4();
- await db.insertWithUUID('accounts', {
- id,
- account_id: externalAccount.account_id,
- name: externalAccount.name,
- official_name: externalAccount.name,
- bank: bank.id,
- offbudget: offBudget ? 1 : 0,
- account_sync_source: 'simpleFin',
- });
- await db.insertPayee({
- name: '',
- transfer_acct: id,
- });
- }
-
- await bankSync.syncAccount(
- undefined,
- undefined,
- id,
- externalAccount.account_id,
- bank.bank_id,
- );
-
- await connection.send('sync-event', {
- type: 'success',
- tables: ['transactions'],
- });
-
- return 'ok';
-};
-
-handlers['account-create'] = mutator(async function ({
- name,
- balance,
- offBudget,
- closed,
-}) {
- return withUndo(async () => {
- const id = await db.insertAccount({
- name,
- offbudget: offBudget ? 1 : 0,
- closed: closed ? 1 : 0,
- });
-
- await db.insertPayee({
- name: '',
- transfer_acct: id,
- });
-
- if (balance != null && balance !== 0) {
- const payee = await getStartingBalancePayee();
-
- await db.insertTransaction({
- account: id,
- amount: amountToInteger(balance),
- category: offBudget ? null : payee.category,
- payee: payee.id,
- date: monthUtils.currentDay(),
- cleared: true,
- starting_balance_flag: true,
- });
- }
-
- return id;
- });
-});
-
-handlers['account-close'] = mutator(async function ({
- id,
- transferAccountId,
- categoryId,
- forced,
-}) {
- // Unlink the account if it's linked. This makes sure to remove it from
- // bank-sync providers. (This should not be undo-able, as it mutates the
- // remote server and the user will have to link the account again)
- await handlers['account-unlink']({ id });
-
- return withUndo(async () => {
- const account = await db.first(
- 'SELECT * FROM accounts WHERE id = ? AND tombstone = 0',
- [id],
- );
-
- // Do nothing if the account doesn't exist or it's already been
- // closed
- if (!account || account.closed === 1) {
- return;
- }
-
- const { balance, numTransactions } = await handlers['account-properties']({
- id,
- });
-
- // If there are no transactions, we can simply delete the account
- if (numTransactions === 0) {
- await db.deleteAccount({ id });
- } else if (forced) {
- const rows = await db.runQuery<
- Pick<db.DbViewTransaction, 'id' | 'transfer_id'>
- >(
- 'SELECT id, transfer_id FROM v_transactions WHERE account = ?',
- [id],
- true,
- );
-
- const { id: payeeId } = await db.first(
- 'SELECT id FROM payees WHERE transfer_acct = ?',
- [id],
- );
-
- await batchMessages(async () => {
- // TODO: what this should really do is send a special message that
- // automatically marks the tombstone value for all transactions
- // within an account... or something? This is problematic
- // because another client could easily add new data that
- // should be marked as deleted.
-
- rows.forEach(row => {
- if (row.transfer_id) {
- db.updateTransaction({
- id: row.transfer_id,
- payee: null,
- transfer_id: null,
- });
- }
-
- db.deleteTransaction({ id: row.id });
- });
-
- db.deleteAccount({ id });
- db.deleteTransferPayee({ id: payeeId });
- });
- } else {
- if (balance !== 0 && transferAccountId == null) {
- throw APIError('balance is non-zero: transferAccountId is required');
- }
-
- await db.update('accounts', { id, closed: 1 });
-
- // If there is a balance we need to transfer it to the specified
- // account (and possibly categorize it)
- if (balance !== 0) {
- const { id: payeeId } = await db.first(
- 'SELECT id FROM payees WHERE transfer_acct = ?',
- [transferAccountId],
- );
-
- await handlers['transaction-add']({
- id: uuidv4(),
- payee: payeeId,
- amount: -balance,
- account: id,
- date: monthUtils.currentDay(),
- notes: 'Closing account',
- category: categoryId || null,
- });
- }
- }
- });
-});
-
-handlers['account-reopen'] = mutator(async function ({ id }) {
- return withUndo(async () => {
- await db.update('accounts', { id, closed: 0 });
- });
-});
-
-handlers['account-move'] = mutator(async function ({ id, targetId }) {
- return withUndo(async () => {
- await db.moveAccount(id, targetId);
- });
-});
-
-let stopPolling = false;
-
-handlers['secret-set'] = async function ({ name, value }) {
- const userToken = await asyncStorage.getItem('user-token');
-
- if (!userToken) {
- return { error: 'unauthorized' };
- }
-
- try {
- return await post(
- getServer().BASE_SERVER + '/secret',
- {
- name,
- value,
- },
- {
- 'X-ACTUAL-TOKEN': userToken,
- },
- );
- } catch (error) {
- return { error: 'failed', reason: error.reason };
- }
-};
-
-handlers['secret-check'] = async function (name) {
- const userToken = await asyncStorage.getItem('user-token');
-
- if (!userToken) {
- return { error: 'unauthorized' };
- }
-
- try {
- return await get(getServer().BASE_SERVER + '/secret/' + name, {
- 'X-ACTUAL-TOKEN': userToken,
- });
- } catch (error) {
- console.error(error);
- return { error: 'failed' };
- }
-};
-
-handlers['gocardless-poll-web-token'] = async function ({
- upgradingAccountId,
- requisitionId,
-}) {
- const userToken = await asyncStorage.getItem('user-token');
- if (!userToken) return { error: 'unknown' };
-
- const startTime = Date.now();
- stopPolling = false;
-
- async function getData(cb) {
- if (stopPolling) {
- return;
- }
-
- if (Date.now() - startTime >= 1000 * 60 * 10) {
- cb('timeout');
- return;
- }
-
- const data = await post(
- getServer().GOCARDLESS_SERVER + '/get-accounts',
- {
- upgradingAccountId,
- requisitionId,
- },
- {
- 'X-ACTUAL-TOKEN': userToken,
- },
- );
-
- if (data) {
- if (data.error) {
- cb('unknown');
- } else {
- cb(null, data);
- }
- } else {
- setTimeout(() => getData(cb), 3000);
- }
- }
-
- return new Promise(resolve => {
- getData((error, data) => {
- if (error) {
- resolve({ error });
- } else {
- resolve({ data });
- }
- });
- });
-};
-
-handlers['gocardless-status'] = async function () {
- const userToken = await asyncStorage.getItem('user-token');
-
- if (!userToken) {
- return { error: 'unauthorized' };
- }
-
- return post(
- getServer().GOCARDLESS_SERVER + '/status',
- {},
- {
- 'X-ACTUAL-TOKEN': userToken,
- },
- );
-};
-
-handlers['simplefin-status'] = async function () {
- const userToken = await asyncStorage.getItem('user-token');
-
- if (!userToken) {
- return { error: 'unauthorized' };
- }
-
- return post(
- getServer().SIMPLEFIN_SERVER + '/status',
- {},
- {
- 'X-ACTUAL-TOKEN': userToken,
- },
- );
-};
-
-handlers['simplefin-accounts'] = async function () {
- const userToken = await asyncStorage.getItem('user-token');
-
- if (!userToken) {
- return { error: 'unauthorized' };
- }
-
- try {
- return await post(
- getServer().SIMPLEFIN_SERVER + '/accounts',
- {},
- {
- 'X-ACTUAL-TOKEN': userToken,
- },
- 60000,
- );
- } catch (error) {
- return { error_code: 'TIMED_OUT' };
- }
-};
-
-handlers['gocardless-get-banks'] = async function (country) {
- const userToken = await asyncStorage.getItem('user-token');
-
- if (!userToken) {
- return { error: 'unauthorized' };
- }
-
- return post(
- getServer().GOCARDLESS_SERVER + '/get-banks',
- { country, showDemo: isNonProductionEnvironment() },
- {
- 'X-ACTUAL-TOKEN': userToken,
- },
- );
-};
-
-handlers['gocardless-poll-web-token-stop'] = async function () {
- stopPolling = true;
- return 'ok';
-};
-
-handlers['gocardless-create-web-token'] = async function ({
- upgradingAccountId,
- institutionId,
- accessValidForDays,
-}) {
- const userToken = await asyncStorage.getItem('user-token');
-
- if (!userToken) {
- return { error: 'unauthorized' };
- }
-
- try {
- return await post(
- getServer().GOCARDLESS_SERVER + '/create-web-token',
- {
- upgradingAccountId,
- institutionId,
- accessValidForDays,
- },
- {
- 'X-ACTUAL-TOKEN': userToken,
- },
- );
- } catch (error) {
- console.error(error);
- return { error: 'failed' };
- }
-};
-
-async function handleSyncResponse(
- res,
- acct,
- newTransactions,
- matchedTransactions,
- updatedAccounts,
-) {
- const { added, updated } = res;
-
- newTransactions.push(...added);
- matchedTransactions.push(...updated);
-
- if (added.length > 0) {
- updatedAccounts.push(acct.id);
- }
-
- const ts = new Date().getTime().toString();
- const id = acct.id;
- await db.runQuery(`UPDATE accounts SET last_sync = ? WHERE id = ?`, [ts, id]);
-}
-
-function handleSyncError(err, acct) {
- if (err.type === 'BankSyncError') {
- return {
- type: 'SyncError',
- accountId: acct.id,
- message: 'Failed syncing account “' + acct.name + '.”',
- category: err.category,
- code: err.code,
- };
- }
-
- if (err instanceof PostError && err.reason !== 'internal') {
- return {
- accountId: acct.id,
- message: err.reason
- ? err.reason
- : `Account “${acct.name}” is not linked properly. Please link it again.`,
- };
- }
-
- return {
- accountId: acct.id,
- message:
- 'There was an internal error. Please get in touch https://actualbudget.org/contact for support.',
- internal: err.stack,
- };
-}
-
-handlers['accounts-bank-sync'] = async function ({ ids = [] }) {
- const [[, userId], [, userKey]] = await asyncStorage.multiGet([
- 'user-id',
- 'user-key',
- ] as const);
-
- const accounts = await db.runQuery<
- db.DbAccount & { bankId: db.DbBank['bank_id'] }
- >(
- `
- SELECT a.*, b.bank_id as bankId
- FROM accounts a
- LEFT JOIN banks b ON a.bank = b.id
- WHERE a.tombstone = 0 AND a.closed = 0
- ${ids.length ? `AND a.id IN (${ids.map(() => '?').join(', ')})` : ''}
- ORDER BY a.offbudget, a.sort_order
- `,
- ids,
- true,
- );
-
- const errors = [];
- const newTransactions = [];
- const matchedTransactions = [];
- const updatedAccounts = [];
-
- for (let i = 0; i < accounts.length; i++) {
- const acct = accounts[i];
- if (acct.bankId) {
- try {
- console.group('Bank Sync operation for account:', acct.name);
- const res = await bankSync.syncAccount(
- userId,
- userKey,
- acct.id,
- acct.account_id,
- acct.bankId,
- );
-
- await handleSyncResponse(
- res,
- acct,
- newTransactions,
- matchedTransactions,
- updatedAccounts,
- );
- } catch (err) {
- errors.push(handleSyncError(err, acct));
- err.message = 'Failed syncing account “' + acct.name + '.”';
- captureException(err);
- } finally {
- console.groupEnd();
- }
- }
- }
-
- if (updatedAccounts.length > 0) {
- connection.send('sync-event', {
- type: 'success',
- tables: ['transactions'],
- });
- }
-
- return { errors, newTransactions, matchedTransactions, updatedAccounts };
-};
-
-handlers['simplefin-batch-sync'] = async function ({ ids = [] }) {
- const accounts = await db.runQuery<
- db.DbAccount & { bankId: db.DbBank['bank_id'] }
- >(
- `SELECT a.*, b.bank_id as bankId FROM accounts a
- LEFT JOIN banks b ON a.bank = b.id
- WHERE
- a.tombstone = 0
- AND a.closed = 0
- AND a.account_sync_source = 'simpleFin'
- ${ids.length ? `AND a.id IN (${ids.map(() => '?').join(', ')})` : ''}
- ORDER BY a.offbudget, a.sort_order`,
- ids.length ? ids : [],
- true,
- );
-
- const retVal = [];
-
- console.group('Bank Sync operation for all SimpleFin accounts');
- try {
- const res = await bankSync.SimpleFinBatchSync(
- accounts.map(a => ({
- id: a.id,
- accountId: a.account_id,
- })),
- );
- for (const account of res) {
- const errors = [];
- const newTransactions = [];
- const matchedTransactions = [];
- const updatedAccounts = [];
-
- if (account.res.error_code) {
- errors.push(
- handleSyncError(
- {
- type: 'BankSyncError',
- category: account.res.error_type,
- code: account.res.error_code,
- },
- accounts.find(a => a.id === account.accountId),
- ),
- );
- } else {
- await handleSyncResponse(
- account.res,
- accounts.find(a => a.id === account.accountId),
- newTransactions,
- matchedTransactions,
- updatedAccounts,
- );
- }
-
- retVal.push({
- accountId: account.accountId,
- res: { errors, newTransactions, matchedTransactions, updatedAccounts },
- });
- }
- } catch (err) {
- const errors = [];
- for (const account of accounts) {
- retVal.push({
- accountId: account.id,
- res: {
- errors,
- newTransactions: [],
- matchedTransactions: [],
- updatedAccounts: [],
- },
- });
- errors.push(handleSyncError(err, account));
- }
- }
-
- if (retVal.some(a => a.res.updatedAccounts.length > 0)) {
- connection.send('sync-event', {
- type: 'success',
- tables: ['transactions'],
- });
- }
-
- console.groupEnd();
-
- return retVal;
-};
-
-handlers['transactions-import'] = mutator(function ({
- accountId,
- transactions,
- isPreview,
- opts,
-}) {
- return withUndo(async () => {
- if (typeof accountId !== 'string') {
- throw APIError('transactions-import: accountId must be an id');
- }
-
- try {
- return await bankSync.reconcileTransactions(
- accountId,
- transactions,
- false,
- true,
- isPreview,
- opts?.defaultCleared,
- );
- } catch (err) {
- if (err instanceof TransactionError) {
- return {
- errors: [{ message: err.message }],
- added: [],
- updated: [],
- updatedPreview: [],
- };
- }
-
- throw err;
- }
- });
-});
-
-handlers['account-unlink'] = mutator(async function ({ id }) {
- const { bank: bankId } = await db.first(
- 'SELECT bank FROM accounts WHERE id = ?',
- [id],
- );
-
- if (!bankId) {
- return 'ok';
- }
-
- const accRow = await db.first('SELECT * FROM accounts WHERE id = ?', [id]);
-
- const isGoCardless = accRow.account_sync_source === 'goCardless';
-
- await db.updateAccount({
- id,
- account_id: null,
- bank: null,
- balance_current: null,
- balance_available: null,
- balance_limit: null,
- account_sync_source: null,
- });
-
- if (isGoCardless === false) {
- return;
- }
-
- const { count } = await db.first(
- 'SELECT COUNT(*) as count FROM accounts WHERE bank = ?',
- [bankId],
- );
-
- // No more accounts are associated with this bank. We can remove
- // it from GoCardless.
- const userToken = await asyncStorage.getItem('user-token');
- if (!userToken) {
- return 'ok';
- }
-
- if (count === 0) {
- const { bank_id: requisitionId } = await db.first(
- 'SELECT bank_id FROM banks WHERE id = ?',
- [bankId],
- );
- try {
- await post(
- getServer().GOCARDLESS_SERVER + '/remove-account',
- {
- requisitionId,
- },
- {
- 'X-ACTUAL-TOKEN': userToken,
- },
- );
- } catch (error) {
- console.log({ error });
- }
- }
-
- return 'ok';
-});
-
handlers['save-global-prefs'] = async function (prefs) {
if ('maxMonths' in prefs) {
await asyncStorage.setItem('max-months', '' + prefs.maxMonths);
@@ -2393,6 +1603,7 @@ app.combine(
rulesApp,
adminApp,
transactionsApp,
+ accountsApp,
);
function getDefaultDocumentDir() {
diff --git a/packages/loot-core/src/server/post.ts b/packages/loot-core/src/server/post.ts
index b521d87a14..90533596be 100644
--- a/packages/loot-core/src/server/post.ts
+++ b/packages/loot-core/src/server/post.ts
@@ -4,7 +4,7 @@ import { fetch } from '../platform/server/fetch';
import { PostError } from './errors';
import * as Platform from './platform';
-function throwIfNot200(res, text) {
+function throwIfNot200(res: Response, text: string) {
if (res.status !== 200) {
if (res.status === 500) {
throw new PostError(res.status === 500 ? 'internal' : text);
@@ -32,9 +32,14 @@ function throwIfNot200(res, text) {
}
}
-export async function post(url, data, headers = {}, timeout = null) {
- let text;
- let res;
+export async function post(
+ url: RequestInfo,
+ data: unknown,
+ headers = {},
+ timeout: number | null = null,
+) {
+ let text: string;
+ let res: Response;
try {
const controller = new AbortController();
@@ -57,14 +62,16 @@ export async function post(url, data, headers = {}, timeout = null) {
throwIfNot200(res, text);
+ let responseData;
+
try {
- res = JSON.parse(text);
+ responseData = JSON.parse(text);
} catch (err) {
// Something seriously went wrong. TODO handle errors
throw new PostError('parse-json', { meta: text });
}
- if (res.status !== 'ok') {
+ if (responseData.status !== 'ok') {
console.log(
'API call failed: ' +
url +
@@ -74,10 +81,12 @@ export async function post(url, data, headers = {}, timeout = null) {
JSON.stringify(res, null, 2),
);
- throw new PostError(res.description || res.reason || 'unknown');
+ throw new PostError(
+ responseData.description || responseData.reason || 'unknown',
+ );
}
- return res.data;
+ return responseData.data;
}
export async function del(url, data, headers = {}, timeout = null) {
diff --git a/packages/loot-core/src/types/handlers.d.ts b/packages/loot-core/src/types/handlers.d.ts
index 5fc1d912a7..1b5057bbcf 100644
--- a/packages/loot-core/src/types/handlers.d.ts
+++ b/packages/loot-core/src/types/handlers.d.ts
@@ -1,3 +1,4 @@
+import type { AccountHandlers } from '../server/accounts/app';
import type { AdminHandlers } from '../server/admin/types/handlers';
import type { BudgetHandlers } from '../server/budget/types/handlers';
import type { DashboardHandlers } from '../server/dashboard/types/handlers';
@@ -26,6 +27,7 @@ export interface Handlers
SchedulesHandlers,
TransactionHandlers,
AdminHandlers,
- ToolsHandlers {}
+ ToolsHandlers,
+ AccountHandlers {}
export type HandlerFunctions = Handlers[keyof Handlers];
diff --git a/packages/loot-core/src/types/models/bank.d.ts b/packages/loot-core/src/types/models/bank.d.ts
new file mode 100644
index 0000000000..6630e5670f
--- /dev/null
+++ b/packages/loot-core/src/types/models/bank.d.ts
@@ -0,0 +1,6 @@
+export type BankEntity = {
+ id: string;
+ name: string;
+ bank_id: string;
+ tombstone: 0 | 1;
+};
diff --git a/packages/loot-core/src/types/models/gocardless.d.ts b/packages/loot-core/src/types/models/gocardless.d.ts
index b1003e8836..365c3314c9 100644
--- a/packages/loot-core/src/types/models/gocardless.d.ts
+++ b/packages/loot-core/src/types/models/gocardless.d.ts
@@ -73,3 +73,11 @@ export type GoCardlessTransaction = {
valueDate?: string;
valueDateTime?: string;
};
+
+export type SyncServerGoCardlessAccount = {
+ institution: string;
+ account_id: string;
+ mask: string;
+ name: string;
+ official_name: string;
+};
diff --git a/packages/loot-core/src/types/models/simplefin.d.ts b/packages/loot-core/src/types/models/simplefin.d.ts
index cd76cb7ba1..31ff188fb9 100644
--- a/packages/loot-core/src/types/models/simplefin.d.ts
+++ b/packages/loot-core/src/types/models/simplefin.d.ts
@@ -17,3 +17,11 @@ export type SimpleFinAccount = {
export interface SimpleFinBatchSyncResponse {
[accountId: AccountEntity['account_id']]: BankSyncResponse;
}
+
+export type SyncServerSimpleFinAccount = {
+ account_id: string;
+ institution?: string;
+ orgDomain?: string;
+ orgId?: string;
+ name: string;
+};
diff --git a/packages/loot-core/src/types/server-handlers.d.ts b/packages/loot-core/src/types/server-handlers.d.ts
index 039ec849e0..80f1001502 100644
--- a/packages/loot-core/src/types/server-handlers.d.ts
+++ b/packages/loot-core/src/types/server-handlers.d.ts
@@ -1,5 +1,3 @@
-import { ImportTransactionsOpts } from '@actual-app/api';
-
import { Backup } from '../server/backups';
import { RemoteFile } from '../server/cloud-storage';
import { Node as SpreadsheetNode } from '../server/spreadsheet/spreadsheet';
@@ -7,12 +5,8 @@ import { Message } from '../server/sync';
import { Budget } from './budget';
import {
- AccountEntity,
CategoryEntity,
CategoryGroupEntity,
- GoCardlessToken,
- GoCardlessInstitution,
- SimpleFinAccount,
RuleEntity,
PayeeEntity,
} from './models';
@@ -124,121 +118,6 @@ export interface ServerHandlers {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
query: (query: Query) => Promise<{ data: any; dependencies: string[] }>;
- 'account-update': (arg: { id; name }) => Promise<unknown>;
-
- 'accounts-get': () => Promise<AccountEntity[]>;
-
- 'account-properties': (arg: {
- id;
- }) => Promise<{ balance: number; numTransactions: number }>;
-
- 'gocardless-accounts-link': (arg: {
- requisitionId;
- account;
- upgradingId;
- offBudget;
- }) => Promise<'ok'>;
-
- 'simplefin-accounts-link': (arg: {
- externalAccount;
- upgradingId;
- offBudget;
- }) => Promise<'ok'>;
-
- 'account-create': (arg: {
- name: string;
- balance?: number;
- offBudget?: boolean;
- closed?: 0 | 1;
- }) => Promise<string>;
-
- 'account-close': (arg: {
- id;
- transferAccountId?;
- categoryId?;
- forced?;
- }) => Promise<unknown>;
-
- 'account-reopen': (arg: { id }) => Promise<unknown>;
-
- 'account-move': (arg: { id; targetId }) => Promise<unknown>;
-
- 'secret-set': (arg: {
- name: string;
- value: string | null;
- }) => Promise<{ error?: string; reason?: string }>;
- 'secret-check': (arg: string) => Promise<string | { error?: string }>;
-
- 'gocardless-poll-web-token': (arg: {
- upgradingAccountId?: string | undefined;
- requisitionId: string;
- }) => Promise<
- { error: 'unknown' } | { error: 'timeout' } | { data: GoCardlessToken }
- >;
-
- 'gocardless-status': () => Promise<{ configured: boolean }>;
-
- 'simplefin-status': () => Promise<{ configured: boolean }>;
-
- 'simplefin-accounts': () => Promise<{
- accounts?: SimpleFinAccount[];
- error_code?: string;
- reason?: string;
- }>;
-
- 'simplefin-batch-sync': ({ ids }: { ids: string[] }) => Promise<
- {
- accountId: string;
- res: {
- errors;
- newTransactions;
- matchedTransactions;
- updatedAccounts;
- };
- }[]
- >;
-
- 'gocardless-get-banks': (country: string) => Promise<{
- data: GoCardlessInstitution[];
- error?: { reason: string };
- }>;
-
- 'gocardless-poll-web-token-stop': () => Promise<'ok'>;
-
- 'gocardless-create-web-token': (arg: {
- upgradingAccountId?: string | undefined;
- institutionId: string;
- accessValidForDays: number;
- }) => Promise<
- | {
- requisitionId: string;
- link: string;
- }
- | { error: 'unauthorized' }
- | { error: 'failed' }
- >;
-
- 'accounts-bank-sync': (arg: { ids?: AccountEntity['id'][] }) => Promise<{
- errors;
- newTransactions;
- matchedTransactions;
- updatedAccounts;
- }>;
-
- 'transactions-import': (arg: {
- accountId;
- transactions;
- isPreview;
- opts?: ImportTransactionsOpts;
- }) => Promise<{
- errors?: { message: string }[];
- added;
- updated;
- updatedPreview;
- }>;
-
- 'account-unlink': (arg: { id }) => Promise<'ok'>;
-
'save-global-prefs': (prefs) => Promise<'ok'>;
'load-global-prefs': () => Promise<GlobalPrefs>;
diff --git a/upcoming-release-notes/4227.md b/upcoming-release-notes/4227.md
new file mode 100644
index 0000000000..3ce168ffa1
--- /dev/null
+++ b/upcoming-release-notes/4227.md
@@ -0,0 +1,6 @@
+---
+category: Maintenance
+authors: [joel-jeremy]
+---
+
+Move accounts related server handlers from main.ts to server/accounts/app.ts