-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'back/main' into feature/api/register-#26
- Loading branch information
Showing
19 changed files
with
1,592 additions
and
653 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,7 +38,6 @@ jobs: | |
working-directory: ./BE | ||
run: npm run build | ||
|
||
jobs: | ||
FE-test-and-build: | ||
runs-on: ubuntu-latest | ||
|
||
|
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
BE/src/stock/index/dto/stock.index.list.chart.element.dto.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { ApiProperty } from '@nestjs/swagger'; | ||
|
||
export class StockIndexListChartElementDto { | ||
constructor(time: string, value: string) { | ||
this.time = time; | ||
this.value = value; | ||
} | ||
|
||
@ApiProperty({ description: 'HHMMSS', example: '130500' }) | ||
time: string; | ||
|
||
@ApiProperty({ description: '주가 지수' }) | ||
value: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
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[]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { ApiProperty } from '@nestjs/swagger'; | ||
import { StockIndexListElementDto } from './stock.index.list.element.dto'; | ||
import { StockIndexValueElementDto } from './stock.index.value.element.dto'; | ||
|
||
export class StockIndexResponseDto { | ||
constructor( | ||
indexList: StockIndexListElementDto[], | ||
indexValue: StockIndexValueElementDto[], | ||
) { | ||
this.indexList = indexList; | ||
this.indexValue = indexValue; | ||
} | ||
|
||
@ApiProperty({ | ||
description: '주가 지수 차트 정보 (코스피, 코스닥, 코스피200, KSQ150)', | ||
type: [StockIndexListElementDto], | ||
}) | ||
indexList: StockIndexListElementDto[]; | ||
|
||
@ApiProperty({ | ||
description: '주가 지수 실시간 값 정보 (코스피, 코스닥, 코스피200, KSQ150)', | ||
type: [StockIndexValueElementDto], | ||
}) | ||
indexValue: StockIndexValueElementDto[]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
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; | ||
this.diff = diff; | ||
this.diffRate = diffRate; | ||
this.sign = sign; | ||
} | ||
|
||
@ApiProperty({ | ||
description: '코스피: 0001, 코스닥: 1001, 코스피200: 2001, KSQ150: 3003', | ||
}) | ||
code: string; | ||
|
||
@ApiProperty({ description: '주가 지수' }) | ||
value: string; | ||
|
||
@ApiProperty({ description: '전일 대비 등락' }) | ||
diff: string; | ||
|
||
@ApiProperty({ description: '전일 대비 등락률' }) | ||
diffRate: string; | ||
|
||
@ApiProperty({ description: '부호... 인데 추후에 알아봐야 함' }) | ||
sign: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { Controller, Get } from '@nestjs/common'; | ||
import { ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger'; | ||
import { StockIndexService } from './stock.index.service'; | ||
import { StockIndexResponseDto } from './dto/stock.index.response.dto'; | ||
|
||
@Controller('/api/stock/index') | ||
@ApiTags('주가 지수 API') | ||
export class StockIndexController { | ||
constructor(private readonly stockIndexService: StockIndexService) {} | ||
|
||
@Get() | ||
@ApiOperation({ | ||
summary: '주가 지수 차트 정보, 현재 값 조회 API', | ||
description: '주가 지수 차트 정보와 현재 값을 리스트로 반환한다.', | ||
}) | ||
@ApiResponse({ | ||
status: 200, | ||
description: '주가 지수 조회 성공', | ||
type: StockIndexResponseDto, | ||
}) | ||
async getStockIndex() { | ||
const stockLists = await Promise.all([ | ||
this.stockIndexService.getDomesticStockIndexListByCode('0001'), // 코스피 | ||
this.stockIndexService.getDomesticStockIndexListByCode('1001'), // 코스닥 | ||
this.stockIndexService.getDomesticStockIndexListByCode('2001'), // 코스피200 | ||
this.stockIndexService.getDomesticStockIndexListByCode('3003'), // KSQ150 | ||
]); | ||
|
||
const stockValues = await Promise.all([ | ||
this.stockIndexService.getDomesticStockIndexValueByCode('0001'), // 코스피 | ||
this.stockIndexService.getDomesticStockIndexValueByCode('1001'), // 코스닥 | ||
this.stockIndexService.getDomesticStockIndexValueByCode('2001'), // 코스피200 | ||
this.stockIndexService.getDomesticStockIndexValueByCode('3003'), // KSQ150 | ||
]); | ||
|
||
return new StockIndexResponseDto(stockLists, stockValues); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { StockIndexController } from './stock.index.controller'; | ||
import { StockIndexService } from './stock.index.service'; | ||
|
||
@Module({ | ||
imports: [], | ||
controllers: [StockIndexController], | ||
providers: [StockIndexService], | ||
exports: [StockIndexService], | ||
}) | ||
export class StockIndexModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
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'; | ||
|
||
@Injectable() | ||
export class StockIndexService { | ||
private accessToken: string; | ||
private expireDateTime: number; | ||
|
||
async getDomesticStockIndexListByCode(code: string) { | ||
const accessToken = await this.getAccessToken(); | ||
|
||
const url = | ||
'https://openapi.koreainvestment.com:9443/uapi/domestic-stock/v1/quotations/inquire-index-timeprice'; | ||
const queryParams = `?FID_INPUT_HOUR_1=300&FID_COND_MRKT_DIV_CODE=U&FID_INPUT_ISCD=${code}`; | ||
|
||
const response = await fetch(url + queryParams, { | ||
method: 'GET', | ||
headers: { | ||
'content-type': 'application/json; charset=utf-8', | ||
authorization: `Bearer ${accessToken}`, | ||
appkey: process.env.APP_KEY, | ||
appsecret: process.env.APP_SECRET, | ||
tr_id: 'FHPUP02110200', | ||
custtype: 'P', | ||
}, | ||
}); | ||
|
||
const result: StockIndexChartInterface = await response.json(); | ||
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) { | ||
const accessToken = await this.getAccessToken(); | ||
|
||
const url = | ||
'https://openapi.koreainvestment.com:9443/uapi/domestic-stock/v1/quotations/inquire-index-price'; | ||
const queryParams = `?FID_COND_MRKT_DIV_CODE=U&FID_INPUT_ISCD=${code}`; | ||
|
||
const response = await fetch(url + queryParams, { | ||
method: 'GET', | ||
headers: { | ||
'content-type': 'application/json; charset=utf-8', | ||
authorization: `Bearer ${accessToken}`, | ||
appkey: process.env.APP_KEY, | ||
appsecret: process.env.APP_SECRET, | ||
tr_id: 'FHPUP02100000', | ||
custtype: 'P', | ||
}, | ||
}); | ||
|
||
const result: StockIndexValueInterface = await response.json(); | ||
return new StockIndexValueElementDto( | ||
code, | ||
result.output.bstp_nmix_prpr, | ||
result.output.bstp_nmix_prdy_vrss, | ||
result.output.bstp_nmix_prdy_vrss, | ||
result.output.prdy_vrss_sign, | ||
); | ||
} | ||
|
||
private async getAccessToken() { | ||
if (!this.accessToken || this.expireDateTime <= Date.now()) { | ||
const url = 'https://openapivts.koreainvestment.com:29443/oauth2/tokenP'; | ||
const response = await fetch(url, { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json; charset=UTF-8', | ||
}, | ||
body: JSON.stringify({ | ||
grant_type: 'client_credentials', | ||
appkey: process.env.APP_KEY, | ||
appsecret: process.env.APP_SECRET, | ||
}), | ||
}); | ||
const result: AccessTokenInterface = await response.json(); | ||
this.accessToken = result.access_token; | ||
this.expireDateTime = new Date( | ||
result.access_token_token_expired, | ||
).getTime(); | ||
return result.access_token; | ||
} | ||
|
||
return this.accessToken; | ||
} | ||
} | ||
|
||
// interfaces | ||
|
||
interface AccessTokenInterface { | ||
access_token: string; | ||
access_token_token_expired: string; | ||
token_type: string; | ||
expires_in: number; | ||
} | ||
|
||
interface StockIndexChartInterface { | ||
output: StockIndexChartElementInterface[]; | ||
rt_cd: string; | ||
msg_cd: string; | ||
msg1: string; | ||
} | ||
|
||
interface StockIndexChartElementInterface { | ||
bsop_hour: string; | ||
bstp_nmix_prpr: string; | ||
bstp_nmix_prdy_vrss: string; | ||
prdy_vrss_sign: string; | ||
bstp_nmix_prdy_ctrt: string; | ||
acml_tr_pbmn: string; | ||
acml_vol: string; | ||
cntg_vol: string; | ||
} | ||
|
||
interface StockIndexValueInterface { | ||
output: { | ||
bstp_nmix_prpr: string; | ||
bstp_nmix_prdy_vrss: string; | ||
prdy_vrss_sign: string; | ||
bstp_nmix_prdy_ctrt: string; | ||
acml_vol: string; | ||
prdy_vol: string; | ||
acml_tr_pbmn: string; | ||
prdy_tr_pbmn: string; | ||
bstp_nmix_oprc: string; | ||
prdy_nmix_vrss_nmix_oprc: string; | ||
oprc_vrss_prpr_sign: string; | ||
bstp_nmix_oprc_prdy_ctrt: string; | ||
bstp_nmix_hgpr: string; | ||
prdy_nmix_vrss_nmix_hgpr: string; | ||
hgpr_vrss_prpr_sign: string; | ||
bstp_nmix_hgpr_prdy_ctrt: string; | ||
bstp_nmix_lwpr: string; | ||
prdy_clpr_vrss_lwpr: string; | ||
lwpr_vrss_prpr_sign: string; | ||
prdy_clpr_vrss_lwpr_rate: string; | ||
ascn_issu_cnt: string; | ||
uplm_issu_cnt: string; | ||
stnr_issu_cnt: string; | ||
down_issu_cnt: string; | ||
lslm_issu_cnt: string; | ||
dryy_bstp_nmix_hgpr: string; | ||
dryy_hgpr_vrss_prpr_rate: string; | ||
dryy_bstp_nmix_hgpr_date: string; | ||
dryy_bstp_nmix_lwpr: string; | ||
dryy_lwpr_vrss_prpr_rate: string; | ||
dryy_bstp_nmix_lwpr_date: string; | ||
total_askp_rsqn: string; | ||
total_bidp_rsqn: string; | ||
seln_rsqn_rate: string; | ||
shnu_rsqn_rate: string; | ||
ntby_rsqn: string; | ||
}; | ||
rt_cd: string; | ||
msg_cd: string; | ||
msg1: string; | ||
} |
Oops, something went wrong.