diff --git a/BE/src/app.module.ts b/BE/src/app.module.ts index fbef051f..114ecadf 100644 --- a/BE/src/app.module.ts +++ b/BE/src/app.module.ts @@ -6,9 +6,9 @@ import { AppController } from './app.controller'; import { AppService } from './app.service'; import { AuthModule } from './auth/auth.module'; import { User } from './auth/user.entity'; -import { StockIndexModule } from './stock/index/stock.index.module'; -import { StockTopfiveModule } from './stock/topfive/stock.topfive.module'; -import { KoreaInvestmentModule } from './koreaInvestment/korea.investment.module'; +import { StockIndexModule } from './stock/index/stock-index.module'; +import { StockTopfiveModule } from './stock/topfive/stock-topfive.module'; +import { KoreaInvestmentModule } from './koreaInvestment/korea-investment.module'; import { SocketModule } from './websocket/socket.module'; @Module({ diff --git a/BE/src/auth/auth.controller.ts b/BE/src/auth/auth.controller.ts index 8411eb48..aee104f0 100644 --- a/BE/src/auth/auth.controller.ts +++ b/BE/src/auth/auth.controller.ts @@ -10,7 +10,7 @@ import { import { AuthGuard } from '@nestjs/passport'; import { ApiOperation } from '@nestjs/swagger'; import { AuthService } from './auth.service'; -import { AuthCredentialsDto } from './dto/authCredentials.dto'; +import { AuthCredentialsDto } from './dto/auth-credentials.dto'; @Controller('auth') export class AuthController { diff --git a/BE/src/auth/auth.service.ts b/BE/src/auth/auth.service.ts index cbc4a69f..6f751e1b 100644 --- a/BE/src/auth/auth.service.ts +++ b/BE/src/auth/auth.service.ts @@ -3,7 +3,7 @@ import { InjectRepository } from '@nestjs/typeorm'; import { JwtService } from '@nestjs/jwt'; import * as bcrypt from 'bcrypt'; import { UserRepository } from './user.repository'; -import { AuthCredentialsDto } from './dto/authCredentials.dto'; +import { AuthCredentialsDto } from './dto/auth-credentials.dto'; @Injectable() export class AuthService { diff --git a/BE/src/auth/dto/authCredentials.dto.ts b/BE/src/auth/dto/auth-credentials.dto.ts similarity index 100% rename from BE/src/auth/dto/authCredentials.dto.ts rename to BE/src/auth/dto/auth-credentials.dto.ts diff --git a/BE/src/auth/user.repository.ts b/BE/src/auth/user.repository.ts index 0a23f980..429437e7 100644 --- a/BE/src/auth/user.repository.ts +++ b/BE/src/auth/user.repository.ts @@ -3,7 +3,7 @@ import { InjectDataSource } from '@nestjs/typeorm'; import { DataSource, Repository } from 'typeorm'; import * as bcrypt from 'bcrypt'; import { User } from './user.entity'; -import { AuthCredentialsDto } from './dto/authCredentials.dto'; +import { AuthCredentialsDto } from './dto/auth-credentials.dto'; @Injectable() export class UserRepository extends Repository { diff --git a/BE/src/koreaInvestment/interface/korea-investment.interface.ts b/BE/src/koreaInvestment/interface/korea-investment.interface.ts new file mode 100644 index 00000000..e290a32c --- /dev/null +++ b/BE/src/koreaInvestment/interface/korea-investment.interface.ts @@ -0,0 +1,6 @@ +export interface AccessTokenInterface { + access_token: string; + access_token_token_expired: string; + token_type: string; + expires_in: number; +} diff --git a/BE/src/koreaInvestment/korea.investment.module.ts b/BE/src/koreaInvestment/korea-investment.module.ts similarity index 76% rename from BE/src/koreaInvestment/korea.investment.module.ts rename to BE/src/koreaInvestment/korea-investment.module.ts index 69679c6b..8e7dd10a 100644 --- a/BE/src/koreaInvestment/korea.investment.module.ts +++ b/BE/src/koreaInvestment/korea-investment.module.ts @@ -1,5 +1,5 @@ import { Module } from '@nestjs/common'; -import { KoreaInvestmentService } from './korea.investment.service'; +import { KoreaInvestmentService } from './korea-investment.service'; @Module({ imports: [], diff --git a/BE/src/koreaInvestment/korea.investment.service.ts b/BE/src/koreaInvestment/korea-investment.service.ts similarity index 53% rename from BE/src/koreaInvestment/korea.investment.service.ts rename to BE/src/koreaInvestment/korea-investment.service.ts index 57458372..29dc323c 100644 --- a/BE/src/koreaInvestment/korea.investment.service.ts +++ b/BE/src/koreaInvestment/korea-investment.service.ts @@ -1,4 +1,7 @@ import axios from 'axios'; +import { UnauthorizedException } from '@nestjs/common'; +import { getFullURL } from '../util/get-full-URL'; +import { AccessTokenInterface } from './interface/korea-investment.interface'; export class KoreaInvestmentService { private accessToken: string; @@ -9,19 +12,20 @@ export class KoreaInvestmentService { if (this.accessToken && this.tokenExpireTime > new Date()) { return this.accessToken; } - const response = await axios.post( - `${process.env.KOREA_INVESTMENT_BASE_URL}/oauth2/tokenP`, - { + const response = await axios + .post(getFullURL('/oauth2/tokenP'), { grant_type: 'client_credentials', appkey: process.env.KOREA_INVESTMENT_APP_KEY, appsecret: process.env.KOREA_INVESTMENT_APP_SECRET, - }, - ); + }) + .catch(() => { + throw new UnauthorizedException('액세스 토큰을 조회하지 못했습니다.'); + }); const { data } = response; this.accessToken = data.access_token; - this.tokenExpireTime = new Date(Date.now() + +data.expires_in); + this.tokenExpireTime = new Date(data.access_token_token_expired); return this.accessToken; } diff --git a/BE/src/stock/enum/MarketType.ts b/BE/src/stock/enum/market-type.ts similarity index 100% rename from BE/src/stock/enum/MarketType.ts rename to BE/src/stock/enum/market-type.ts diff --git a/BE/src/stock/index/dto/stock.index.list.chart.element.dto.ts b/BE/src/stock/index/dto/stock-index-list-chart.element.dto.ts similarity index 64% rename from BE/src/stock/index/dto/stock.index.list.chart.element.dto.ts rename to BE/src/stock/index/dto/stock-index-list-chart.element.dto.ts index 1f2abdce..ba6ce1f2 100644 --- a/BE/src/stock/index/dto/stock.index.list.chart.element.dto.ts +++ b/BE/src/stock/index/dto/stock-index-list-chart.element.dto.ts @@ -1,9 +1,10 @@ import { ApiProperty } from '@nestjs/swagger'; export class StockIndexListChartElementDto { - constructor(time: string, value: string) { + constructor(time: string, value: string, diff: string) { this.time = time; this.value = value; + this.diff = diff; } @ApiProperty({ description: 'HHMMSS', example: '130500' }) @@ -11,4 +12,7 @@ export class StockIndexListChartElementDto { @ApiProperty({ description: '주가 지수' }) value: string; + + @ApiProperty({ description: '전일 대비 주가 지수' }) + diff: string; } diff --git a/BE/src/stock/index/dto/stock-index-response-element.dto.ts b/BE/src/stock/index/dto/stock-index-response-element.dto.ts new file mode 100644 index 00000000..79cf6af5 --- /dev/null +++ b/BE/src/stock/index/dto/stock-index-response-element.dto.ts @@ -0,0 +1,14 @@ +import { ApiProperty } from '@nestjs/swagger'; +import { StockIndexValueElementDto } from './stock-index-value-element.dto'; +import { StockIndexListChartElementDto } from './stock-index-list-chart.element.dto'; + +export class StockIndexResponseElementDto { + @ApiProperty({ description: '실시간 값', type: StockIndexValueElementDto }) + value: StockIndexValueElementDto; + + @ApiProperty({ + description: '실시간 차트', + type: [StockIndexListChartElementDto], + }) + chart: StockIndexListChartElementDto[]; +} diff --git a/BE/src/stock/index/dto/stock.index.response.dto.ts b/BE/src/stock/index/dto/stock-index-response.dto.ts similarity index 88% rename from BE/src/stock/index/dto/stock.index.response.dto.ts rename to BE/src/stock/index/dto/stock-index-response.dto.ts index 7f26af32..026fecf3 100644 --- a/BE/src/stock/index/dto/stock.index.response.dto.ts +++ b/BE/src/stock/index/dto/stock-index-response.dto.ts @@ -1,5 +1,5 @@ import { ApiProperty } from '@nestjs/swagger'; -import { StockIndexResponseElementDto } from './stock.index.response.element.dto'; +import { StockIndexResponseElementDto } from './stock-index-response-element.dto'; export class StockIndexResponseDto { @ApiProperty({ diff --git a/BE/src/stock/index/dto/stock.index.value.element.dto.ts b/BE/src/stock/index/dto/stock-index-value-element.dto.ts similarity index 54% rename from BE/src/stock/index/dto/stock.index.value.element.dto.ts rename to BE/src/stock/index/dto/stock-index-value-element.dto.ts index 88ca4949..1eb73a50 100644 --- a/BE/src/stock/index/dto/stock.index.value.element.dto.ts +++ b/BE/src/stock/index/dto/stock-index-value-element.dto.ts @@ -1,33 +1,21 @@ import { ApiProperty } from '@nestjs/swagger'; export class StockIndexValueElementDto { - constructor( - code: string, - value: string, - diff: string, - diffRate: string, - sign: string, - ) { - this.code = code; - this.value = value; + constructor(value: string, diff: string, diffRate: string, sign: string) { + this.curr_value = value; this.diff = diff; - this.diffRate = diffRate; + this.diff_rate = diffRate; this.sign = sign; } - @ApiProperty({ - description: '코스피: 0001, 코스닥: 1001, 코스피200: 2001, KSQ150: 3003', - }) - code: string; - @ApiProperty({ description: '주가 지수' }) - value: string; + curr_value: string; @ApiProperty({ description: '전일 대비 등락' }) diff: string; @ApiProperty({ description: '전일 대비 등락률' }) - diffRate: string; + diff_rate: string; @ApiProperty({ description: '부호... 인데 추후에 알아봐야 함' }) sign: string; diff --git a/BE/src/stock/index/dto/stock.index.list.element.dto.ts b/BE/src/stock/index/dto/stock.index.list.element.dto.ts deleted file mode 100644 index 03ec0176..00000000 --- a/BE/src/stock/index/dto/stock.index.list.element.dto.ts +++ /dev/null @@ -1,17 +0,0 @@ -import { ApiProperty } from '@nestjs/swagger'; -import { StockIndexListChartElementDto } from './stock.index.list.chart.element.dto'; - -export class StockIndexListElementDto { - constructor(code: string, chart: StockIndexListChartElementDto[]) { - this.code = code; - this.chart = chart; - } - - @ApiProperty({ - description: '코스피: 0001, 코스닥: 1001, 코스피200: 2001, KSQ150: 3003', - }) - code: string; - - @ApiProperty({ type: [StockIndexListChartElementDto] }) - chart: StockIndexListChartElementDto[]; -} diff --git a/BE/src/stock/index/dto/stock.index.response.element.dto.ts b/BE/src/stock/index/dto/stock.index.response.element.dto.ts deleted file mode 100644 index 7ce0ef1a..00000000 --- a/BE/src/stock/index/dto/stock.index.response.element.dto.ts +++ /dev/null @@ -1,16 +0,0 @@ -import { ApiProperty } from '@nestjs/swagger'; -import { StockIndexValueElementDto } from './stock.index.value.element.dto'; -import { StockIndexListElementDto } from './stock.index.list.element.dto'; - -export class StockIndexResponseElementDto { - @ApiProperty({ - description: '코스피: 0001, 코스닥: 1001, 코스피200: 2001, KSQ150: 3003', - }) - code: string; - - @ApiProperty({ description: '실시간 값', type: StockIndexValueElementDto }) - value: StockIndexValueElementDto; - - @ApiProperty({ description: '실시간 차트', type: StockIndexListElementDto }) - chart: StockIndexListElementDto; -} diff --git a/BE/src/stock/index/interface/stock.index.interface.ts b/BE/src/stock/index/interface/stock-index.interface.ts similarity index 100% rename from BE/src/stock/index/interface/stock.index.interface.ts rename to BE/src/stock/index/interface/stock-index.interface.ts diff --git a/BE/src/stock/index/stock.index.controller.ts b/BE/src/stock/index/stock-index.controller.ts similarity index 91% rename from BE/src/stock/index/stock.index.controller.ts rename to BE/src/stock/index/stock-index.controller.ts index 949df5a5..330685e2 100644 --- a/BE/src/stock/index/stock.index.controller.ts +++ b/BE/src/stock/index/stock-index.controller.ts @@ -1,9 +1,9 @@ import { Controller, Get } from '@nestjs/common'; import { ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger'; import { Cron } from '@nestjs/schedule'; -import { StockIndexService } from './stock.index.service'; -import { StockIndexResponseDto } from './dto/stock.index.response.dto'; -import { KoreaInvestmentService } from '../../koreaInvestment/korea.investment.service'; +import { StockIndexService } from './stock-index.service'; +import { StockIndexResponseDto } from './dto/stock-index-response.dto'; +import { KoreaInvestmentService } from '../../koreaInvestment/korea-investment.service'; import { SocketGateway } from '../../websocket/socket.gateway'; @Controller('/api/stocks/index') @@ -70,22 +70,18 @@ export class StockIndexController { const stockIndexResponse = new StockIndexResponseDto(); stockIndexResponse.KOSPI = { - code: '0001', value: kospiValue, chart: kospiChart, }; stockIndexResponse.KOSDAQ = { - code: '1001', value: kosdaqValue, chart: kosdaqChart, }; stockIndexResponse.KOSPI200 = { - code: '2001', value: kospi200Value, chart: kospi200Chart, }; stockIndexResponse.KSQ150 = { - code: '3003', value: ksq150Value, chart: ksq150Chart, }; @@ -115,6 +111,11 @@ export class StockIndexController { ), // KSQ150 ]); - this.socketGateway.sendStockIndexListToClient(stockLists); + this.socketGateway.sendStockIndexListToClient({ + KOSPI: stockLists[0], + KOSDAQ: stockLists[1], + KOSPI200: stockLists[2], + KSQ150: stockLists[3], + }); } } diff --git a/BE/src/stock/index/stock.index.module.ts b/BE/src/stock/index/stock-index.module.ts similarity index 71% rename from BE/src/stock/index/stock.index.module.ts rename to BE/src/stock/index/stock-index.module.ts index 1227e4f7..64e0c575 100644 --- a/BE/src/stock/index/stock.index.module.ts +++ b/BE/src/stock/index/stock-index.module.ts @@ -1,7 +1,7 @@ import { Module } from '@nestjs/common'; -import { StockIndexController } from './stock.index.controller'; -import { StockIndexService } from './stock.index.service'; -import { KoreaInvestmentModule } from '../../koreaInvestment/korea.investment.module'; +import { StockIndexController } from './stock-index.controller'; +import { StockIndexService } from './stock-index.service'; +import { KoreaInvestmentModule } from '../../koreaInvestment/korea-investment.module'; import { SocketModule } from '../../websocket/socket.module'; @Module({ diff --git a/BE/src/stock/index/stock-index.service.ts b/BE/src/stock/index/stock-index.service.ts new file mode 100644 index 00000000..fa05ed4c --- /dev/null +++ b/BE/src/stock/index/stock-index.service.ts @@ -0,0 +1,95 @@ +import { Injectable, InternalServerErrorException } from '@nestjs/common'; +import axios from 'axios'; +import { StockIndexListChartElementDto } from './dto/stock-index-list-chart.element.dto'; +import { StockIndexValueElementDto } from './dto/stock-index-value-element.dto'; +import { + StockIndexChartInterface, + StockIndexValueInterface, +} from './interface/stock-index.interface'; +import { getFullURL } from '../../util/get-full-URL'; +import { getHeader } from '../../util/get-header'; + +@Injectable() +export class StockIndexService { + async getDomesticStockIndexListByCode(code: string, accessToken: string) { + const result = await this.requestDomesticStockIndexListApi( + code, + accessToken, + ); + + return result.output.map((element) => { + return new StockIndexListChartElementDto( + element.bsop_hour, + element.bstp_nmix_prpr, + element.bstp_nmix_prdy_vrss, + ); + }); + } + + async getDomesticStockIndexValueByCode(code: string, accessToken: string) { + const result = await this.requestDomesticStockIndexValueApi( + code, + accessToken, + ); + + const data = result.output; + + return new StockIndexValueElementDto( + data.bstp_nmix_prpr, + data.bstp_nmix_prdy_vrss, + data.bstp_nmix_prdy_ctrt, + data.prdy_vrss_sign, + ); + } + + private async requestDomesticStockIndexListApi( + code: string, + accessToken: string, + ) { + const response = await axios + .get( + getFullURL( + '/uapi/domestic-stock/v1/quotations/inquire-index-timeprice', + ), + { + headers: getHeader(accessToken, 'FHPUP02110200'), + params: { + fid_input_hour_1: 300, + fid_cond_mrkt_div_code: 'U', + fid_input_iscd: code, + }, + }, + ) + .catch(() => { + throw new InternalServerErrorException( + '주가 지수 차트 정보를 조회하지 못했습니다.', + ); + }); + + return response.data; + } + + private async requestDomesticStockIndexValueApi( + code: string, + accessToken: string, + ) { + const response = await axios + .get( + getFullURL('/uapi/domestic-stock/v1/quotations/inquire-index-price'), + { + headers: getHeader(accessToken, 'FHPUP02100000'), + params: { + fid_cond_mrkt_div_code: 'U', + fid_input_iscd: code, + }, + }, + ) + .catch(() => { + throw new InternalServerErrorException( + '주가 지수 값 정보를 조회하지 못했습니다.', + ); + }); + + return response.data; + } +} diff --git a/BE/src/stock/index/stock.index.service.ts b/BE/src/stock/index/stock.index.service.ts deleted file mode 100644 index 875be12d..00000000 --- a/BE/src/stock/index/stock.index.service.ts +++ /dev/null @@ -1,101 +0,0 @@ -import { Injectable } from '@nestjs/common'; -import axios from 'axios'; -import { StockIndexListChartElementDto } from './dto/stock.index.list.chart.element.dto'; -import { StockIndexListElementDto } from './dto/stock.index.list.element.dto'; -import { StockIndexValueElementDto } from './dto/stock.index.value.element.dto'; -import { - StockIndexChartInterface, - StockIndexValueInterface, -} from './interface/stock.index.interface'; - -@Injectable() -export class StockIndexService { - async getDomesticStockIndexListByCode(code: string, accessToken: string) { - const result = await this.requestDomesticStockIndexListApi( - code, - accessToken, - ); - - if (result.rt_cd !== '0') - throw new Error('데이터를 정상적으로 조회하지 못했습니다.'); - - return new StockIndexListElementDto( - code, - result.output.map((element) => { - return new StockIndexListChartElementDto( - element.bsop_hour, - element.bstp_nmix_prpr, - ); - }), - ); - } - - async getDomesticStockIndexValueByCode(code: string, accessToken: string) { - const result = await this.requestDomesticStockIndexValueApi( - code, - accessToken, - ); - - if (result.rt_cd !== '0') - throw new Error('데이터를 정상적으로 조회하지 못했습니다.'); - - return new StockIndexValueElementDto( - code, - result.output.bstp_nmix_prpr, - result.output.bstp_nmix_prdy_vrss, - result.output.bstp_nmix_prdy_ctrt, - result.output.prdy_vrss_sign, - ); - } - - private async requestDomesticStockIndexListApi( - code: string, - accessToken: string, - ) { - const response = await axios.get( - `${process.env.KOREA_INVESTMENT_BASE_URL}/uapi/domestic-stock/v1/quotations/inquire-index-timeprice`, - { - headers: { - 'content-type': 'application/json; charset=utf-8', - authorization: `Bearer ${accessToken}`, - appkey: process.env.KOREA_INVESTMENT_APP_KEY, - appsecret: process.env.KOREA_INVESTMENT_APP_SECRET, - tr_id: 'FHPUP02110200', - custtype: 'P', - }, - params: { - fid_input_hour_1: 300, - fid_cond_mrkt_div_code: 'U', - fid_input_iscd: code, - }, - }, - ); - - return response.data; - } - - private async requestDomesticStockIndexValueApi( - code: string, - accessToken: string, - ) { - const response = await axios.get( - `${process.env.KOREA_INVESTMENT_BASE_URL}/uapi/domestic-stock/v1/quotations/inquire-index-price`, - { - headers: { - 'content-type': 'application/json; charset=utf-8', - authorization: `Bearer ${accessToken}`, - appkey: process.env.KOREA_INVESTMENT_APP_KEY, - appsecret: process.env.KOREA_INVESTMENT_APP_SECRET, - tr_id: 'FHPUP02100000', - custtype: 'P', - }, - params: { - fid_cond_mrkt_div_code: 'U', - fid_input_iscd: code, - }, - }, - ); - - return response.data; - } -} diff --git a/BE/src/stock/topfive/interface/stock.topfive.interface.ts b/BE/src/stock/topfive/interface/stock-topfive.interface.ts similarity index 100% rename from BE/src/stock/topfive/interface/stock.topfive.interface.ts rename to BE/src/stock/topfive/interface/stock-topfive.interface.ts diff --git a/BE/src/stock/topfive/stock.topfive.controller.ts b/BE/src/stock/topfive/stock-topfive.controller.ts similarity index 88% rename from BE/src/stock/topfive/stock.topfive.controller.ts rename to BE/src/stock/topfive/stock-topfive.controller.ts index f90d300a..bac275e3 100644 --- a/BE/src/stock/topfive/stock.topfive.controller.ts +++ b/BE/src/stock/topfive/stock-topfive.controller.ts @@ -1,8 +1,8 @@ import { ApiOperation, ApiQuery, ApiResponse } from '@nestjs/swagger'; import { Controller, Get, Query } from '@nestjs/common'; -import { StockTopfiveService } from './stock.topfive.service'; +import { StockTopfiveService } from './stock-topfive.service'; import { StockRankingResponseDto } from './dto/stock-ranking-response.dto'; -import { MarketType } from '../enum/MarketType'; +import { MarketType } from '../enum/market-type'; @Controller('/api/stocks') export class StockTopfiveController { diff --git a/BE/src/stock/topfive/stock.topfive.module.ts b/BE/src/stock/topfive/stock-topfive.module.ts similarity index 68% rename from BE/src/stock/topfive/stock.topfive.module.ts rename to BE/src/stock/topfive/stock-topfive.module.ts index 33a63aae..5be62a4f 100644 --- a/BE/src/stock/topfive/stock.topfive.module.ts +++ b/BE/src/stock/topfive/stock-topfive.module.ts @@ -1,8 +1,8 @@ import { Module } from '@nestjs/common'; import { ConfigModule } from '@nestjs/config'; -import { StockTopfiveController } from './stock.topfive.controller'; -import { StockTopfiveService } from './stock.topfive.service'; -import { KoreaInvestmentModule } from '../../koreaInvestment/korea.investment.module'; +import { StockTopfiveController } from './stock-topfive.controller'; +import { StockTopfiveService } from './stock-topfive.service'; +import { KoreaInvestmentModule } from '../../koreaInvestment/korea-investment.module'; @Module({ imports: [ConfigModule, KoreaInvestmentModule], diff --git a/BE/src/stock/topfive/stock.topfive.service.ts b/BE/src/stock/topfive/stock-topfive.service.ts similarity index 95% rename from BE/src/stock/topfive/stock.topfive.service.ts rename to BE/src/stock/topfive/stock-topfive.service.ts index d8da1643..82f659d5 100644 --- a/BE/src/stock/topfive/stock.topfive.service.ts +++ b/BE/src/stock/topfive/stock-topfive.service.ts @@ -3,14 +3,14 @@ import { Injectable, Logger } from '@nestjs/common'; import { StockRankingQueryParameterDto } from './dto/stock-ranking-request.dto'; import { StockRankingResponseDto } from './dto/stock-ranking-response.dto'; import { StockRankingDataDto } from './dto/stock-ranking-data.dto'; -import { MarketType } from '../enum/MarketType'; +import { MarketType } from '../enum/market-type'; import { StockApiOutputData, StockApiResponse, -} from './interface/stock.topfive.interface'; -import { getHeader } from '../../util/getHeader'; -import { getFullURL } from '../../util/getFullURL'; -import { KoreaInvestmentService } from '../../koreaInvestment/korea.investment.service'; +} from './interface/stock-topfive.interface'; +import { getHeader } from '../../util/get-header'; +import { getFullURL } from '../../util/get-full-URL'; +import { KoreaInvestmentService } from '../../koreaInvestment/korea-investment.service'; @Injectable() export class StockTopfiveService { diff --git a/BE/src/util/getFullURL.ts b/BE/src/util/get-full-URL.ts similarity index 100% rename from BE/src/util/getFullURL.ts rename to BE/src/util/get-full-URL.ts diff --git a/BE/src/util/getHeader.ts b/BE/src/util/get-header.ts similarity index 100% rename from BE/src/util/getHeader.ts rename to BE/src/util/get-header.ts diff --git a/BE/src/websocket/socket.gateway.ts b/BE/src/websocket/socket.gateway.ts index 921d9503..e65eac69 100644 --- a/BE/src/websocket/socket.gateway.ts +++ b/BE/src/websocket/socket.gateway.ts @@ -6,11 +6,11 @@ export class SocketGateway { @WebSocketServer() private server: Server; - sendStockIndexListToClient(stockIndex) { - this.server.emit('index', stockIndex); + sendStockIndexListToClient(stockChart) { + this.server.emit('chart', stockChart); } - sendStockIndexValueToClient(stockIndexValue) { - this.server.emit('indexValue', stockIndexValue); + sendStockIndexValueToClient(event, stockIndexValue) { + this.server.emit(event, stockIndexValue); } } diff --git a/BE/src/websocket/socket.service.ts b/BE/src/websocket/socket.service.ts index 0c25bf03..0bcb7b64 100644 --- a/BE/src/websocket/socket.service.ts +++ b/BE/src/websocket/socket.service.ts @@ -2,8 +2,9 @@ import { Injectable, OnModuleInit } from '@nestjs/common'; import { WebSocket } from 'ws'; import axios from 'axios'; import { SocketGateway } from './socket.gateway'; -import { StockIndexValueElementDto } from '../stock/index/dto/stock.index.value.element.dto'; +import { StockIndexValueElementDto } from '../stock/index/dto/stock-index-value-element.dto'; import { SocketConnectTokenInterface } from './interface/socket.interface'; +import { getFullURL } from '../util/get-full-URL'; @Injectable() export class SocketService implements OnModuleInit { @@ -12,13 +13,19 @@ export class SocketService implements OnModuleInit { H0UPCNT0: this.handleStockIndexValue.bind(this), }; + private STOCK_CODE = { + '0001': 'KOSPI', + '1001': 'KOSDAQ', + '2001': 'KOSPI200', + '3003': 'KSQ150', + }; + constructor(private readonly socketGateway: SocketGateway) {} async onModuleInit() { const socketConnectionKey = await this.getSocketConnectionKey(); - const url = 'ws://ops.koreainvestment.com:21000'; - this.socket = new WebSocket(url); + this.socket = new WebSocket(process.env.KOREA_INVESTMENT_SOCKET_URL); this.socket.onopen = () => { this.registerStockIndexByCode('0001', socketConnectionKey); // 코스피 @@ -41,8 +48,8 @@ export class SocketService implements OnModuleInit { private handleStockIndexValue(responseData: string) { const responseList = responseData.split('^'); this.socketGateway.sendStockIndexValueToClient( + this.STOCK_CODE[responseList[0]], new StockIndexValueElementDto( - responseList[0], responseList[2], responseList[4], responseList[9], @@ -53,7 +60,7 @@ export class SocketService implements OnModuleInit { private async getSocketConnectionKey() { const response = await axios.post( - `${process.env.KOREA_INVESTMENT_BASE_URL}/oauth2/Approval`, + getFullURL('/oauth2/Approval'), { grant_type: 'client_credentials', appkey: process.env.KOREA_INVESTMENT_APP_KEY, diff --git a/BE/test/stock/index/stock.index.list.e2e-spec.ts b/BE/test/stock/index/stock.index.list.e2e-spec.ts index 7518b75d..9a844581 100644 --- a/BE/test/stock/index/stock.index.list.e2e-spec.ts +++ b/BE/test/stock/index/stock.index.list.e2e-spec.ts @@ -1,6 +1,6 @@ import { Test } from '@nestjs/testing'; import axios from 'axios'; -import { StockIndexService } from '../../../src/stock/index/stock.index.service'; +import { StockIndexService } from '../../../src/stock/index/stock-index.service'; import { STOCK_INDEX_LIST_MOCK } from './mockdata/stock.index.list.mockdata'; jest.mock('axios'); diff --git a/BE/test/stock/index/stock.index.value.e2e-spec.ts b/BE/test/stock/index/stock.index.value.e2e-spec.ts index 12ff2590..2008f2e4 100644 --- a/BE/test/stock/index/stock.index.value.e2e-spec.ts +++ b/BE/test/stock/index/stock.index.value.e2e-spec.ts @@ -1,6 +1,6 @@ import { Test } from '@nestjs/testing'; import axios from 'axios'; -import { StockIndexService } from '../../../src/stock/index/stock.index.service'; +import { StockIndexService } from '../../../src/stock/index/stock-index.service'; import { STOCK_INDEX_VALUE_MOCK } from './mockdata/stock.index.value.mockdata'; jest.mock('axios');