Browse Source

Replace OrderType with Type (prisma) (#560)

pull/561/head
Thomas Kaul 3 years ago
committed by GitHub
parent
commit
e78755c280
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 5
      apps/api/src/app/portfolio/interfaces/portfolio-order.interface.ts
  2. 37
      apps/api/src/app/portfolio/portfolio-calculator.spec.ts
  3. 8
      apps/api/src/app/portfolio/portfolio-calculator.ts
  4. 15
      apps/api/src/app/portfolio/portfolio.service.ts
  5. 8
      apps/api/src/models/order-type.ts
  6. 5
      apps/api/src/models/order.ts
  7. 7
      apps/api/src/services/interfaces/interfaces.ts

5
apps/api/src/app/portfolio/interfaces/portfolio-order.interface.ts

@ -1,5 +1,4 @@
import { OrderType } from '@ghostfolio/api/models/order-type'; import { DataSource, Type as TypeOfOrder } from '@prisma/client';
import { DataSource } from '@prisma/client';
import Big from 'big.js'; import Big from 'big.js';
export interface PortfolioOrder { export interface PortfolioOrder {
@ -10,6 +9,6 @@ export interface PortfolioOrder {
name: string; name: string;
quantity: Big; quantity: Big;
symbol: string; symbol: string;
type: OrderType; type: TypeOfOrder;
unitPrice: Big; unitPrice: Big;
} }

37
apps/api/src/app/portfolio/portfolio-calculator.spec.ts

@ -1,4 +1,3 @@
import { OrderType } from '@ghostfolio/api/models/order-type';
import { parseDate, resetHours } from '@ghostfolio/common/helper'; import { parseDate, resetHours } from '@ghostfolio/common/helper';
import { DataSource } from '@prisma/client'; import { DataSource } from '@prisma/client';
import Big from 'big.js'; import Big from 'big.js';
@ -155,7 +154,7 @@ describe('PortfolioCalculator', () => {
name: 'Vanguard Total Stock Market Index Fund ETF Shares', name: 'Vanguard Total Stock Market Index Fund ETF Shares',
quantity: new Big('10'), quantity: new Big('10'),
symbol: 'VTI', symbol: 'VTI',
type: OrderType.Buy, type: 'BUY',
unitPrice: new Big('144.38'), unitPrice: new Big('144.38'),
currency: 'USD', currency: 'USD',
dataSource: DataSource.YAHOO, dataSource: DataSource.YAHOO,
@ -166,7 +165,7 @@ describe('PortfolioCalculator', () => {
name: 'Vanguard Total Stock Market Index Fund ETF Shares', name: 'Vanguard Total Stock Market Index Fund ETF Shares',
quantity: new Big('10'), quantity: new Big('10'),
symbol: 'VTI', symbol: 'VTI',
type: OrderType.Buy, type: 'BUY',
unitPrice: new Big('147.99'), unitPrice: new Big('147.99'),
currency: 'USD', currency: 'USD',
dataSource: DataSource.YAHOO, dataSource: DataSource.YAHOO,
@ -177,7 +176,7 @@ describe('PortfolioCalculator', () => {
name: 'Vanguard Total Stock Market Index Fund ETF Shares', name: 'Vanguard Total Stock Market Index Fund ETF Shares',
quantity: new Big('15'), quantity: new Big('15'),
symbol: 'VTI', symbol: 'VTI',
type: OrderType.Sell, type: 'SELL',
unitPrice: new Big('151.41'), unitPrice: new Big('151.41'),
currency: 'USD', currency: 'USD',
dataSource: DataSource.YAHOO, dataSource: DataSource.YAHOO,
@ -248,7 +247,7 @@ describe('PortfolioCalculator', () => {
name: 'Vanguard Total Stock Market Index Fund ETF Shares', name: 'Vanguard Total Stock Market Index Fund ETF Shares',
quantity: new Big('10'), quantity: new Big('10'),
symbol: 'VTI', symbol: 'VTI',
type: OrderType.Buy, type: 'BUY',
unitPrice: new Big('144.38'), unitPrice: new Big('144.38'),
currency: 'USD', currency: 'USD',
dataSource: DataSource.YAHOO, dataSource: DataSource.YAHOO,
@ -259,7 +258,7 @@ describe('PortfolioCalculator', () => {
name: 'Something else', name: 'Something else',
quantity: new Big('10'), quantity: new Big('10'),
symbol: 'VTX', symbol: 'VTX',
type: OrderType.Buy, type: 'BUY',
unitPrice: new Big('147.99'), unitPrice: new Big('147.99'),
currency: 'USD', currency: 'USD',
dataSource: DataSource.YAHOO, dataSource: DataSource.YAHOO,
@ -270,7 +269,7 @@ describe('PortfolioCalculator', () => {
name: 'Vanguard Total Stock Market Index Fund ETF Shares', name: 'Vanguard Total Stock Market Index Fund ETF Shares',
quantity: new Big('5'), quantity: new Big('5'),
symbol: 'VTI', symbol: 'VTI',
type: OrderType.Sell, type: 'SELL',
unitPrice: new Big('151.41'), unitPrice: new Big('151.41'),
currency: 'USD', currency: 'USD',
dataSource: DataSource.YAHOO, dataSource: DataSource.YAHOO,
@ -360,7 +359,7 @@ describe('PortfolioCalculator', () => {
name: 'Vanguard Total Stock Market Index Fund ETF Shares', name: 'Vanguard Total Stock Market Index Fund ETF Shares',
quantity: new Big('20'), quantity: new Big('20'),
symbol: 'VTI', symbol: 'VTI',
type: OrderType.Buy, type: 'BUY',
unitPrice: new Big('197.15'), unitPrice: new Big('197.15'),
fee: new Big(0) fee: new Big(0)
} }
@ -462,7 +461,7 @@ describe('PortfolioCalculator', () => {
name: 'Amazon.com, Inc.', name: 'Amazon.com, Inc.',
quantity: new Big('5'), quantity: new Big('5'),
symbol: 'AMZN', symbol: 'AMZN',
type: OrderType.Buy, type: 'BUY',
unitPrice: new Big('2021.99'), unitPrice: new Big('2021.99'),
fee: new Big(0) fee: new Big(0)
} }
@ -617,7 +616,7 @@ describe('PortfolioCalculator', () => {
name: 'Amazon.com, Inc.', name: 'Amazon.com, Inc.',
quantity: new Big('5'), quantity: new Big('5'),
symbol: 'AMZN', symbol: 'AMZN',
type: OrderType.Buy, type: 'BUY',
unitPrice: new Big('2021.99'), unitPrice: new Big('2021.99'),
currency: 'USD', currency: 'USD',
dataSource: DataSource.YAHOO, dataSource: DataSource.YAHOO,
@ -628,7 +627,7 @@ describe('PortfolioCalculator', () => {
name: 'Amazon.com, Inc.', name: 'Amazon.com, Inc.',
quantity: new Big('5'), quantity: new Big('5'),
symbol: 'AMZN', symbol: 'AMZN',
type: OrderType.Sell, type: 'SELL',
unitPrice: new Big('2412.23'), unitPrice: new Big('2412.23'),
currency: 'USD', currency: 'USD',
dataSource: DataSource.YAHOO, dataSource: DataSource.YAHOO,
@ -2391,7 +2390,7 @@ const ordersMixedSymbols: PortfolioOrder[] = [
name: 'Tesla, Inc.', name: 'Tesla, Inc.',
quantity: new Big('50'), quantity: new Big('50'),
symbol: 'TSLA', symbol: 'TSLA',
type: OrderType.Buy, type: 'BUY',
unitPrice: new Big('42.97'), unitPrice: new Big('42.97'),
currency: 'USD', currency: 'USD',
dataSource: DataSource.YAHOO, dataSource: DataSource.YAHOO,
@ -2402,7 +2401,7 @@ const ordersMixedSymbols: PortfolioOrder[] = [
name: 'Bitcoin USD', name: 'Bitcoin USD',
quantity: new Big('0.5614682'), quantity: new Big('0.5614682'),
symbol: 'BTCUSD', symbol: 'BTCUSD',
type: OrderType.Buy, type: 'BUY',
unitPrice: new Big('3562.089535970158'), unitPrice: new Big('3562.089535970158'),
currency: 'USD', currency: 'USD',
dataSource: DataSource.YAHOO, dataSource: DataSource.YAHOO,
@ -2413,7 +2412,7 @@ const ordersMixedSymbols: PortfolioOrder[] = [
name: 'Amazon.com, Inc.', name: 'Amazon.com, Inc.',
quantity: new Big('5'), quantity: new Big('5'),
symbol: 'AMZN', symbol: 'AMZN',
type: OrderType.Buy, type: 'BUY',
unitPrice: new Big('2021.99'), unitPrice: new Big('2021.99'),
currency: 'USD', currency: 'USD',
dataSource: DataSource.YAHOO, dataSource: DataSource.YAHOO,
@ -2427,7 +2426,7 @@ const ordersVTI: PortfolioOrder[] = [
name: 'Vanguard Total Stock Market Index Fund ETF Shares', name: 'Vanguard Total Stock Market Index Fund ETF Shares',
quantity: new Big('10'), quantity: new Big('10'),
symbol: 'VTI', symbol: 'VTI',
type: OrderType.Buy, type: 'BUY',
unitPrice: new Big('144.38'), unitPrice: new Big('144.38'),
currency: 'USD', currency: 'USD',
dataSource: DataSource.YAHOO, dataSource: DataSource.YAHOO,
@ -2438,7 +2437,7 @@ const ordersVTI: PortfolioOrder[] = [
name: 'Vanguard Total Stock Market Index Fund ETF Shares', name: 'Vanguard Total Stock Market Index Fund ETF Shares',
quantity: new Big('10'), quantity: new Big('10'),
symbol: 'VTI', symbol: 'VTI',
type: OrderType.Buy, type: 'BUY',
unitPrice: new Big('147.99'), unitPrice: new Big('147.99'),
currency: 'USD', currency: 'USD',
dataSource: DataSource.YAHOO, dataSource: DataSource.YAHOO,
@ -2449,7 +2448,7 @@ const ordersVTI: PortfolioOrder[] = [
name: 'Vanguard Total Stock Market Index Fund ETF Shares', name: 'Vanguard Total Stock Market Index Fund ETF Shares',
quantity: new Big('15'), quantity: new Big('15'),
symbol: 'VTI', symbol: 'VTI',
type: OrderType.Sell, type: 'SELL',
unitPrice: new Big('151.41'), unitPrice: new Big('151.41'),
currency: 'USD', currency: 'USD',
dataSource: DataSource.YAHOO, dataSource: DataSource.YAHOO,
@ -2460,7 +2459,7 @@ const ordersVTI: PortfolioOrder[] = [
name: 'Vanguard Total Stock Market Index Fund ETF Shares', name: 'Vanguard Total Stock Market Index Fund ETF Shares',
quantity: new Big('10'), quantity: new Big('10'),
symbol: 'VTI', symbol: 'VTI',
type: OrderType.Buy, type: 'BUY',
unitPrice: new Big('177.69'), unitPrice: new Big('177.69'),
currency: 'USD', currency: 'USD',
dataSource: DataSource.YAHOO, dataSource: DataSource.YAHOO,
@ -2471,7 +2470,7 @@ const ordersVTI: PortfolioOrder[] = [
name: 'Vanguard Total Stock Market Index Fund ETF Shares', name: 'Vanguard Total Stock Market Index Fund ETF Shares',
quantity: new Big('10'), quantity: new Big('10'),
symbol: 'VTI', symbol: 'VTI',
type: OrderType.Buy, type: 'BUY',
unitPrice: new Big('203.15'), unitPrice: new Big('203.15'),
currency: 'USD', currency: 'USD',
dataSource: DataSource.YAHOO, dataSource: DataSource.YAHOO,

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

@ -1,9 +1,9 @@
import { TimelineInfoInterface } from '@ghostfolio/api/app/portfolio/interfaces/timeline-info.interface'; import { TimelineInfoInterface } from '@ghostfolio/api/app/portfolio/interfaces/timeline-info.interface';
import { OrderType } from '@ghostfolio/api/models/order-type';
import { IDataGatheringItem } from '@ghostfolio/api/services/interfaces/interfaces'; import { IDataGatheringItem } from '@ghostfolio/api/services/interfaces/interfaces';
import { DATE_FORMAT, parseDate, resetHours } from '@ghostfolio/common/helper'; import { DATE_FORMAT, parseDate, resetHours } from '@ghostfolio/common/helper';
import { TimelinePosition } from '@ghostfolio/common/interfaces'; import { TimelinePosition } from '@ghostfolio/common/interfaces';
import { Logger } from '@nestjs/common'; import { Logger } from '@nestjs/common';
import { Type as TypeOfOrder } from '@prisma/client';
import Big from 'big.js'; import Big from 'big.js';
import { import {
addDays, addDays,
@ -660,14 +660,14 @@ export class PortfolioCalculator {
}; };
} }
private getFactor(type: OrderType) { private getFactor(type: TypeOfOrder) {
let factor: number; let factor: number;
switch (type) { switch (type) {
case OrderType.Buy: case 'BUY':
factor = 1; factor = 1;
break; break;
case OrderType.Sell: case 'SELL':
factor = -1; factor = -1;
break; break;
default: default:

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

@ -6,7 +6,6 @@ import { PortfolioOrder } from '@ghostfolio/api/app/portfolio/interfaces/portfol
import { TimelineSpecification } from '@ghostfolio/api/app/portfolio/interfaces/timeline-specification.interface'; import { TimelineSpecification } from '@ghostfolio/api/app/portfolio/interfaces/timeline-specification.interface';
import { TransactionPoint } from '@ghostfolio/api/app/portfolio/interfaces/transaction-point.interface'; import { TransactionPoint } from '@ghostfolio/api/app/portfolio/interfaces/transaction-point.interface';
import { PortfolioCalculator } from '@ghostfolio/api/app/portfolio/portfolio-calculator'; import { PortfolioCalculator } from '@ghostfolio/api/app/portfolio/portfolio-calculator';
import { OrderType } from '@ghostfolio/api/models/order-type';
import { AccountClusterRiskCurrentInvestment } from '@ghostfolio/api/models/rules/account-cluster-risk/current-investment'; import { AccountClusterRiskCurrentInvestment } from '@ghostfolio/api/models/rules/account-cluster-risk/current-investment';
import { AccountClusterRiskInitialInvestment } from '@ghostfolio/api/models/rules/account-cluster-risk/initial-investment'; import { AccountClusterRiskInitialInvestment } from '@ghostfolio/api/models/rules/account-cluster-risk/initial-investment';
import { AccountClusterRiskSingleAccount } from '@ghostfolio/api/models/rules/account-cluster-risk/single-account'; import { AccountClusterRiskSingleAccount } from '@ghostfolio/api/models/rules/account-cluster-risk/single-account';
@ -21,11 +20,7 @@ import { ImpersonationService } from '@ghostfolio/api/services/impersonation.ser
import { MarketState } from '@ghostfolio/api/services/interfaces/interfaces'; import { MarketState } from '@ghostfolio/api/services/interfaces/interfaces';
import { EnhancedSymbolProfile } from '@ghostfolio/api/services/interfaces/symbol-profile.interface'; import { EnhancedSymbolProfile } from '@ghostfolio/api/services/interfaces/symbol-profile.interface';
import { SymbolProfileService } from '@ghostfolio/api/services/symbol-profile.service'; import { SymbolProfileService } from '@ghostfolio/api/services/symbol-profile.service';
import { import { UNKNOWN_KEY, baseCurrency } from '@ghostfolio/common/config';
UNKNOWN_KEY,
baseCurrency,
ghostfolioCashSymbol
} from '@ghostfolio/common/config';
import { DATE_FORMAT, parseDate } from '@ghostfolio/common/helper'; import { DATE_FORMAT, parseDate } from '@ghostfolio/common/helper';
import { import {
Accounts, Accounts,
@ -413,7 +408,7 @@ export class PortfolioService {
name: order.SymbolProfile?.name, name: order.SymbolProfile?.name,
quantity: new Big(order.quantity), quantity: new Big(order.quantity),
symbol: order.symbol, symbol: order.symbol,
type: <OrderType>order.type, type: order.type,
unitPrice: new Big(order.unitPrice) unitPrice: new Big(order.unitPrice)
})); }));
@ -850,8 +845,8 @@ export class PortfolioService {
const fees = this.getFees(orders); const fees = this.getFees(orders);
const firstOrderDate = orders[0]?.date; const firstOrderDate = orders[0]?.date;
const totalBuy = this.getTotalByType(orders, currency, TypeOfOrder.BUY); const totalBuy = this.getTotalByType(orders, currency, 'BUY');
const totalSell = this.getTotalByType(orders, currency, TypeOfOrder.SELL); const totalSell = this.getTotalByType(orders, currency, 'SELL');
const committedFunds = new Big(totalBuy).sub(totalSell); const committedFunds = new Big(totalBuy).sub(totalSell);
@ -990,7 +985,7 @@ export class PortfolioService {
name: order.SymbolProfile?.name, name: order.SymbolProfile?.name,
quantity: new Big(order.quantity), quantity: new Big(order.quantity),
symbol: order.symbol, symbol: order.symbol,
type: <OrderType>order.type, type: order.type,
unitPrice: new Big( unitPrice: new Big(
this.exchangeRateDataService.toCurrency( this.exchangeRateDataService.toCurrency(
order.unitPrice, order.unitPrice,

8
apps/api/src/models/order-type.ts

@ -1,8 +0,0 @@
export enum OrderType {
CorporateAction = 'CORPORATE_ACTION',
Bonus = 'BONUS',
Buy = 'BUY',
Dividend = 'DIVIDEND',
Sell = 'SELL',
Split = 'SPLIT'
}

5
apps/api/src/models/order.ts

@ -1,8 +1,7 @@
import { Account, SymbolProfile } from '@prisma/client'; import { Account, SymbolProfile, Type as TypeOfOrder } from '@prisma/client';
import { v4 as uuidv4 } from 'uuid'; import { v4 as uuidv4 } from 'uuid';
import { IOrder } from '../services/interfaces/interfaces'; import { IOrder } from '../services/interfaces/interfaces';
import { OrderType } from './order-type';
export class Order { export class Order {
private account: Account; private account: Account;
@ -15,7 +14,7 @@ export class Order {
private symbol: string; private symbol: string;
private symbolProfile: SymbolProfile; private symbolProfile: SymbolProfile;
private total: number; private total: number;
private type: OrderType; private type: TypeOfOrder;
private unitPrice: number; private unitPrice: number;
public constructor(data: IOrder) { public constructor(data: IOrder) {

7
apps/api/src/services/interfaces/interfaces.ts

@ -3,11 +3,10 @@ import {
AssetClass, AssetClass,
AssetSubClass, AssetSubClass,
DataSource, DataSource,
SymbolProfile SymbolProfile,
Type as TypeOfOrder
} from '@prisma/client'; } from '@prisma/client';
import { OrderType } from '../../models/order-type';
export const MarketState = { export const MarketState = {
closed: 'closed', closed: 'closed',
delayed: 'delayed', delayed: 'delayed',
@ -24,7 +23,7 @@ export interface IOrder {
quantity: number; quantity: number;
symbol: string; symbol: string;
symbolProfile: SymbolProfile; symbolProfile: SymbolProfile;
type: OrderType; type: TypeOfOrder;
unitPrice: number; unitPrice: number;
} }

Loading…
Cancel
Save