Browse Source

Fix currency conversion in accounts

pull/937/head
Thomas 3 years ago
parent
commit
66683379c9
  1. 20
      apps/api/src/app/portfolio/current-rate.service.ts
  2. 2
      apps/api/src/app/portfolio/interfaces/get-value-object.interface.ts
  3. 8
      apps/api/src/app/portfolio/portfolio-calculator.ts
  4. 41
      apps/api/src/app/portfolio/portfolio.service.ts

20
apps/api/src/app/portfolio/current-rate.service.ts

@ -28,13 +28,7 @@ export class CurrentRateService {
(!dateQuery.gte || isBefore(dateQuery.gte, new Date())) && (!dateQuery.gte || isBefore(dateQuery.gte, new Date())) &&
(!dateQuery.in || this.containsToday(dateQuery.in)); (!dateQuery.in || this.containsToday(dateQuery.in));
const promises: Promise< const promises: Promise<GetValueObject[]>[] = [];
{
date: Date;
marketPrice: number;
symbol: string;
}[]
>[] = [];
if (includeToday) { if (includeToday) {
const today = resetHours(new Date()); const today = resetHours(new Date());
@ -42,13 +36,14 @@ export class CurrentRateService {
this.dataProviderService this.dataProviderService
.getQuotes(dataGatheringItems) .getQuotes(dataGatheringItems)
.then((dataResultProvider) => { .then((dataResultProvider) => {
const result = []; const result: GetValueObject[] = [];
for (const dataGatheringItem of dataGatheringItems) { for (const dataGatheringItem of dataGatheringItems) {
result.push({ result.push({
date: today, date: today,
marketPrice: this.exchangeRateDataService.toCurrency( marketPriceInBaseCurrency:
dataResultProvider?.[dataGatheringItem.symbol]?.marketPrice ?? this.exchangeRateDataService.toCurrency(
0, dataResultProvider?.[dataGatheringItem.symbol]
?.marketPrice ?? 0,
dataResultProvider?.[dataGatheringItem.symbol]?.currency, dataResultProvider?.[dataGatheringItem.symbol]?.currency,
userCurrency userCurrency
), ),
@ -74,7 +69,8 @@ export class CurrentRateService {
return data.map((marketDataItem) => { return data.map((marketDataItem) => {
return { return {
date: marketDataItem.date, date: marketDataItem.date,
marketPrice: this.exchangeRateDataService.toCurrency( marketPriceInBaseCurrency:
this.exchangeRateDataService.toCurrency(
marketDataItem.marketPrice, marketDataItem.marketPrice,
currencies[marketDataItem.symbol], currencies[marketDataItem.symbol],
userCurrency userCurrency

2
apps/api/src/app/portfolio/interfaces/get-value-object.interface.ts

@ -1,5 +1,5 @@
export interface GetValueObject { export interface GetValueObject {
date: Date; date: Date;
marketPrice: number; marketPriceInBaseCurrency: number;
symbol: string; symbol: string;
} }

8
apps/api/src/app/portfolio/portfolio-calculator.ts

@ -231,9 +231,9 @@ export class PortfolioCalculator {
if (!marketSymbolMap[date]) { if (!marketSymbolMap[date]) {
marketSymbolMap[date] = {}; marketSymbolMap[date] = {};
} }
if (marketSymbol.marketPrice) { if (marketSymbol.marketPriceInBaseCurrency) {
marketSymbolMap[date][marketSymbol.symbol] = new Big( marketSymbolMap[date][marketSymbol.symbol] = new Big(
marketSymbol.marketPrice marketSymbol.marketPriceInBaseCurrency
); );
} }
} }
@ -548,9 +548,9 @@ export class PortfolioCalculator {
if (!marketSymbolMap[date]) { if (!marketSymbolMap[date]) {
marketSymbolMap[date] = {}; marketSymbolMap[date] = {};
} }
if (marketSymbol.marketPrice) { if (marketSymbol.marketPriceInBaseCurrency) {
marketSymbolMap[date][marketSymbol.symbol] = new Big( marketSymbolMap[date][marketSymbol.symbol] = new Big(
marketSymbol.marketPrice marketSymbol.marketPriceInBaseCurrency
); );
} }
} }

41
apps/api/src/app/portfolio/portfolio.service.ts

@ -462,8 +462,9 @@ export class PortfolioService {
const accounts = await this.getValueOfAccounts({ const accounts = await this.getValueOfAccounts({
orders, orders,
userId,
portfolioItemsNow, portfolioItemsNow,
userCurrency,
userId,
filters: aFilters filters: aFilters
}); });
@ -899,7 +900,8 @@ export class PortfolioService {
const accounts = await this.getValueOfAccounts({ const accounts = await this.getValueOfAccounts({
orders, orders,
portfolioItemsNow, portfolioItemsNow,
userId userId,
userCurrency: currency
}); });
return { return {
rules: { rules: {
@ -1268,11 +1270,13 @@ export class PortfolioService {
filters = [], filters = [],
orders, orders,
portfolioItemsNow, portfolioItemsNow,
userCurrency,
userId userId
}: { }: {
filters?: Filter[]; filters?: Filter[];
orders: OrderWithAccount[]; orders: OrderWithAccount[];
portfolioItemsNow: { [p: string]: TimelinePosition }; portfolioItemsNow: { [p: string]: TimelinePosition };
userCurrency: string;
userId: string; userId: string;
}) { }) {
const accounts: PortfolioDetails['accounts'] = {}; const accounts: PortfolioDetails['accounts'] = {};
@ -1301,34 +1305,47 @@ export class PortfolioService {
accounts[account.id] = { accounts[account.id] = {
balance: account.balance, balance: account.balance,
currency: account.currency, currency: account.currency,
current: account.balance, current: this.exchangeRateDataService.toCurrency(
account.balance,
account.currency,
userCurrency
),
name: account.name, name: account.name,
original: account.balance original: this.exchangeRateDataService.toCurrency(
account.balance,
account.currency,
userCurrency
)
}; };
for (const order of ordersByAccount) { for (const order of ordersByAccount) {
let currentValueOfSymbol = let currentValueOfSymbolInBaseCurrency =
order.quantity * order.quantity *
portfolioItemsNow[order.SymbolProfile.symbol].marketPrice; portfolioItemsNow[order.SymbolProfile.symbol].marketPrice;
let originalValueOfSymbol = order.quantity * order.unitPrice; let originalValueOfSymbolInBaseCurrency =
this.exchangeRateDataService.toCurrency(
order.quantity * order.unitPrice,
order.SymbolProfile.currency,
userCurrency
);
if (order.type === 'SELL') { if (order.type === 'SELL') {
currentValueOfSymbol *= -1; currentValueOfSymbolInBaseCurrency *= -1;
originalValueOfSymbol *= -1; originalValueOfSymbolInBaseCurrency *= -1;
} }
if (accounts[order.Account?.id || UNKNOWN_KEY]?.current) { if (accounts[order.Account?.id || UNKNOWN_KEY]?.current) {
accounts[order.Account?.id || UNKNOWN_KEY].current += accounts[order.Account?.id || UNKNOWN_KEY].current +=
currentValueOfSymbol; currentValueOfSymbolInBaseCurrency;
accounts[order.Account?.id || UNKNOWN_KEY].original += accounts[order.Account?.id || UNKNOWN_KEY].original +=
originalValueOfSymbol; originalValueOfSymbolInBaseCurrency;
} else { } else {
accounts[order.Account?.id || UNKNOWN_KEY] = { accounts[order.Account?.id || UNKNOWN_KEY] = {
balance: 0, balance: 0,
currency: order.Account?.currency, currency: order.Account?.currency,
current: currentValueOfSymbol, current: currentValueOfSymbolInBaseCurrency,
name: account.name, name: account.name,
original: originalValueOfSymbol original: originalValueOfSymbolInBaseCurrency
}; };
} }
} }

Loading…
Cancel
Save