Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 43 additions & 13 deletions src/services/ledgerService.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -152,10 +153,12 @@
description: string,
entries: LedgerEntry[],
transactionId?: string,
postedBy?: string
postedBy?: string,
currency?: SupportedCurrency,
conversionRate?: number
): Promise<PostedEntry[]> {
const client = await this.pool.connect();

try {
await client.query('BEGIN');

Expand All @@ -174,6 +177,18 @@
);
}

// 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)`,
Expand All @@ -182,7 +197,7 @@
description,
transactionId || null,
postedBy || null,
JSON.stringify(entries)
JSON.stringify(enrichedEntries)
]
);

Expand All @@ -201,15 +216,18 @@
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
Expand All @@ -228,8 +246,13 @@
const entries: LedgerEntry[] = [
{
account_code: '1100', // Mobile Money Float
debit_amount: amount,
description: 'Customer deposit received'
debit_amount: amountConversion.convertedAmount,

Check failure on line 249 in src/services/ledgerService.ts

View workflow job for this annotation

GitHub Actions / test

Cannot find name 'amountConversion'.
description: 'Customer deposit received',
metadata: {
originalAmount: amount,
originalCurrency: currency,
conversionRate: amountConversion.rate,

Check failure on line 254 in src/services/ledgerService.ts

View workflow job for this annotation

GitHub Actions / mutation-test

Cannot find name 'amountConversion'.

Check failure on line 254 in src/services/ledgerService.ts

View workflow job for this annotation

GitHub Actions / mutation-test

Cannot find name 'amountConversion'.

Check failure on line 254 in src/services/ledgerService.ts

View workflow job for this annotation

GitHub Actions / test

Cannot find name 'amountConversion'.
},
},
{
account_code: '2000', // Customer Balances
Expand All @@ -243,17 +266,24 @@
if (fee > 0) {
entries.push({
account_code: '4100', // Deposit Fee Revenue
credit_amount: fee,
description: 'Deposit fee earned'
credit_amount: feeConversion.convertedAmount,

Check failure on line 269 in src/services/ledgerService.ts

View workflow job for this annotation

GitHub Actions / mutation-test

Cannot find name 'feeConversion'.

Check failure on line 269 in src/services/ledgerService.ts

View workflow job for this annotation

GitHub Actions / mutation-test

Cannot find name 'feeConversion'.

Check failure on line 269 in src/services/ledgerService.ts

View workflow job for this annotation

GitHub Actions / test

Cannot find name 'feeConversion'.
description: 'Deposit fee earned',
metadata: {
originalAmount: fee,
originalCurrency: currency,
conversionRate: feeConversion.rate,

Check failure on line 274 in src/services/ledgerService.ts

View workflow job for this annotation

GitHub Actions / mutation-test

Cannot find name 'feeConversion'.

Check failure on line 274 in src/services/ledgerService.ts

View workflow job for this annotation

GitHub Actions / mutation-test

Cannot find name 'feeConversion'.

Check failure on line 274 in src/services/ledgerService.ts

View workflow job for this annotation

GitHub Actions / test

Cannot find name 'feeConversion'.
},
});
}

return this.postTransaction(
referenceNumber,
`Deposit: ${amount} (fee: ${fee})`,
`Deposit: ${amount} ${currency} (fee: ${fee} ${currency})`,
entries,
transactionId,
userId
userId,
currency,
amountConversion.rate

Check failure on line 286 in src/services/ledgerService.ts

View workflow job for this annotation

GitHub Actions / mutation-test

Cannot find name 'amountConversion'.

Check failure on line 286 in src/services/ledgerService.ts

View workflow job for this annotation

GitHub Actions / mutation-test

Cannot find name 'amountConversion'.

Check failure on line 286 in src/services/ledgerService.ts

View workflow job for this annotation

GitHub Actions / test

Cannot find name 'amountConversion'.
);
}

Expand Down
Loading