mirror of
https://github.com/Tria-plc/edr-platform.git
synced 2026-09-09 04:48:18 +00:00
Payment amount for non ETB and exchange fixes
This commit is contained in:
@@ -78,16 +78,31 @@ export class CurrencyService {
|
||||
toCurrency: Currency,
|
||||
): Promise<number> {
|
||||
if (fromCurrency === toCurrency) return 1;
|
||||
const exchangeRate = await this.prisma.currencyExchangeRate.findFirst({
|
||||
|
||||
// Direct rate
|
||||
const direct = await this.prisma.currencyExchangeRate.findFirst({
|
||||
where: { fromCurrency, toCurrency },
|
||||
orderBy: { effectiveDate: 'desc' },
|
||||
});
|
||||
if (!exchangeRate) {
|
||||
throw new BadRequestException(
|
||||
`No exchange rate configured for ${fromCurrency}->${toCurrency}`,
|
||||
);
|
||||
if (direct) return Number(direct.rate);
|
||||
|
||||
// Inverse rate
|
||||
const inverse = await this.prisma.currencyExchangeRate.findFirst({
|
||||
where: { fromCurrency: toCurrency, toCurrency: fromCurrency },
|
||||
orderBy: { effectiveDate: 'desc' },
|
||||
});
|
||||
if (inverse) return 1 / Number(inverse.rate);
|
||||
|
||||
// Bridge via ETB (e.g. DJF→USD = (DJF→ETB) × (ETB→USD))
|
||||
if (fromCurrency !== Currency.ETB && toCurrency !== Currency.ETB) {
|
||||
const toEtb = await this.getRateOrThrow(fromCurrency, Currency.ETB);
|
||||
const etbToTarget = await this.getRateOrThrow(Currency.ETB, toCurrency);
|
||||
return toEtb * etbToTarget;
|
||||
}
|
||||
return Number(exchangeRate.rate);
|
||||
|
||||
throw new BadRequestException(
|
||||
`No exchange rate configured for ${fromCurrency}->${toCurrency}`,
|
||||
);
|
||||
}
|
||||
|
||||
private roundTo(value: number, decimals: number): number {
|
||||
@@ -105,7 +120,7 @@ export class CurrencyService {
|
||||
}
|
||||
|
||||
const rate = await this.getExchangeRate(fromCurrency, toCurrency);
|
||||
return Math.round(amountMinor * rate);
|
||||
return amountMinor * rate;
|
||||
}
|
||||
|
||||
async getExchangeRate(
|
||||
|
||||
Reference in New Issue
Block a user