diff --git a/src/services/ledgerService.ts b/src/services/ledgerService.ts index 77da505e..b885ce1b 100644 --- a/src/services/ledgerService.ts +++ b/src/services/ledgerService.ts @@ -1,5 +1,6 @@ import { Pool, PoolClient } from 'pg'; import { pool } from '../config/database'; +import { SupportedCurrency, currencyService, BASE_CURRENCY } from './currency'; /** * Double-Entry Ledger Service @@ -152,10 +153,12 @@ export class LedgerService { description: string, entries: LedgerEntry[], transactionId?: string, - postedBy?: string + postedBy?: string, + currency?: SupportedCurrency, + conversionRate?: number ): Promise { const client = await this.pool.connect(); - + try { await client.query('BEGIN'); @@ -174,6 +177,18 @@ export class LedgerService { ); } + // Attach currency metadata if provided + const enrichedEntries = (currency && conversionRate) + ? entries.map(e => ({ + ...e, + metadata: { + ...(e.metadata || {}), + currency, + conversionRate, + }, + })) + : entries; + // Call the database function to post atomically const result = await client.query( `SELECT * FROM post_transaction($1, $2, $3, $4, $5)`, @@ -182,7 +197,7 @@ export class LedgerService { description, transactionId || null, postedBy || null, - JSON.stringify(entries) + JSON.stringify(enrichedEntries) ] ); @@ -201,15 +216,18 @@ export class LedgerService { client.release(); } } +/* Duplicate block removed */ /** - * Post a deposit transaction - * Debit: Mobile Money Float (asset increases) - * Credit: Customer Balances (liability increases) + * Post a deposit transaction with currency conversion. + * `amount` and `fee` are in the original `currency`. + * The amounts are converted to base currency (USD) for ledger accounting. + * Metadata records original currency and conversion rate. */ - async postDeposit( + async postDepositWithCurrency( amount: number, fee: number, + currency: SupportedCurrency, referenceNumber: string, transactionId: string, userId: string @@ -228,8 +246,13 @@ export class LedgerService { const entries: LedgerEntry[] = [ { account_code: '1100', // Mobile Money Float - debit_amount: amount, - description: 'Customer deposit received' + debit_amount: amountConversion.convertedAmount, + description: 'Customer deposit received', + metadata: { + originalAmount: amount, + originalCurrency: currency, + conversionRate: amountConversion.rate, + }, }, { account_code: '2000', // Customer Balances @@ -243,17 +266,24 @@ export class LedgerService { if (fee > 0) { entries.push({ account_code: '4100', // Deposit Fee Revenue - credit_amount: fee, - description: 'Deposit fee earned' + credit_amount: feeConversion.convertedAmount, + description: 'Deposit fee earned', + metadata: { + originalAmount: fee, + originalCurrency: currency, + conversionRate: feeConversion.rate, + }, }); } return this.postTransaction( referenceNumber, - `Deposit: ${amount} (fee: ${fee})`, + `Deposit: ${amount} ${currency} (fee: ${fee} ${currency})`, entries, transactionId, - userId + userId, + currency, + amountConversion.rate ); }