Skip to content

Commit

Permalink
Update db.first calls
Browse files Browse the repository at this point in the history
  • Loading branch information
joel-jeremy committed Feb 21, 2025
1 parent 19fa629 commit e06d75a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 15 deletions.
29 changes: 14 additions & 15 deletions packages/loot-core/src/server/accounts/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,9 @@ 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 {
Expand Down Expand Up @@ -77,19 +75,19 @@ async function getAccountBalance({
id: string;
cutoff: string | Date;
}) {
const { balance }: { balance: number } = await db.first(
const { balance } = await db.first<{ balance: number }>(

Check failure on line 78 in packages/loot-core/src/server/accounts/app.ts

View workflow job for this annotation

GitHub Actions / typecheck

Property 'balance' does not exist on type '{ balance: number; } | null'.
'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(
const { balance } = await db.first<{ balance: number }>(

Check failure on line 86 in packages/loot-core/src/server/accounts/app.ts

View workflow job for this annotation

GitHub Actions / typecheck

Property 'balance' does not exist on type '{ balance: number; } | null'.
'SELECT sum(amount) as balance FROM transactions WHERE acct = ? AND isParent = 0 AND tombstone = 0',
[id],
);
const { count }: { count: number } = await db.first(
const { count } = await db.first<{ count: number }>(

Check failure on line 90 in packages/loot-core/src/server/accounts/app.ts

View workflow job for this annotation

GitHub Actions / typecheck

Property 'count' does not exist on type '{ count: number; } | null'.
'SELECT count(id) as count FROM transactions WHERE acct = ? AND tombstone = 0',
[id],
);
Expand All @@ -112,7 +110,7 @@ async function linkGoCardlessAccount({
const bank = await link.findOrCreateBank(account.institution, requisitionId);

if (upgradingId) {
const accRow: AccountEntity = await db.first(
const accRow = await db.first<db.DbAccount>(
'SELECT * FROM accounts WHERE id = ?',
[upgradingId],
);
Expand Down Expand Up @@ -178,7 +176,7 @@ async function linkSimpleFinAccount({
);

if (upgradingId) {
const accRow: AccountEntity = await db.first(
const accRow = await db.first<db.DbAccount>(
'SELECT * FROM accounts WHERE id = ?',
[upgradingId],
);
Expand Down Expand Up @@ -278,7 +276,7 @@ async function closeAccount({
await unlinkAccount({ id });

return withUndo(async () => {
const account: AccountEntity = await db.first(
const account = await db.first<db.DbAccount>(
'SELECT * FROM accounts WHERE id = ? AND tombstone = 0',
[id],
);
Expand All @@ -303,7 +301,7 @@ async function closeAccount({
true,
);

const { id: payeeId }: Pick<PayeeEntity, 'id'> = await db.first(
const { id: payeeId } = await db.first<Pick<db.DbPayee, 'id'>>(

Check failure on line 304 in packages/loot-core/src/server/accounts/app.ts

View workflow job for this annotation

GitHub Actions / typecheck

Property 'id' does not exist on type 'Pick<DbPayee, "id"> | null'.
'SELECT id FROM payees WHERE transfer_acct = ?',
[id],
);
Expand Down Expand Up @@ -340,7 +338,7 @@ async function closeAccount({
// 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(
const { id: payeeId } = await db.first<Pick<db.DbPayee, 'id'>>(

Check failure on line 341 in packages/loot-core/src/server/accounts/app.ts

View workflow job for this annotation

GitHub Actions / typecheck

Property 'id' does not exist on type 'Pick<DbPayee, "id"> | null'.
'SELECT id FROM payees WHERE transfer_acct = ?',
[transferAccountId],
);
Expand Down Expand Up @@ -942,7 +940,7 @@ async function importTransactions({
}

async function unlinkAccount({ id }: { id: AccountEntity['id'] }) {
const { bank: bankId }: Pick<AccountEntity, 'bank'> = await db.first(
const { bank: bankId } = await db.first<Pick<db.DbAccount, 'bank'>>(

Check failure on line 943 in packages/loot-core/src/server/accounts/app.ts

View workflow job for this annotation

GitHub Actions / typecheck

Property 'bank' does not exist on type 'Pick<DbAccount, "bank"> | null'.
'SELECT bank FROM accounts WHERE id = ?',
[id],
);
Expand All @@ -951,7 +949,7 @@ async function unlinkAccount({ id }: { id: AccountEntity['id'] }) {
return 'ok';
}

const accRow: AccountEntity = await db.first(
const accRow = await db.first<db.DbAccount>(
'SELECT * FROM accounts WHERE id = ?',
[id],
);
Expand All @@ -972,7 +970,7 @@ async function unlinkAccount({ id }: { id: AccountEntity['id'] }) {
return;
}

const { count }: { count: number } = await db.first(
const { count } = await db.first<{ count: number }>(

Check failure on line 973 in packages/loot-core/src/server/accounts/app.ts

View workflow job for this annotation

GitHub Actions / typecheck

Property 'count' does not exist on type '{ count: number; } | null'.
'SELECT COUNT(*) as count FROM accounts WHERE bank = ?',
[bankId],
);
Expand All @@ -985,8 +983,9 @@ async function unlinkAccount({ id }: { id: AccountEntity['id'] }) {
}

if (count === 0) {
const { bank_id: requisitionId }: Pick<BankEntity, 'bank_id'> =
await db.first('SELECT bank_id FROM banks WHERE id = ?', [bankId]);
const { bank_id: requisitionId } = await db.first<
Pick<db.DbBank, 'bank_id'>
>('SELECT bank_id FROM banks WHERE id = ?', [bankId]);

const serverConfig = getServer();
if (!serverConfig) {
Expand Down
1 change: 1 addition & 0 deletions packages/loot-core/src/types/models/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ export type * from './schedule';
export type * from './transaction';
export type * from './transaction-filter';
export type * from './user';
export type * from './bank';

0 comments on commit e06d75a

Please sign in to comment.