Skip to content

Commit

Permalink
Merge branch 'back/main' of https://github.com/boostcampwm-2024/web16…
Browse files Browse the repository at this point in the history
…-JuGa into back/main
  • Loading branch information
jinddings committed Nov 11, 2024
2 parents 1c7c1e9 + 5920449 commit cc7592a
Show file tree
Hide file tree
Showing 37 changed files with 697 additions and 432 deletions.
12 changes: 6 additions & 6 deletions BE/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@ 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 { 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 { 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({
imports: [
Expand All @@ -30,8 +29,9 @@ import { KoreaInvestmentModule } from './koreaInvestment/korea.investment.module
AuthModule,
StockIndexModule,
StockTopfiveModule,
SocketModule,
],
controllers: [AppController],
providers: [AppService, SocketService, SocketGateway],
providers: [AppService],
})
export class AppModule {}
2 changes: 1 addition & 1 deletion BE/src/auth/auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion BE/src/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion BE/src/auth/user.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<User> {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export interface AccessTokenInterface {
access_token: string;
access_token_token_expired: string;
token_type: string;
expires_in: number;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Module } from '@nestjs/common';
import { KoreaInvestmentService } from './korea.investment.service';
import { KoreaInvestmentService } from './korea-investment.service';

@Module({
imports: [],
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<AccessTokenInterface>(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;
}
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
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' })
time: string;

@ApiProperty({ description: '주가 지수' })
value: string;

@ApiProperty({ description: '전일 대비 주가 지수' })
diff: string;
}
14 changes: 14 additions & 0 deletions BE/src/stock/index/dto/stock-index-response-element.dto.ts
Original file line number Diff line number Diff line change
@@ -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[];
}
28 changes: 28 additions & 0 deletions BE/src/stock/index/dto/stock-index-response.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { ApiProperty } from '@nestjs/swagger';
import { StockIndexResponseElementDto } from './stock-index-response-element.dto';

export class StockIndexResponseDto {
@ApiProperty({
description: '코스피 지수',
type: StockIndexResponseElementDto,
})
KOSPI: StockIndexResponseElementDto;

@ApiProperty({
description: '코스닥 지수',
type: StockIndexResponseElementDto,
})
KOSDAQ: StockIndexResponseElementDto;

@ApiProperty({
description: '코스피200 지수',
type: StockIndexResponseElementDto,
})
KOSPI200: StockIndexResponseElementDto;

@ApiProperty({
description: 'KSQ150 지수',
type: StockIndexResponseElementDto,
})
KSQ150: StockIndexResponseElementDto;
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
17 changes: 0 additions & 17 deletions BE/src/stock/index/dto/stock.index.list.element.dto.ts

This file was deleted.

25 changes: 0 additions & 25 deletions BE/src/stock/index/dto/stock.index.response.dto.ts

This file was deleted.

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
121 changes: 121 additions & 0 deletions BE/src/stock/index/stock-index.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
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()
@ApiOperation({
summary: '주가 지수 차트 정보, 현재 값 조회 API',
description: '주가 지수 차트 정보와 현재 값을 리스트로 반환한다.',
})
@ApiResponse({
status: 200,
description: '주가 지수 조회 성공',
type: StockIndexResponseDto,
})
async getStockIndex() {
const accessToken = await this.koreaInvestmentService.getAccessToken();

const [kospiChart, kosdaqChart, kospi200Chart, ksq150Chart] =
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
]);

const [kospiValue, kosdaqValue, kospi200Value, ksq150Value] =
await Promise.all([
this.stockIndexService.getDomesticStockIndexValueByCode(
'0001',
accessToken,
), // 코스피
this.stockIndexService.getDomesticStockIndexValueByCode(
'1001',
accessToken,
), // 코스닥
this.stockIndexService.getDomesticStockIndexValueByCode(
'2001',
accessToken,
), // 코스피200
this.stockIndexService.getDomesticStockIndexValueByCode(
'3003',
accessToken,
), // KSQ150
]);

const stockIndexResponse = new StockIndexResponseDto();
stockIndexResponse.KOSPI = {
value: kospiValue,
chart: kospiChart,
};
stockIndexResponse.KOSDAQ = {
value: kosdaqValue,
chart: kosdaqChart,
};
stockIndexResponse.KOSPI200 = {
value: kospi200Value,
chart: kospi200Chart,
};
stockIndexResponse.KSQ150 = {
value: ksq150Value,
chart: ksq150Chart,
};
return stockIndexResponse;
}

@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.socketGateway.sendStockIndexListToClient({
KOSPI: stockLists[0],
KOSDAQ: stockLists[1],
KOSPI200: stockLists[2],
KSQ150: stockLists[3],
});
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
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({
imports: [KoreaInvestmentModule],
imports: [KoreaInvestmentModule, SocketModule],
controllers: [StockIndexController],
providers: [StockIndexService],
exports: [StockIndexService],
Expand Down
Loading

0 comments on commit cc7592a

Please sign in to comment.