Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master' into reportsOptimize
Browse files Browse the repository at this point in the history
  • Loading branch information
carkom committed Dec 6, 2023
2 parents c844907 + cde4551 commit ab40197
Show file tree
Hide file tree
Showing 10 changed files with 168 additions and 84 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ export const chartTheme = {
},
};

export function getColorScale(name) {
const scales = {
export function getColorScale(name: string): string[] {
const scales: Record<string, string[]> = {
grayscale: ['#cccccc', '#969696', '#636363', '#252525'],
qualitative: [
'#45B29D', //Dark Teal
Expand Down
36 changes: 0 additions & 36 deletions packages/desktop-client/src/components/reports/util.js

This file was deleted.

57 changes: 57 additions & 0 deletions packages/desktop-client/src/components/reports/util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { runQuery } from 'loot-core/src/client/query-helpers';
import type { Query } from 'loot-core/src/shared/query';

export function fromDateRepr(date: string): string {
return date.slice(0, 7);
}

export async function runAll(
queries: Query[],
cb: (data) => void,
): Promise<void> {
const data = await Promise.all(
queries.map(q => {
return runQuery(q).then(({ data }) => data);
}),
);
cb(data);
}

export function index<
T extends Record<string, string | number>,
K extends keyof T,
>(data: T[], field: K) {
const result: Record<string | number, T> = {};
data.forEach(item => {
const key = item[field];
result[key] = item;
});
return result;
}

export function indexStack<

Check warning on line 32 in packages/desktop-client/src/components/reports/util.ts

View workflow job for this annotation

GitHub Actions / lint

exported declaration 'indexStack' not used within other modules
T extends Record<string, string | number>,
K extends keyof T,
>(data: T[], fieldName: K, field: K) {
const result: Record<string | number, T[K]> = {};
data.forEach(item => {
result[item[fieldName]] = item[field];
});
return result;
}

export function indexCashFlow<
T extends { date: string; isTransfer: boolean; amount: number },
>(data: T[], date: string, isTransfer: string) {
const results = {};
data.forEach(item => {
const findExisting = results[item.date]
? results[item.date][item.isTransfer]
? results[item.date][item.isTransfer]
: 0
: 0;
const result = { [item[isTransfer]]: item.amount + findExisting };
results[item[date]] = { ...results[item[date]], ...result };
});
return results;
}
38 changes: 24 additions & 14 deletions packages/loot-core/src/mocks/budget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,26 @@ import q from '../shared/query';
import type {
CategoryGroupEntity,
PayeeEntity,
TransactionEntity,
NewTransactionEntity,
} from '../types/models';

import random from './random';

function pickRandom(list) {
type MockPayeeEntity = PayeeEntity & { bill?: boolean };

function pickRandom<T>(list: T[]): T {
return list[Math.floor(random() * list.length) % list.length];
}

function number(start, end) {
function number(start: number, end: number) {
return start + (end - start) * random();
}

function integer(start, end) {
function integer(start: number, end: number) {
return Math.round(number(start, end));
}

function findMin(items, field) {
function findMin<T, K extends keyof T>(items: T[], field: K) {
let item = items[0];
for (let i = 0; i < items.length; i++) {
if (items[i][field] < item[field]) {
Expand All @@ -39,17 +41,20 @@ function findMin(items, field) {
return item;
}

function getStartingBalanceCat(categories) {
function getStartingBalanceCat(categories: CategoryGroupEntity[]) {
return categories.find(c => c.name === 'Starting Balances').id;
}

function extractCommonThings(payees, groups) {
function extractCommonThings(
payees: MockPayeeEntity[],
groups: CategoryGroupEntity[],
) {
const incomePayee = payees.find(p => p.name === 'Deposit');
const expensePayees = payees.filter(
p => p.name !== 'Deposit' && p.name !== 'Starting Balance',
);
const expenseGroup = groups.find(g => g.is_income === 0);
const incomeGroup = groups.find(g => g.is_income === 1);
const expenseGroup = groups.find(g => !g.is_income);
const incomeGroup = groups.find(g => g.is_income);
const categories = expenseGroup.categories.filter(
c =>
[
Expand All @@ -73,7 +78,12 @@ function extractCommonThings(payees, groups) {
};
}

async function fillPrimaryChecking(handlers, account, payees, groups) {
async function fillPrimaryChecking(
handlers,
account,
payees: MockPayeeEntity[],
groups: CategoryGroupEntity[],
) {
const {
incomePayee,
expensePayees,
Expand Down Expand Up @@ -107,7 +117,7 @@ async function fillPrimaryChecking(handlers, account, payees, groups) {
amount = integer(0, random() < 0.05 ? -8000 : -700);
}

const transaction: TransactionEntity = {
const transaction: NewTransactionEntity = {
amount,
payee: payee.id,
account: account.id,
Expand All @@ -129,7 +139,7 @@ async function fillPrimaryChecking(handlers, account, payees, groups) {
amount: transaction.amount - a * 2,
category: pick(),
},
] as TransactionEntity[];
];
}
}

Expand Down Expand Up @@ -391,7 +401,7 @@ async function fillOther(handlers, account, payees, groups) {
const numTransactions = integer(3, 6);
const category = incomeGroup.categories.find(c => c.name === 'Income');

const transactions: TransactionEntity[] = [
const transactions: NewTransactionEntity[] = [
{
amount: integer(3250, 3700) * 100 * 100,
payee: payees.find(p => p.name === 'Starting Balance').id,
Expand Down Expand Up @@ -585,7 +595,7 @@ export async function createTestBudget(handlers) {
}),
);

const payees: Array<PayeeEntity & { bill?: boolean }> = [
const payees: Array<MockPayeeEntity> = [
{ name: 'Starting Balance' },
{ name: 'Kroger' },
{ name: 'Publix' },
Expand Down
8 changes: 6 additions & 2 deletions packages/loot-core/src/server/accounts/transaction-rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ import {
getApproxNumberThreshold,
} from '../../shared/rules';
import { partitionByField, fastSetMerge } from '../../shared/util';
import { type RuleActionEntity, type RuleEntity } from '../../types/models';
import {
type TransactionEntity,
type RuleActionEntity,
type RuleEntity,
} from '../../types/models';
import { schemaConfig } from '../aql';
import * as db from '../db';
import { getMappings } from '../db/mappings';
Expand Down Expand Up @@ -682,7 +686,7 @@ export async function updateCategoryRules(transactions) {

// Also look 180 days in the future to get any future transactions
// (this might change when we think about scheduled transactions)
let register = await db.all(
let register: TransactionEntity[] = await db.all(
`SELECT t.* FROM v_transactions t
LEFT JOIN accounts a ON a.id = t.account
WHERE date >= ? AND date <= ? AND is_parent = 0 AND a.closed = 0
Expand Down
29 changes: 21 additions & 8 deletions packages/loot-core/src/shared/transactions.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { v4 as uuidv4 } from 'uuid';

import { type TransactionEntity } from '../types/models';

import { last, diffItems, applyChanges } from './util';

export function isPreviewId(id) {
Expand Down Expand Up @@ -80,8 +82,8 @@ function getSplit(transactions, parentIndex) {
return split;
}

export function ungroupTransactions(transactions) {
const x = transactions.reduce((list, parent) => {
export function ungroupTransactions(transactions: TransactionEntity[]) {
return transactions.reduce<TransactionEntity[]>((list, parent) => {
const { subtransactions, ...trans } = parent;
const _subtransactions = subtransactions || [];

Expand All @@ -92,25 +94,31 @@ export function ungroupTransactions(transactions) {
}
return list;
}, []);
return x;
}

function groupTransaction(split) {
return { ...split[0], subtransactions: split.slice(1) };
}

export function ungroupTransaction(split) {
export function ungroupTransaction(split: TransactionEntity | null) {
if (split == null) {
return null;
}
return ungroupTransactions([split]);
}

export function applyTransactionDiff(groupedTrans, diff) {
export function applyTransactionDiff(
groupedTrans: Parameters<typeof ungroupTransaction>[0],
diff: Parameters<typeof applyChanges>[0],
) {
return groupTransaction(applyChanges(diff, ungroupTransaction(groupedTrans)));
}

function replaceTransactions(transactions, id, func) {
function replaceTransactions(
transactions: TransactionEntity[],
id: string,
func: (transaction: TransactionEntity) => TransactionEntity,
) {
const idx = transactions.findIndex(t => t.id === id);
const trans = transactions[idx];
const transactionsCopy = [...transactions];
Expand All @@ -127,7 +135,9 @@ function replaceTransactions(transactions, id, func) {
}

const split = getSplit(transactions, parentIndex);
let grouped = func(groupTransaction(split));
let grouped: TransactionEntity | { id: string; _deleted: boolean } = func(
groupTransaction(split),
);
const newSplit = ungroupTransaction(grouped);

let diff;
Expand Down Expand Up @@ -159,7 +169,10 @@ function replaceTransactions(transactions, id, func) {
}
}

export function addSplitTransaction(transactions, id) {
export function addSplitTransaction(
transactions: TransactionEntity[],
id: string,
) {
return replaceTransactions(transactions, id, trans => {
if (!trans.is_parent) {
return trans;
Expand Down
Loading

0 comments on commit ab40197

Please sign in to comment.