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

100% Statements 211/211
93.05% Branches 67/72
100% Functions 4/4
100% Lines 211/211

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 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 21251x 51x 51x 783x 783x 29x 29x 783x 783x 783x 783x 783x 783x 783x 783x 783x 783x 783x 783x 783x 60x 60x 60x 60x 60x 60x 60x 60x 60x 60x 60x 10x 10x 10x 60x 8x 8x 8x 8x 8x 8x 8x 8x 8x 60x 60x 60x 60x 60x 60x 60x 60x 60x 60x 60x 60x 60x 60x 60x 60x 60x 60x 60x 60x 60x 60x 60x 60x 60x 60x 60x 39x 39x 39x 39x 40x 40x 39x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 39x 39x 1341x 1341x 1341x 1341x 1341x 1341x 1341x 1341x 1341x 1341x 1341x 1341x 1341x 1341x 1341x 1341x 1341x 26x 26x 26x 1341x 1341x 1341x 1341x 16x 16x 16x 1341x 1341x 1341x 1341x 1341x 1341x 1341x 1341x 1341x 39x 40x 40x 40x 40x 40x 40x 40x 39x 39x 40x 40x 40x 40x 40x 40x 40x 40x 39x 39x 40x 40x 40x 40x 40x 40x 40x 39x 39x 40x 40x 40x 40x 40x 40x 40x 40x 39x 39x 39x 38x 38x 38x 38x 38x 38x 38x 39x 17x 17x 17x 17x 17x 17x 17x 17x 17x 39x 43x 43x 60x 60x 60x 60x 668x 668x 60x 60x 60x 783x 51x  
'use strict';
Object.defineProperty(exports, "__esModule", { value: true });
module.exports = ({ apiOptions: { accounts, parseOptions }, accountFilter: { accountFilter }, external: { zod: { z } }, }) => {
    const positiveInteger = z.union([z.string(), z.number()])
        .refine((value) => {
        const number = Number(value);
        return Number.isSafeInteger(number) && number > 0 && String(number) === String(value);
    }, { error: 'must be a positive integer' })
        .transform(Number);
    const optionsSchema = z.strictObject({
        accounts,
        id: positiveInteger.optional(),
        order: z.enum(['newest', 'oldest'], { error: 'must be newest or oldest' })
            .default('oldest'),
        page: positiveInteger.optional(),
        pageSize: positiveInteger.optional(),
    }).refine(({ page, pageSize }) => (page === undefined) === (pageSize === undefined), {
        error: 'page and pageSize must be provided together',
    });
    function queryTransactions(database, options, _caches) {
        const { accounts, id, order, page, pageSize } = parseOptions(optionsSchema, options, 'transactions');
        const filters = [`EXISTS (
      SELECT 1
      FROM postings AS nonzero_postings
      JOIN resolved_posting_amounts AS nonzero_amounts
        ON nonzero_amounts.posting_id = nonzero_postings.id
      WHERE nonzero_postings.transaction_id = transactions.entry_id
        AND decimal_cmp(nonzero_amounts.amount_quantity, '0') != 0
    )`];
        const filterParameters = [];
        if (id !== undefined) {
            filters.push('transactions.entry_id = ?');
            filterParameters.push(id);
        }
        if (accounts.length > 0) {
            const accountMatch = accountFilter('matching_postings.account', accounts);
            filters.push(`EXISTS (
        SELECT 1
        FROM postings AS matching_postings
        WHERE matching_postings.transaction_id = transactions.entry_id
          AND ${accountMatch.sql}
      )`);
            filterParameters.push(...accountMatch.parameters);
        }
        const filter = filters.length === 0 ? '' : `WHERE ${filters.join(' AND ')}`;
        const totalTransactions = database.prepare(`
      SELECT COUNT(*) AS count
      FROM transactions
      ${filter}
    `).get(...filterParameters).count;
        const totalPages = page === undefined ? undefined : Math.ceil(totalTransactions / pageSize);
        const selectedPage = page === undefined
            ? undefined
            : Math.min(page, Math.max(totalPages ?? 1, 1));
        const paginationClause = page === undefined ? '' : 'LIMIT ? OFFSET ?';
        const paginationParameters = page === undefined
            ? []
            : [pageSize, ((selectedPage ?? 1) - 1) * pageSize];
        const direction = order === 'newest' ? 'DESC' : 'ASC';
        const transactionRows = database.prepare(`
      SELECT
        transactions.entry_id AS transactionId,
        transactions.date AS transactionDate,
        transactions.description
      FROM transactions
      ${filter}
      ORDER BY transactions.entry_id ${direction}
      ${paginationClause}
    `).all(...filterParameters, ...paginationParameters);
        const transactions = transactionRows.map((row) => ({ ...row, comments: [], postings: [] }));
        if (transactions.length > 0) {
            const byId = new Map(transactions.map((transaction) => [transaction.transactionId, transaction]));
            const transactionIds = transactions.map((transaction) => transaction.transactionId);
            const batches = [];
            for (let offset = 0; offset < transactionIds.length; offset += 500) {
                batches.push(transactionIds.slice(offset, offset + 500));
            }
            const postingRows = batches.flatMap((ids) => {
                const placeholders = ids.map(() => '?').join(', ');
                return database.prepare(`
          SELECT
            postings.transaction_id AS transactionId,
            postings.id AS postingId,
            postings.posting_date AS postingDate,
            postings.account,
            postings.amount_quantity AS amountQuantity,
            postings.amount_commodity AS amountCommodity,
            postings.lot_cost_quantity AS lotCostQuantity,
            postings.lot_cost_commodity AS lotCostCommodity,
            postings.lot_cost_is_total AS lotCostIsTotal,
            postings.cost_quantity AS costQuantity,
            postings.cost_commodity AS costCommodity,
            postings.cost_is_total AS costIsTotal,
            postings.balance_quantity AS balanceQuantity,
            postings.balance_commodity AS balanceCommodity,
            amounts.amount_quantity AS quantity,
            amounts.amount_commodity AS commodity
          FROM postings
          JOIN resolved_posting_amounts AS amounts ON amounts.posting_id = postings.id
          WHERE postings.transaction_id IN (${placeholders})
          ORDER BY postings.transaction_id, postings.position, amounts.position
        `).all(...ids);
            });
            for (const row of postingRows) {
                const transaction = byId.get(row.transactionId);
                let posting = transaction.postings.find((item) => item.id === row.postingId);
                if (!posting) {
                    const balance = row.balanceQuantity === null
                        ? null
                        : { quantity: row.balanceQuantity, commodity: row.balanceCommodity };
                    posting = {
                        id: row.postingId,
                        postingDate: row.postingDate,
                        account: row.account,
                        comments: [],
                        amount: row.amountQuantity === null
                            ? null
                            : { quantity: row.amountQuantity, commodity: row.amountCommodity },
                        lotCost: row.lotCostQuantity === null
                            ? null
                            : {
                                quantity: row.lotCostQuantity,
                                commodity: row.lotCostCommodity,
                                isTotal: Boolean(row.lotCostIsTotal),
                            },
                        cost: row.costQuantity === null
                            ? null
                            : {
                                quantity: row.costQuantity,
                                commodity: row.costCommodity,
                                isTotal: Boolean(row.costIsTotal),
                            },
                        balanceAssignment: row.amountQuantity === null ? balance : null,
                        balanceAssertion: row.amountQuantity === null ? null : balance,
                        amounts: [],
                    };
                    transaction.postings.push(posting);
                }
                posting.amounts.push({ quantity: row.quantity, commodity: row.commodity });
            }
            const transactionCommentRows = batches.flatMap((ids) => {
                const placeholders = ids.map(() => '?').join(', ');
                return database.prepare(`
          SELECT transaction_id AS transactionId, text
          FROM comments
          WHERE transaction_id IN (${placeholders})
          ORDER BY transaction_id, position
        `).all(...ids);
            });
            const postingCommentRows = batches.flatMap((ids) => {
                const placeholders = ids.map(() => '?').join(', ');
                return database.prepare(`
          SELECT comments.posting_id AS postingId, comments.text
          FROM postings
          JOIN comments ON comments.posting_id = postings.id
          WHERE postings.transaction_id IN (${placeholders})
          ORDER BY postings.transaction_id, postings.position, comments.position
        `).all(...ids);
            });
            const transactionTagRows = batches.flatMap((ids) => {
                const placeholders = ids.map(() => '?').join(', ');
                return database.prepare(`
          SELECT transaction_id AS transactionId, name, value
          FROM tags
          WHERE transaction_id IN (${placeholders})
          ORDER BY transaction_id, position, ordinal
        `).all(...ids);
            });
            const postingTagRows = batches.flatMap((ids) => {
                const placeholders = ids.map(() => '?').join(', ');
                return database.prepare(`
          SELECT tags.posting_id AS postingId, tags.name, tags.value
          FROM postings
          JOIN tags ON tags.posting_id = postings.id
          WHERE postings.transaction_id IN (${placeholders})
          ORDER BY postings.transaction_id, postings.position, tags.position, tags.ordinal
        `).all(...ids);
            });
            const postingsById = new Map(transactions.flatMap((transaction) => transaction.postings.map((posting) => [posting.id, posting])));
            for (const row of [...transactionCommentRows, ...postingCommentRows]) {
                const owner = row.postingId === undefined
                    ? byId.get(row.transactionId)
                    : postingsById.get(row.postingId);
                if (!owner)
                    continue;
                owner.comments.push(row.text);
            }
            for (const row of [...transactionTagRows, ...postingTagRows]) {
                const owner = row.postingId === undefined
                    ? byId.get(row.transactionId)
                    : postingsById.get(row.postingId);
                if (!owner)
                    continue;
                if (!owner.tags)
                    owner.tags = [];
                owner.tags.push({ name: row.name, value: row.value });
            }
        }
        return {
            order,
            ...(page === undefined ? {} : { page: selectedPage, pageSize }),
            totalTransactions,
            ...(page === undefined ? {} : { totalPages }),
            transactions: transactions.map((transaction) => ({
                ...transaction,
                postings: transaction.postings.map(({ id: _id, ...posting }) => posting),
            })),
        };
    }
    return { inputSchema: optionsSchema, execute: queryTransactions };
};