feat(server): ability to assign the base currency as api query when getting latest ex. rate

This commit is contained in:
Ahmed Bouhuolia
2024-01-28 20:10:23 +02:00
parent 1740226294
commit 74a07847a4
6 changed files with 43 additions and 16 deletions

View File

@@ -2,6 +2,7 @@ import { Service } from 'typedi';
import { ExchangeRate } from '@/lib/ExchangeRate/ExchangeRate';
import { ExchangeRateServiceType } from '@/lib/ExchangeRate/types';
import { EchangeRateLatestPOJO, ExchangeRateLatestDTO } from '@/interfaces';
import { TenantMetadata } from '@/system/models';
@Service()
export class ExchangeRatesService {
@@ -15,14 +16,20 @@ export class ExchangeRatesService {
tenantId: number,
exchangeRateLatestDTO: ExchangeRateLatestDTO
): Promise<EchangeRateLatestPOJO> {
const organization = await TenantMetadata.query().findOne({ tenantId });
// Assign the organization base currency as a default currency
// if no currency is provided
const fromCurrency =
exchangeRateLatestDTO.fromCurrency || organization.baseCurrency;
const toCurrency =
exchangeRateLatestDTO.toCurrency || organization.baseCurrency;
const exchange = new ExchangeRate(ExchangeRateServiceType.OpenExchangeRate);
const exchangeRate = await exchange.latest(
'USD',
exchangeRateLatestDTO.toCurrency
);
const exchangeRate = await exchange.latest(fromCurrency, toCurrency);
return {
baseCurrency: 'USD',
baseCurrency: fromCurrency,
toCurrency: exchangeRateLatestDTO.toCurrency,
exchangeRate,
};