mirror of https://github.com/ghostfolio/ghostfolio
629 changed files with 40133 additions and 34516 deletions
@ -0,0 +1,14 @@ |
|||
import { defineConfig } from '@prisma/config'; |
|||
import { config } from 'dotenv'; |
|||
import { expand } from 'dotenv-expand'; |
|||
import { join } from 'node:path'; |
|||
|
|||
expand(config({ quiet: true })); |
|||
|
|||
export default defineConfig({ |
|||
migrations: { |
|||
path: join(__dirname, '..', 'prisma', 'migrations'), |
|||
seed: `node ${join(__dirname, '..', 'prisma', 'seed.mts')}` |
|||
}, |
|||
schema: join(__dirname, '..', 'prisma', 'schema.prisma') |
|||
}); |
|||
@ -1 +1,2 @@ |
|||
custom: ['https://www.buymeacoffee.com/ghostfolio'] |
|||
buy_me_a_coffee: ghostfolio |
|||
github: ghostfolio |
|||
|
|||
@ -1,8 +1,8 @@ |
|||
{ |
|||
"recommendations": [ |
|||
"angular.ng-template", |
|||
"esbenp.prettier-vscode", |
|||
"firsttris.vscode-jest-runner", |
|||
"nrwl.angular-console" |
|||
"nrwl.angular-console", |
|||
"prettier.prettier-vscode" |
|||
] |
|||
} |
|||
|
|||
@ -1,4 +1,4 @@ |
|||
{ |
|||
"editor.defaultFormatter": "esbenp.prettier-vscode", |
|||
"editor.defaultFormatter": "prettier.prettier-vscode", |
|||
"editor.formatOnSave": true |
|||
} |
|||
|
|||
@ -0,0 +1,114 @@ |
|||
import ms from 'ms'; |
|||
|
|||
/** |
|||
* Custom state store for OIDC authentication that doesn't rely on express-session. |
|||
* This store manages OAuth2 state parameters in memory with automatic cleanup. |
|||
*/ |
|||
export class OidcStateStore { |
|||
private readonly STATE_EXPIRY_MS = ms('10 minutes'); |
|||
|
|||
private stateMap = new Map< |
|||
string, |
|||
{ |
|||
appState?: unknown; |
|||
ctx: { issued?: Date; maxAge?: number; nonce?: string }; |
|||
meta?: unknown; |
|||
timestamp: number; |
|||
} |
|||
>(); |
|||
|
|||
/** |
|||
* Store request state. |
|||
* Signature matches passport-openidconnect SessionStore |
|||
*/ |
|||
public store( |
|||
_req: unknown, |
|||
_meta: unknown, |
|||
appState: unknown, |
|||
ctx: { maxAge?: number; nonce?: string; issued?: Date }, |
|||
callback: (err: Error | null, handle?: string) => void |
|||
) { |
|||
try { |
|||
// Generate a unique handle for this state
|
|||
const handle = this.generateHandle(); |
|||
|
|||
this.stateMap.set(handle, { |
|||
appState, |
|||
ctx, |
|||
meta: _meta, |
|||
timestamp: Date.now() |
|||
}); |
|||
|
|||
// Clean up expired states
|
|||
this.cleanup(); |
|||
|
|||
callback(null, handle); |
|||
} catch (error) { |
|||
callback(error as Error); |
|||
} |
|||
} |
|||
|
|||
/** |
|||
* Verify request state. |
|||
* Signature matches passport-openidconnect SessionStore |
|||
*/ |
|||
public verify( |
|||
_req: unknown, |
|||
handle: string, |
|||
callback: ( |
|||
err: Error | null, |
|||
appState?: unknown, |
|||
ctx?: { maxAge?: number; nonce?: string; issued?: Date } |
|||
) => void |
|||
) { |
|||
try { |
|||
const data = this.stateMap.get(handle); |
|||
|
|||
if (!data) { |
|||
return callback(null, undefined, undefined); |
|||
} |
|||
|
|||
if (Date.now() - data.timestamp > this.STATE_EXPIRY_MS) { |
|||
// State has expired
|
|||
this.stateMap.delete(handle); |
|||
return callback(null, undefined, undefined); |
|||
} |
|||
|
|||
// Remove state after verification (one-time use)
|
|||
this.stateMap.delete(handle); |
|||
|
|||
callback(null, data.ctx, data.appState); |
|||
} catch (error) { |
|||
callback(error as Error); |
|||
} |
|||
} |
|||
|
|||
/** |
|||
* Clean up expired states |
|||
*/ |
|||
private cleanup() { |
|||
const now = Date.now(); |
|||
const expiredKeys: string[] = []; |
|||
|
|||
for (const [key, value] of this.stateMap.entries()) { |
|||
if (now - value.timestamp > this.STATE_EXPIRY_MS) { |
|||
expiredKeys.push(key); |
|||
} |
|||
} |
|||
|
|||
for (const key of expiredKeys) { |
|||
this.stateMap.delete(key); |
|||
} |
|||
} |
|||
|
|||
/** |
|||
* Generate a cryptographically secure random handle |
|||
*/ |
|||
private generateHandle() { |
|||
return ( |
|||
Math.random().toString(36).substring(2, 15) + |
|||
Math.random().toString(36).substring(2, 15) + |
|||
Date.now().toString(36) |
|||
); |
|||
} |
|||
} |
|||
@ -0,0 +1,69 @@ |
|||
import { Injectable, Logger } from '@nestjs/common'; |
|||
import { PassportStrategy } from '@nestjs/passport'; |
|||
import { Provider } from '@prisma/client'; |
|||
import { Request } from 'express'; |
|||
import { Strategy, type StrategyOptions } from 'passport-openidconnect'; |
|||
|
|||
import { AuthService } from './auth.service'; |
|||
import { |
|||
OidcContext, |
|||
OidcIdToken, |
|||
OidcParams, |
|||
OidcProfile |
|||
} from './interfaces/interfaces'; |
|||
import { OidcStateStore } from './oidc-state.store'; |
|||
|
|||
@Injectable() |
|||
export class OidcStrategy extends PassportStrategy(Strategy, 'oidc') { |
|||
private static readonly stateStore = new OidcStateStore(); |
|||
|
|||
public constructor( |
|||
private readonly authService: AuthService, |
|||
options: StrategyOptions |
|||
) { |
|||
super({ |
|||
...options, |
|||
passReqToCallback: true, |
|||
store: OidcStrategy.stateStore |
|||
}); |
|||
} |
|||
|
|||
public async validate( |
|||
_request: Request, |
|||
issuer: string, |
|||
profile: OidcProfile, |
|||
context: OidcContext, |
|||
idToken: OidcIdToken, |
|||
_accessToken: string, |
|||
_refreshToken: string, |
|||
params: OidcParams |
|||
) { |
|||
try { |
|||
const thirdPartyId = |
|||
profile?.id ?? |
|||
profile?.sub ?? |
|||
idToken?.sub ?? |
|||
params?.sub ?? |
|||
context?.claims?.sub; |
|||
|
|||
const jwt = await this.authService.validateOAuthLogin({ |
|||
thirdPartyId, |
|||
provider: Provider.OIDC |
|||
}); |
|||
|
|||
if (!thirdPartyId) { |
|||
Logger.error( |
|||
`Missing subject identifier in OIDC response from ${issuer}`, |
|||
'OidcStrategy' |
|||
); |
|||
|
|||
throw new Error('Missing subject identifier in OIDC response'); |
|||
} |
|||
|
|||
return { jwt }; |
|||
} catch (error) { |
|||
Logger.error(error, 'OidcStrategy'); |
|||
throw error; |
|||
} |
|||
} |
|||
} |
|||
@ -1,24 +0,0 @@ |
|||
import { Type } from 'class-transformer'; |
|||
import { |
|||
ArrayNotEmpty, |
|||
IsArray, |
|||
IsISO8601, |
|||
IsNumber, |
|||
IsOptional |
|||
} from 'class-validator'; |
|||
|
|||
export class UpdateBulkMarketDataDto { |
|||
@ArrayNotEmpty() |
|||
@IsArray() |
|||
@Type(() => UpdateMarketDataDto) |
|||
marketData: UpdateMarketDataDto[]; |
|||
} |
|||
|
|||
class UpdateMarketDataDto { |
|||
@IsISO8601() |
|||
@IsOptional() |
|||
date?: string; |
|||
|
|||
@IsNumber() |
|||
marketPrice: number; |
|||
} |
|||
@ -0,0 +1,22 @@ |
|||
import { PlatformService } from '@ghostfolio/api/app/platform/platform.service'; |
|||
import { HasPermission } from '@ghostfolio/api/decorators/has-permission.decorator'; |
|||
import { HasPermissionGuard } from '@ghostfolio/api/guards/has-permission.guard'; |
|||
import { PlatformsResponse } from '@ghostfolio/common/interfaces'; |
|||
import { permissions } from '@ghostfolio/common/permissions'; |
|||
|
|||
import { Controller, Get, UseGuards } from '@nestjs/common'; |
|||
import { AuthGuard } from '@nestjs/passport'; |
|||
|
|||
@Controller('platforms') |
|||
export class PlatformsController { |
|||
public constructor(private readonly platformService: PlatformService) {} |
|||
|
|||
@Get() |
|||
@HasPermission(permissions.readPlatforms) |
|||
@UseGuards(AuthGuard('jwt'), HasPermissionGuard) |
|||
public async getPlatforms(): Promise<PlatformsResponse> { |
|||
const platforms = await this.platformService.getPlatforms(); |
|||
|
|||
return { platforms }; |
|||
} |
|||
} |
|||
@ -0,0 +1,11 @@ |
|||
import { PlatformModule } from '@ghostfolio/api/app/platform/platform.module'; |
|||
|
|||
import { Module } from '@nestjs/common'; |
|||
|
|||
import { PlatformsController } from './platforms.controller'; |
|||
|
|||
@Module({ |
|||
controllers: [PlatformsController], |
|||
imports: [PlatformModule] |
|||
}) |
|||
export class PlatformsModule {} |
|||
@ -0,0 +1,205 @@ |
|||
import { |
|||
activityDummyData, |
|||
symbolProfileDummyData, |
|||
userDummyData |
|||
} from '@ghostfolio/api/app/portfolio/calculator/portfolio-calculator-test-utils'; |
|||
import { PortfolioCalculatorFactory } from '@ghostfolio/api/app/portfolio/calculator/portfolio-calculator.factory'; |
|||
import { CurrentRateService } from '@ghostfolio/api/app/portfolio/current-rate.service'; |
|||
import { CurrentRateServiceMock } from '@ghostfolio/api/app/portfolio/current-rate.service.mock'; |
|||
import { RedisCacheService } from '@ghostfolio/api/app/redis-cache/redis-cache.service'; |
|||
import { RedisCacheServiceMock } from '@ghostfolio/api/app/redis-cache/redis-cache.service.mock'; |
|||
import { ConfigurationService } from '@ghostfolio/api/services/configuration/configuration.service'; |
|||
import { ExchangeRateDataService } from '@ghostfolio/api/services/exchange-rate-data/exchange-rate-data.service'; |
|||
import { PortfolioSnapshotService } from '@ghostfolio/api/services/queues/portfolio-snapshot/portfolio-snapshot.service'; |
|||
import { PortfolioSnapshotServiceMock } from '@ghostfolio/api/services/queues/portfolio-snapshot/portfolio-snapshot.service.mock'; |
|||
import { parseDate } from '@ghostfolio/common/helper'; |
|||
import { Activity } from '@ghostfolio/common/interfaces'; |
|||
import { PerformanceCalculationType } from '@ghostfolio/common/types/performance-calculation-type.type'; |
|||
|
|||
import { Big } from 'big.js'; |
|||
|
|||
jest.mock('@ghostfolio/api/app/portfolio/current-rate.service', () => { |
|||
return { |
|||
CurrentRateService: jest.fn().mockImplementation(() => { |
|||
return CurrentRateServiceMock; |
|||
}) |
|||
}; |
|||
}); |
|||
|
|||
jest.mock( |
|||
'@ghostfolio/api/services/queues/portfolio-snapshot/portfolio-snapshot.service', |
|||
() => { |
|||
return { |
|||
PortfolioSnapshotService: jest.fn().mockImplementation(() => { |
|||
return PortfolioSnapshotServiceMock; |
|||
}) |
|||
}; |
|||
} |
|||
); |
|||
|
|||
jest.mock('@ghostfolio/api/app/redis-cache/redis-cache.service', () => { |
|||
return { |
|||
RedisCacheService: jest.fn().mockImplementation(() => { |
|||
return RedisCacheServiceMock; |
|||
}) |
|||
}; |
|||
}); |
|||
|
|||
describe('PortfolioCalculator', () => { |
|||
let configurationService: ConfigurationService; |
|||
let currentRateService: CurrentRateService; |
|||
let exchangeRateDataService: ExchangeRateDataService; |
|||
let portfolioCalculatorFactory: PortfolioCalculatorFactory; |
|||
let portfolioSnapshotService: PortfolioSnapshotService; |
|||
let redisCacheService: RedisCacheService; |
|||
|
|||
beforeEach(() => { |
|||
configurationService = new ConfigurationService(); |
|||
|
|||
currentRateService = new CurrentRateService(null, null, null, null); |
|||
|
|||
exchangeRateDataService = new ExchangeRateDataService( |
|||
null, |
|||
null, |
|||
null, |
|||
null |
|||
); |
|||
|
|||
portfolioSnapshotService = new PortfolioSnapshotService(null); |
|||
|
|||
redisCacheService = new RedisCacheService(null, null); |
|||
|
|||
portfolioCalculatorFactory = new PortfolioCalculatorFactory( |
|||
configurationService, |
|||
currentRateService, |
|||
exchangeRateDataService, |
|||
portfolioSnapshotService, |
|||
redisCacheService |
|||
); |
|||
}); |
|||
|
|||
describe('get current positions', () => { |
|||
it.only('with BALN.SW buy and buy', async () => { |
|||
jest.useFakeTimers().setSystemTime(parseDate('2021-12-18').getTime()); |
|||
|
|||
const activities: Activity[] = [ |
|||
{ |
|||
...activityDummyData, |
|||
date: new Date('2021-11-22'), |
|||
feeInAssetProfileCurrency: 1.55, |
|||
quantity: 2, |
|||
SymbolProfile: { |
|||
...symbolProfileDummyData, |
|||
currency: 'CHF', |
|||
dataSource: 'YAHOO', |
|||
name: 'Bâloise Holding AG', |
|||
symbol: 'BALN.SW' |
|||
}, |
|||
type: 'BUY', |
|||
unitPriceInAssetProfileCurrency: 142.9 |
|||
}, |
|||
{ |
|||
...activityDummyData, |
|||
date: new Date('2021-11-30'), |
|||
feeInAssetProfileCurrency: 1.65, |
|||
quantity: 2, |
|||
SymbolProfile: { |
|||
...symbolProfileDummyData, |
|||
currency: 'CHF', |
|||
dataSource: 'YAHOO', |
|||
name: 'Bâloise Holding AG', |
|||
symbol: 'BALN.SW' |
|||
}, |
|||
type: 'BUY', |
|||
unitPriceInAssetProfileCurrency: 136.6 |
|||
} |
|||
]; |
|||
|
|||
const portfolioCalculator = portfolioCalculatorFactory.createCalculator({ |
|||
activities, |
|||
calculationType: PerformanceCalculationType.ROAI, |
|||
currency: 'CHF', |
|||
userId: userDummyData.id |
|||
}); |
|||
|
|||
const portfolioSnapshot = await portfolioCalculator.computeSnapshot(); |
|||
|
|||
const investments = portfolioCalculator.getInvestments(); |
|||
|
|||
const investmentsByMonth = portfolioCalculator.getInvestmentsByGroup({ |
|||
data: portfolioSnapshot.historicalData, |
|||
groupBy: 'month' |
|||
}); |
|||
|
|||
expect(portfolioSnapshot).toMatchObject({ |
|||
currentValueInBaseCurrency: new Big('595.6'), |
|||
errors: [], |
|||
hasErrors: false, |
|||
positions: [ |
|||
{ |
|||
averagePrice: new Big('139.75'), |
|||
currency: 'CHF', |
|||
dataSource: 'YAHOO', |
|||
dividend: new Big('0'), |
|||
dividendInBaseCurrency: new Big('0'), |
|||
fee: new Big('3.2'), |
|||
feeInBaseCurrency: new Big('3.2'), |
|||
firstBuyDate: '2021-11-22', |
|||
grossPerformance: new Big('36.6'), |
|||
grossPerformancePercentage: new Big('0.07706261539956593567'), |
|||
grossPerformancePercentageWithCurrencyEffect: new Big( |
|||
'0.07706261539956593567' |
|||
), |
|||
grossPerformanceWithCurrencyEffect: new Big('36.6'), |
|||
investment: new Big('559'), |
|||
investmentWithCurrencyEffect: new Big('559'), |
|||
netPerformance: new Big('33.4'), |
|||
netPerformancePercentage: new Big('0.07032490039195361342'), |
|||
netPerformancePercentageWithCurrencyEffectMap: { |
|||
max: new Big('0.06986689805847808234') |
|||
}, |
|||
netPerformanceWithCurrencyEffectMap: { |
|||
max: new Big('33.4') |
|||
}, |
|||
marketPrice: 148.9, |
|||
marketPriceInBaseCurrency: 148.9, |
|||
quantity: new Big('4'), |
|||
symbol: 'BALN.SW', |
|||
tags: [], |
|||
timeWeightedInvestment: new Big('474.93846153846153846154'), |
|||
timeWeightedInvestmentWithCurrencyEffect: new Big( |
|||
'474.93846153846153846154' |
|||
), |
|||
transactionCount: 2, |
|||
valueInBaseCurrency: new Big('595.6') |
|||
} |
|||
], |
|||
totalFeesWithCurrencyEffect: new Big('3.2'), |
|||
totalInterestWithCurrencyEffect: new Big('0'), |
|||
totalInvestment: new Big('559'), |
|||
totalInvestmentWithCurrencyEffect: new Big('559'), |
|||
totalLiabilitiesWithCurrencyEffect: new Big('0') |
|||
}); |
|||
|
|||
expect(portfolioSnapshot.historicalData.at(-1)).toMatchObject( |
|||
expect.objectContaining({ |
|||
netPerformance: 33.4, |
|||
netPerformanceInPercentage: 0.07032490039195362, |
|||
netPerformanceInPercentageWithCurrencyEffect: 0.07032490039195362, |
|||
netPerformanceWithCurrencyEffect: 33.4, |
|||
totalInvestmentValueWithCurrencyEffect: 559 |
|||
}) |
|||
); |
|||
|
|||
expect(investments).toEqual([ |
|||
{ date: '2021-11-22', investment: new Big('285.8') }, |
|||
{ date: '2021-11-30', investment: new Big('559') } |
|||
]); |
|||
|
|||
expect(investmentsByMonth).toEqual([ |
|||
{ date: '2021-11-01', investment: 559 }, |
|||
{ date: '2021-12-01', investment: 0 } |
|||
]); |
|||
}); |
|||
}); |
|||
}); |
|||
@ -0,0 +1,150 @@ |
|||
import { |
|||
activityDummyData, |
|||
loadExportFile, |
|||
symbolProfileDummyData, |
|||
userDummyData |
|||
} from '@ghostfolio/api/app/portfolio/calculator/portfolio-calculator-test-utils'; |
|||
import { PortfolioCalculatorFactory } from '@ghostfolio/api/app/portfolio/calculator/portfolio-calculator.factory'; |
|||
import { CurrentRateService } from '@ghostfolio/api/app/portfolio/current-rate.service'; |
|||
import { CurrentRateServiceMock } from '@ghostfolio/api/app/portfolio/current-rate.service.mock'; |
|||
import { RedisCacheService } from '@ghostfolio/api/app/redis-cache/redis-cache.service'; |
|||
import { RedisCacheServiceMock } from '@ghostfolio/api/app/redis-cache/redis-cache.service.mock'; |
|||
import { ConfigurationService } from '@ghostfolio/api/services/configuration/configuration.service'; |
|||
import { ExchangeRateDataService } from '@ghostfolio/api/services/exchange-rate-data/exchange-rate-data.service'; |
|||
import { ExchangeRateDataServiceMock } from '@ghostfolio/api/services/exchange-rate-data/exchange-rate-data.service.mock'; |
|||
import { PortfolioSnapshotService } from '@ghostfolio/api/services/queues/portfolio-snapshot/portfolio-snapshot.service'; |
|||
import { PortfolioSnapshotServiceMock } from '@ghostfolio/api/services/queues/portfolio-snapshot/portfolio-snapshot.service.mock'; |
|||
import { parseDate } from '@ghostfolio/common/helper'; |
|||
import { Activity, ExportResponse } from '@ghostfolio/common/interfaces'; |
|||
import { PerformanceCalculationType } from '@ghostfolio/common/types/performance-calculation-type.type'; |
|||
|
|||
import { Big } from 'big.js'; |
|||
import { join } from 'node:path'; |
|||
|
|||
jest.mock('@ghostfolio/api/app/portfolio/current-rate.service', () => { |
|||
return { |
|||
CurrentRateService: jest.fn().mockImplementation(() => { |
|||
return CurrentRateServiceMock; |
|||
}) |
|||
}; |
|||
}); |
|||
|
|||
jest.mock( |
|||
'@ghostfolio/api/services/exchange-rate-data/exchange-rate-data.service', |
|||
() => { |
|||
return { |
|||
ExchangeRateDataService: jest.fn().mockImplementation(() => { |
|||
return ExchangeRateDataServiceMock; |
|||
}) |
|||
}; |
|||
} |
|||
); |
|||
|
|||
jest.mock( |
|||
'@ghostfolio/api/services/queues/portfolio-snapshot/portfolio-snapshot.service', |
|||
() => { |
|||
return { |
|||
PortfolioSnapshotService: jest.fn().mockImplementation(() => { |
|||
return PortfolioSnapshotServiceMock; |
|||
}) |
|||
}; |
|||
} |
|||
); |
|||
|
|||
jest.mock('@ghostfolio/api/app/redis-cache/redis-cache.service', () => { |
|||
return { |
|||
RedisCacheService: jest.fn().mockImplementation(() => { |
|||
return RedisCacheServiceMock; |
|||
}) |
|||
}; |
|||
}); |
|||
|
|||
describe('PortfolioCalculator', () => { |
|||
let exportResponse: ExportResponse; |
|||
|
|||
let configurationService: ConfigurationService; |
|||
let currentRateService: CurrentRateService; |
|||
let exchangeRateDataService: ExchangeRateDataService; |
|||
let portfolioCalculatorFactory: PortfolioCalculatorFactory; |
|||
let portfolioSnapshotService: PortfolioSnapshotService; |
|||
let redisCacheService: RedisCacheService; |
|||
|
|||
beforeAll(() => { |
|||
exportResponse = loadExportFile( |
|||
join(__dirname, '../../../../../../../test/import/ok/btceur.json') |
|||
); |
|||
}); |
|||
|
|||
beforeEach(() => { |
|||
configurationService = new ConfigurationService(); |
|||
|
|||
currentRateService = new CurrentRateService(null, null, null, null); |
|||
|
|||
exchangeRateDataService = new ExchangeRateDataService( |
|||
null, |
|||
null, |
|||
null, |
|||
null |
|||
); |
|||
|
|||
portfolioSnapshotService = new PortfolioSnapshotService(null); |
|||
|
|||
redisCacheService = new RedisCacheService(null, null); |
|||
|
|||
portfolioCalculatorFactory = new PortfolioCalculatorFactory( |
|||
configurationService, |
|||
currentRateService, |
|||
exchangeRateDataService, |
|||
portfolioSnapshotService, |
|||
redisCacheService |
|||
); |
|||
}); |
|||
|
|||
describe('get current positions', () => { |
|||
it.only('with BTCUSD buy (in EUR)', async () => { |
|||
jest.useFakeTimers().setSystemTime(parseDate('2022-01-14').getTime()); |
|||
|
|||
const activities: Activity[] = exportResponse.activities.map( |
|||
(activity) => ({ |
|||
...activityDummyData, |
|||
...activity, |
|||
date: parseDate(activity.date), |
|||
feeInAssetProfileCurrency: 4.46, |
|||
SymbolProfile: { |
|||
...symbolProfileDummyData, |
|||
currency: 'USD', |
|||
dataSource: activity.dataSource, |
|||
name: 'Bitcoin', |
|||
symbol: activity.symbol |
|||
}, |
|||
unitPriceInAssetProfileCurrency: 44558.42 |
|||
}) |
|||
); |
|||
|
|||
const portfolioCalculator = portfolioCalculatorFactory.createCalculator({ |
|||
activities, |
|||
calculationType: PerformanceCalculationType.ROAI, |
|||
currency: 'EUR', |
|||
userId: userDummyData.id |
|||
}); |
|||
|
|||
const portfolioSnapshot = await portfolioCalculator.computeSnapshot(); |
|||
|
|||
const historicalDataDates = portfolioSnapshot.historicalData.map( |
|||
({ date }) => { |
|||
return date; |
|||
} |
|||
); |
|||
|
|||
expect(historicalDataDates).not.toContain('2021-01-01'); |
|||
expect(historicalDataDates).toContain('2021-12-31'); |
|||
expect(historicalDataDates).toContain('2022-01-01'); |
|||
expect(historicalDataDates).not.toContain('2022-12-31'); |
|||
|
|||
expect(portfolioSnapshot.positions[0].fee).toEqual(new Big(4.46)); |
|||
expect( |
|||
portfolioSnapshot.positions[0].feeInBaseCurrency.toNumber() |
|||
).toBeCloseTo(3.94, 1); |
|||
}); |
|||
}); |
|||
}); |
|||
@ -0,0 +1,126 @@ |
|||
import { |
|||
activityDummyData, |
|||
loadExportFile, |
|||
symbolProfileDummyData, |
|||
userDummyData |
|||
} from '@ghostfolio/api/app/portfolio/calculator/portfolio-calculator-test-utils'; |
|||
import { PortfolioCalculatorFactory } from '@ghostfolio/api/app/portfolio/calculator/portfolio-calculator.factory'; |
|||
import { CurrentRateService } from '@ghostfolio/api/app/portfolio/current-rate.service'; |
|||
import { CurrentRateServiceMock } from '@ghostfolio/api/app/portfolio/current-rate.service.mock'; |
|||
import { RedisCacheService } from '@ghostfolio/api/app/redis-cache/redis-cache.service'; |
|||
import { RedisCacheServiceMock } from '@ghostfolio/api/app/redis-cache/redis-cache.service.mock'; |
|||
import { ConfigurationService } from '@ghostfolio/api/services/configuration/configuration.service'; |
|||
import { ExchangeRateDataService } from '@ghostfolio/api/services/exchange-rate-data/exchange-rate-data.service'; |
|||
import { PortfolioSnapshotService } from '@ghostfolio/api/services/queues/portfolio-snapshot/portfolio-snapshot.service'; |
|||
import { PortfolioSnapshotServiceMock } from '@ghostfolio/api/services/queues/portfolio-snapshot/portfolio-snapshot.service.mock'; |
|||
import { parseDate } from '@ghostfolio/common/helper'; |
|||
import { Activity, ExportResponse } from '@ghostfolio/common/interfaces'; |
|||
import { PerformanceCalculationType } from '@ghostfolio/common/types/performance-calculation-type.type'; |
|||
|
|||
import { Big } from 'big.js'; |
|||
import { join } from 'node:path'; |
|||
|
|||
jest.mock('@ghostfolio/api/app/portfolio/current-rate.service', () => { |
|||
return { |
|||
CurrentRateService: jest.fn().mockImplementation(() => { |
|||
return CurrentRateServiceMock; |
|||
}) |
|||
}; |
|||
}); |
|||
|
|||
jest.mock( |
|||
'@ghostfolio/api/services/queues/portfolio-snapshot/portfolio-snapshot.service', |
|||
() => { |
|||
return { |
|||
PortfolioSnapshotService: jest.fn().mockImplementation(() => { |
|||
return PortfolioSnapshotServiceMock; |
|||
}) |
|||
}; |
|||
} |
|||
); |
|||
|
|||
jest.mock('@ghostfolio/api/app/redis-cache/redis-cache.service', () => { |
|||
return { |
|||
RedisCacheService: jest.fn().mockImplementation(() => { |
|||
return RedisCacheServiceMock; |
|||
}) |
|||
}; |
|||
}); |
|||
|
|||
describe('PortfolioCalculator', () => { |
|||
let exportResponse: ExportResponse; |
|||
|
|||
let configurationService: ConfigurationService; |
|||
let currentRateService: CurrentRateService; |
|||
let exchangeRateDataService: ExchangeRateDataService; |
|||
let portfolioCalculatorFactory: PortfolioCalculatorFactory; |
|||
let portfolioSnapshotService: PortfolioSnapshotService; |
|||
let redisCacheService: RedisCacheService; |
|||
|
|||
beforeAll(() => { |
|||
exportResponse = loadExportFile( |
|||
join(__dirname, '../../../../../../../test/import/ok/btcusd-short.json') |
|||
); |
|||
}); |
|||
|
|||
beforeEach(() => { |
|||
configurationService = new ConfigurationService(); |
|||
|
|||
currentRateService = new CurrentRateService(null, null, null, null); |
|||
|
|||
exchangeRateDataService = new ExchangeRateDataService( |
|||
null, |
|||
null, |
|||
null, |
|||
null |
|||
); |
|||
|
|||
portfolioSnapshotService = new PortfolioSnapshotService(null); |
|||
|
|||
redisCacheService = new RedisCacheService(null, null); |
|||
|
|||
portfolioCalculatorFactory = new PortfolioCalculatorFactory( |
|||
configurationService, |
|||
currentRateService, |
|||
exchangeRateDataService, |
|||
portfolioSnapshotService, |
|||
redisCacheService |
|||
); |
|||
}); |
|||
|
|||
describe('get current positions', () => { |
|||
it.only('with BTCUSD short sell (in USD)', async () => { |
|||
jest.useFakeTimers().setSystemTime(parseDate('2022-01-14').getTime()); |
|||
|
|||
const activities: Activity[] = exportResponse.activities.map( |
|||
(activity) => ({ |
|||
...activityDummyData, |
|||
...activity, |
|||
date: parseDate(activity.date), |
|||
feeInAssetProfileCurrency: activity.fee, |
|||
SymbolProfile: { |
|||
...symbolProfileDummyData, |
|||
currency: 'USD', |
|||
dataSource: activity.dataSource, |
|||
name: 'Bitcoin', |
|||
symbol: activity.symbol |
|||
}, |
|||
unitPriceInAssetProfileCurrency: activity.unitPrice |
|||
}) |
|||
); |
|||
|
|||
const portfolioCalculator = portfolioCalculatorFactory.createCalculator({ |
|||
activities, |
|||
calculationType: PerformanceCalculationType.ROAI, |
|||
currency: exportResponse.user.settings.currency, |
|||
userId: userDummyData.id |
|||
}); |
|||
|
|||
const portfolioSnapshot = await portfolioCalculator.computeSnapshot(); |
|||
|
|||
expect(portfolioSnapshot.positions[0].averagePrice).toEqual( |
|||
Big(45647.95) |
|||
); |
|||
}); |
|||
}); |
|||
}); |
|||
@ -0,0 +1,290 @@ |
|||
import { AccountBalanceService } from '@ghostfolio/api/app/account-balance/account-balance.service'; |
|||
import { AccountService } from '@ghostfolio/api/app/account/account.service'; |
|||
import { OrderService } from '@ghostfolio/api/app/order/order.service'; |
|||
import { userDummyData } from '@ghostfolio/api/app/portfolio/calculator/portfolio-calculator-test-utils'; |
|||
import { PortfolioCalculatorFactory } from '@ghostfolio/api/app/portfolio/calculator/portfolio-calculator.factory'; |
|||
import { CurrentRateService } from '@ghostfolio/api/app/portfolio/current-rate.service'; |
|||
import { CurrentRateServiceMock } from '@ghostfolio/api/app/portfolio/current-rate.service.mock'; |
|||
import { RedisCacheService } from '@ghostfolio/api/app/redis-cache/redis-cache.service'; |
|||
import { RedisCacheServiceMock } from '@ghostfolio/api/app/redis-cache/redis-cache.service.mock'; |
|||
import { ConfigurationService } from '@ghostfolio/api/services/configuration/configuration.service'; |
|||
import { DataProviderService } from '@ghostfolio/api/services/data-provider/data-provider.service'; |
|||
import { ExchangeRateDataService } from '@ghostfolio/api/services/exchange-rate-data/exchange-rate-data.service'; |
|||
import { ExchangeRateDataServiceMock } from '@ghostfolio/api/services/exchange-rate-data/exchange-rate-data.service.mock'; |
|||
import { PortfolioSnapshotService } from '@ghostfolio/api/services/queues/portfolio-snapshot/portfolio-snapshot.service'; |
|||
import { PortfolioSnapshotServiceMock } from '@ghostfolio/api/services/queues/portfolio-snapshot/portfolio-snapshot.service.mock'; |
|||
import { parseDate } from '@ghostfolio/common/helper'; |
|||
import { HistoricalDataItem } from '@ghostfolio/common/interfaces'; |
|||
import { PerformanceCalculationType } from '@ghostfolio/common/types/performance-calculation-type.type'; |
|||
|
|||
import { DataSource } from '@prisma/client'; |
|||
import { randomUUID } from 'node:crypto'; |
|||
|
|||
jest.mock('@ghostfolio/api/app/portfolio/current-rate.service', () => { |
|||
return { |
|||
CurrentRateService: jest.fn().mockImplementation(() => { |
|||
return CurrentRateServiceMock; |
|||
}) |
|||
}; |
|||
}); |
|||
|
|||
jest.mock( |
|||
'@ghostfolio/api/services/exchange-rate-data/exchange-rate-data.service', |
|||
() => { |
|||
return { |
|||
ExchangeRateDataService: jest.fn().mockImplementation(() => { |
|||
return ExchangeRateDataServiceMock; |
|||
}) |
|||
}; |
|||
} |
|||
); |
|||
|
|||
jest.mock( |
|||
'@ghostfolio/api/services/queues/portfolio-snapshot/portfolio-snapshot.service', |
|||
() => { |
|||
return { |
|||
PortfolioSnapshotService: jest.fn().mockImplementation(() => { |
|||
return PortfolioSnapshotServiceMock; |
|||
}) |
|||
}; |
|||
} |
|||
); |
|||
|
|||
jest.mock('@ghostfolio/api/app/redis-cache/redis-cache.service', () => { |
|||
return { |
|||
RedisCacheService: jest.fn().mockImplementation(() => { |
|||
return RedisCacheServiceMock; |
|||
}) |
|||
}; |
|||
}); |
|||
|
|||
describe('PortfolioCalculator', () => { |
|||
let accountBalanceService: AccountBalanceService; |
|||
let accountService: AccountService; |
|||
let configurationService: ConfigurationService; |
|||
let currentRateService: CurrentRateService; |
|||
let dataProviderService: DataProviderService; |
|||
let exchangeRateDataService: ExchangeRateDataService; |
|||
let orderService: OrderService; |
|||
let portfolioCalculatorFactory: PortfolioCalculatorFactory; |
|||
let portfolioSnapshotService: PortfolioSnapshotService; |
|||
let redisCacheService: RedisCacheService; |
|||
|
|||
beforeEach(() => { |
|||
configurationService = new ConfigurationService(); |
|||
|
|||
exchangeRateDataService = new ExchangeRateDataService( |
|||
null, |
|||
null, |
|||
null, |
|||
null |
|||
); |
|||
|
|||
accountBalanceService = new AccountBalanceService( |
|||
null, |
|||
exchangeRateDataService, |
|||
null |
|||
); |
|||
|
|||
accountService = new AccountService( |
|||
accountBalanceService, |
|||
null, |
|||
exchangeRateDataService, |
|||
null |
|||
); |
|||
|
|||
redisCacheService = new RedisCacheService(null, configurationService); |
|||
|
|||
dataProviderService = new DataProviderService( |
|||
configurationService, |
|||
null, |
|||
null, |
|||
null, |
|||
null, |
|||
redisCacheService |
|||
); |
|||
|
|||
currentRateService = new CurrentRateService( |
|||
dataProviderService, |
|||
null, |
|||
null, |
|||
null |
|||
); |
|||
|
|||
orderService = new OrderService( |
|||
accountBalanceService, |
|||
accountService, |
|||
null, |
|||
dataProviderService, |
|||
null, |
|||
exchangeRateDataService, |
|||
null, |
|||
null |
|||
); |
|||
|
|||
portfolioSnapshotService = new PortfolioSnapshotService(null); |
|||
|
|||
portfolioCalculatorFactory = new PortfolioCalculatorFactory( |
|||
configurationService, |
|||
currentRateService, |
|||
exchangeRateDataService, |
|||
portfolioSnapshotService, |
|||
redisCacheService |
|||
); |
|||
}); |
|||
|
|||
describe('Cash Performance', () => { |
|||
it('should calculate performance for cash assets in CHF default currency', async () => { |
|||
jest.useFakeTimers().setSystemTime(parseDate('2025-01-01').getTime()); |
|||
|
|||
const accountId = randomUUID(); |
|||
|
|||
jest |
|||
.spyOn(accountBalanceService, 'getAccountBalances') |
|||
.mockResolvedValue({ |
|||
balances: [ |
|||
{ |
|||
accountId, |
|||
id: randomUUID(), |
|||
date: parseDate('2023-12-31'), |
|||
value: 1000, |
|||
valueInBaseCurrency: 850 |
|||
}, |
|||
{ |
|||
accountId, |
|||
id: randomUUID(), |
|||
date: parseDate('2024-12-31'), |
|||
value: 2000, |
|||
valueInBaseCurrency: 1800 |
|||
} |
|||
] |
|||
}); |
|||
|
|||
jest.spyOn(accountService, 'getCashDetails').mockResolvedValue({ |
|||
accounts: [ |
|||
{ |
|||
balance: 2000, |
|||
comment: null, |
|||
createdAt: parseDate('2023-12-31'), |
|||
currency: 'USD', |
|||
id: accountId, |
|||
isExcluded: false, |
|||
name: 'USD', |
|||
platformId: null, |
|||
updatedAt: parseDate('2023-12-31'), |
|||
userId: userDummyData.id |
|||
} |
|||
], |
|||
balanceInBaseCurrency: 1820 |
|||
}); |
|||
|
|||
jest |
|||
.spyOn(dataProviderService, 'getDataSourceForExchangeRates') |
|||
.mockReturnValue(DataSource.YAHOO); |
|||
|
|||
jest.spyOn(orderService, 'getOrders').mockResolvedValue({ |
|||
activities: [], |
|||
count: 0 |
|||
}); |
|||
|
|||
const { activities } = await orderService.getOrdersForPortfolioCalculator( |
|||
{ |
|||
userCurrency: 'CHF', |
|||
userId: userDummyData.id |
|||
} |
|||
); |
|||
|
|||
jest.spyOn(currentRateService, 'getValues').mockResolvedValue({ |
|||
dataProviderInfos: [], |
|||
errors: [], |
|||
values: [] |
|||
}); |
|||
|
|||
const portfolioCalculator = portfolioCalculatorFactory.createCalculator({ |
|||
activities, |
|||
calculationType: PerformanceCalculationType.ROAI, |
|||
currency: 'CHF', |
|||
userId: userDummyData.id |
|||
}); |
|||
|
|||
const { historicalData } = await portfolioCalculator.computeSnapshot(); |
|||
|
|||
const historicalData20231231 = historicalData.find(({ date }) => { |
|||
return date === '2023-12-31'; |
|||
}); |
|||
const historicalData20240101 = historicalData.find(({ date }) => { |
|||
return date === '2024-01-01'; |
|||
}); |
|||
const historicalData20241231 = historicalData.find(({ date }) => { |
|||
return date === '2024-12-31'; |
|||
}); |
|||
|
|||
/** |
|||
* Investment value with currency effect: 1000 USD * 0.85 = 850 CHF |
|||
* Total investment: 1000 USD * 0.91 = 910 CHF |
|||
* Value (current): 1000 USD * 0.91 = 910 CHF |
|||
* Value with currency effect: 1000 USD * 0.85 = 850 CHF |
|||
*/ |
|||
expect(historicalData20231231).toMatchObject({ |
|||
date: '2023-12-31', |
|||
investmentValueWithCurrencyEffect: 850, |
|||
netPerformance: 0, |
|||
netPerformanceInPercentage: 0, |
|||
netPerformanceInPercentageWithCurrencyEffect: 0, |
|||
netPerformanceWithCurrencyEffect: 0, |
|||
netWorth: 850, |
|||
totalAccountBalance: 0, |
|||
totalInvestment: 910, |
|||
totalInvestmentValueWithCurrencyEffect: 850, |
|||
value: 910, |
|||
valueWithCurrencyEffect: 850 |
|||
}); |
|||
|
|||
/** |
|||
* Net performance with currency effect: (1000 * 0.86) - (1000 * 0.85) = 10 CHF |
|||
* Total investment: 1000 USD * 0.91 = 910 CHF |
|||
* Total investment value with currency effect: 1000 USD * 0.85 = 850 CHF |
|||
* Value (current): 1000 USD * 0.91 = 910 CHF |
|||
* Value with currency effect: 1000 USD * 0.86 = 860 CHF |
|||
*/ |
|||
expect(historicalData20240101).toMatchObject({ |
|||
date: '2024-01-01', |
|||
investmentValueWithCurrencyEffect: 0, |
|||
netPerformance: 0, |
|||
netPerformanceInPercentage: 0, |
|||
netPerformanceInPercentageWithCurrencyEffect: 0.011764705882352941, |
|||
netPerformanceWithCurrencyEffect: 10, |
|||
netWorth: 860, |
|||
totalAccountBalance: 0, |
|||
totalInvestment: 910, |
|||
totalInvestmentValueWithCurrencyEffect: 850, |
|||
value: 910, |
|||
valueWithCurrencyEffect: 860 |
|||
}); |
|||
|
|||
/** |
|||
* Investment value with currency effect: 1000 USD * 0.90 = 900 CHF |
|||
* Net performance: (1000 USD * 1.0) - (1000 USD * 1.0) = 0 CHF |
|||
* Net performance with currency effect: (1000 USD * 0.9) - (1000 USD * 0.85) = 50 CHF |
|||
* Total investment: 2000 USD * 0.91 = 1820 CHF |
|||
* Total investment value with currency effect: (1000 USD * 0.85) + (1000 USD * 0.90) = 1750 CHF |
|||
* Value (current): 2000 USD * 0.91 = 1820 CHF |
|||
* Value with currency effect: 2000 USD * 0.9 = 1800 CHF |
|||
*/ |
|||
expect(historicalData20241231).toMatchObject<HistoricalDataItem>({ |
|||
date: '2024-12-31', |
|||
investmentValueWithCurrencyEffect: 900, |
|||
netPerformance: 0, |
|||
netPerformanceInPercentage: 0, |
|||
netPerformanceInPercentageWithCurrencyEffect: 0.058823529411764705, |
|||
netPerformanceWithCurrencyEffect: 50, |
|||
netWorth: 1800, |
|||
totalAccountBalance: 0, |
|||
totalInvestment: 1820, |
|||
totalInvestmentValueWithCurrencyEffect: 1750, |
|||
value: 1820, |
|||
valueWithCurrencyEffect: 1800 |
|||
}); |
|||
}); |
|||
}); |
|||
}); |
|||
@ -0,0 +1,144 @@ |
|||
import { |
|||
activityDummyData, |
|||
symbolProfileDummyData, |
|||
userDummyData |
|||
} from '@ghostfolio/api/app/portfolio/calculator/portfolio-calculator-test-utils'; |
|||
import { PortfolioCalculatorFactory } from '@ghostfolio/api/app/portfolio/calculator/portfolio-calculator.factory'; |
|||
import { CurrentRateService } from '@ghostfolio/api/app/portfolio/current-rate.service'; |
|||
import { CurrentRateServiceMock } from '@ghostfolio/api/app/portfolio/current-rate.service.mock'; |
|||
import { RedisCacheService } from '@ghostfolio/api/app/redis-cache/redis-cache.service'; |
|||
import { RedisCacheServiceMock } from '@ghostfolio/api/app/redis-cache/redis-cache.service.mock'; |
|||
import { ConfigurationService } from '@ghostfolio/api/services/configuration/configuration.service'; |
|||
import { ExchangeRateDataService } from '@ghostfolio/api/services/exchange-rate-data/exchange-rate-data.service'; |
|||
import { PortfolioSnapshotService } from '@ghostfolio/api/services/queues/portfolio-snapshot/portfolio-snapshot.service'; |
|||
import { PortfolioSnapshotServiceMock } from '@ghostfolio/api/services/queues/portfolio-snapshot/portfolio-snapshot.service.mock'; |
|||
import { parseDate } from '@ghostfolio/common/helper'; |
|||
import { Activity } from '@ghostfolio/common/interfaces'; |
|||
import { PerformanceCalculationType } from '@ghostfolio/common/types/performance-calculation-type.type'; |
|||
|
|||
jest.mock('@ghostfolio/api/app/portfolio/current-rate.service', () => { |
|||
return { |
|||
CurrentRateService: jest.fn().mockImplementation(() => { |
|||
return CurrentRateServiceMock; |
|||
}) |
|||
}; |
|||
}); |
|||
|
|||
jest.mock( |
|||
'@ghostfolio/api/services/queues/portfolio-snapshot/portfolio-snapshot.service', |
|||
() => { |
|||
return { |
|||
PortfolioSnapshotService: jest.fn().mockImplementation(() => { |
|||
return PortfolioSnapshotServiceMock; |
|||
}) |
|||
}; |
|||
} |
|||
); |
|||
|
|||
jest.mock('@ghostfolio/api/app/redis-cache/redis-cache.service', () => { |
|||
return { |
|||
RedisCacheService: jest.fn().mockImplementation(() => { |
|||
return RedisCacheServiceMock; |
|||
}) |
|||
}; |
|||
}); |
|||
|
|||
describe('PortfolioCalculator', () => { |
|||
let configurationService: ConfigurationService; |
|||
let currentRateService: CurrentRateService; |
|||
let exchangeRateDataService: ExchangeRateDataService; |
|||
let portfolioCalculatorFactory: PortfolioCalculatorFactory; |
|||
let portfolioSnapshotService: PortfolioSnapshotService; |
|||
let redisCacheService: RedisCacheService; |
|||
|
|||
beforeEach(() => { |
|||
configurationService = new ConfigurationService(); |
|||
currentRateService = new CurrentRateService(null, null, null, null); |
|||
exchangeRateDataService = new ExchangeRateDataService( |
|||
null, |
|||
null, |
|||
null, |
|||
null |
|||
); |
|||
portfolioSnapshotService = new PortfolioSnapshotService(null); |
|||
redisCacheService = new RedisCacheService(null, null); |
|||
portfolioCalculatorFactory = new PortfolioCalculatorFactory( |
|||
configurationService, |
|||
currentRateService, |
|||
exchangeRateDataService, |
|||
portfolioSnapshotService, |
|||
redisCacheService |
|||
); |
|||
}); |
|||
|
|||
describe('get transaction point', () => { |
|||
it('with MSFT buy and sell with fractional quantities (multiples of 1/3)', () => { |
|||
jest.useFakeTimers().setSystemTime(parseDate('2024-04-01').getTime()); |
|||
|
|||
const activities: Activity[] = [ |
|||
{ |
|||
...activityDummyData, |
|||
date: new Date('2024-03-08'), |
|||
feeInAssetProfileCurrency: 0, |
|||
quantity: 0.3333333333333333, |
|||
SymbolProfile: { |
|||
...symbolProfileDummyData, |
|||
currency: 'USD', |
|||
dataSource: 'YAHOO', |
|||
name: 'Microsoft Inc.', |
|||
symbol: 'MSFT' |
|||
}, |
|||
type: 'BUY', |
|||
unitPriceInAssetProfileCurrency: 408 |
|||
}, |
|||
{ |
|||
...activityDummyData, |
|||
date: new Date('2024-03-13'), |
|||
quantity: 0.6666666666666666, |
|||
feeInAssetProfileCurrency: 0, |
|||
SymbolProfile: { |
|||
...symbolProfileDummyData, |
|||
currency: 'USD', |
|||
dataSource: 'YAHOO', |
|||
name: 'Microsoft Inc.', |
|||
symbol: 'MSFT' |
|||
}, |
|||
type: 'BUY', |
|||
unitPriceInAssetProfileCurrency: 400 |
|||
}, |
|||
{ |
|||
...activityDummyData, |
|||
date: new Date('2024-03-14'), |
|||
quantity: 1, |
|||
feeInAssetProfileCurrency: 0, |
|||
SymbolProfile: { |
|||
...symbolProfileDummyData, |
|||
currency: 'USD', |
|||
dataSource: 'YAHOO', |
|||
name: 'Microsoft Inc.', |
|||
symbol: 'MSFT' |
|||
}, |
|||
type: 'SELL', |
|||
unitPriceInAssetProfileCurrency: 411 |
|||
} |
|||
]; |
|||
|
|||
const portfolioCalculator = portfolioCalculatorFactory.createCalculator({ |
|||
activities, |
|||
calculationType: PerformanceCalculationType.ROAI, |
|||
currency: 'USD', |
|||
userId: userDummyData.id |
|||
}); |
|||
|
|||
const transactionPoints = portfolioCalculator.getTransactionPoints(); |
|||
const lastTransactionPoint = |
|||
transactionPoints[transactionPoints.length - 1]; |
|||
const position = lastTransactionPoint.items.find( |
|||
(item) => item.symbol === 'MSFT' |
|||
); |
|||
|
|||
expect(position.investment.toNumber()).toBe(0); |
|||
expect(position.quantity.toNumber()).toBe(0); |
|||
}); |
|||
}); |
|||
}); |
|||
@ -1,8 +1,8 @@ |
|||
import { IDataGatheringItem } from '@ghostfolio/api/services/interfaces/interfaces'; |
|||
import { DataGatheringItem } from '@ghostfolio/api/services/interfaces/interfaces'; |
|||
|
|||
import { DateQuery } from './date-query.interface'; |
|||
|
|||
export interface GetValuesParams { |
|||
dataGatheringItems: IDataGatheringItem[]; |
|||
dataGatheringItems: DataGatheringItem[]; |
|||
dateQuery: DateQuery; |
|||
} |
|||
|
|||
File diff suppressed because it is too large
File diff suppressed because it is too large
@ -0,0 +1,3 @@ |
|||
// Dependencies required by .config/prisma.ts in Docker container
|
|||
import 'dotenv'; |
|||
import 'dotenv-expand'; |
|||
@ -0,0 +1,11 @@ |
|||
import { AssetProfileIdentifier } from '@ghostfolio/common/interfaces'; |
|||
|
|||
export class AssetProfileChangedEvent { |
|||
public constructor( |
|||
public readonly data: AssetProfileIdentifier & { currency: string } |
|||
) {} |
|||
|
|||
public static getName(): string { |
|||
return 'assetProfile.changed'; |
|||
} |
|||
} |
|||
@ -0,0 +1,61 @@ |
|||
import { OrderService } from '@ghostfolio/api/app/order/order.service'; |
|||
import { ConfigurationService } from '@ghostfolio/api/services/configuration/configuration.service'; |
|||
import { DataProviderService } from '@ghostfolio/api/services/data-provider/data-provider.service'; |
|||
import { ExchangeRateDataService } from '@ghostfolio/api/services/exchange-rate-data/exchange-rate-data.service'; |
|||
import { DataGatheringService } from '@ghostfolio/api/services/queues/data-gathering/data-gathering.service'; |
|||
import { DEFAULT_CURRENCY } from '@ghostfolio/common/config'; |
|||
|
|||
import { Injectable, Logger } from '@nestjs/common'; |
|||
import { OnEvent } from '@nestjs/event-emitter'; |
|||
|
|||
import { AssetProfileChangedEvent } from './asset-profile-changed.event'; |
|||
|
|||
@Injectable() |
|||
export class AssetProfileChangedListener { |
|||
public constructor( |
|||
private readonly configurationService: ConfigurationService, |
|||
private readonly dataGatheringService: DataGatheringService, |
|||
private readonly dataProviderService: DataProviderService, |
|||
private readonly exchangeRateDataService: ExchangeRateDataService, |
|||
private readonly orderService: OrderService |
|||
) {} |
|||
|
|||
@OnEvent(AssetProfileChangedEvent.getName()) |
|||
public async handleAssetProfileChanged(event: AssetProfileChangedEvent) { |
|||
Logger.log( |
|||
`Asset profile of ${event.data.symbol} (${event.data.dataSource}) has changed`, |
|||
'AssetProfileChangedListener' |
|||
); |
|||
|
|||
if ( |
|||
this.configurationService.get( |
|||
'ENABLE_FEATURE_GATHER_NEW_EXCHANGE_RATES' |
|||
) === false || |
|||
event.data.currency === DEFAULT_CURRENCY |
|||
) { |
|||
return; |
|||
} |
|||
|
|||
const existingCurrencies = this.exchangeRateDataService.getCurrencies(); |
|||
|
|||
if (!existingCurrencies.includes(event.data.currency)) { |
|||
Logger.log( |
|||
`New currency ${event.data.currency} has been detected`, |
|||
'AssetProfileChangedListener' |
|||
); |
|||
|
|||
await this.exchangeRateDataService.initialize(); |
|||
} |
|||
|
|||
const { dateOfFirstActivity } = |
|||
await this.orderService.getStatisticsByCurrency(event.data.currency); |
|||
|
|||
if (dateOfFirstActivity) { |
|||
await this.dataGatheringService.gatherSymbol({ |
|||
dataSource: this.dataProviderService.getDataSourceForExchangeRates(), |
|||
date: dateOfFirstActivity, |
|||
symbol: `${DEFAULT_CURRENCY}${event.data.currency}` |
|||
}); |
|||
} |
|||
} |
|||
} |
|||
@ -1,11 +1,24 @@ |
|||
import { OrderModule } from '@ghostfolio/api/app/order/order.module'; |
|||
import { RedisCacheModule } from '@ghostfolio/api/app/redis-cache/redis-cache.module'; |
|||
import { ConfigurationModule } from '@ghostfolio/api/services/configuration/configuration.module'; |
|||
import { DataProviderModule } from '@ghostfolio/api/services/data-provider/data-provider.module'; |
|||
import { ExchangeRateDataModule } from '@ghostfolio/api/services/exchange-rate-data/exchange-rate-data.module'; |
|||
import { DataGatheringModule } from '@ghostfolio/api/services/queues/data-gathering/data-gathering.module'; |
|||
|
|||
import { Module } from '@nestjs/common'; |
|||
|
|||
import { AssetProfileChangedListener } from './asset-profile-changed.listener'; |
|||
import { PortfolioChangedListener } from './portfolio-changed.listener'; |
|||
|
|||
@Module({ |
|||
imports: [RedisCacheModule], |
|||
providers: [PortfolioChangedListener] |
|||
imports: [ |
|||
ConfigurationModule, |
|||
DataGatheringModule, |
|||
DataProviderModule, |
|||
ExchangeRateDataModule, |
|||
OrderModule, |
|||
RedisCacheModule |
|||
], |
|||
providers: [AssetProfileChangedListener, PortfolioChangedListener] |
|||
}) |
|||
export class EventsModule {} |
|||
|
|||
Some files were not shown because too many files changed in this diff
Loading…
Reference in new issue