UNPKG

@accounter/server

Version:
55 lines (54 loc) 2.15 kB
/** * Transaction Aggregator * * Aggregates multiple transactions from a single charge into a unified representation * for matching purposes. Handles fee exclusion, currency validation, business ID * validation, amount summation, date selection, and description concatenation. */ import type { currency } from '../../transactions/types.js'; import type { AggregatedTransaction } from '../types.js'; /** * Minimal transaction interface for aggregation * Based on database schema from accounter_schema.transactions */ export interface Transaction { id: string; charge_id: string; amount: string; currency: currency; business_id: string | null; event_date: Date; debit_date: Date | null; debit_timestamp: Date | null; source_description: string | null; is_fee: boolean | null; } /** * Aggregate multiple transactions into a single representation * * Per specification (section 4.2): * 1. Exclude transactions where is_fee = true * 2. If multiple currencies exist: throw error * 3. If multiple non-null business IDs exist: throw error * 4. Amount: sum of all amounts * 5. Currency: the common currency * 6. Business ID: the single non-null business ID (or null if all null) * 7. Date: earliest event_date * 8. Description: concatenate all source_description values with line breaks * * @param transactions - Array of transactions from a charge * @returns Aggregated transaction data * * @throws {Error} If transactions array is empty * @throws {Error} If no non-fee transactions exist after filtering * @throws {Error} If multiple different currencies exist * @throws {Error} If multiple different non-null business IDs exist * * @example * const aggregated = aggregateTransactions([ * { amount: 100, currency: 'USD', business_id: 'b1', event_date: new Date('2024-01-15'), ... }, * { amount: 50, currency: 'USD', business_id: 'b1', event_date: new Date('2024-01-20'), ... } * ]); * // Returns: { amount: 150, currency: 'USD', businessId: 'b1', date: Date('2024-01-15'), ... } */ export declare function aggregateTransactions(transactions: Transaction[]): AggregatedTransaction;