Skip to content

Commit

Permalink
Payee model mapping methods
Browse files Browse the repository at this point in the history
  • Loading branch information
joel-jeremy committed Feb 24, 2025
1 parent d2e5234 commit 8f73264
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 7 deletions.
8 changes: 4 additions & 4 deletions packages/loot-core/src/server/db/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -593,7 +593,7 @@ export async function mergePayees(
});
}

export function getPayees() {
export function getPayees(): Promise<DbPayee[]> {
return all(`
SELECT p.*, COALESCE(a.name, p.name) AS name FROM payees p
LEFT JOIN accounts a ON (p.transfer_acct = a.id AND a.tombstone = 0)
Expand All @@ -602,7 +602,7 @@ export function getPayees() {
`);
}

export function getCommonPayees() {
export function getCommonPayees(): Promise<DbPayee[]> {
const twelveWeeksAgo = toDateRepr(
monthUtils.subWeeks(monthUtils.currentDate(), 12),
);
Expand Down Expand Up @@ -644,11 +644,11 @@ const orphanedPayeesQuery = `
`;
/* eslint-enable rulesdir/typography */

export function syncGetOrphanedPayees() {
export function syncGetOrphanedPayees(): Promise<Array<Pick<DbPayee, 'id'>>> {
return all(orphanedPayeesQuery);
}

export async function getOrphanedPayees() {
export async function getOrphanedPayees(): Promise<Array<DbPayee['id']>> {
const rows = await all(orphanedPayeesQuery);
return rows.map(row => row.id);
}
Expand Down
23 changes: 23 additions & 0 deletions packages/loot-core/src/server/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ import {
PayeeEntity,
} from '../types/models';

import {
convertForInsert,
convertForUpdate,
convertFromSelect,
schema,
schemaConfig,
} from './aql';
import { DbPayee } from './db';
import { ValidationError } from './errors';

export function requiredFields<T extends object, K extends keyof T>(
Expand Down Expand Up @@ -98,4 +106,19 @@ export const payeeModel = {
requiredFields('payee', payee, ['name'], update);
return payee;
},
fromDb(payee: DbPayee): PayeeEntity {
return convertFromSelect(
schema,
schemaConfig,
'payees',
payee,
) as PayeeEntity;
},
toDb(payee: PayeeEntity, { update }: { update?: boolean } = {}): DbPayee {
return (
update
? convertForUpdate(schema, schemaConfig, 'payees', payee)
: convertForInsert(schema, schemaConfig, 'payees', payee)
) as DbPayee;
},
};
9 changes: 6 additions & 3 deletions packages/loot-core/src/server/payees/app.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { PayeeEntity, RuleEntity } from '../../types/models';
import { createApp } from '../app';
import * as db from '../db';
import { payeeModel } from '../models';
import { mutator } from '../mutators';
import { batchMessages } from '../sync';
import * as rules from '../transactions/transaction-rules';
Expand Down Expand Up @@ -33,20 +34,22 @@ async function createPayee({ name }: { name: PayeeEntity['name'] }) {
return db.insertPayee({ name });
}

// Server must return AQL entities not the raw DB data
async function getCommonPayees(): Promise<PayeeEntity[]> {
return await db.getCommonPayees();
return (await db.getCommonPayees()).map(payeeModel.fromDb);
}

// Server must return AQL entities not the raw DB data
async function getPayees(): Promise<PayeeEntity[]> {
return await db.getPayees();
return (await db.getPayees()).map(payeeModel.fromDb);
}

async function getOrphanedPayees(): Promise<Array<Pick<PayeeEntity, 'id'>>> {
return await db.syncGetOrphanedPayees();
}

async function getPayeeRuleCounts() {
const payeeCounts: Record<string, number> = {};
const payeeCounts: Record<PayeeEntity['id'], number> = {};

rules.iterateIds(rules.getRules(), 'payee', (rule, id) => {
if (payeeCounts[id] == null) {
Expand Down

0 comments on commit 8f73264

Please sign in to comment.