Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#6] 3.02 주식차트 정보 기능 구현 수정 #37

Merged
merged 3 commits into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion BE/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { SocketService } from './websocket/socket.service';
import { SocketGateway } from './websocket/socket.gateway';
import { StockTopfiveModule } from './stock/topfive/stock.topfive.module';
import { KoreaInvestmentModule } from './koreaInvestment/korea.investment.module';
import { SocketModule } from './websocket/socket.module';

@Module({
imports: [
Expand All @@ -30,8 +31,9 @@ import { KoreaInvestmentModule } from './koreaInvestment/korea.investment.module
AuthModule,
StockIndexModule,
StockTopfiveModule,
SocketModule,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 모듈로 분리해서 socket 통신도 accessToken 성공하신건가요? 좋네요!

],
controllers: [AppController],
providers: [AppService, SocketService, SocketGateway],
providers: [AppService],
})
export class AppModule {}
7 changes: 0 additions & 7 deletions BE/src/stock/index/interface/stock.index.interface.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
export interface AccessTokenInterface {
access_token: string;
access_token_token_expired: string;
token_type: string;
expires_in: number;
}

export interface StockIndexChartInterface {
output: StockIndexChartElementInterface[];
rt_cd: string;
Expand Down
29 changes: 29 additions & 0 deletions BE/src/stock/index/stock.index.controller.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
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 { SocketGateway } from '../../websocket/socket.gateway';

@Controller('/api/stocks/index')
@ApiTags('주가 지수 API')
export class StockIndexController {
constructor(
private readonly stockIndexService: StockIndexService,
private readonly koreaInvestmentService: KoreaInvestmentService,
private readonly socketGateway: SocketGateway,
) {}

@Get()
Expand Down Expand Up @@ -65,4 +68,30 @@ export class StockIndexController {

return new StockIndexResponseDto(stockLists, stockValues);
}

@Cron('*/1 9-16 * * 1-5')
async cronStockIndexLists() {
const accessToken = await this.koreaInvestmentService.getAccessToken();

const stockLists = await Promise.all([
this.stockIndexService.getDomesticStockIndexListByCode(
'0001',
accessToken,
), // 코스피
this.stockIndexService.getDomesticStockIndexListByCode(
'1001',
accessToken,
), // 코스닥
this.stockIndexService.getDomesticStockIndexListByCode(
'2001',
accessToken,
), // 코스피200
this.stockIndexService.getDomesticStockIndexListByCode(
'3003',
accessToken,
), // KSQ150
]);

this.socketGateway.sendStockIndexListToClient(stockLists);
}
}
3 changes: 2 additions & 1 deletion BE/src/stock/index/stock.index.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ 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 { SocketModule } from '../../websocket/socket.module';

@Module({
imports: [KoreaInvestmentModule],
imports: [KoreaInvestmentModule, SocketModule],
controllers: [StockIndexController],
providers: [StockIndexService],
exports: [StockIndexService],
Expand Down
12 changes: 12 additions & 0 deletions BE/src/websocket/socket.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Module } from '@nestjs/common';
import { SocketService } from './socket.service';
import { SocketGateway } from './socket.gateway';
import { KoreaInvestmentModule } from '../koreaInvestment/korea.investment.module';

@Module({
imports: [KoreaInvestmentModule],
controllers: [],
providers: [SocketService, SocketGateway],
exports: [SocketGateway],
})
export class SocketModule {}
43 changes: 5 additions & 38 deletions BE/src/websocket/socket.service.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import { Injectable, OnModuleInit } from '@nestjs/common';
import { WebSocket } from 'ws';
import { Cron } from '@nestjs/schedule';
import { SocketGateway } from './socket.gateway';
import { StockIndexValueElementDto } from '../stock/index/dto/stock.index.value.element.dto';
import { StockIndexService } from '../stock/index/stock.index.service';
import { KoreaInvestmentService } from '../koreaInvestment/korea.investment.service';

@Injectable()
export class SocketService implements OnModuleInit {
Expand All @@ -13,11 +10,7 @@ export class SocketService implements OnModuleInit {
H0UPCNT0: this.handleStockIndexValue.bind(this),
};

constructor(
private readonly stockIndexGateway: SocketGateway,
private readonly stockIndexService: StockIndexService,
private readonly koreaInvestmentService: KoreaInvestmentService,
) {}
constructor(private readonly socketGateway: SocketGateway) {}

async onModuleInit() {
const socketConnectionKey = await this.getSocketConnectionKey();
Expand All @@ -43,35 +36,9 @@ export class SocketService implements OnModuleInit {
};
}

@Cron('*/5 9-16 * * 1-5')
async cronStockIndexLists() {
const accessToken = await this.koreaInvestmentService.getAccessToken();

const stockLists = await Promise.all([
this.stockIndexService.getDomesticStockIndexListByCode(
'0001',
accessToken,
), // 코스피
this.stockIndexService.getDomesticStockIndexListByCode(
'1001',
accessToken,
), // 코스닥
this.stockIndexService.getDomesticStockIndexListByCode(
'2001',
accessToken,
), // 코스피200
this.stockIndexService.getDomesticStockIndexListByCode(
'3003',
accessToken,
), // KSQ150
]);

this.stockIndexGateway.sendStockIndexListToClient(stockLists);
}

private handleStockIndexValue(responseData: string) {
const responseList = responseData.split('^');
this.stockIndexGateway.sendStockIndexValueToClient(
this.socketGateway.sendStockIndexValueToClient(
new StockIndexValueElementDto(
responseList[0],
responseList[2],
Expand All @@ -83,7 +50,7 @@ export class SocketService implements OnModuleInit {
}

private async getSocketConnectionKey() {
const url = 'https://openapi.koreainvestment.com:9443/oauth2/Approval';
const url = `${process.env.KOREA_INVESTMENT_BASE_URL}/oauth2/Approval`;

const response = await fetch(url, {
method: 'POST',
Expand All @@ -92,8 +59,8 @@ export class SocketService implements OnModuleInit {
},
body: JSON.stringify({
grant_type: 'client_credentials',
appkey: process.env.APP_KEY,
secretkey: process.env.APP_SECRET,
appkey: process.env.KOREA_INVESTMENT_APP_KEY,
secretkey: process.env.KOREA_INVESTMENT_APP_SECRET,
}),
});
const result: SocketConnectTokenInterface = await response.json();
Expand Down