mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-11 10:20:30 +00:00
46 lines
1.2 KiB
TypeScript
46 lines
1.2 KiB
TypeScript
import { OpenExchangeRate } from './OpenExchangeRate';
|
|
import { ExchangeRateServiceType, IExchangeRateService } from './types';
|
|
|
|
export class ExchangeRate {
|
|
private exchangeRateService: IExchangeRateService;
|
|
private exchangeRateServiceType: ExchangeRateServiceType;
|
|
|
|
/**
|
|
* Constructor method.
|
|
* @param {ExchangeRateServiceType} service
|
|
*/
|
|
constructor(service: ExchangeRateServiceType) {
|
|
this.exchangeRateServiceType = service;
|
|
this.initService();
|
|
}
|
|
|
|
/**
|
|
* Initialize the exchange rate service based on the service type.
|
|
*/
|
|
private initService() {
|
|
if (
|
|
this.exchangeRateServiceType === ExchangeRateServiceType.OpenExchangeRate
|
|
) {
|
|
this.setExchangeRateService(new OpenExchangeRate());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Sets the exchange rate service.
|
|
* @param {IExchangeRateService} service
|
|
*/
|
|
private setExchangeRateService(service: IExchangeRateService) {
|
|
this.exchangeRateService = service;
|
|
}
|
|
|
|
/**
|
|
* Gets the latest exchange rate.
|
|
* @param {string} baseCurrency
|
|
* @param {string} toCurrency
|
|
* @returns {number}
|
|
*/
|
|
public latest(baseCurrency: string, toCurrency: string): Promise<number> {
|
|
return this.exchangeRateService.latest(baseCurrency, toCurrency);
|
|
}
|
|
}
|