mirror of https://github.com/ghostfolio/ghostfolio
392 changed files with 12790 additions and 7656 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,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,139 @@ |
|||||
|
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(); |
||||
|
|
||||
|
expect(portfolioSnapshot.positions[0].fee).toEqual(new Big(4.46)); |
||||
|
expect( |
||||
|
portfolioSnapshot.positions[0].feeInBaseCurrency.toNumber() |
||||
|
).toBeCloseTo(3.94, 1); |
||||
|
}); |
||||
|
}); |
||||
|
}); |
||||
@ -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'; |
import { DateQuery } from './date-query.interface'; |
||||
|
|
||||
export interface GetValuesParams { |
export interface GetValuesParams { |
||||
dataGatheringItems: IDataGatheringItem[]; |
dataGatheringItems: DataGatheringItem[]; |
||||
dateQuery: DateQuery; |
dateQuery: DateQuery; |
||||
} |
} |
||||
|
|||||
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 { 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 { Module } from '@nestjs/common'; |
||||
|
|
||||
|
import { AssetProfileChangedListener } from './asset-profile-changed.listener'; |
||||
import { PortfolioChangedListener } from './portfolio-changed.listener'; |
import { PortfolioChangedListener } from './portfolio-changed.listener'; |
||||
|
|
||||
@Module({ |
@Module({ |
||||
imports: [RedisCacheModule], |
imports: [ |
||||
providers: [PortfolioChangedListener] |
ConfigurationModule, |
||||
|
DataGatheringModule, |
||||
|
DataProviderModule, |
||||
|
ExchangeRateDataModule, |
||||
|
OrderModule, |
||||
|
RedisCacheModule |
||||
|
], |
||||
|
providers: [AssetProfileChangedListener, PortfolioChangedListener] |
||||
}) |
}) |
||||
export class EventsModule {} |
export class EventsModule {} |
||||
|
|||||
@ -1 +1 @@ |
|||||
export interface IAlphaVantageHistoricalResponse {} |
export interface AlphaVantageHistoricalResponse {} |
||||
|
|||||
Some files were not shown because too many files changed in this diff
Loading…
Reference in new issue