mirror of https://github.com/ghostfolio/ghostfolio
Thomas
4 years ago
committed by
GitHub
16 changed files with 251 additions and 3 deletions
@ -0,0 +1,7 @@ |
|||||
|
import { Order } from '@prisma/client'; |
||||
|
import { IsArray } from 'class-validator'; |
||||
|
|
||||
|
export class ImportDataDto { |
||||
|
@IsArray() |
||||
|
orders: Partial<Order>[]; |
||||
|
} |
@ -0,0 +1,50 @@ |
|||||
|
import { ConfigurationService } from '@ghostfolio/api/services/configuration.service'; |
||||
|
import { RequestWithUser } from '@ghostfolio/common/types'; |
||||
|
import { |
||||
|
Body, |
||||
|
Controller, |
||||
|
HttpException, |
||||
|
Inject, |
||||
|
Post, |
||||
|
UseGuards |
||||
|
} from '@nestjs/common'; |
||||
|
import { REQUEST } from '@nestjs/core'; |
||||
|
import { AuthGuard } from '@nestjs/passport'; |
||||
|
import { StatusCodes, getReasonPhrase } from 'http-status-codes'; |
||||
|
|
||||
|
import { ImportDataDto } from './import-data.dto'; |
||||
|
import { ImportService } from './import.service'; |
||||
|
|
||||
|
@Controller('import') |
||||
|
export class ImportController { |
||||
|
public constructor( |
||||
|
private readonly configurationService: ConfigurationService, |
||||
|
private readonly importService: ImportService, |
||||
|
@Inject(REQUEST) private readonly request: RequestWithUser |
||||
|
) {} |
||||
|
|
||||
|
@Post() |
||||
|
@UseGuards(AuthGuard('jwt')) |
||||
|
public async import(@Body() importData: ImportDataDto): Promise<void> { |
||||
|
if (!this.configurationService.get('ENABLE_FEATURE_IMPORT')) { |
||||
|
throw new HttpException( |
||||
|
getReasonPhrase(StatusCodes.FORBIDDEN), |
||||
|
StatusCodes.FORBIDDEN |
||||
|
); |
||||
|
} |
||||
|
|
||||
|
try { |
||||
|
return await this.importService.import({ |
||||
|
orders: importData.orders, |
||||
|
userId: this.request.user.id |
||||
|
}); |
||||
|
} catch (error) { |
||||
|
console.error(error); |
||||
|
|
||||
|
throw new HttpException( |
||||
|
getReasonPhrase(StatusCodes.BAD_REQUEST), |
||||
|
StatusCodes.BAD_REQUEST |
||||
|
); |
||||
|
} |
||||
|
} |
||||
|
} |
@ -0,0 +1,34 @@ |
|||||
|
import { CacheService } from '@ghostfolio/api/app/cache/cache.service'; |
||||
|
import { OrderService } from '@ghostfolio/api/app/order/order.service'; |
||||
|
import { RedisCacheModule } from '@ghostfolio/api/app/redis-cache/redis-cache.module'; |
||||
|
import { ConfigurationService } from '@ghostfolio/api/services/configuration.service'; |
||||
|
import { DataGatheringService } from '@ghostfolio/api/services/data-gathering.service'; |
||||
|
import { DataProviderService } from '@ghostfolio/api/services/data-provider.service'; |
||||
|
import { AlphaVantageService } from '@ghostfolio/api/services/data-provider/alpha-vantage/alpha-vantage.service'; |
||||
|
import { GhostfolioScraperApiService } from '@ghostfolio/api/services/data-provider/ghostfolio-scraper-api/ghostfolio-scraper-api.service'; |
||||
|
import { RakutenRapidApiService } from '@ghostfolio/api/services/data-provider/rakuten-rapid-api/rakuten-rapid-api.service'; |
||||
|
import { YahooFinanceService } from '@ghostfolio/api/services/data-provider/yahoo-finance/yahoo-finance.service'; |
||||
|
import { PrismaService } from '@ghostfolio/api/services/prisma.service'; |
||||
|
import { Module } from '@nestjs/common'; |
||||
|
|
||||
|
import { ImportController } from './import.controller'; |
||||
|
import { ImportService } from './import.service'; |
||||
|
|
||||
|
@Module({ |
||||
|
imports: [RedisCacheModule], |
||||
|
controllers: [ImportController], |
||||
|
providers: [ |
||||
|
AlphaVantageService, |
||||
|
CacheService, |
||||
|
ConfigurationService, |
||||
|
DataGatheringService, |
||||
|
DataProviderService, |
||||
|
GhostfolioScraperApiService, |
||||
|
ImportService, |
||||
|
OrderService, |
||||
|
PrismaService, |
||||
|
RakutenRapidApiService, |
||||
|
YahooFinanceService |
||||
|
] |
||||
|
}) |
||||
|
export class ImportModule {} |
@ -0,0 +1,43 @@ |
|||||
|
import { OrderService } from '@ghostfolio/api/app/order/order.service'; |
||||
|
import { Injectable } from '@nestjs/common'; |
||||
|
import { Order } from '@prisma/client'; |
||||
|
import { parseISO } from 'date-fns'; |
||||
|
|
||||
|
@Injectable() |
||||
|
export class ImportService { |
||||
|
public constructor(private readonly orderService: OrderService) {} |
||||
|
|
||||
|
public async import({ |
||||
|
orders, |
||||
|
userId |
||||
|
}: { |
||||
|
orders: Partial<Order>[]; |
||||
|
userId: string; |
||||
|
}): Promise<void> { |
||||
|
for (const { |
||||
|
currency, |
||||
|
dataSource, |
||||
|
date, |
||||
|
fee, |
||||
|
quantity, |
||||
|
symbol, |
||||
|
type, |
||||
|
unitPrice |
||||
|
} of orders) { |
||||
|
await this.orderService.createOrder( |
||||
|
{ |
||||
|
currency, |
||||
|
dataSource, |
||||
|
fee, |
||||
|
quantity, |
||||
|
symbol, |
||||
|
type, |
||||
|
unitPrice, |
||||
|
date: parseISO(<string>(<unknown>date)), |
||||
|
User: { connect: { id: userId } } |
||||
|
}, |
||||
|
userId |
||||
|
); |
||||
|
} |
||||
|
} |
||||
|
} |
Loading…
Reference in new issue