Feature/add dry run to import api endpoint (#1526)

* Add dry run to import API endpoint

* Update changelog
pull/1528/head
Thomas Kaul 2 years ago committed by GitHub
parent 61dfc1f819
commit b56111ae85
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added ### Added
- Added the position detail dialog to the _Top 3_ and _Bottom 3_ performers of the analysis page - Added the position detail dialog to the _Top 3_ and _Bottom 3_ performers of the analysis page
- Added the `dryRun` option to the import activities endpoint
### Changed ### Changed

@ -1,4 +1,5 @@
import { ConfigurationService } from '@ghostfolio/api/services/configuration.service'; import { ConfigurationService } from '@ghostfolio/api/services/configuration.service';
import { ImportResponse } from '@ghostfolio/common/interfaces';
import type { RequestWithUser } from '@ghostfolio/common/types'; import type { RequestWithUser } from '@ghostfolio/common/types';
import { import {
Body, Body,
@ -7,6 +8,7 @@ import {
Inject, Inject,
Logger, Logger,
Post, Post,
Query,
UseGuards UseGuards
} from '@nestjs/common'; } from '@nestjs/common';
import { REQUEST } from '@nestjs/core'; import { REQUEST } from '@nestjs/core';
@ -26,7 +28,10 @@ export class ImportController {
@Post() @Post()
@UseGuards(AuthGuard('jwt')) @UseGuards(AuthGuard('jwt'))
public async import(@Body() importData: ImportDataDto): Promise<void> { public async import(
@Body() importData: ImportDataDto,
@Query('dryRun') isDryRun?: boolean
): Promise<ImportResponse> {
if (!this.configurationService.get('ENABLE_FEATURE_IMPORT')) { if (!this.configurationService.get('ENABLE_FEATURE_IMPORT')) {
throw new HttpException( throw new HttpException(
getReasonPhrase(StatusCodes.FORBIDDEN), getReasonPhrase(StatusCodes.FORBIDDEN),
@ -45,12 +50,18 @@ export class ImportController {
maxActivitiesToImport = Number.MAX_SAFE_INTEGER; maxActivitiesToImport = Number.MAX_SAFE_INTEGER;
} }
const userCurrency = this.request.user.Settings.settings.baseCurrency;
try { try {
return await this.importService.import({ const activities = await this.importService.import({
maxActivitiesToImport, maxActivitiesToImport,
activities: importData.activities, isDryRun,
userCurrency,
activitiesDto: importData.activities,
userId: this.request.user.id userId: this.request.user.id
}); });
return { activities };
} catch (error) { } catch (error) {
Logger.error(error, ImportController); Logger.error(error, ImportController);

@ -5,6 +5,7 @@ import { RedisCacheModule } from '@ghostfolio/api/app/redis-cache/redis-cache.mo
import { ConfigurationModule } from '@ghostfolio/api/services/configuration.module'; import { ConfigurationModule } from '@ghostfolio/api/services/configuration.module';
import { DataGatheringModule } from '@ghostfolio/api/services/data-gathering.module'; import { DataGatheringModule } from '@ghostfolio/api/services/data-gathering.module';
import { DataProviderModule } from '@ghostfolio/api/services/data-provider/data-provider.module'; import { DataProviderModule } from '@ghostfolio/api/services/data-provider/data-provider.module';
import { ExchangeRateDataModule } from '@ghostfolio/api/services/exchange-rate-data.module';
import { PrismaModule } from '@ghostfolio/api/services/prisma.module'; import { PrismaModule } from '@ghostfolio/api/services/prisma.module';
import { Module } from '@nestjs/common'; import { Module } from '@nestjs/common';
@ -19,6 +20,7 @@ import { ImportService } from './import.service';
ConfigurationModule, ConfigurationModule,
DataGatheringModule, DataGatheringModule,
DataProviderModule, DataProviderModule,
ExchangeRateDataModule,
OrderModule, OrderModule,
PrismaModule, PrismaModule,
RedisCacheModule RedisCacheModule

@ -1,30 +1,38 @@
import { AccountService } from '@ghostfolio/api/app/account/account.service'; import { AccountService } from '@ghostfolio/api/app/account/account.service';
import { CreateOrderDto } from '@ghostfolio/api/app/order/create-order.dto'; import { CreateOrderDto } from '@ghostfolio/api/app/order/create-order.dto';
import { Activity } from '@ghostfolio/api/app/order/interfaces/activities.interface';
import { OrderService } from '@ghostfolio/api/app/order/order.service'; import { OrderService } from '@ghostfolio/api/app/order/order.service';
import { ConfigurationService } from '@ghostfolio/api/services/configuration.service';
import { DataProviderService } from '@ghostfolio/api/services/data-provider/data-provider.service'; import { DataProviderService } from '@ghostfolio/api/services/data-provider/data-provider.service';
import { ExchangeRateDataService } from '@ghostfolio/api/services/exchange-rate-data.service';
import { OrderWithAccount } from '@ghostfolio/common/types';
import { Injectable } from '@nestjs/common'; import { Injectable } from '@nestjs/common';
import { isSameDay, parseISO } from 'date-fns'; import Big from 'big.js';
import { endOfToday, isAfter, isSameDay, parseISO } from 'date-fns';
import { v4 as uuidv4 } from 'uuid';
@Injectable() @Injectable()
export class ImportService { export class ImportService {
public constructor( public constructor(
private readonly accountService: AccountService, private readonly accountService: AccountService,
private readonly configurationService: ConfigurationService,
private readonly dataProviderService: DataProviderService, private readonly dataProviderService: DataProviderService,
private readonly exchangeRateDataService: ExchangeRateDataService,
private readonly orderService: OrderService private readonly orderService: OrderService
) {} ) {}
public async import({ public async import({
activities, activitiesDto,
isDryRun = false,
maxActivitiesToImport, maxActivitiesToImport,
userCurrency,
userId userId
}: { }: {
activities: Partial<CreateOrderDto>[]; activitiesDto: Partial<CreateOrderDto>[];
isDryRun?: boolean;
maxActivitiesToImport: number; maxActivitiesToImport: number;
userCurrency: string;
userId: string; userId: string;
}): Promise<void> { }): Promise<Activity[]> {
for (const activity of activities) { for (const activity of activitiesDto) {
if (!activity.dataSource) { if (!activity.dataSource) {
if (activity.type === 'ITEM') { if (activity.type === 'ITEM') {
activity.dataSource = 'MANUAL'; activity.dataSource = 'MANUAL';
@ -35,7 +43,7 @@ export class ImportService {
} }
await this.validateActivities({ await this.validateActivities({
activities, activitiesDto,
maxActivitiesToImport, maxActivitiesToImport,
userId userId
}); });
@ -46,27 +54,71 @@ export class ImportService {
} }
); );
const activities: Activity[] = [];
for (const { for (const {
accountId, accountId,
comment, comment,
currency, currency,
dataSource, dataSource,
date, date: dateString,
fee, fee,
quantity, quantity,
symbol, symbol,
type, type,
unitPrice unitPrice
} of activities) { } of activitiesDto) {
await this.orderService.createOrder({ const date = parseISO(<string>(<unknown>dateString));
const validatedAccountId = accountIds.includes(accountId)
? accountId
: undefined;
let order: OrderWithAccount;
if (isDryRun) {
order = {
comment,
date,
fee,
quantity,
type,
unitPrice,
userId,
accountId: validatedAccountId,
accountUserId: undefined,
createdAt: new Date(),
id: uuidv4(),
isDraft: isAfter(date, endOfToday()),
SymbolProfile: {
currency,
dataSource,
symbol,
assetClass: null,
assetSubClass: null,
comment: null,
countries: null,
createdAt: undefined,
id: undefined,
name: null,
scraperConfiguration: null,
sectors: null,
symbolMapping: null,
updatedAt: undefined,
url: null
},
symbolProfileId: undefined,
updatedAt: new Date()
};
} else {
order = await this.orderService.createOrder({
comment, comment,
date,
fee, fee,
quantity, quantity,
type, type,
unitPrice, unitPrice,
userId, userId,
accountId: accountIds.includes(accountId) ? accountId : undefined, accountId: validatedAccountId,
date: parseISO(<string>(<unknown>date)),
SymbolProfile: { SymbolProfile: {
connectOrCreate: { connectOrCreate: {
create: { create: {
@ -85,18 +137,38 @@ export class ImportService {
User: { connect: { id: userId } } User: { connect: { id: userId } }
}); });
} }
const value = new Big(quantity).mul(unitPrice).toNumber();
activities.push({
...order,
value,
feeInBaseCurrency: this.exchangeRateDataService.toCurrency(
fee,
currency,
userCurrency
),
valueInBaseCurrency: this.exchangeRateDataService.toCurrency(
value,
currency,
userCurrency
)
});
}
return activities;
} }
private async validateActivities({ private async validateActivities({
activities, activitiesDto,
maxActivitiesToImport, maxActivitiesToImport,
userId userId
}: { }: {
activities: Partial<CreateOrderDto>[]; activitiesDto: Partial<CreateOrderDto>[];
maxActivitiesToImport: number; maxActivitiesToImport: number;
userId: string; userId: string;
}) { }) {
if (activities?.length > maxActivitiesToImport) { if (activitiesDto?.length > maxActivitiesToImport) {
throw new Error(`Too many activities (${maxActivitiesToImport} at most)`); throw new Error(`Too many activities (${maxActivitiesToImport} at most)`);
} }
@ -109,7 +181,7 @@ export class ImportService {
for (const [ for (const [
index, index,
{ currency, dataSource, date, fee, quantity, symbol, type, unitPrice } { currency, dataSource, date, fee, quantity, symbol, type, unitPrice }
] of activities.entries()) { ] of activitiesDto.entries()) {
const duplicateActivity = existingActivities.find((activity) => { const duplicateActivity = existingActivities.find((activity) => {
return ( return (
activity.SymbolProfile.currency === currency && activity.SymbolProfile.currency === currency &&

@ -32,6 +32,7 @@ import { PortfolioSummary } from './portfolio-summary.interface';
import { Position } from './position.interface'; import { Position } from './position.interface';
import { BenchmarkResponse } from './responses/benchmark-response.interface'; import { BenchmarkResponse } from './responses/benchmark-response.interface';
import { ResponseError } from './responses/errors.interface'; import { ResponseError } from './responses/errors.interface';
import { ImportResponse } from './responses/import-response.interface';
import { OAuthResponse } from './responses/oauth-response.interface'; import { OAuthResponse } from './responses/oauth-response.interface';
import { PortfolioPerformanceResponse } from './responses/portfolio-performance-response.interface'; import { PortfolioPerformanceResponse } from './responses/portfolio-performance-response.interface';
import { ScraperConfiguration } from './scraper-configuration.interface'; import { ScraperConfiguration } from './scraper-configuration.interface';
@ -58,6 +59,7 @@ export {
Filter, Filter,
FilterGroup, FilterGroup,
HistoricalDataItem, HistoricalDataItem,
ImportResponse,
InfoItem, InfoItem,
LineChartItem, LineChartItem,
OAuthResponse, OAuthResponse,

@ -0,0 +1,5 @@
import { Activity } from '@ghostfolio/api/app/order/interfaces/activities.interface';
export interface ImportResponse {
activities: Activity[];
}
Loading…
Cancel
Save