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] [TypeScript] Make runQuery generic #4238

Closed
wants to merge 4 commits into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,8 @@ export function AccountAutocomplete({
})
.sort(
(a, b) =>
a.closed - b.closed ||
a.offbudget - b.offbudget ||
(a.closed ? 1 : 0) - (b.closed ? 1 : 0) ||
(a.offbudget ? 1 : 0) - (b.offbudget ? 1 : 0) ||
a.sort_order - b.sort_order,
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ function makePayee(name: string, options?: { favorite: boolean }): PayeeEntity {
return {
id: name.toLowerCase() + '-id',
name,
favorite: options?.favorite ? 1 : 0,
favorite: options?.favorite ? true : false,
transfer_acct: undefined,
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ export function PayeeAutocomplete({
return filteredSuggestions;
}

return [{ id: 'new', favorite: 0, name: '' }, ...filteredSuggestions];
return [{ id: 'new', favorite: false, name: '' }, ...filteredSuggestions];
}, [commonPayees, payees, focusTransferPayees, accounts, hasPayeeInput]);

const dispatch = useDispatch();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,8 @@ function AccountList({
const { t } = useTranslation();
const failedAccounts = useFailedAccounts();
const syncingAccountIds = useSelector(state => state.account.accountsSyncing);
const onBudgetAccounts = accounts.filter(account => account.offbudget === 0);
const offBudgetAccounts = accounts.filter(account => account.offbudget === 1);
const onBudgetAccounts = accounts.filter(account => !account.offbudget);
const offBudgetAccounts = accounts.filter(account => account.offbudget);

return (
<Page
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export function CategoryGroupMenuModal({
};

const _onAddCategory = () => {
onAddCategory?.(group.id, group.is_income);
onAddCategory?.(group.id, !!group.is_income);
};

const _onEditNotes = () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ function needsCategory(
accounts: AccountEntity[],
) {
const acct = accounts.find(a => a.id === currentTransfer);
const isOffBudget = acct && acct.offbudget === 1;
const isOffBudget = acct && acct.offbudget;

// The user must select a category if transferring from a budgeted
// account to an off budget account
return account.offbudget === 0 && isOffBudget;
return !account.offbudget && isOffBudget;
}

type CloseAccountModalProps = {
Expand All @@ -49,7 +49,7 @@ export function CloseAccountModal({
canDelete,
}: CloseAccountModalProps) {
const { t } = useTranslation(); // Initialize translation hook
const accounts = useAccounts().filter(a => a.closed === 0);
const accounts = useAccounts().filter(a => !a.closed);
const { grouped: categoryGroups, list: categories } = useCategories();
const [loading, setLoading] = useState(false);
const [transferAccountId, setTransferAccountId] = useState('');
Expand Down
18 changes: 12 additions & 6 deletions packages/desktop-client/src/components/payees/ManagePayees.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -154,16 +154,16 @@ export const ManagePayees = ({
function onFavorite() {
const allFavorited = [...selected.items]
.map(id => payeesById[id].favorite)
.every(f => f === 1);
.every(f => f === true);
if (allFavorited) {
onBatchChange({
updated: [...selected.items].map(id => ({ id, favorite: 0 })),
updated: [...selected.items].map(id => ({ id, favorite: false })),
added: [],
deleted: [],
});
} else {
onBatchChange({
updated: [...selected.items].map(id => ({ id, favorite: 1 })),
updated: [...selected.items].map(id => ({ id, favorite: true })),
added: [],
deleted: [],
});
Expand All @@ -174,16 +174,22 @@ export const ManagePayees = ({
function onLearn() {
const allLearnCategories = [...selected.items]
.map(id => payeesById[id].learn_categories)
.every(f => f === 1);
.every(f => f === true);
if (allLearnCategories) {
onBatchChange({
updated: [...selected.items].map(id => ({ id, learn_categories: 0 })),
updated: [...selected.items].map(id => ({
id,
learn_categories: false,
})),
added: [],
deleted: [],
});
} else {
onBatchChange({
updated: [...selected.items].map(id => ({ id, learn_categories: 1 })),
updated: [...selected.items].map(id => ({
id,
learn_categories: true,
})),
added: [],
deleted: [],
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,13 +179,13 @@ export const PayeeTableRow = memo(
onDelete(id);
break;
case 'favorite':
onUpdate(id, 'favorite', payee.favorite ? 0 : 1);
onUpdate(id, 'favorite', payee.favorite ? false : true);
break;
case 'learn':
onUpdate(
id,
'learn_categories',
payee.learn_categories ? 0 : 1,
payee.learn_categories ? false : true,
);
break;
case 'view-rules':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ export function ScheduleDetails({ id, transaction }: ScheduleDetailsProps) {
amount: schedule._amount || 0,
amountOp: schedule._amountOp || 'isapprox',
date: schedule._date ?? null,
posts_transaction: action.schedule.posts_transaction ?? false,
posts_transaction: !!action.schedule.posts_transaction,
name: schedule.name ?? null,
},
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,16 @@ const payees: PayeeEntity[] = [
{
id: 'bob-id',
name: 'Bob',
favorite: 1,
favorite: true,
},
{
id: 'alice-id',
name: 'Alice',
favorite: 1,
favorite: true,
},
{
id: 'guy',
favorite: 0,
favorite: false,
name: 'This guy on the side of the road',
},
];
Expand Down
5 changes: 1 addition & 4 deletions packages/desktop-client/src/hooks/useClosedAccounts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,5 @@ import { useAccounts } from './useAccounts';

export function useClosedAccounts() {
const accounts = useAccounts();
return useMemo(
() => accounts.filter(account => account.closed === 1),
[accounts],
);
return useMemo(() => accounts.filter(account => account.closed), [accounts]);
}
5 changes: 1 addition & 4 deletions packages/desktop-client/src/hooks/useOffBudgetAccounts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ import { useAccounts } from './useAccounts';
export function useOffBudgetAccounts() {
const accounts = useAccounts();
return useMemo(
() =>
accounts.filter(
account => account.closed === 0 && account.offbudget === 1,
),
() => accounts.filter(account => !account.closed && account.offbudget),
[accounts],
);
}
5 changes: 1 addition & 4 deletions packages/desktop-client/src/hooks/useOnBudgetAccounts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ import { useAccounts } from './useAccounts';
export function useOnBudgetAccounts() {
const accounts = useAccounts();
return useMemo(
() =>
accounts.filter(
account => account.closed === 0 && account.offbudget === 0,
),
() => accounts.filter(account => !account.closed && !account.offbudget),
[accounts],
);
}
2 changes: 1 addition & 1 deletion packages/loot-core/src/client/accounts/accountsSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ export const syncAccounts = createAppAsyncThunk(
.sort((a, b) =>
a.offbudget === b.offbudget
? a.sort_order - b.sort_order
: a.offbudget - b.offbudget,
: (a.offbudget ? 1 : 0) - (b.offbudget ? 1 : 0),
)
.map(({ id }) => id);

Expand Down
2 changes: 1 addition & 1 deletion packages/loot-core/src/client/data-hooks/schedules.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ function loadStatuses(
s.id,
getStatus(
s.next_date,
s.completed,
!!s.completed,
hasTrans.has(s.id),
upcomingLength,
),
Expand Down
3 changes: 2 additions & 1 deletion packages/loot-core/src/client/transfer.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// @ts-strict-ignore
import * as db from '../server/db';
import { transactionModel } from '../server/models';

import * as transfer from './transfer';

Expand All @@ -22,7 +23,7 @@ async function createTransaction(account: string, amount: number, extra = {}) {
...extra,
};
transaction.id = await db.insertTransaction(transaction);
return await db.getTransaction(transaction.id);
return transactionModel.fromDbView(await db.getTransaction(transaction.id));
}

describe('Transfer', () => {
Expand Down
19 changes: 11 additions & 8 deletions packages/loot-core/src/mocks/budget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -461,20 +461,20 @@ async function fillOther(handlers, account, payees, groups) {
async function createBudget(accounts, payees, groups) {
const primaryAccount = accounts.find(a => (a.name = 'Bank of America'));
const earliestDate = (
await db.first(
await db.first<db.DbViewTransaction>(
`SELECT * FROM v_transactions t LEFT JOIN accounts a ON t.account = a.id
WHERE a.offbudget = 0 AND t.is_child = 0 ORDER BY date ASC LIMIT 1`,
)
).date;
const earliestPrimaryDate = (
await db.first(
await db.first<db.DbViewTransaction>(
`SELECT * FROM v_transactions t LEFT JOIN accounts a ON t.account = a.id
WHERE a.id = ? AND a.offbudget = 0 AND t.is_child = 0 ORDER BY date ASC LIMIT 1`,
[primaryAccount.id],
)
).date;

const start = monthUtils.monthFromDate(db.fromDateRepr(earliestDate));
const start = monthUtils.monthFromDate(db.fromDateRepr(Number(earliestDate)));
const end = monthUtils.currentMonth();
const months = monthUtils.rangeInclusive(start, end);

Expand Down Expand Up @@ -507,7 +507,7 @@ async function createBudget(accounts, payees, groups) {
for (const month of months) {
if (
month >=
monthUtils.monthFromDate(db.fromDateRepr(earliestPrimaryDate))
monthUtils.monthFromDate(db.fromDateRepr(Number(earliestPrimaryDate)))
) {
setBudget(month, category('Food'), 40000);
setBudget(month, category('Restaurants'), 30000);
Expand Down Expand Up @@ -549,7 +549,9 @@ async function createBudget(accounts, payees, groups) {
for (const month of months) {
if (
month >=
monthUtils.monthFromDate(db.fromDateRepr(earliestPrimaryDate)) &&
monthUtils.monthFromDate(
db.fromDateRepr(Number(earliestPrimaryDate)),
) &&
month <= monthUtils.currentMonth()
) {
const sheetName = monthUtils.sheetForMonth(month);
Expand Down Expand Up @@ -691,7 +693,7 @@ export async function createTestBudget(handlers: Handlers) {
for (const group of newCategoryGroups) {
const groupId = await handlers['category-group-create']({
name: group.name,
isIncome: group.is_income,
isIncome: !!group.is_income,
});

categoryGroups.push({
Expand All @@ -700,10 +702,11 @@ export async function createTestBudget(handlers: Handlers) {
categories: [],
});

for (const category of group.categories) {
for (const { is_income, hidden, ...category } of group.categories) {
const categoryId = await handlers['category-create']({
...category,
isIncome: category.is_income ? 1 : 0,
isIncome: !!is_income,
hidden: !!hidden,
groupId,
});

Expand Down
10 changes: 5 additions & 5 deletions packages/loot-core/src/mocks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ export function generateAccount(
name,
bankId: null,
bankName: null,
offbudget: offbudget ? 1 : 0,
offbudget: offbudget ? true : false,
sort_order: 0,
tombstone: 0,
closed: 0,
tombstone: false,
closed: false,
...emptySyncFields(),
};

Expand Down Expand Up @@ -96,12 +96,12 @@ export function generateCategoryGroups(
definition: Partial<NewCategoryGroupEntity>[],
): CategoryGroupEntity[] {
return definition.map(group => {
const g = generateCategoryGroup(group.name ?? '', group.is_income);
const g = generateCategoryGroup(group.name ?? '', !!group.is_income);

return {
...g,
categories: group.categories?.map(cat =>
generateCategory(cat.name, g.id, cat.is_income),
generateCategory(cat.name, g.id, !!cat.is_income),
),
};
});
Expand Down
2 changes: 1 addition & 1 deletion packages/loot-core/src/server/accounts/link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { v4 as uuidv4 } from 'uuid';
import * as db from '../db';

export async function findOrCreateBank(institution, requisitionId) {
const bank = await db.first(
const bank = await db.first<Pick<db.DbBank, 'id' | 'bank_id'>>(
'SELECT id, bank_id, name FROM banks WHERE bank_id = ?',
[requisitionId],
);
Expand Down
2 changes: 1 addition & 1 deletion packages/loot-core/src/server/accounts/parse-file.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ afterAll(() => {
});

async function getTransactions(accountId) {
return db.runQuery(
return db.runQuery<db.DbTransaction>(
'SELECT * FROM transactions WHERE acct = ?',
[accountId],
true,
Expand Down
6 changes: 3 additions & 3 deletions packages/loot-core/src/server/accounts/payees.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ 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 = await db.first<Pick<db.DbPayee, 'id'>>(
`SELECT id FROM payees WHERE UNICODE_LOWER(name) = ? AND tombstone = 0`,
[description.toLowerCase()],
);
Expand All @@ -17,14 +17,14 @@ export async function createPayee(description) {
}

export async function getStartingBalancePayee() {
let category = await db.first(`
let category = await db.first<db.DbCategory>(`
SELECT * FROM categories
WHERE is_income = 1 AND
LOWER(name) = 'starting balances' AND
tombstone = 0
`);
if (category === null) {
category = await db.first(
category = await db.first<db.DbCategory>(
'SELECT * FROM categories WHERE is_income = 1 AND tombstone = 0',
);
}
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 @@ -24,7 +24,7 @@ beforeEach(async () => {
});

function getAllTransactions() {
return db.all(
return db.all<db.DbViewTransactionInternal & { payee_name: string }>(
`SELECT t.*, p.name as payee_name
FROM v_transactions_internal t
LEFT JOIN payees p ON p.id = t.payee
Expand Down
Loading
Loading