Payment amount for non ETB and exchange fixes

This commit is contained in:
Stephanos A
2026-07-15 10:27:17 +03:00
parent c0b99b50a9
commit e2529b9317
9 changed files with 216 additions and 125 deletions

View File

@@ -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(