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

100% Statements 109/109
93.33% Branches 28/30
100% Functions 3/3
100% Lines 109/109

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 11051x 51x 51x 783x 783x 783x 783x 783x 783x 44x 44x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 44x 156x 156x 156x 34x 34x 34x 34x 34x 34x 34x 44x 135x 135x 126x 126x 135x 135x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 126x 126x 126x 126x 126x 126x 126x 126x 126x 34x 20x 134x 8x 34x 44x 783x 51x  
'use strict';
Object.defineProperty(exports, "__esModule", { value: true });
module.exports = ({ apiOptions: { parseOptions }, numeric: { decimal: { divideDecimals, formatDecimal, parseDecimal }, }, external: { zod: { z } }, }) => {
    const optionsSchema = z.strictObject({
        mode: z.enum(['effective', 'directives']).default('effective'),
    });
    const inferredPriceScale = 30;
    const decimalScale = (quantity) => quantity.split('.')[1]?.length ?? 0;
    function queryPrices(database, options, _caches) {
        const { mode } = parseOptions(optionsSchema, options, 'prices');
        if (mode === 'directives') {
            return database.prepare(`
        SELECT
          prices.date,
          prices.base_commodity AS baseCommodity,
          prices.quote_quantity AS quoteQuantity,
          prices.quote_commodity AS quoteCommodity,
          prices.comment
        FROM prices
        ORDER BY prices.base_commodity, prices.date, prices.entry_id
      `).all();
        }
        const events = database.prepare(`
      SELECT
        prices.date,
        prices.base_commodity AS baseCommodity,
        prices.quote_quantity AS quoteQuantity,
        prices.quote_commodity AS quoteCommodity,
        prices.comment,
        prices.entry_id AS sourceOrder,
        -1 AS position,
        0 AS isTotal,
        NULL AS baseQuantity,
        NULL AS transactionId,
        0 AS hasTransactionCost
      FROM prices
      UNION ALL
      SELECT
        transactions.date,
        postings.amount_commodity,
        COALESCE(postings.cost_quantity, postings.lot_cost_quantity),
        COALESCE(postings.cost_commodity, postings.lot_cost_commodity),
        NULL,
        transactions.entry_id,
        postings.position,
        COALESCE(postings.cost_is_total, postings.lot_cost_is_total),
        postings.amount_quantity,
        postings.transaction_id,
        postings.cost_quantity IS NOT NULL
      FROM postings
      JOIN transactions ON transactions.entry_id = postings.transaction_id
      WHERE postings.amount_quantity IS NOT NULL
        AND (postings.cost_quantity IS NOT NULL OR postings.lot_cost_quantity IS NOT NULL)
      ORDER BY sourceOrder, position
    `).all();
        const transactionScales = new Map();
        for (const row of database.prepare(`
      SELECT transaction_id, amount_commodity, amount_quantity
      FROM postings WHERE amount_quantity IS NOT NULL
    `).all()) {
            const key = `${row.transaction_id}\u0000${row.amount_commodity}`;
            transactionScales.set(key, Math.max(transactionScales.get(key) ?? 0, decimalScale(row.amount_quantity)));
        }
        const usedCommodities = new Set(database.prepare(`
      SELECT DISTINCT amount_commodity
      FROM resolved_posting_amounts
      WHERE decimal_cmp(amount_quantity, '0') != 0
    `).pluck().all());
        const byKey = new Map();
        const quoteOrder = new Map();
        for (const event of events) {
            if (!usedCommodities.has(event.baseCommodity))
                continue;
            const pair = `${event.baseCommodity}\u0000${event.quoteCommodity}`;
            if (!quoteOrder.has(pair))
                quoteOrder.set(pair, quoteOrder.size);
            if (event.isTotal) {
                // Preserve the source operands for text rendering without changing the
                // public row shape or rounding the high-precision API quantity twice.
                Object.defineProperty(event, 'ledgerPrice', { value: {
                        totalQuantity: event.quoteQuantity,
                        baseQuantity: event.baseQuantity,
                        hasTransactionCost: Boolean(event.hasTransactionCost),
                        baseScale: transactionScales.get(`${event.transactionId}\u0000${event.baseCommodity}`) ?? 0,
                        quoteScale: transactionScales.get(`${event.transactionId}\u0000${event.quoteCommodity}`) ?? 0,
                    } });
                const amount = parseDecimal(event.baseQuantity);
                const absoluteAmount = amount.coefficient < 0n
                    ? { ...amount, coefficient: -amount.coefficient }
                    : amount;
                event.quoteQuantity = formatDecimal(divideDecimals(parseDecimal(event.quoteQuantity), absoluteAmount, inferredPriceScale));
            }
            delete event.sourceOrder;
            delete event.position;
            delete event.isTotal;
            delete event.baseQuantity;
            delete event.transactionId;
            delete event.hasTransactionCost;
            const key = `${pair}\u0000${event.date}`;
            byKey.set(key, event);
        }
        return [...byKey.values()].sort((left, right) => left.date.localeCompare(right.date) ||
            (left.baseCommodity < right.baseCommodity ? -1 :
                left.baseCommodity > right.baseCommodity ? 1 : 0) ||
            quoteOrder.get(`${left.baseCommodity}\u0000${left.quoteCommodity}`) -
                quoteOrder.get(`${right.baseCommodity}\u0000${right.quoteCommodity}`));
    }
    return { inputSchema: optionsSchema, execute: queryPrices };
};