From f8658d63b815ae2d0de1038d6f64f001d88b972d Mon Sep 17 00:00:00 2001 From: Abubeker Yasin Date: Wed, 15 Jul 2026 00:45:24 +0300 Subject: [PATCH] feat: ( currency ) add multiple from currency --- .../src/modules/currency/currency.service.ts | 23 +++++++++++-------- .../src/modules/payments/payments.service.ts | 3 ++- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/apps/edr-passenger-api/src/modules/currency/currency.service.ts b/apps/edr-passenger-api/src/modules/currency/currency.service.ts index 806ab423e..57276b116 100644 --- a/apps/edr-passenger-api/src/modules/currency/currency.service.ts +++ b/apps/edr-passenger-api/src/modules/currency/currency.service.ts @@ -51,26 +51,31 @@ export class CurrencyService { } /** - * Converts an ETB minor-unit amount to the charge major-unit amount sent to the - * payment provider. Applies the exchange rate for foreign currencies then divides - * by 100 to yield major units (e.g. 300000 ETB minor → 3000.00 ETB major). + * Converts a booking's stored minor-unit amount (in its own `fromCurrency`) to the charge + * major-unit amount sent to the payment provider in `targetCurrency`. When the two currencies + * match, no exchange rate is applied — the stored amount is charged as-is. Otherwise the + * fromCurrency→targetCurrency rate is applied. In both cases the result is divided by 100 to + * yield major units and rounded to the target currency's precision + * (e.g. 300000 ETB minor → 3000.00 ETB major; DJF rounds to whole francs). */ - async convertEtbMinorToChargeMajor( - amountMinorEtb: number, + async convertMinorToChargeMajor( + amountMinor: number, + fromCurrency: string, targetCurrency: string, ): Promise { + const from = fromCurrency.toUpperCase(); const target = targetCurrency.toUpperCase(); if (CHARGE_CURRENCY_DECIMALS[target] === undefined) { throw new BadRequestException(`Unsupported charge currency: ${targetCurrency}`); } const decimals = CHARGE_CURRENCY_DECIMALS[target]; - if (target === Currency.ETB) { - return this.roundTo(amountMinorEtb / 100, decimals); + if (from === target) { + return this.roundTo(amountMinor / 100, decimals); } - const rate = await this.getRateOrThrow(Currency.ETB, target as Currency); - return this.roundTo((amountMinorEtb * rate) / 100, decimals); + const rate = await this.getRateOrThrow(from as Currency, target as Currency); + return this.roundTo((amountMinor * rate) / 100, decimals); } async getRateOrThrow( diff --git a/apps/edr-passenger-api/src/modules/payments/payments.service.ts b/apps/edr-passenger-api/src/modules/payments/payments.service.ts index 20fac61a5..ca1c116c8 100644 --- a/apps/edr-passenger-api/src/modules/payments/payments.service.ts +++ b/apps/edr-passenger-api/src/modules/payments/payments.service.ts @@ -240,8 +240,9 @@ export class PaymentsService { const chargeCurrency = ( paymentMethod?.currency ?? booking.currency ).toUpperCase(); - const chargeAmount = await this.currencyService.convertEtbMinorToChargeMajor( + const chargeAmount = await this.currencyService.convertMinorToChargeMajor( booking.totalMinor, + booking.currency, chargeCurrency, );