mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-16 04:40:32 +00:00
feat(server): ability to assign the base currency as api query when getting latest ex. rate
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import { Service, Inject } from 'typedi';
|
||||
import { Router, Request, Response, NextFunction } from 'express';
|
||||
import { query } from 'express-validator';
|
||||
import { query, oneOf } from 'express-validator';
|
||||
import asyncMiddleware from '@/api/middleware/asyncMiddleware';
|
||||
import BaseController from './BaseController';
|
||||
import { ServiceError } from '@/exceptions';
|
||||
@@ -20,7 +20,12 @@ export default class ExchangeRatesController extends BaseController {
|
||||
|
||||
router.get(
|
||||
'/latest',
|
||||
[query('to_currency').exists().isString()],
|
||||
[
|
||||
oneOf([
|
||||
query('to_currency').exists().isString().isISO4217(),
|
||||
query('from_currency').exists().isString().isISO4217(),
|
||||
]),
|
||||
],
|
||||
this.validationResult,
|
||||
asyncMiddleware(this.latestExchangeRate.bind(this)),
|
||||
this.handleServiceError
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
export interface ExchangeRateLatestDTO {
|
||||
toCurrency: string;
|
||||
fromCurrency: string;
|
||||
}
|
||||
|
||||
export interface EchangeRateLatestPOJO {
|
||||
|
||||
@@ -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,
|
||||
};
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
import BaseModel from 'models/Model';
|
||||
|
||||
export default class TenantMetadata extends BaseModel {
|
||||
baseCurrency: string;
|
||||
|
||||
/**
|
||||
* Table name.
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user