All files / src/modules/implementations/commands/list-definitions/raw postings.js

100% Statements 80/80
86.95% Branches 40/46
100% Functions 16/16
100% Lines 80/80

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 8140x 40x 3x 3x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 3x 3x 40x 768x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 3x 3x 2x 768x 768x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 768x 40x  
'use strict';
Object.defineProperty(exports, "__esModule", { value: true });
function postingCsvRows(postings) {
    return postings.flatMap((posting) => posting.amounts.map((resolvedAmount) => ({
        filename: posting.filename,
        transactionSourceLine: posting.transactionSourceLine,
        postingId: posting.postingId,
        transactionId: posting.transactionId,
        transactionDate: posting.transactionDate,
        description: posting.description,
        transactionComments: JSON.stringify(posting.transactionComments),
        transactionTags: JSON.stringify(posting.transactionTags || []),
        postingDate: posting.postingDate,
        account: posting.account,
        postingComments: JSON.stringify(posting.postingComments),
        postingTags: JSON.stringify(posting.postingTags || []),
        amountQuantity: posting.amount?.quantity ?? '',
        amountCommodity: posting.amount?.commodity ?? '',
        lotCostQuantity: posting.lotCost?.quantity ?? '',
        lotCostCommodity: posting.lotCost?.commodity ?? '',
        lotCostIsTotal: posting.lotCost?.isTotal ?? '',
        costQuantity: posting.cost?.quantity ?? '',
        costCommodity: posting.cost?.commodity ?? '',
        costIsTotal: posting.cost?.isTotal ?? '',
        balanceAssignmentQuantity: posting.balanceAssignment?.quantity ?? '',
        balanceAssignmentCommodity: posting.balanceAssignment?.commodity ?? '',
        balanceAssertionQuantity: posting.balanceAssertion?.quantity ?? '',
        balanceAssertionCommodity: posting.balanceAssertion?.commodity ?? '',
        resolvedQuantity: resolvedAmount.quantity,
        resolvedCommodity: resolvedAmount.commodity,
        resolvedBalance: resolvedAmount.balance,
    })));
}
module.exports = () => ({
    formatCsv(postings, _cliOptions, { csvField }) {
        const fields = [
            'postingId', 'transactionId', 'transactionDate', 'description',
            'transactionComments', 'transactionTags', 'postingDate', 'account',
            'postingComments', 'postingTags',
            'amountQuantity', 'amountCommodity', 'lotCostQuantity', 'lotCostCommodity',
            'lotCostIsTotal', 'costQuantity', 'costCommodity', 'costIsTotal',
            'balanceAssignmentQuantity', 'balanceAssignmentCommodity',
            'balanceAssertionQuantity', 'balanceAssertionCommodity',
            'resolvedQuantity', 'resolvedCommodity', 'resolvedBalance',
            'filename', 'transactionSourceLine',
        ];
        const lines = [fields.join(',')];
        for (const row of postingCsvRows(postings)) {
            lines.push(fields.map((field) => csvField(row[field])).join(','));
        }
        return `${lines.join('\n')}\n`;
    },
    formatText(postings, _cliOptions, { formatTextTable }) {
        if (postings.length === 0)
            return '';
        const rows = postingCsvRows(postings).map((row) => ({
            ...row,
            transactionComments: JSON.parse(row.transactionComments).join(' | '),
            transactionTags: JSON.parse(row.transactionTags)
                .map(({ name, value }) => value === null ? `:${name}:` : `${name}: ${value}`).join(' | '),
            postingComments: JSON.parse(row.postingComments).join(' | '),
            postingTags: JSON.parse(row.postingTags)
                .map(({ name, value }) => value === null ? `:${name}:` : `${name}: ${value}`).join(' | '),
        }));
        return formatTextTable(rows, [
            { heading: 'Transaction', value: (row) => row.transactionDate },
            { heading: 'Posting', value: (row) => row.postingDate },
            { heading: 'Description', value: (row) => row.description },
            { heading: 'Account', value: (row) => row.account },
            { heading: 'Amount', value: (row) => row.resolvedQuantity, align: 'right' },
            { heading: 'Commodity', value: (row) => row.resolvedCommodity },
            { heading: 'Transaction comments', value: (row) => row.transactionComments },
            { heading: 'Transaction tags', value: (row) => row.transactionTags },
            { heading: 'Posting comments', value: (row) => row.postingComments },
            { heading: 'Posting tags', value: (row) => row.postingTags },
            { heading: 'Filename', value: (row) => row.filename },
            { heading: 'Transaction source line', value: (row) => row.transactionSourceLine },
        ]);
    },
});