2022-02-01 20:35:25 +01:00
|
|
|
import { TransformDataSourceInRequestInterceptor } from '@ghostfolio/api/interceptors/transform-data-source-in-request.interceptor';
|
|
|
|
import { TransformDataSourceInResponseInterceptor } from '@ghostfolio/api/interceptors/transform-data-source-in-response.interceptor';
|
2021-12-19 10:57:50 +01:00
|
|
|
import { IDataProviderHistoricalResponse } from '@ghostfolio/api/services/interfaces/interfaces';
|
2023-03-18 10:09:11 +01:00
|
|
|
import type { RequestWithUser } from '@ghostfolio/common/types';
|
2021-04-13 21:53:58 +02:00
|
|
|
import {
|
|
|
|
Controller,
|
|
|
|
Get,
|
|
|
|
HttpException,
|
2023-03-18 10:09:11 +01:00
|
|
|
Inject,
|
2021-04-13 21:53:58 +02:00
|
|
|
Param,
|
|
|
|
Query,
|
2022-02-01 20:35:25 +01:00
|
|
|
UseGuards,
|
|
|
|
UseInterceptors
|
2021-04-13 21:53:58 +02:00
|
|
|
} from '@nestjs/common';
|
2023-03-18 10:09:11 +01:00
|
|
|
import { REQUEST } from '@nestjs/core';
|
2021-04-13 21:53:58 +02:00
|
|
|
import { AuthGuard } from '@nestjs/passport';
|
2022-01-08 18:19:25 +01:00
|
|
|
import { DataSource } from '@prisma/client';
|
2021-04-13 21:53:58 +02:00
|
|
|
import { StatusCodes, getReasonPhrase } from 'http-status-codes';
|
2021-12-19 10:57:50 +01:00
|
|
|
import { isDate, isEmpty } from 'lodash';
|
2021-04-13 21:53:58 +02:00
|
|
|
|
|
|
|
import { LookupItem } from './interfaces/lookup-item.interface';
|
|
|
|
import { SymbolItem } from './interfaces/symbol-item.interface';
|
|
|
|
import { SymbolService } from './symbol.service';
|
2023-08-15 19:24:31 +02:00
|
|
|
import { parseISO } from 'date-fns';
|
2021-04-13 21:53:58 +02:00
|
|
|
|
|
|
|
@Controller('symbol')
|
|
|
|
export class SymbolController {
|
2023-03-18 10:09:11 +01:00
|
|
|
public constructor(
|
|
|
|
@Inject(REQUEST) private readonly request: RequestWithUser,
|
|
|
|
private readonly symbolService: SymbolService
|
|
|
|
) {}
|
2021-04-13 21:53:58 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Must be before /:symbol
|
|
|
|
*/
|
|
|
|
@Get('lookup')
|
|
|
|
@UseGuards(AuthGuard('jwt'))
|
2022-02-01 20:35:25 +01:00
|
|
|
@UseInterceptors(TransformDataSourceInResponseInterceptor)
|
2021-05-20 20:36:08 +02:00
|
|
|
public async lookupSymbol(
|
2023-06-26 22:08:24 +05:30
|
|
|
@Query('includeIndices') includeIndices: boolean = false,
|
|
|
|
@Query('query') query = ''
|
2021-05-20 20:36:08 +02:00
|
|
|
): Promise<{ items: LookupItem[] }> {
|
2021-04-13 21:53:58 +02:00
|
|
|
try {
|
2023-03-18 10:09:11 +01:00
|
|
|
return this.symbolService.lookup({
|
2023-06-26 22:08:24 +05:30
|
|
|
includeIndices,
|
2023-03-18 10:09:11 +01:00
|
|
|
query: query.toLowerCase(),
|
|
|
|
user: this.request.user
|
|
|
|
});
|
2021-04-13 21:53:58 +02:00
|
|
|
} catch {
|
|
|
|
throw new HttpException(
|
|
|
|
getReasonPhrase(StatusCodes.INTERNAL_SERVER_ERROR),
|
|
|
|
StatusCodes.INTERNAL_SERVER_ERROR
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Must be after /lookup
|
|
|
|
*/
|
2021-09-18 19:32:22 +02:00
|
|
|
@Get(':dataSource/:symbol')
|
2022-02-01 20:35:25 +01:00
|
|
|
@UseInterceptors(TransformDataSourceInRequestInterceptor)
|
|
|
|
@UseInterceptors(TransformDataSourceInResponseInterceptor)
|
2021-09-18 19:32:22 +02:00
|
|
|
public async getSymbolData(
|
|
|
|
@Param('dataSource') dataSource: DataSource,
|
2021-12-04 11:49:00 +01:00
|
|
|
@Param('symbol') symbol: string,
|
2023-06-03 19:32:48 +02:00
|
|
|
@Query('includeHistoricalData') includeHistoricalData = 0
|
2021-09-18 19:32:22 +02:00
|
|
|
): Promise<SymbolItem> {
|
|
|
|
if (!DataSource[dataSource]) {
|
|
|
|
throw new HttpException(
|
|
|
|
getReasonPhrase(StatusCodes.NOT_FOUND),
|
|
|
|
StatusCodes.NOT_FOUND
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-12-04 11:49:00 +01:00
|
|
|
const result = await this.symbolService.get({
|
|
|
|
includeHistoricalData,
|
|
|
|
dataGatheringItem: { dataSource, symbol }
|
|
|
|
});
|
2021-08-30 18:08:21 +02:00
|
|
|
|
|
|
|
if (!result || isEmpty(result)) {
|
|
|
|
throw new HttpException(
|
|
|
|
getReasonPhrase(StatusCodes.NOT_FOUND),
|
|
|
|
StatusCodes.NOT_FOUND
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
2021-04-13 21:53:58 +02:00
|
|
|
}
|
2021-12-19 10:57:50 +01:00
|
|
|
|
|
|
|
@Get(':dataSource/:symbol/:dateString')
|
|
|
|
@UseGuards(AuthGuard('jwt'))
|
|
|
|
public async gatherSymbolForDate(
|
|
|
|
@Param('dataSource') dataSource: DataSource,
|
|
|
|
@Param('dateString') dateString: string,
|
|
|
|
@Param('symbol') symbol: string
|
|
|
|
): Promise<IDataProviderHistoricalResponse> {
|
2023-08-15 19:24:31 +02:00
|
|
|
const date = parseISO(dateString);
|
2021-12-19 10:57:50 +01:00
|
|
|
|
|
|
|
if (!isDate(date)) {
|
|
|
|
throw new HttpException(
|
|
|
|
getReasonPhrase(StatusCodes.BAD_REQUEST),
|
|
|
|
StatusCodes.BAD_REQUEST
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-12-03 18:22:19 +01:00
|
|
|
const result = await this.symbolService.getForDate({
|
2021-12-19 10:57:50 +01:00
|
|
|
dataSource,
|
|
|
|
date,
|
|
|
|
symbol
|
|
|
|
});
|
2022-12-03 18:22:19 +01:00
|
|
|
|
|
|
|
if (!result || isEmpty(result)) {
|
|
|
|
throw new HttpException(
|
|
|
|
getReasonPhrase(StatusCodes.NOT_FOUND),
|
|
|
|
StatusCodes.NOT_FOUND
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
2021-12-19 10:57:50 +01:00
|
|
|
}
|
2021-04-13 21:53:58 +02:00
|
|
|
}
|