UNPKG

@accounter/server

Version:

The test suite is split into three Vitest projects for efficiency and isolation:

67 lines 2.58 kB
import { DocumentType } from '../../../shared/enums.js'; /** * Accounting document types that count toward matched/unmatched status */ const ACCOUNTING_DOC_TYPES = [ DocumentType.Invoice, DocumentType.CreditInvoice, DocumentType.Receipt, DocumentType.InvoiceReceipt, DocumentType.Proforma, ]; /** * Validate a charge is properly formed for matching * @throws Error with descriptive message if invalid */ export function validateChargeForMatching(charge) { if (!charge) { throw new Error('Charge is required'); } if (!charge.id) { throw new Error('Charge must have an ID'); } if (!charge.owner_id) { throw new Error(`Charge ${charge.id} must have an owner_id`); } // Check if charge has data const hasTransactions = charge.transactions && charge.transactions.length > 0; const hasDocuments = charge.documents && charge.documents.length > 0; if (!hasTransactions && !hasDocuments) { throw new Error(`Charge ${charge.id} has no transactions or documents - cannot be matched`); } } /** * Check if charge is matched (has both transactions and accounting documents) */ export function isChargeMatched(charge) { const hasTransactions = charge.transactions && charge.transactions.length > 0; const hasAccountingDocs = charge.documents?.some(doc => ACCOUNTING_DOC_TYPES.includes(doc.type)); return !!hasTransactions && !!hasAccountingDocs; } /** * Check if charge has only transactions (no accounting documents) */ export function hasOnlyTransactions(charge) { const hasTransactions = charge.transactions && charge.transactions.length > 0; const hasAccountingDocs = charge.documents?.some(doc => ACCOUNTING_DOC_TYPES.includes(doc.type)); return !!hasTransactions && !hasAccountingDocs; } /** * Check if charge has only accounting documents (no transactions) */ export function hasOnlyDocuments(charge) { const hasTransactions = charge.transactions && charge.transactions.length > 0; const hasAccountingDocs = charge.documents?.some(doc => ACCOUNTING_DOC_TYPES.includes(doc.type)); return !hasTransactions && !!hasAccountingDocs; } /** * Validate that a charge is unmatched (for matching operations) * @throws Error if charge is already matched */ export function validateChargeIsUnmatched(charge) { validateChargeForMatching(charge); if (isChargeMatched(charge)) { throw new Error(`Charge ${charge.id} is already matched (has both transactions and accounting documents)`); } } //# sourceMappingURL=charge-validator.helper.js.map