All files / src/modules/implementations/queries/list-definitions accounts.js

100% Statements 27/27
100% Branches 6/6
100% Functions 2/2
100% Lines 27/27

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 2851x 51x 51x 783x 783x 783x 783x 783x 95x 95x 95x 95x 95x 95x 95x 95x 95x 95x 95x 95x 95x 95x 95x 95x 95x 783x 51x  
'use strict';
Object.defineProperty(exports, "__esModule", { value: true });
module.exports = ({ apiOptions: { accounts, usage, parseOptions }, accountFilter: { accountFilter }, external: { zod: { z } }, }) => {
    const optionsSchema = z.strictObject({
        accounts,
        usage,
    });
    function queryAccounts(database, options, _caches) {
        const { accounts, usage } = parseOptions(optionsSchema, options, 'accounts');
        const filter = accounts.length === 0
            ? { sql: '1 = 1', parameters: [] }
            : accountFilter('declarations.name', accounts);
        return database.prepare(`
      SELECT
        declarations.name AS account,
        declarations.comment,
        declarations.used,
        COUNT(DISTINCT postings.transaction_id) AS transactionCount
      FROM account_declarations AS declarations
      LEFT JOIN postings ON postings.account = declarations.name
      WHERE (? = 'all' OR declarations.used = (? = 'used')) AND ${filter.sql}
      GROUP BY declarations.entry_id
      ORDER BY declarations.name || char(1114111)
    `).all(usage, usage, ...filter.parameters).map((row) => ({ ...row, used: Boolean(row.used) }));
    }
    return { inputSchema: optionsSchema, execute: queryAccounts };
};