mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-17 13:20:31 +00:00
feat: get latest exchange rate from third party services
This commit is contained in:
45
packages/server/src/lib/ExchangeRate/ExchangeRate.ts
Normal file
45
packages/server/src/lib/ExchangeRate/ExchangeRate.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user