2021-07-14 20:54:05 +02:00
|
|
|
import { ConfigurationService } from '@ghostfolio/api/services/configuration.service';
|
2021-09-11 09:27:22 +02:00
|
|
|
import type { RequestWithUser } from '@ghostfolio/common/types';
|
2021-07-14 20:54:05 +02:00
|
|
|
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(
|
2021-10-12 22:19:32 +02:00
|
|
|
{
|
|
|
|
error: getReasonPhrase(StatusCodes.BAD_REQUEST),
|
|
|
|
message: [error.message]
|
|
|
|
},
|
2021-07-14 20:54:05 +02:00
|
|
|
StatusCodes.BAD_REQUEST
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|