Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Move transaction related server handlers from main.ts to server/transactions/app.ts #4221

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/loot-core/src/mocks/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import * as nativeFs from 'fs';

import * as fetchClient from '../platform/client/fetch';
import * as sqlite from '../platform/server/sqlite';
import * as rules from '../server/accounts/transaction-rules';
import * as db from '../server/db';
import {
enableGlobalMutations,
Expand All @@ -12,6 +11,7 @@ import {
import { setServer } from '../server/server-config';
import * as sheet from '../server/sheet';
import { setSyncingMode } from '../server/sync';
import * as rules from '../server/transactions/transaction-rules';
import { updateVersion } from '../server/update';
import { resetTracer, tracer } from '../shared/test-helpers';

Expand Down
2 changes: 1 addition & 1 deletion packages/loot-core/src/server/accounts/sync.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import * as db from '../db';
import { loadMappings } from '../db/mappings';
import { post } from '../post';
import { getServer } from '../server-config';
import { loadRules, insertRule } from '../transactions/transaction-rules';

import { reconcileTransactions, addTransactions } from './sync';
import { loadRules, insertRule } from './transaction-rules';

jest.mock('../../shared/months', () => ({
...jest.requireActual('../../shared/months'),
Expand Down
4 changes: 2 additions & 2 deletions packages/loot-core/src/server/accounts/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ import { runMutator } from '../mutators';
import { post } from '../post';
import { getServer } from '../server-config';
import { batchMessages } from '../sync';
import { batchUpdateTransactions } from '../transactions';
import { runRules } from '../transactions/transaction-rules';

import { getStartingBalancePayee } from './payees';
import { title } from './title';
import { runRules } from './transaction-rules';
import { batchUpdateTransactions } from './transactions';

function BankSyncError(type: string, code: string, details?: object) {
return { type: 'BankSyncError', category: type, code, details };
Expand Down
2 changes: 2 additions & 0 deletions packages/loot-core/src/server/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -461,12 +461,14 @@ handlers['api/transactions-export'] = async function ({
transactions,
categoryGroups,
payees,
accounts,
}) {
checkFileOpen();
return handlers['transactions-export']({
transactions,
categoryGroups,
payees,
accounts,
});
};

Expand Down
2 changes: 1 addition & 1 deletion packages/loot-core/src/server/filters/app.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// @ts-strict-ignore
import { v4 as uuidv4 } from 'uuid';

import { parseConditionsOrActions } from '../accounts/transaction-rules';
import { createApp } from '../app';
import * as db from '../db';
import { requiredFields } from '../models';
import { mutator } from '../mutators';
import { parseConditionsOrActions } from '../transactions/transaction-rules';
import { undoable } from '../undo';

import { FiltersHandlers } from './types/handlers';
Expand Down
2 changes: 2 additions & 0 deletions packages/loot-core/src/server/main.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// @ts-strict-ignore
import { getClock, deserializeClock } from '@actual-app/crdt';
import { v4 as uuidv4 } from 'uuid';

import { expectSnapshotWithDiffer } from '../mocks/util';
import * as connection from '../platform/server/connection';
Expand Down Expand Up @@ -175,6 +176,7 @@ describe('Budget', () => {
// budgets for the earlier months
await db.runQuery("INSERT INTO accounts (id, name) VALUES ('one', 'boa')");
await runHandler(handlers['transaction-add'], {
id: uuidv4(),
date: '2016-05-06',
amount: 50,
account: 'one',
Expand Down
70 changes: 4 additions & 66 deletions packages/loot-core/src/server/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,15 @@ 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, Query } from '../shared/query';
import { q } from '../shared/query';
import { amountToInteger, stringToInteger } from '../shared/util';
import { type Budget } from '../types/budget';
import { Handlers } from '../types/handlers';
import { OpenIdConfig } from '../types/models/openid';

import { exportToCSV, exportQueryToCSV } from './accounts/export-to-csv';
import * as link from './accounts/link';
import { parseFile } from './accounts/parse-file';
import { getStartingBalancePayee } from './accounts/payees';
import * as bankSync from './accounts/sync';
import * as rules from './accounts/transaction-rules';
import { batchUpdateTransactions } from './accounts/transactions';
import { app as adminApp } from './admin/app';
import { installAPI } from './api';
import { runQuery as aqlQuery } from './aql';
Expand Down Expand Up @@ -73,6 +69,8 @@ import {
} from './sync';
import * as syncMigrations from './sync/migrate';
import { app as toolsApp } from './tools/app';
import { app as transactionsApp } from './transactions/app';
import * as rules from './transactions/transaction-rules';
import { withUndo, clearUndo, undo, redo } from './undo';
import { updateVersion } from './update';
import {
Expand Down Expand Up @@ -108,74 +106,13 @@ handlers['redo'] = mutator(function () {
return redo();
});

handlers['transactions-batch-update'] = mutator(async function ({
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These have been moved to server/transactions/app.ts

added,
deleted,
updated,
learnCategories,
}) {
return withUndo(async () => {
const result = await batchUpdateTransactions({
added,
updated,
deleted,
learnCategories,
});

return result;
});
});

handlers['transaction-add'] = mutator(async function (transaction) {
await handlers['transactions-batch-update']({ added: [transaction] });
return {};
});

handlers['transaction-update'] = mutator(async function (transaction) {
await handlers['transactions-batch-update']({ updated: [transaction] });
return {};
});

handlers['transaction-delete'] = mutator(async function (transaction) {
await handlers['transactions-batch-update']({ deleted: [transaction] });
return {};
});

handlers['transactions-parse-file'] = async function ({ filepath, options }) {
return parseFile(filepath, options);
};

handlers['transactions-export'] = async function ({
transactions,
accounts,
categoryGroups,
payees,
}) {
return exportToCSV(transactions, accounts, categoryGroups, payees);
};

handlers['transactions-export-query'] = async function ({ query: queryState }) {
return exportQueryToCSV(new Query(queryState));
};

handlers['get-categories'] = async function () {
return {
grouped: await db.getCategoriesGrouped(),
list: await db.getCategories(),
};
};

handlers['get-earliest-transaction'] = async function () {
const { data } = await aqlQuery(
q('transactions')
.options({ splits: 'none' })
.orderBy({ date: 'asc' })
.select('*')
.limit(1),
);
return data[0] || null;
};

handlers['get-budget-bounds'] = async function () {
return budget.createAllBudgets();
};
Expand Down Expand Up @@ -2437,6 +2374,7 @@ app.combine(
reportsApp,
rulesApp,
adminApp,
transactionsApp,
);

function getDefaultDocumentDir() {
Expand Down
5 changes: 3 additions & 2 deletions packages/loot-core/src/server/rules/app.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
// @ts-strict-ignore
import { type RuleEntity } from '../../types/models';
import { Condition, Action, rankRules } from '../accounts/rules';
import * as rules from '../accounts/transaction-rules';
import { createApp } from '../app';
import { RuleError } from '../errors';
import { mutator } from '../mutators';
import { batchMessages } from '../sync';
import * as rules from '../transactions/transaction-rules';
import { undoable } from '../undo';

import { RulesHandlers } from './types/handlers';

import { Condition, Action, rankRules } from '.';

function validateRule(rule: Partial<RuleEntity>) {
// Returns an array of errors, the array is the same link as the
// passed-in `array`, or null if there are no errors
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
Action,
Rule,
RuleIndexer,
} from './rules';
} from '.';

describe('Condition', () => {
test('parses date formats correctly', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/loot-core/src/server/rules/types/handlers.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// @ts-strict-ignore
import { type Action } from '..';
import {
type RuleEntity,
type TransactionEntity,
type RuleActionEntity,
} from '../../../types/models';
import { type Action } from '../../accounts/rules';

type ValidationError = {
conditionErrors: string[];
Expand Down
2 changes: 1 addition & 1 deletion packages/loot-core/src/server/schedules/app.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import MockDate from 'mockdate';

import { q } from '../../shared/query';
import { getNextDate } from '../../shared/schedules';
import { loadRules, updateRule } from '../accounts/transaction-rules';
import { runQuery as aqlQuery } from '../aql';
import { loadMappings } from '../db/mappings';
import { loadRules, updateRule } from '../transactions/transaction-rules';

import {
updateConditions,
Expand Down
14 changes: 7 additions & 7 deletions packages/loot-core/src/server/schedules/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,21 @@ import {
getStatus,
recurConfigToRSchedule,
} from '../../shared/schedules';
import { Rule } from '../accounts/rules';
import { addTransactions } from '../accounts/sync';
import {
getRules,
insertRule,
ruleModel,
updateRule,
} from '../accounts/transaction-rules';
import { createApp } from '../app';
import { runQuery as aqlQuery } from '../aql';
import * as db from '../db';
import { toDateRepr } from '../models';
import { mutator, runMutator } from '../mutators';
import * as prefs from '../prefs';
import { Rule } from '../rules';
import { addSyncListener, batchMessages } from '../sync';
import {
getRules,
insertRule,
ruleModel,
updateRule,
} from '../transactions/transaction-rules';
import { undoable } from '../undo';
import { Schedule as RSchedule } from '../util/rschedule';

Expand Down
2 changes: 1 addition & 1 deletion packages/loot-core/src/server/schedules/find-schedules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import { q } from '../../shared/query';
import { getApproxNumberThreshold } from '../../shared/rules';
import { recurConfigToRSchedule } from '../../shared/schedules';
import { groupBy } from '../../shared/util';
import { conditionsToAQL } from '../accounts/transaction-rules';
import { runQuery as aqlQuery } from '../aql';
import * as db from '../db';
import { fromDateRepr } from '../models';
import { conditionsToAQL } from '../transactions/transaction-rules';
import { Schedule as RSchedule } from '../util/rschedule';

import { SchedulesHandlers } from './types/handlers';
Expand Down
2 changes: 1 addition & 1 deletion packages/loot-core/src/server/tools/app.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// @ts-strict-ignore
import { q } from '../../shared/query';
import { batchUpdateTransactions } from '../accounts/transactions';
import { createApp } from '../app';
import { runQuery } from '../aql';
import * as db from '../db';
import { runMutator } from '../mutators';
import { batchUpdateTransactions } from '../transactions';

import { ToolsHandlers } from './types/handlers';

Expand Down
Loading
Loading